Skip to content

Commit

Permalink
repo-sql-impl: massive query(2)/definition cleanup + bit of generics
Browse files Browse the repository at this point in the history
  • Loading branch information
virgo47 committed Jun 17, 2020
1 parent f02ac2e commit 3869db3
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 137 deletions.
Expand Up @@ -22,7 +22,6 @@
import com.evolveum.midpoint.repo.sql.data.common.id.RContainerId;
import com.evolveum.midpoint.repo.sql.query.definition.JaxbType;
import com.evolveum.midpoint.repo.sql.query.definition.OwnerIdGetter;
import com.evolveum.midpoint.repo.sql.query.definition.QueryEntity;
import com.evolveum.midpoint.repo.sql.query2.definition.IdQueryProperty;
import com.evolveum.midpoint.repo.sql.query2.definition.NotQueryable;
import com.evolveum.midpoint.repo.sql.util.DtoTranslationException;
Expand All @@ -34,7 +33,6 @@

@JaxbType(type = OperationExecutionType.class)
@Entity
@QueryEntity
@IdClass(RContainerId.class)
@Table(name = "m_operation_execution", indexes = {
@Index(name = "iOpExecTaskOid", columnList = "taskRef_targetOid"),
Expand Down
@@ -1,26 +1,24 @@
/*
* Copyright (c) 2010-2013 Evolveum and contributors
* Copyright (c) 2010-2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/

package com.evolveum.midpoint.repo.sql.query.definition;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* @author lazyman
*/
@Target({TYPE, METHOD, FIELD})
@Target({ TYPE, METHOD, FIELD })
@Retention(RUNTIME)
public @interface JaxbType {

Class type();
Class<?> type();
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2015 Evolveum and contributors
* Copyright (c) 2010-2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
Expand All @@ -18,9 +18,10 @@
* @author lazyman
* @author mederly
*/
@Target({ElementType.METHOD})
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface OwnerGetter {

Class<?> ownerClass();
// hard to generify, can be <? extends RObject> but also a Container<? extends RObject>
Class ownerClass();
}
Expand Up @@ -19,11 +19,7 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface QueryEntity {

VirtualProperty[] properties() default {};

VirtualCollection[] collections() default {};

VirtualEntity[] entities() default {};

VirtualAny[] anyElements() default {};
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2013 Evolveum and contributors
* Copyright (c) 2010-2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
Expand All @@ -15,19 +15,19 @@
/**
* @author lazyman
*/
@Target({ElementType.TYPE})
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface VirtualCollection {

JaxbName jaxbName();

Class jaxbType();
Class<?> jaxbType();

String jpaName();

Class jpaType();
Class<?> jpaType();

VirtualQueryParam[] additionalParams() default {};

Class collectionType();
Class<?> collectionType();
}
@@ -1,13 +1,29 @@
/*
* Copyright (c) 2010-2015 Evolveum and contributors
* Copyright (c) 2010-2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/

package com.evolveum.midpoint.repo.sql.query2.definition;

import com.evolveum.midpoint.prism.path.*;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.persistence.*;
import javax.xml.namespace.QName;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Index;

import com.evolveum.midpoint.prism.path.IdentifierPathSegment;
import com.evolveum.midpoint.prism.path.ItemName;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.path.ParentPathSegment;
import com.evolveum.midpoint.repo.sql.data.Marker;
import com.evolveum.midpoint.repo.sql.data.common.ObjectReference;
import com.evolveum.midpoint.repo.sql.data.common.RObject;
Expand All @@ -19,23 +35,6 @@
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Index;

import javax.persistence.Embeddable;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.Lob;
import javax.persistence.Transient;
import javax.xml.namespace.QName;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
* @author lazyman
Expand All @@ -45,7 +44,7 @@ public class ClassDefinitionParser {

private static final Trace LOGGER = TraceManager.getTrace(ClassDefinitionParser.class);

public JpaEntityDefinition parseRootClass(Class jpaClass) {
public JpaEntityDefinition parseRootClass(Class<?> jpaClass) {
return parseClass(jpaClass);
}

Expand Down Expand Up @@ -107,7 +106,7 @@ private JpaLinkDefinition parseMethod(Method method) {

ItemPath itemPath = getJaxbName(method);
String jpaName = getJpaName(method);
Class jpaClass = getClass(returnedContentType);
Class<? extends RObject> jpaClass = getClass(returnedContentType);

// sanity check
if (Set.class.isAssignableFrom(jpaClass)) {
Expand Down Expand Up @@ -167,9 +166,10 @@ private JpaLinkDefinition parseMethod(Method method) {
return linkDefinition;
}

private Class<?> getClass(Type type) {
private Class<? extends RObject> getClass(Type type) {
if (type instanceof Class) {
return ((Class) type);
//noinspection unchecked
return ((Class<? extends RObject>) type);
} else if (type instanceof ParameterizedType) {
return getClass(((ParameterizedType) type).getRawType());
} else {
Expand All @@ -186,12 +186,11 @@ private void addVirtualDefinitions(Class jpaClass, JpaEntityDefinition entityDef
}

private void addVirtualDefinitionsForClass(Class jpaClass, JpaEntityDefinition entityDef) {
if (!jpaClass.isAnnotationPresent(QueryEntity.class)) {
QueryEntity qEntity = (QueryEntity) jpaClass.getAnnotation(QueryEntity.class);
if (qEntity == null) {
return;
}

QueryEntity qEntity = (QueryEntity) jpaClass.getAnnotation(QueryEntity.class);

for (VirtualAny any : qEntity.anyElements()) {
ItemName jaxbName = new ItemName(any.jaxbNameNamespace(), any.jaxbNameLocalPart());
VirtualAnyContainerDefinition def = new VirtualAnyContainerDefinition(any.ownerType());
Expand All @@ -208,31 +207,12 @@ private void addVirtualDefinitionsForClass(Class jpaClass, JpaEntityDefinition e
JpaLinkDefinition linkDefinition = new JpaLinkDefinition<>(jaxbName, jpaName, colSpec, false, content);
entityDef.addDefinition(linkDefinition);
}

for (VirtualEntity entity : qEntity.entities()) {
ItemName jaxbName = createItemName(entity.jaxbName());
String jpaName = normalizeJpaName(entity.jpaName());
if (jpaName != null) {
throw new IllegalStateException("Only self-pointing virtual entities are supported for now; this one is not: " + jaxbName + " in " + entityDef);
}
JpaDataNodeDefinition target = new JpaEntityPointerDefinition(entityDef); // pointer to avoid loops
JpaLinkDefinition linkDefinition = new JpaLinkDefinition<>(jaxbName, jpaName, null, false, target);
entityDef.addDefinition(linkDefinition);
}
}

private ItemName createItemName(JaxbName name) {
return new ItemName(name.namespace(), name.localPart());
}

private String normalizeJpaName(String name) {
if (StringUtils.isEmpty(name)) {
return null; // "" -> null
} else {
return name;
}
}

private boolean isEntity(Class type) {
if (RPolyString.class.isAssignableFrom(type)) {
//it's hibernate entity but from prism point of view it's property
Expand Down Expand Up @@ -263,7 +243,7 @@ private ItemPath getJaxbName(Method method) {

// second parameter is just to optimize
private Class getJaxbClass(Method method, Class returnedClass) {
JaxbType annotation = (JaxbType) method.getAnnotation(JaxbType.class);
JaxbType annotation = method.getAnnotation(JaxbType.class);
if (annotation != null) {
return annotation.type();
}
Expand Down Expand Up @@ -304,6 +284,6 @@ private String getPropertyName(String methodName) {
}

char first = Character.toLowerCase(methodName.charAt(startIndex));
return Character.toString(first) + StringUtils.substring(methodName, startIndex + 1, methodName.length());
return first + StringUtils.substring(methodName, startIndex + 1, methodName.length());
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2015 Evolveum and contributors
* Copyright (c) 2010-2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
Expand All @@ -19,9 +19,10 @@
/**
* @author lazyman
*/
public class JpaAnyContainerDefinition extends JpaDataNodeDefinition {
public class JpaAnyContainerDefinition<T extends JpaAnyContainerDefinition<T>>
extends JpaDataNodeDefinition<T> {

public JpaAnyContainerDefinition(Class jpaClass) {
public JpaAnyContainerDefinition(Class<? extends RObject> jpaClass) {
super(jpaClass, null);
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2015 Evolveum and contributors
* Copyright (c) 2010-2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
Expand All @@ -16,7 +16,7 @@
*
* @author mederly
*/
public class JpaAnyPropertyDefinition extends JpaPropertyDefinition {
public class JpaAnyPropertyDefinition extends JpaPropertyDefinition<JpaAnyPropertyDefinition> {

// enumerated extension items are not supported
JpaAnyPropertyDefinition(Class jpaClass, Class jaxbClass) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2017 Evolveum and contributors
* Copyright (c) 2010-2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
Expand All @@ -10,13 +10,13 @@
/**
* Specifies "any" reference. In contrast to other JPA definitions, it is not derived by analyzing R-class
* structure, but created on demand in the process if ItemPath translation.
*
* <p>
* It was created to ensure consistency of resolution mechanism, which should provide
* HQL property + JPA definition for any item path provided.
*
* @author mederly
*/
public class JpaAnyReferenceDefinition extends JpaReferenceDefinition {
public class JpaAnyReferenceDefinition extends JpaReferenceDefinition<JpaAnyReferenceDefinition> {

public JpaAnyReferenceDefinition(Class jpaClass, Class referencedEntityJpaClass) {
super(jpaClass, referencedEntityJpaClass);
Expand Down

0 comments on commit 3869db3

Please sign in to comment.