Skip to content

Commit

Permalink
Fixed searching by enums in extensions: MID-3801 (ActivationStatusTyp…
Browse files Browse the repository at this point in the history
…e cannot be cast to java.lang.String)

Now respecting indexed=false for all extension data types
  • Loading branch information
mederly committed Mar 8, 2017
1 parent 62581da commit ca4e1e3
Show file tree
Hide file tree
Showing 48 changed files with 271 additions and 3,391 deletions.
Expand Up @@ -16,6 +16,8 @@

package com.evolveum.midpoint.prism;

import javax.xml.namespace.QName;

/**
* Primarily for enums. (Experimental.)
*
Expand All @@ -25,4 +27,11 @@
*/
public interface SimpleTypeDefinition extends TypeDefinition {

enum DerivationMethod {
EXTENSION, RESTRICTION, SUBSTITUTION
}

QName getBaseTypeName();

DerivationMethod getDerivationMethod();
}
Expand Up @@ -25,8 +25,14 @@
*/
public class SimpleTypeDefinitionImpl extends TypeDefinitionImpl implements SimpleTypeDefinition {

public SimpleTypeDefinitionImpl(QName typeName, PrismContext prismContext) {
private QName baseTypeName;
private DerivationMethod derivationMethod; // usually RESTRICTION

public SimpleTypeDefinitionImpl(QName typeName, QName baseTypeName, DerivationMethod derivationMethod,
PrismContext prismContext) {
super(typeName, prismContext);
this.baseTypeName = baseTypeName;
this.derivationMethod = derivationMethod;
}

@Override
Expand All @@ -43,10 +49,19 @@ public String getDocClassName() {
return "simple type";
}

public QName getBaseTypeName() {
return baseTypeName;
}

@Override
public DerivationMethod getDerivationMethod() {
return derivationMethod;
}

@NotNull
@Override
public SimpleTypeDefinitionImpl clone() {
SimpleTypeDefinitionImpl clone = new SimpleTypeDefinitionImpl(typeName, prismContext);
SimpleTypeDefinitionImpl clone = new SimpleTypeDefinitionImpl(typeName, baseTypeName, derivationMethod, prismContext);
super.copyDefinitionData(clone);
return clone;
}
Expand Down
Expand Up @@ -20,14 +20,15 @@
import javax.xml.namespace.QName;

import com.evolveum.midpoint.prism.*;
import com.sun.xml.xsom.XSSimpleType;
import com.sun.xml.xsom.*;
import org.w3c.dom.Element;

import com.evolveum.midpoint.util.DisplayableValue;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.sun.xml.xsom.XSAnnotation;
import com.sun.xml.xsom.XSComplexType;
import com.sun.xml.xsom.XSParticle;

import static com.evolveum.midpoint.prism.SimpleTypeDefinition.DerivationMethod.EXTENSION;
import static com.evolveum.midpoint.prism.SimpleTypeDefinition.DerivationMethod.RESTRICTION;
import static com.evolveum.midpoint.prism.SimpleTypeDefinition.DerivationMethod.SUBSTITUTION;

/**
* @author semancik
Expand All @@ -46,7 +47,16 @@ public SimpleTypeDefinition createSimpleTypeDefinition(XSSimpleType simpleType,
PrismContext prismContext, XSAnnotation annotation) throws SchemaException {

QName typeName = new QName(simpleType.getTargetNamespace(), simpleType.getName());
return new SimpleTypeDefinitionImpl(typeName, prismContext);
XSType baseType = simpleType.getBaseType();
QName baseTypeName = baseType != null ? new QName(baseType.getTargetNamespace(), baseType.getName()) : null;
SimpleTypeDefinition.DerivationMethod derivationMethod;
switch (simpleType.getDerivationMethod()) {
case XSSimpleType.EXTENSION: derivationMethod = EXTENSION; break;
case XSSimpleType.RESTRICTION: derivationMethod = RESTRICTION; break;
case XSSimpleType.SUBSTITUTION: derivationMethod = SUBSTITUTION; break;
default: derivationMethod = null; // TODO are combinations allowed? e.g. EXTENSION+SUBSTITUTION?
}
return new SimpleTypeDefinitionImpl(typeName, baseTypeName, derivationMethod, prismContext);
}

public <T> PrismPropertyDefinition<T> createPropertyDefinition(QName elementName, QName typeName, ComplexTypeDefinition complexTypeDefinition,
Expand Down
Expand Up @@ -18,40 +18,25 @@

import com.evolveum.midpoint.audit.api.AuditService;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.prism.query.QueryJaxbConvertor;
import com.evolveum.midpoint.prism.util.PrismTestUtil;
import com.evolveum.midpoint.repo.api.RepositoryService;
import com.evolveum.midpoint.repo.sql.helpers.BaseHelper;
import com.evolveum.midpoint.repo.sql.query.QueryEngine;
import com.evolveum.midpoint.repo.sql.query.RQuery;
import com.evolveum.midpoint.repo.sql.query.RQueryCriteriaImpl;
import com.evolveum.midpoint.repo.sql.query.RQueryImpl;
import com.evolveum.midpoint.repo.sql.util.HibernateToSqlTranslator;
import com.evolveum.midpoint.repo.sql.util.RUtil;
import com.evolveum.midpoint.schema.MidPointPrismContextFactory;
import com.evolveum.midpoint.schema.constants.MidPointConstants;
import com.evolveum.midpoint.schema.util.ObjectQueryUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.prism.xml.ns._public.query_3.QueryType;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.dialect.H2Dialect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.AssertJUnit;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.*;
import org.xml.sax.SAXException;

import java.io.File;
Expand Down Expand Up @@ -84,7 +69,7 @@ public class BaseSQLRepoTest extends AbstractTestNGSpringContextTests {
@Autowired
protected SessionFactory factory;

protected static Set<Class> initializedClasses = new HashSet<Class>();
protected static Set<Class> initializedClasses = new HashSet<>();

@BeforeSuite
public void setup() throws SchemaException, SAXException, IOException {
Expand Down Expand Up @@ -171,56 +156,8 @@ protected void close(Session session) {
session.close();
}

protected <T extends ObjectType> String getInterpretedQuery(Session session, Class<T> type, ObjectQuery query)
throws Exception {
return getInterpretedQuery(session, type, query, false);
}

protected <T extends ObjectType> String getInterpretedQuery(Session session, Class<T> type, ObjectQuery query,
boolean interpretCount) throws Exception {

LOGGER.info("QUERY TYPE TO CONVERT : {}", (query.getFilter() != null ? query.getFilter().debugDump(3) : null));

QueryEngine engine = new QueryEngine(baseHelper.getConfiguration(), prismContext);
RQuery rQuery = engine.interpret(query, type, null, interpretCount, session);
//just test if DB will handle it or throws some exception
if (interpretCount) {
rQuery.uniqueResult();
} else {
rQuery.list();
}

if (rQuery instanceof RQueryCriteriaImpl) {
Criteria criteria = ((RQueryCriteriaImpl) rQuery).getCriteria();
return HibernateToSqlTranslator.toSql(criteria);
}

return HibernateToSqlTranslator.toSql(factory, ((RQueryImpl) rQuery).getQuery().getQueryString());
}

protected String hqlToSql(String hql) {
return HibernateToSqlTranslator.toSql(factory, hql);
}

protected <T extends ObjectType> String getInterpretedQuery(Session session, Class<T> type, File file) throws
Exception {
return getInterpretedQuery(session, type, file, false);
}

protected <T extends ObjectType> String getInterpretedQuery(Session session, Class<T> type, File file,
boolean interpretCount) throws Exception {

QueryType queryType = PrismTestUtil.parseAtomicValue(file, QueryType.COMPLEX_TYPE);

LOGGER.info("QUERY TYPE TO CONVERT : {}", ObjectQueryUtil.dump(queryType, prismContext));

ObjectQuery query = null;
try {
query = QueryJaxbConvertor.createObjectQuery(type, queryType, prismContext);
} catch (Exception ex) {
LOGGER.info("error while converting query: " + ex.getMessage(), ex);
}

return getInterpretedQuery(session, type, query, interpretCount);
}
}
Expand Up @@ -2614,6 +2614,37 @@ public void test640queryAssignmentExtensionBoolean() throws Exception {
}
}

@Test
public void test650QueryExtensionEnum() throws Exception {
Session session = open();
try {
ObjectQuery query = QueryBuilder.queryFor(UserType.class, prismContext)
.item(F_EXTENSION, new QName("overrideActivation")).eq(ActivationStatusType.ENABLED)
.build();
String real = getInterpretedQuery2(session, UserType.class, query);
String expected = "select\n"
+ " u.fullObject,\n"
+ " u.stringsCount,\n"
+ " u.longsCount,\n"
+ " u.datesCount,\n"
+ " u.referencesCount,\n"
+ " u.polysCount,\n"
+ " u.booleansCount\n"
+ "from\n"
+ " RUser u\n"
+ " left join u.strings s with (\n"
+ " s.ownerType = :ownerType and\n"
+ " s.name = :name\n"
+ ")\n"
+ "where\n"
+ " s.value = :value\n";
assertEqualsIgnoreWhitespace(expected, real);
} finally {
close(session);
}
}


@Test
public void test700QueryCertCaseAll() throws Exception {
Session session = open();
Expand Down
Expand Up @@ -170,6 +170,13 @@
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="overrideActivation" type="c:ActivationStatusType" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:appinfo>
<a:indexed>true</a:indexed>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>

Expand Down
Expand Up @@ -19,10 +19,8 @@
import com.evolveum.midpoint.repo.api.RepositoryService;
import com.evolveum.midpoint.repo.api.RepositoryServiceFactory;
import com.evolveum.midpoint.repo.api.RepositoryServiceFactoryException;
import com.evolveum.midpoint.repo.sql.query.QueryDefinitionRegistry;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
Expand All @@ -32,7 +30,6 @@
import org.hibernate.dialect.H2Dialect;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
Expand Down Expand Up @@ -138,13 +135,6 @@ public synchronized void init(Configuration configuration) throws RepositoryServ
LOGGER.info("Repository is not running in embedded mode.");
}

try {
QueryDefinitionRegistry.getInstance();
} catch (Exception ex) {
throw new RepositoryServiceFactoryException("Couldn't initialize query registry, reason: "
+ ex.getMessage(), ex);
}

performanceMonitor = new SqlPerformanceMonitor();
performanceMonitor.initialize(this);

Expand Down

0 comments on commit ca4e1e3

Please sign in to comment.