Skip to content

Commit

Permalink
EntityRegistry: more generics used, related fixes in ObjectDeltaUpdater
Browse files Browse the repository at this point in the history
No functionality was changed.
  • Loading branch information
virgo47 committed Mar 23, 2020
1 parent eee9272 commit d237c48
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 68 deletions.
Expand Up @@ -7,6 +7,26 @@

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

import static com.evolveum.midpoint.repo.sql.helpers.modify.DeltaUpdaterUtils.*;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.ManagedType;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.hibernate.collection.spi.PersistentCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.path.ItemName;
Expand Down Expand Up @@ -39,25 +59,6 @@
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 org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.hibernate.collection.spi.PersistentCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.ManagedType;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

import static com.evolveum.midpoint.repo.sql.helpers.modify.DeltaUpdaterUtils.*;

/**
* @author Viliam Repan (lazyman).
Expand Down Expand Up @@ -128,7 +129,7 @@ public <T extends ObjectType> RObject<T> modifyObject(Class<T> type, String oid,
//noinspection unchecked
RObject<T> object = session.byId(objectClass).getReference(oid);

ManagedType mainEntityType = entityRegistry.getJaxbMapping(type);
ManagedType<T> mainEntityType = entityRegistry.getJaxbMapping(type);

boolean shadowPendingOperationModified = false;

Expand Down Expand Up @@ -283,6 +284,7 @@ private boolean isDelete(ItemDelta delta) {

return false;
}

@SuppressWarnings("Duplicates")
private void handleWholeMetadata(Metadata<?> bean, ItemDelta delta) {
PrismValue value = null;
Expand Down Expand Up @@ -576,7 +578,7 @@ private static <T> void markNewValuesTransientAndAddToExistingNoFetch(Collection

/**
* Similar to DeltaUpdaterUtils.markNewValuesTransientAndAddToExisting but simpler.
*
* <p>
* We rely on the fact that SAVE/UPDATE is now cascaded to extension items.
*/
private static <T> void markNewValuesTransientAndAddToExistingNoFetchNoPersist(Collection<? extends RAnyValue<?>> dbCollection,
Expand All @@ -598,12 +600,12 @@ private void deleteExtensionValues(Collection<? extends RAnyValue<?>> dbCollecti
.collect(Collectors.toList());

boolean collectionLoaded = dbCollection instanceof PersistentCollection &&
((PersistentCollection) dbCollection).wasInitialized();
((PersistentCollection) dbCollection).wasInitialized();

// Note: as for 4.0 "no fetch" deletion is available only for ROExtString (MID-5558).
boolean noFetchDeleteSupported =
Boolean.TRUE.equals(RepoModifyOptions.getUseNoFetchExtensionValuesDeletion(ctx.options)) &&
rValuesToDelete.stream().allMatch(rValue -> rValue instanceof ROExtString);
rValuesToDelete.stream().allMatch(rValue -> rValue instanceof ROExtString);

//System.out.println("Collection loaded = " + collectionLoaded + ", noFetchDeleteSupported = " + noFetchDeleteSupported + " for " + rValuesToDelete);
if (!collectionLoaded && noFetchDeleteSupported) {
Expand Down Expand Up @@ -848,7 +850,7 @@ private Attribute findAttribute(TypeValuePair typeValuePair, String nameLocalPar
return attribute;
}

Attribute attributeOverride = entityRegistry.findAttributeOverride(typeValuePair.type, nameLocalPart);
Attribute<?, ?> attributeOverride = entityRegistry.findAttributeOverride(typeValuePair.type, nameLocalPart);
if (attributeOverride != null) {
return attributeOverride;
}
Expand Down Expand Up @@ -962,7 +964,7 @@ private void handleAttribute(Attribute attribute, Object bean, ItemDelta delta,
}
}

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

PrismValue prismValue;
Expand Down Expand Up @@ -997,7 +999,7 @@ private void handleBasicOrEmbedded(Object bean, ItemDelta<?,?> delta, Attribute
}
}

private PrismValue getSingleValue(Attribute<?,?> attribute, Collection<? extends PrismValue> valuesToSet) {
private PrismValue getSingleValue(Attribute<?, ?> attribute, Collection<? extends PrismValue> valuesToSet) {
Set<PrismValue> uniqueValues = new HashSet<>(valuesToSet);
// This uniqueness check might be too strict: the values can be different from the point of default equality,
// yet the final verdict (when applying the delta to prism structure) can be that they are equal.
Expand Down Expand Up @@ -1050,7 +1052,7 @@ private void handleOneToMany(Collection collection, ItemDelta delta, Attribute a
}

private Collection<PrismEntityPair> processDeltaValues(Collection<? extends PrismValue> values, Class outputType,
ItemDelta delta, Object bean) {
ItemDelta delta, Object bean) {
if (values == null) {
return new ArrayList<>();
}
Expand Down
Expand Up @@ -7,30 +7,30 @@

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

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.ManagedType;
import javax.xml.namespace.QName;

import org.hibernate.Metamodel;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.path.UniformItemPath;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.path.UniformItemPath;
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;
import com.evolveum.midpoint.repo.sql.query.definition.JaxbPath;
import com.evolveum.midpoint.repo.sql.query.definition.JaxbType;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import org.hibernate.Metamodel;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
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;
import java.util.Set;

/**
* @author Viliam Repan (lazyman)
Expand All @@ -45,30 +45,33 @@ public class EntityRegistry {

private Metamodel metamodel;

private Map<Class, ManagedType> jaxbMappings = new HashMap<>();
// we know that some <?> represent the same type in one entry, but there is no way to capture that information

private Map<Class<?>, ManagedType<?>> jaxbMappings = new HashMap<>();

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

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

@PostConstruct
public void init() {
LOGGER.debug("Starting initialization");

metamodel = sessionFactory.getMetamodel();

for (EntityType entity : metamodel.getEntities()) {
Class javaType = entity.getJavaType();
Ignore ignore = (Ignore) javaType.getAnnotation(Ignore.class);
for (EntityType<?> entity : metamodel.getEntities()) {
Class<?> javaType = entity.getJavaType();
Ignore ignore = javaType.getAnnotation(Ignore.class);
if (ignore != null) {
continue;
}

Class jaxb;
Class<?> jaxb;
if (RObject.class.isAssignableFrom(javaType)) {
jaxb = RObjectType.getType(javaType).getJaxbClass();
//noinspection unchecked,rawtypes
jaxb = RObjectType.getType((Class<? extends RObject>) javaType).getJaxbClass();
} else {
JaxbType jaxbType = (JaxbType) javaType.getAnnotation(JaxbType.class);
JaxbType jaxbType = javaType.getAnnotation(JaxbType.class);
if (jaxbType == null) {
throw new IllegalStateException("Unknown jaxb type for " + javaType.getName());
}
Expand All @@ -78,12 +81,12 @@ public void init() {
jaxbMappings.put(jaxb, entity);

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

for (Attribute attribute : (Set<Attribute>) entity.getAttributes()) {
Class jType = attribute.getJavaType();
JaxbPath[] paths = (JaxbPath[]) jType.getAnnotationsByType(JaxbPath.class);
for (Attribute<?, ?> attribute : entity.getAttributes()) {
Class<?> jType = attribute.getJavaType();
JaxbPath[] paths = jType.getAnnotationsByType(JaxbPath.class);
if (paths == null || paths.length == 0) {
paths = ((Method) attribute.getJavaMember()).getAnnotationsByType(JaxbPath.class);
}
Expand Down Expand Up @@ -121,33 +124,36 @@ public void init() {
LOGGER.debug("Initialization finished");
}

public ManagedType getJaxbMapping(Class jaxbType) {
return jaxbMappings.get(jaxbType);
public <T> ManagedType<T> getJaxbMapping(Class<T> jaxbType) {
//noinspection unchecked
return (ManagedType<T>) jaxbMappings.get(jaxbType);
}

public ManagedType getMapping(Class entityType) {
public <T> ManagedType<T> getMapping(Class<T> entityType) {
return metamodel.managedType(entityType);
}

public Attribute findAttribute(ManagedType type, String name) {
public <T> Attribute<T, ?> findAttribute(ManagedType<T> type, String name) {
try {
return type.getAttribute(name);
//noinspection unchecked
return (Attribute<T, ?>) type.getAttribute(name);
} catch (IllegalArgumentException ex) {
return null;
}
}

public Attribute findAttributeOverride(ManagedType type, String nameOverride) {
Map<String, Attribute> overrides = attributeNameOverrides.get(type);
public <T> Attribute<T, ?> findAttributeOverride(ManagedType<T> type, String nameOverride) {
Map<String, Attribute<?, ?>> overrides = attributeNameOverrides.get(type);
if (overrides == null) {
return null;
}

return overrides.get(nameOverride);
//noinspection unchecked
return (Attribute<T, ?>) overrides.get(nameOverride);
}

public boolean hasAttributePathOverride(ManagedType type, ItemPath pathOverride) {
Map<UniformItemPath, Attribute> overrides = attributeNamePathOverrides.get(type);
public boolean hasAttributePathOverride(ManagedType<?> type, ItemPath pathOverride) {
Map<UniformItemPath, Attribute<?, ?>> overrides = attributeNamePathOverrides.get(type);
if (overrides == null) {
return false;
}
Expand All @@ -163,8 +169,9 @@ public boolean hasAttributePathOverride(ManagedType type, ItemPath pathOverride)
return false;
}

public Attribute findAttributePathOverride(ManagedType type, ItemPath pathOverride) {
Map<UniformItemPath, Attribute> overrides = attributeNamePathOverrides.get(type);
public <T> Attribute<T,?> findAttributePathOverride(ManagedType<T> type, ItemPath pathOverride) {
//noinspection unchecked,rawtypes
Map<UniformItemPath, Attribute<T, ?>> overrides = (Map) attributeNamePathOverrides.get(type);
if (overrides == null) {
return null;
}
Expand Down

0 comments on commit d237c48

Please sign in to comment.