Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Evolveum/midpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
1azyman committed May 6, 2014
2 parents 18d9b98 + f9d7158 commit 485a220
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 20 deletions.
15 changes: 15 additions & 0 deletions infra/prism/src/main/java/com/evolveum/midpoint/prism/Item.java
Expand Up @@ -316,6 +316,21 @@ public boolean containsEquivalentValue(V value) {
return contains(value, true);
}

public boolean contains(V value, boolean ignoreMetadata, Comparator<V> comparator) {
if (comparator == null){
return contains(value, ignoreMetadata);
} else{
for (V myValue: getValues()) {
if (comparator.compare(myValue, value) == 0) {
return true;
}
}

}

return false;
}

public boolean contains(V value, boolean ignoreMetadata) {
for (V myValue: getValues()) {
if (myValue.equals(value, ignoreMetadata)) {
Expand Down
Expand Up @@ -194,7 +194,7 @@ public static <F extends ObjectType> LensProjectionContext createAccountContext(
*/
public static <V extends PrismValue, I extends ItemValueWithOrigin<V>> ItemDelta<V> consolidateTripleToDelta(ItemPath itemPath,
DeltaSetTriple<I> triple, ItemDefinition itemDefinition,
ItemDelta<V> aprioriItemDelta, PrismContainer<?> itemContainer, ValueMatcher<?> valueMatcher,
ItemDelta<V> aprioriItemDelta, PrismContainer<?> itemContainer, ValueMatcher<?> valueMatcher, Comparator<V> comparator,
boolean addUnchangedValues, boolean filterExistingValues, boolean isExclusiveStrong,
String contextDescription, boolean applyWeak) throws ExpressionEvaluationException, PolicyViolationException, SchemaException {

Expand Down Expand Up @@ -232,6 +232,7 @@ public void visit(I pvwo) {
// a single item (e.g. attribute). But this loop iterates over every potential value of that item.
for (V value : allValues) {

LOGGER.trace("item existing: {}, value: {}", itemExisting, value);
// Check what to do with the value using the usual "triple routine". It means that if a value is
// in zero set than we need no delta, plus set means add delta and minus set means delete delta.
// The first set that the value is present determines the result.
Expand Down Expand Up @@ -320,7 +321,7 @@ public void visit(I pvwo) {
"skipping adding in {}", new Object[]{value, itemPath, contextDescription});
continue;
}
if (filterExistingValues && hasValue(itemExisting, value, valueMatcher)) {
if (filterExistingValues && hasValue(itemExisting, value, valueMatcher, comparator)) {
LOGGER.trace("Value {} NOT added to delta for item {} because the item already has that value in {}",
new Object[]{value, itemPath, contextDescription});
continue;
Expand Down Expand Up @@ -374,7 +375,7 @@ public void visit(I pvwo) {
new Object[]{value, itemPath, contextDescription});
continue;
}
if (filterExistingValues && !hasValue(itemExisting, value, valueMatcher)) {
if (filterExistingValues && !hasValue(itemExisting, value, valueMatcher, comparator)) {
LOGGER.trace("Value {} NOT deleted to delta for item {} the item does not have that value in {}",
new Object[]{value, itemPath, contextDescription});
continue;
Expand Down Expand Up @@ -470,14 +471,14 @@ private static <V extends PrismValue> Collection<V> addWeakValues(Collection<? e
return values;
}

private static <V extends PrismValue> boolean hasValue(Item<V> existingUserItem, V newValue, ValueMatcher<?> valueMatcher) {
private static <V extends PrismValue> boolean hasValue(Item<V> existingUserItem, V newValue, ValueMatcher<?> valueMatcher, Comparator<V> comparator) {
if (existingUserItem == null) {
return false;
}
if (valueMatcher != null && newValue instanceof PrismPropertyValue) {
return valueMatcher.hasRealValue((PrismProperty)existingUserItem, (PrismPropertyValue)newValue);
} else {
return existingUserItem.contains(newValue, true);
return existingUserItem.contains(newValue, true, comparator);
}
}

Expand Down
Expand Up @@ -40,6 +40,7 @@
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismProperty;
import com.evolveum.midpoint.prism.PrismReference;
import com.evolveum.midpoint.prism.PrismValue;
import com.evolveum.midpoint.prism.PrismPropertyValue;
import com.evolveum.midpoint.prism.delta.ChangeType;
Expand Down Expand Up @@ -82,6 +83,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -274,7 +276,7 @@ private <T,V extends PrismValue> PropertyDelta<T> consolidateAttribute(RefinedOb
ValueMatcher<T> valueMatcher = ValueMatcher.createMatcher(attributeDefinition, matchingRuleRegistry);

return (PropertyDelta<T>) consolidateItem(rOcDef, discr, existingDelta, projCtx, addUnchangedValues, completeShadow,
attributeDefinition.isExlusiveStrong(), itemPath, attributeDefinition, triple, valueMatcher, "attribute "+itemName);
attributeDefinition.isExlusiveStrong(), itemPath, attributeDefinition, triple, valueMatcher, null, "attribute "+itemName);
}

private <V extends PrismValue> ContainerDelta<ShadowAssociationType> consolidateAssociation(RefinedObjectClassDefinition rOcDef,
Expand All @@ -286,9 +288,34 @@ private <V extends PrismValue> ContainerDelta<ShadowAssociationType> consolidate
PrismContainerDefinition<ShadowAssociationType> asspcContainerDef = getAssociationDefinition();
RefinedAssociationDefinition associationDef = rOcDef.findAssociation(associationName);

Comparator<PrismContainerValue<ShadowAssociationType>> comparator = new Comparator<PrismContainerValue<ShadowAssociationType>>() {

@Override
public int compare(PrismContainerValue<ShadowAssociationType> o1,
PrismContainerValue<ShadowAssociationType> o2) {

if (o1 == null && o2 == null){
return 0;
}

if (o1 == null || o2 == null){
return 1;
}

PrismReference ref1 = o1.findReference(ShadowAssociationType.F_SHADOW_REF);
PrismReference ref2 = o2.findReference(ShadowAssociationType.F_SHADOW_REF);

if (ref1.equals(ref2)){
return 0;
}

return 1;
}
};

ContainerDelta<ShadowAssociationType> delta = (ContainerDelta<ShadowAssociationType>) consolidateItem(rOcDef, discr, existingDelta,
projCtx, addUnchangedValues, completeShadow, associationDef.isExclusiveStrong(), itemPath,
asspcContainerDef, triple, null, "association "+associationName);
asspcContainerDef, triple, null, comparator, "association "+associationName);

if (delta != null) {
setAssociationName(delta.getValuesToAdd(), associationName);
Expand Down Expand Up @@ -320,7 +347,7 @@ private <V extends PrismValue> ItemDelta<V> consolidateItem(RefinedObjectClassDe
ResourceShadowDiscriminator discr, ObjectDelta<ShadowType> existingDelta, LensProjectionContext projCtx,
boolean addUnchangedValues, boolean completeShadow, boolean isExclusiveStrong,
ItemPath itemPath, ItemDefinition itemDefinition,
DeltaSetTriple<ItemValueWithOrigin<V>> triple, ValueMatcher<?> valueMatcher, String itemDesc)
DeltaSetTriple<ItemValueWithOrigin<V>> triple, ValueMatcher<?> valueMatcher, Comparator<V> comparator, String itemDesc)
throws SchemaException, ExpressionEvaluationException, PolicyViolationException {

boolean forceAddUnchangedValues = false;
Expand All @@ -340,7 +367,7 @@ private <V extends PrismValue> ItemDelta<V> consolidateItem(RefinedObjectClassDe
// Use this common utility method to do the computation. It does most of the work.
ItemDelta<V> itemDelta = LensUtil.consolidateTripleToDelta(
itemPath, (DeltaSetTriple)triple, itemDefinition, existingItemDelta, projCtx.getObjectNew(),
valueMatcher, addUnchangedValues || forceAddUnchangedValues, completeShadow, isExclusiveStrong,
valueMatcher, comparator, addUnchangedValues || forceAddUnchangedValues, completeShadow, isExclusiveStrong,
discr.toHumanReadableString(), completeShadow);

if (LOGGER.isTraceEnabled()) {
Expand Down
Expand Up @@ -216,7 +216,7 @@ <F extends FocusType> Collection<ItemDelta<? extends PrismValue>> computeItemDel
// We need to add unchanged values otherwise the unconditional mappings will not be applies
boolean addUnchangedValues = true;
ItemDelta<? extends PrismValue> itemDelta = LensUtil.consolidateTripleToDelta(itemPath, (DeltaSetTriple)outputTriple,
focusDefinition.findItemDefinition(itemPath), apropriItemDelta, focusOdo.getNewObject(), null,
focusDefinition.findItemDefinition(itemPath), apropriItemDelta, focusOdo.getNewObject(), null, null,
addUnchangedValues, true, false, contextDesc, true);

itemDelta.simplify();
Expand Down
Expand Up @@ -39,6 +39,8 @@
import com.evolveum.midpoint.prism.PrismPropertyValue;
import com.evolveum.midpoint.prism.delta.ContainerDelta;
import com.evolveum.midpoint.prism.delta.PropertyDelta;
import com.evolveum.midpoint.prism.match.MatchingRule;
import com.evolveum.midpoint.prism.match.MatchingRuleRegistry;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.query.EqualsFilter;
import com.evolveum.midpoint.prism.query.ObjectFilter;
Expand Down Expand Up @@ -82,6 +84,9 @@ class EntitlementConverter {

@Autowired(required=true)
private PrismContext prismContext;

@Autowired(required = true)
private MatchingRuleRegistry matchingRuleRegistry;

//////////
// GET
Expand Down Expand Up @@ -194,8 +199,17 @@ private <S extends ShadowType,T> void postProcessEntitlementEntitlementToSubject
throw new SchemaException("Value attribute "+valueAttrName+" has no more than one value; attribute defined in entitlement association '"+associationName+"' in "+resourceType);
}

ObjectFilter filter = EqualsFilter.createEqual(new ItemPath(ShadowType.F_ATTRIBUTES, assocAttrDef.getName()), assocAttrDef, valueAttr.getValue());
ObjectQuery query = ObjectQuery.createObjectQuery(filter);
ObjectQuery query = createQuery(assocDefType, assocAttrDef, valueAttr);

// MatchingRule matchingRule = matchingRuleRegistry.getMatchingRule(assocDefType.getResourceObjectAssociationType().getMatchingRule(), valueAttr.getDefinition().getTypeName());
// PrismPropertyValue normalized = valueAttr.getValue();
// if (matchingRule != null) {
// Object normalizedRealValue = matchingRule.normalize(valueAttr.getRealValue());
// normalized = new PrismPropertyValue(normalizedRealValue);
// }
//
// ObjectFilter filter = EqualsFilter.createEqual(new ItemPath(ShadowType.F_ATTRIBUTES, assocAttrDef.getName()), assocAttrDef, normalized);
// ObjectQuery query = ObjectQuery.createObjectQuery(filter);
// ObjectQuery query = new ObjectQuery();
// query.setFilter(filter);

Expand Down Expand Up @@ -229,6 +243,21 @@ public boolean handle(PrismObject<ShadowType> entitlementShadow) {
}

}

private <T> ObjectQuery createQuery(RefinedAssociationDefinition assocDefType, RefinedAttributeDefinition assocAttrDef, ResourceAttribute<T> valueAttr) throws SchemaException{
MatchingRule matchingRule = matchingRuleRegistry.getMatchingRule(assocDefType
.getResourceObjectAssociationType().getMatchingRule(), valueAttr.getDefinition()
.getTypeName());
PrismPropertyValue normalized = valueAttr.getValue();
if (matchingRule != null) {
Object normalizedRealValue = matchingRule.normalize(valueAttr.getRealValue());
normalized = new PrismPropertyValue(normalizedRealValue);
}

ObjectFilter filter = EqualsFilter.createEqual(new ItemPath(ShadowType.F_ATTRIBUTES, assocAttrDef.getName()), assocAttrDef, normalized);
ObjectQuery query = ObjectQuery.createObjectQuery(filter);
return query;
}

//////////
// ADD
Expand Down Expand Up @@ -358,10 +387,12 @@ public <T> void collectEntitlementsAsObjectOperationDelete(ConnectorInstance con
throw new SchemaException("Value attribute "+valueAttrName+" has no more than one value; attribute defined in entitlement association '"+associationName+"' in "+resourceType);
}

ObjectQuery query = createQuery(assocDefType, assocAttrDef, valueAttr);

// ObjectFilter filter = EqualsFilter.createEqual(new ItemPath(ShadowType.F_ATTRIBUTES), assocAttrDef, valueAttr.getValue());
// ObjectFilter filter = InFilter.createIn(new ItemPath(ShadowType.F_ATTRIBUTES, assocAttrDef.getName()), assocAttrDef, valueAttr.getValue());
ObjectFilter filter = EqualsFilter.createEqual(new ItemPath(ShadowType.F_ATTRIBUTES, assocAttrDef.getName()), assocAttrDef, valueAttr.getValue());
ObjectQuery query = ObjectQuery.createObjectQuery(filter);
// ObjectFilter filter = EqualsFilter.createEqual(new ItemPath(ShadowType.F_ATTRIBUTES, assocAttrDef.getName()), assocAttrDef, valueAttr.getValue());
// ObjectQuery query = ObjectQuery.createObjectQuery(filter);
// new ObjectQuery();
// query.setFilter(filter);

Expand Down Expand Up @@ -400,7 +431,7 @@ public boolean handle(PrismObject<ShadowType> entitlementShadow) {
}
};
try {
LOGGER.trace("Searching for associations in deleted shadow, query: {}", filter);
LOGGER.trace("Searching for associations in deleted shadow, query: {}", query);
connector.search(entitlementOcDef, query, handler, attributesToReturn, parentResult);
} catch (TunnelException e) {
throw (SchemaException)e.getCause();
Expand Down
Expand Up @@ -1609,6 +1609,7 @@ private PrismObject<ShadowType> completeShadow(ConnectorInstance connector, Pris
PrismObject<ShadowType> entitlementRepoShadow = lookupOrCreateShadowInRepository(connector, entitlementShadow, entitlementObjectClassDef, resource, parentResult);
ObjectReferenceType shadowRefType = new ObjectReferenceType();
shadowRefType.setOid(entitlementRepoShadow.getOid());
shadowRefType.setType(ShadowType.COMPLEX_TYPE);
shadowAssociationType.setShadowRef(shadowRefType);
}
}
Expand Down

0 comments on commit 485a220

Please sign in to comment.