Skip to content

Commit

Permalink
fixed more todos
Browse files Browse the repository at this point in the history
  • Loading branch information
1azyman committed Jan 23, 2018
1 parent 70def51 commit b40b4cb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
Expand Up @@ -33,6 +33,7 @@
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.test.annotation.DirtiesContext;
Expand Down Expand Up @@ -114,6 +115,8 @@ public <T extends ObjectType> void addLinkRef() throws Exception {
ObjectDelta delta = ObjectDelta.createModificationReplaceProperty(UserType.class, oid, UserType.F_GIVEN_NAME,
prismContext, new PolyString("asdf", "asdf"));

delta.addModificationReplaceProperty(UserType.F_NAME, new PolyString("super name"));

delta.addModificationReplaceProperty(UserType.F_GIVEN_NAME, new PolyString("one"));
delta.addModificationReplaceProperty(UserType.F_FAMILY_NAME, new PolyString("one"));
delta.addModificationAddProperty(UserType.F_EMPLOYEE_TYPE, "one","two");
Expand Down
Expand Up @@ -68,7 +68,6 @@ public <T extends ObjectType> RObject<T> update(Class<T> type, String oid, Colle
return merge(objectToMerge, session);
}

// todo handle nameCopy/name correctly
// todo handle extension attributes

RObject object = session.byId(objectToMerge.getClass()).getReference(oid);
Expand All @@ -93,11 +92,7 @@ public <T extends ObjectType> RObject<T> update(Class<T> type, String oid, Colle
NameItemPathSegment nameSegment = (NameItemPathSegment) segment;
String nameLocalPart = nameSegment.getName().getLocalPart();

Attribute attribute = entityModificationRegistry.findAttribute(attributeStep.managedType, nameLocalPart);
if (attribute == null) {
attribute = entityModificationRegistry.findAttributeOverride(attributeStep.managedType, nameLocalPart);
}

Attribute attribute = findAttribute(attributeStep, nameLocalPart);
if (attribute == null) {
// there's no table/column that needs update
break;
Expand All @@ -110,6 +105,12 @@ public <T extends ObjectType> RObject<T> update(Class<T> type, String oid, Colle
}

handleAttribute(attribute, attributeStep.bean, delta);

if ("name".equals(attribute.getName()) && RObject.class.isAssignableFrom(attribute.getDeclaringType().getJavaType())) {
// we also need to handle "nameCopy" column
Attribute nameCopyAttribute = findAttribute(attributeStep, "nameCopy");
handleAttribute(nameCopyAttribute, attributeStep.bean, delta);
}
}
}

Expand All @@ -120,6 +121,15 @@ public <T extends ObjectType> RObject<T> update(Class<T> type, String oid, Colle
return objectToMerge;
}

private Attribute findAttribute(AttributeStep attributeStep, String nameLocalPart) {
Attribute attribute = entityModificationRegistry.findAttribute(attributeStep.managedType, nameLocalPart);
if (attribute != null) {
return attribute;
}

return entityModificationRegistry.findAttributeOverride(attributeStep.managedType, nameLocalPart);
}

private AttributeStep stepThroughAttribute(Attribute attribute, AttributeStep step, Iterator<ItemPathSegment> segments) {
Method method = (Method) attribute.getJavaMember();

Expand Down Expand Up @@ -154,8 +164,11 @@ private AttributeStep stepThroughAttribute(Attribute attribute, AttributeStep st
throw new RuntimeException("Can't go over collection"); // todo error handling
}
break;
case ONE_TO_ONE:
// todo implement, it's assignment extension
break;
default:
// throw new RuntimeException("Don't know what to do"); // todo error handling
// nothing to do for other cases
}

return step;
Expand All @@ -167,18 +180,7 @@ private void handleAttribute(Attribute attribute, Object bean, ItemDelta delta)
switch (attribute.getPersistentAttributeType()) {
case BASIC:
case EMBEDDED:
// todo qnames
// todo how to handle add/delete/replace
try {
Object realValue = delta.getAnyValue().getRealValue();
Class outputType = method.getReturnType();

realValue = prismEntityMapper.map(realValue, outputType);

PropertyUtils.setSimpleProperty(bean, attribute.getName(), realValue);
} catch (Exception ex) {
throw new RuntimeException(ex); //todo error handling
}
handleBasicOrEmbedded(bean, delta, attribute);
break;
case MANY_TO_MANY:
// not used in our mappings
Expand All @@ -200,6 +202,27 @@ private void handleAttribute(Attribute attribute, Object bean, ItemDelta delta)
}
}

private void handleBasicOrEmbedded(Object bean, ItemDelta delta, Attribute attribute) {
Class outputType = getRealOutputType(attribute);

// todo qnames

Object value;
if (delta.isDelete()) {
value = null;
} else {
value = delta.getAnyValue().getRealValue();
}

value = prismEntityMapper.map(value, outputType);

try {
PropertyUtils.setSimpleProperty(bean, attribute.getName(), value);
} catch (Exception ex) {
throw new SystemException("Couldn't set simple property for '" + attribute.getName() + "'", ex);
}
}

private void handleElementCollection(Collection collection, ItemDelta delta, Attribute attribute) {
handleOneToMany(collection, delta, attribute);
}
Expand Down

0 comments on commit b40b4cb

Please sign in to comment.