Skip to content

Commit

Permalink
Reworking assignment mapping code.
Browse files Browse the repository at this point in the history
  • Loading branch information
semancik committed Jul 30, 2014
1 parent ecf5a31 commit be1f933
Show file tree
Hide file tree
Showing 31 changed files with 933 additions and 354 deletions.
Expand Up @@ -145,13 +145,10 @@ public Class<V> getCompileTimeClass() {
}

@Override
public void applyTo(Item item) throws SchemaException {
if (!(item instanceof PrismContainer)) {
throw new SchemaException("Cannot apply container delta "+this+" to item "+item+" of type "+item.getClass());
}
super.applyTo(item);
protected boolean isApplicableToType(Item item) {
return item instanceof PrismContainer;
}

public ItemDelta<?> findItemDelta(ItemPath path) {
if (path.isEmpty()) {
return this;
Expand Down
Expand Up @@ -34,6 +34,7 @@
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismObjectDefinition;
import com.evolveum.midpoint.prism.PrismProperty;
import com.evolveum.midpoint.prism.PrismValue;
import com.evolveum.midpoint.prism.Visitable;
import com.evolveum.midpoint.prism.Visitor;
Expand All @@ -49,6 +50,7 @@
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.prism.xml.ns._public.types_3.ItemPathType;

import org.apache.commons.lang.Validate;

/**
Expand Down Expand Up @@ -907,19 +909,6 @@ public void simplify() {
valuesToDelete = null;
}
}

/**
* Apply this delta (path) to a property container.
*/
public void applyTo(PrismContainer<?> propertyContainer) throws SchemaException {
ItemPath itemPath = getPath();
Item<?> item = propertyContainer.findOrCreateItem(itemPath, getItemClass(), getDefinition());
if (item == null) {
throw new IllegalStateException("Cannot apply delta because cannot find item "+itemPath+" in "+propertyContainer);
}
applyTo(item);
cleanupAllTheWayUp(item);
}

private void cleanupAllTheWayUp(Item<?> item) {
if (item.isEmpty()) {
Expand Down Expand Up @@ -947,30 +936,44 @@ public static void applyTo(Collection<? extends ItemDelta> deltas, PrismContaine
}
}

/**
* Apply this delta (path) to a property container.
*/
public void applyTo(PrismContainerValue<?> propertyContainerVal) throws SchemaException {
ItemPath itemPath = getPath();
Item<?> item = propertyContainerVal.findOrCreateItem(itemPath, getItemClass(), getDefinition());
if (item == null) {
throw new IllegalStateException("Cannot apply delta because cannot find item "+itemPath+" in "+propertyContainerVal);
}
applyTo(item);
if (item.isEmpty()) {
propertyContainerVal.remove(item);
public void applyTo(Item item) throws SchemaException {
ItemPath itemPath = item.getPath();
ItemPath deltaPath = getPath();
CompareResult compareComplex = itemPath.compareComplex(deltaPath);
if (compareComplex == CompareResult.EQUIVALENT) {
applyToMatchingPath(item);
cleanupAllTheWayUp(item);
} else if (compareComplex == CompareResult.SUBPATH) {
if (item instanceof PrismContainer<?>) {
PrismContainer<?> container = (PrismContainer<?>)item;
ItemPath remainderPath = deltaPath.remainder(itemPath);
Item subItem = container.findOrCreateItem(remainderPath, getItemClass());
applyToMatchingPath(subItem);
} else {
throw new SchemaException("Cannot apply delta "+this+" to "+item+" as delta path is below the item path and the item is not a container");
}
} else if (compareComplex == CompareResult.SUPERPATH) {
throw new SchemaException("Cannot apply delta "+this+" to "+item+" as delta path is above the item path");
} else if (compareComplex == CompareResult.NO_RELATION) {
throw new SchemaException("Cannot apply delta "+this+" to "+item+" as paths do not match");
}
}

/**
* Apply this delta (path) to a property.
* Applies delta to item were path of the delta and path of the item matches (skips path checks).
*/
public void applyTo(Item item) throws SchemaException {
public void applyToMatchingPath(Item item) throws SchemaException {
if (item.getDefinition() == null && getDefinition() != null){
item.applyDefinition(getDefinition());
}
if (!getItemClass().isAssignableFrom(item.getClass())) {
throw new SchemaException("Cannot apply delta "+this+" to "+item+" because the deltas is applicable only to "+getItemClass().getSimpleName());
}
if (valuesToReplace != null) {
item.replaceAll(PrismValue.cloneCollection(valuesToReplace));
// Application of delta might have removed values therefore leaving empty items.
// Those needs to be cleaned-up (removed) as empty item is not a legal state.
cleanupAllTheWayUp(item);
return;
}
if (valuesToAdd != null) {
Expand All @@ -987,9 +990,24 @@ public void applyTo(Item item) throws SchemaException {
if (valuesToDelete != null) {
item.removeAll(valuesToDelete);
}

// Application of delta might have removed values therefore leaving empty items.
// Those needs to be cleaned-up (removed) as empty item is not a legal state.
cleanupAllTheWayUp(item);
}

public boolean isApplicableTo(Item item) {
if (item == null) {
return false;
}
if (!isApplicableToType(item)) {
return false;
}
// TODO: maybe check path?
return true;
}

protected abstract boolean isApplicableToType(Item item);

public static void accept(Collection<? extends ItemDelta> modifications, Visitor visitor, ItemPath path,
boolean recursive) {
for (ItemDelta modification: modifications) {
Expand All @@ -1003,19 +1021,6 @@ public static void accept(Collection<? extends ItemDelta> modifications, Visitor
}
}


public <I extends Item> I computeChangedItem(I oldItem) throws SchemaException {
if (isEmpty()) {
return oldItem;
}
if (oldItem == null) {
// Instantiate empty item
oldItem = (I) getDefinition().instantiate();
}
applyTo(oldItem);
return oldItem;
}

/**
* Returns the "new" state of the property - the state that would be after
* the delta is applied.
Expand All @@ -1032,17 +1037,36 @@ public Item<V> getItemNew(Item<V> itemOld) throws SchemaException {
if (definition == null) {
throw new IllegalStateException("No definition in "+this);
}
Item<V> itemNew;
if (itemOld == null) {
if (isEmpty()) {
return null;
}
itemOld = definition.instantiate(getElementName());
itemNew = definition.instantiate(getElementName());
} else {
itemNew = itemOld.clone();
}
Item<V> itemNew = itemOld.clone();
applyTo(itemNew);
return itemNew;
}

public Item<V> getItemNewMatchingPath(Item<V> itemOld) throws SchemaException {
if (definition == null) {
throw new IllegalStateException("No definition in "+this);
}
Item<V> itemNew;
if (itemOld == null) {
if (isEmpty()) {
return null;
}
itemNew = definition.instantiate(getElementName());
} else {
itemNew = itemOld.clone();
}
applyToMatchingPath(itemNew);
return itemNew;
}

/**
* Returns true if the other delta is a complete subset of this delta.
* I.e. if all the statements of the other delta are already contained
Expand Down Expand Up @@ -1101,6 +1125,18 @@ private Collection<V> cloneSet(ItemDelta clone, Collection<V> thisSet) {
}
return clonedSet;
}

public static <D extends ItemDelta<?>> Collection<D> cloneCollection(Collection<D> orig) {
if (orig == null) {
return null;
}
Collection<D> clone = new ArrayList<>(orig.size());
for (D delta: orig) {
clone.add((D)delta.clone());
}
return clone;
}


@Deprecated
public static <T extends PrismValue> PrismValueDeltaSetTriple<T> toDeltaSetTriple(Item<T> item, ItemDelta<T> delta,
Expand Down
Expand Up @@ -123,20 +123,8 @@ public <P extends PrismProperty> P instantiateEmptyProperty() {
}

@Override
public void applyTo(Item item) throws SchemaException {
if (item instanceof PrismProperty) {
super.applyTo(item);
} else if (item instanceof PrismContainer<?>) {
PrismContainer<?> container = (PrismContainer<?>)item;
ItemPath remainderPath = getPath().remainder(item.getPath());
PrismProperty<?> property = container.findProperty(remainderPath);
if (property == null) {
throw new SchemaException("Cannot apply property delta "+this+" to item "+item+" because there is no property with path "+remainderPath);
}
applyTo(property);
} else {
throw new SchemaException("Cannot apply property delta "+this+" to item "+item+" of type "+item.getClass());
}
protected boolean isApplicableToType(Item item) {
return item instanceof PrismProperty;
}

@Override
Expand Down Expand Up @@ -265,16 +253,16 @@ public boolean isRealValueToDelete(PrismPropertyValue<?> value) {
* Returns the "new" state of the property - the state that would be after the delta
* is applied.
*/
public PrismProperty<T> getPropertyNew() throws SchemaException {
return (PrismProperty<T>) super.getItemNew();
public PrismProperty<T> getPropertyNewMatchingPath() throws SchemaException {
return (PrismProperty<T>) super.getItemNewMatchingPath(null);
}

/**
* Returns the "new" state of the property - the state that would be after the delta
* is applied.
*/
public PrismProperty<T> getPropertyNew(PrismProperty<T> propertyOld) throws SchemaException {
return (PrismProperty<T>) super.getItemNew(propertyOld);
public PrismProperty<T> getPropertyNewMatchingPath(PrismProperty<T> propertyOld) throws SchemaException {
return (PrismProperty<T>) super.getItemNewMatchingPath(propertyOld);
}

@Override
Expand Down
Expand Up @@ -23,6 +23,7 @@
import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.Objectable;
import com.evolveum.midpoint.prism.PrismContainer;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismObjectDefinition;
Expand Down Expand Up @@ -75,13 +76,10 @@ public void applyDefinition(ItemDefinition definition) throws SchemaException {
}
super.applyDefinition(definition);
}

@Override
public void applyTo(Item item) throws SchemaException {
if (!(item instanceof PrismReference)) {
throw new SchemaException("Cannot apply reference delta "+this+" to item "+item+" of type "+item.getClass());
}
super.applyTo(item);
protected boolean isApplicableToType(Item item) {
return item instanceof PrismReference;
}

@Override
Expand Down

This file was deleted.

0 comments on commit be1f933

Please sign in to comment.