Skip to content

Commit

Permalink
more bugfixes, not finished yet
Browse files Browse the repository at this point in the history
  • Loading branch information
1azyman committed Jan 25, 2018
1 parent e2ae773 commit 36a094c
Showing 1 changed file with 83 additions and 13 deletions.
Expand Up @@ -42,6 +42,8 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -216,6 +218,11 @@ private void processAnyExtensionDeltaValues(Collection<PrismValue> values,
extValues.add(new PrismEntityPair(value, extValue));
}

if (extValues.isEmpty()) {
// no changes in indexed values
return;
}

RAnyValue first = extValues.iterator().next().repository;
Class type = first.getClass();

Expand Down Expand Up @@ -254,6 +261,8 @@ private void processAnyExtensionDeltaValues(ItemDelta delta,
if (delta.getValuesToReplace() != null && !delta.getValuesToReplace().isEmpty()) {
processAnyExtensionDeltaValues(delta.getValuesToReplace(), object, objectOwnerType, assignmentExtension, assignmentExtensionType,
(existing, fromDelta) -> {
// todo don't remove all if not necessary, just the ones that don't exist in fromDelta,
// than add only those items from fromDelta that were not in existing
existing.clear();
markNewOnesTransientAndAddToExisting(existing, (Collection) fromDelta);
},
Expand Down Expand Up @@ -473,7 +482,56 @@ private void handleOneToMany(Collection collection, ItemDelta delta, Attribute a
// handle replace
Collection<PrismEntityPair<?>> valuesToReplace = processDeltaValues(delta.getValuesToReplace(), outputType, delta, bean);
if (!valuesToReplace.isEmpty()) {
collection.clear();
// remove all items from existing which don't exist in valuesToReplace
// add items from valuesToReplace to existing, only those which aren't already there

Pair<Collection<Object>, Set<Long>> split = splitPrismEntityPairs(valuesToReplace);
Collection<Object> repositoryObjects = split.getLeft();
Set<Long> containerIds = split.getRight();

Collection skipAddingTheseObjects = new ArrayList();
Collection skipAddingTheseIds = new ArrayList();
Collection toDelete = new ArrayList();
for (Object obj : collection) {
if (obj instanceof Container) {
Container container = (Container) obj;

long id = container.getId().longValue();
if (!containerIds.contains(id)) {
toDelete.add(container);
} else {
skipAddingTheseIds.add(id);
}
} else {
// e.g. RObjectReference
if (!repositoryObjects.contains(obj)) {
toDelete.add(obj);
} else {
skipAddingTheseObjects.add(obj);
}
}
}
collection.removeAll(toDelete);

Iterator<PrismEntityPair<?>> iterator = valuesToReplace.iterator();
while (iterator.hasNext()) {
PrismEntityPair pair = iterator.next();
Object obj = pair.repository;
if (obj instanceof Container) {
Container container = (Container) obj;

// todo this will fail as container.getId() returns null at this time
// new id was not generated yet
if (skipAddingTheseIds.contains(container.getId())) {
iterator.remove();
}
} else {
if (skipAddingTheseObjects.contains(obj)) {
iterator.remove();
}
}
}

markNewOnesTransientAndAddToExisting(collection, valuesToReplace);

return;
Expand All @@ -484,18 +542,12 @@ private void handleOneToMany(Collection collection, ItemDelta delta, Attribute a
markNewOnesTransientAndAddToExisting(collection, valuesToAdd);

// handle delete
Collection<PrismEntityPair> valuesToDelete = processDeltaValues(delta.getValuesToDelete(), outputType, delta, bean);
Set<Long> containerIdsToDelete = new HashSet<>();
for (PrismEntityPair pair : valuesToDelete) {
if (pair.repository instanceof Container) {
Container container = (Container) pair.repository;

long id = container.getId().longValue();
containerIdsToDelete.add(id);
}
}

Collection<PrismEntityPair<?>> valuesToDelete = processDeltaValues(delta.getValuesToDelete(), outputType, delta, bean);
if (!valuesToDelete.isEmpty()) {
Pair<Collection<Object>, Set<Long>> split = splitPrismEntityPairs(valuesToDelete);
Collection<Object> repositoryObjectsToDelete = split.getLeft();
Set<Long> containerIdsToDelete = split.getRight();

Collection toDelete = new ArrayList();
for (Object obj : collection) {
if (obj instanceof Container) {
Expand All @@ -507,7 +559,7 @@ private void handleOneToMany(Collection collection, ItemDelta delta, Attribute a
}
} else {
// e.g. RObjectReference
if (valuesToDelete.contains(obj)) {
if (repositoryObjectsToDelete.contains(obj)) {
toDelete.add(obj);
}
}
Expand All @@ -516,6 +568,23 @@ private void handleOneToMany(Collection collection, ItemDelta delta, Attribute a
}
}

private Pair<Collection<Object>, Set<Long>> splitPrismEntityPairs(Collection<PrismEntityPair<?>> collection) {
Collection<Object> repositoryObjects = new ArrayList<>();
Set<Long> containerIds = new HashSet<>();
for (PrismEntityPair pair : collection) {
if (pair.repository instanceof Container) {
Container container = (Container) pair.repository;

long id = container.getId().longValue();
containerIds.add(id);
}

repositoryObjects.add(pair.repository);
}

return new ImmutablePair<>(repositoryObjects, containerIds);
}

private void markNewOnesTransientAndAddToExisting(Collection existing, Collection<PrismEntityPair<?>> newOnes) {
Set<Integer> usedIds = new HashSet<>();
for (Object obj : existing) {
Expand All @@ -541,6 +610,7 @@ private void markNewOnesTransientAndAddToExisting(Collection existing, Collectio
nextId++;
}

usedIds.add(nextId);
((Container) item.repository).setId(nextId);
((PrismContainerValue) item.prism).setId(nextId.longValue());
}
Expand Down

0 comments on commit 36a094c

Please sign in to comment.