Skip to content

Commit

Permalink
more bugfixing, some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
1azyman committed Jan 26, 2018
1 parent a837ae4 commit 1df9b8a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 15 deletions.
Expand Up @@ -71,7 +71,7 @@ public class ObjectDeltaUpdater {
private static final Trace LOGGER = TraceManager.getTrace(ObjectDeltaUpdater.class);

@Autowired
private EntityRegistry entityModificationRegistry;
private EntityRegistry entityRegistry;
@Autowired
private PrismContext prismContext;
@Autowired
Expand All @@ -97,10 +97,12 @@ public <T extends ObjectType> RObject<T> modifyObject(Class<T> type, String oid,

// todo mark newly added containers/references as transient

// todo validate metadata/*, assignment/metadata/*, assignment/construction/resourceRef changes

Class<? extends RObject> objectClass = RObjectType.getByJaxbType(type).getClazz();
RObject<T> object = session.byId(objectClass).getReference(oid);

ManagedType mainEntityType = entityModificationRegistry.getJaxbMapping(type);
ManagedType mainEntityType = entityRegistry.getJaxbMapping(type);

for (ItemDelta delta : modifications) {
ItemPath path = delta.getPath();
Expand Down Expand Up @@ -129,6 +131,28 @@ public <T extends ObjectType> RObject<T> modifyObject(Class<T> type, String oid,
}

Attribute attribute = findAttribute(attributeStep, nameLocalPart);
if (attribute == null && segments.hasNext()) {
// try to search path overrides like metadata/* or assignment/metadata/* or assignment/construction/resourceRef
ItemPath subPath = new ItemPath(nameSegment);
while (segments.hasNext()) {
if (!entityRegistry.hasAttributePathOverride(attributeStep.managedType, subPath)) {
subPath = subPath.allUpToLastNamed();
break;
}

segment = segments.next();
if (!(segment instanceof NameItemPathSegment)) {
throw new SystemException("Segment '" + segment + "' in '" + path + "' is not a name item");
}

nameSegment = (NameItemPathSegment) segment;

subPath = subPath.append(nameSegment.getName());
}

attribute = entityRegistry.findAttributePathOverride(attributeStep.managedType, subPath);
}

if (attribute == null) {
// there's no table/column that needs update
break;
Expand Down Expand Up @@ -382,20 +406,20 @@ private void handleObjectExtensionOrAttributesDelta(RObject object, ItemDelta de
}

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

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

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

switch (attribute.getPersistentAttributeType()) {
case EMBEDDED:
step.managedType = entityModificationRegistry.getMapping(attribute.getJavaType());
step.managedType = entityRegistry.getMapping(attribute.getJavaType());
Object child = invoke(step.bean, method);
if (child == null) {
// embedded entity doesn't exist we have to create it first, so it can be populated later
Expand Down Expand Up @@ -425,7 +449,7 @@ private AttributeStep stepThroughAttribute(Attribute attribute, AttributeStep st
for (Container o : (Collection<Container>) c) {
long l = o.getId().longValue();
if (l == id.getId()) {
step.managedType = entityModificationRegistry.getMapping(clazz);
step.managedType = entityRegistry.getMapping(clazz);
step.bean = o;

found = true;
Expand Down
Expand Up @@ -16,6 +16,7 @@

package com.evolveum.midpoint.repo.sql.helpers.modify;

import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.repo.sql.data.common.RObject;
import com.evolveum.midpoint.repo.sql.data.common.other.RObjectType;
import com.evolveum.midpoint.repo.sql.query.definition.JaxbName;
Expand All @@ -32,6 +33,7 @@
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.ManagedType;
import javax.xml.namespace.QName;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -54,6 +56,8 @@ public class EntityRegistry {

private Map<ManagedType, Map<String, Attribute>> attributeNameOverrides = new HashMap<>();

private Map<ManagedType, Map<ItemPath, Attribute>> attributeNamePathOverrides = new HashMap<>();

@PostConstruct
public void init() {
LOGGER.debug("Starting initialization");
Expand Down Expand Up @@ -82,6 +86,7 @@ public void init() {

// create override map
Map<String, Attribute> overrides = new HashMap<>();
Map<ItemPath, Attribute> pathOverrides = new HashMap<>();

for (Attribute attribute : (Set<Attribute>) entity.getAttributes()) {
Class jType = attribute.getJavaType();
Expand All @@ -94,14 +99,24 @@ public void init() {
continue;
}

for (JaxbName name : path.itemPath()) {
overrides.put(name.localPart(), attribute);
JaxbName[] names = path.itemPath();
if (names.length == 1) {
overrides.put(names[0].localPart(), attribute);
} else {
ItemPath customPath = ItemPath.EMPTY_PATH;
for (JaxbName name : path.itemPath()) {
customPath = customPath.append(new QName(name.namespace(), name.localPart()));
}
pathOverrides.put(customPath, attribute);
}
}

if (!overrides.isEmpty()) {
attributeNameOverrides.put(entity, overrides);
}
if (!pathOverrides.isEmpty()) {
attributeNamePathOverrides.put(entity, pathOverrides);
}
}

LOGGER.debug("Initialization finished");
Expand Down Expand Up @@ -131,4 +146,30 @@ public Attribute findAttributeOverride(ManagedType type, String nameOverride) {

return overrides.get(nameOverride);
}

public boolean hasAttributePathOverride(ManagedType type, ItemPath pathOverride) {
Map<ItemPath, Attribute> overrides = attributeNamePathOverrides.get(type);
if (overrides == null) {
return false;
}

ItemPath namedOnly = pathOverride.namedSegmentsOnly();

for (ItemPath path : overrides.keySet()) {
if (path.startsWith(namedOnly) || path.equals(namedOnly)) {
return true;
}
}

return false;
}

public Attribute findAttributePathOverride(ManagedType type, ItemPath pathOverride) {
Map<ItemPath, Attribute> overrides = attributeNamePathOverrides.get(type);
if (overrides == null) {
return null;
}

return overrides.get(pathOverride);
}
}
Expand Up @@ -22,7 +22,7 @@
import java.lang.annotation.Target;

/**
* // todo documentation
* Used to ignore some entities when populating {@link EntityRegistry} during initialization
*
* @author Viliam Repan (lazyman)
*/
Expand Down
Expand Up @@ -22,11 +22,9 @@
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.repo.api.RepositoryService;
import com.evolveum.midpoint.repo.sql.data.RepositoryContext;
import com.evolveum.midpoint.repo.sql.data.common.ObjectReference;
import com.evolveum.midpoint.repo.sql.data.common.RObject;
import com.evolveum.midpoint.repo.sql.data.common.RObjectReference;
import com.evolveum.midpoint.repo.sql.data.common.container.RAssignment;
import com.evolveum.midpoint.repo.sql.data.common.container.RExclusion;
import com.evolveum.midpoint.repo.sql.data.common.container.ROperationExecution;
import com.evolveum.midpoint.repo.sql.data.common.container.RTrigger;
import com.evolveum.midpoint.repo.sql.data.common.embedded.*;
Expand Down Expand Up @@ -106,6 +104,7 @@ public <I, O> O map(I input, Class<O> outputType, MapperContext context) {
return mapper.map(input, context);
}

// todo implement transformation from prism to entity
//RObjectTextInfo
//RLookupTableRow
//RAccessCertificationWorkItem
Expand Down Expand Up @@ -133,13 +132,11 @@ public <O> O mapPrismValue(PrismValue input, Class<O> outputType, MapperContext
container.setupContainerValue((PrismContainerValue) input);

return map(container, outputType, context);
} catch (Exception ex) {
throw new RuntimeException(ex); //todo error handling
} catch (InstantiationException | IllegalAccessException ex) {
throw new SystemException("Couldn't create instance of container '" + inputType + "'");
}
}

// todo implement transformation from prism to entity

return (O) input;
}

Expand Down

0 comments on commit 1df9b8a

Please sign in to comment.