Skip to content

Commit

Permalink
improved querying for type filters MID-1881
Browse files Browse the repository at this point in the history
  • Loading branch information
1azyman committed May 14, 2014
1 parent 98b2981 commit e14f56f
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 121 deletions.
Expand Up @@ -29,22 +29,32 @@
public class TypeFilter extends ObjectFilter {

private QName type;
private ObjectFilter filter;

public TypeFilter(QName type) {
public TypeFilter(QName type, ObjectFilter filter) {
this.type = type;
this.filter = filter;
}

public QName getType() {
return type;
}

public static TypeFilter createType(QName type) {
return new TypeFilter(type);
public ObjectFilter getFilter() {
return filter;
}


public void setFilter(ObjectFilter filter) {
this.filter = filter;
}

public static TypeFilter createType(QName type, ObjectFilter filter) {
return new TypeFilter(type, filter);
}

@Override
public ObjectFilter clone() {
return new TypeFilter(type);
return new TypeFilter(type, filter.clone());
}

@Override
Expand All @@ -63,6 +73,10 @@ public String debugDump(int indent) {
DebugUtil.indentDebugDump(sb, indent);
sb.append("TYPE: ");
sb.append(type.getLocalPart());
sb.append('\n');
if (filter != null) {
sb.append(filter.debugDump(indent + 1));
}

return sb.toString();
}
Expand Down
Expand Up @@ -22,6 +22,8 @@
import org.apache.commons.lang.Validate;

import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.List;

/**
* @author lazyman
Expand Down Expand Up @@ -283,5 +285,13 @@ public static Class getClassFromRestType(String restType) {
throw new IllegalArgumentException("Not suitable class found for rest type: " + restType);
}

public static List<Class<? extends ObjectType>> getAllObjectTypes() {
List<Class<? extends ObjectType>> list = new ArrayList<>();
for (ObjectTypes t : ObjectTypes.values()) {
list.add(t.getClassDefinition());
}

return list;
}
}

Expand Up @@ -16,56 +16,26 @@

package com.evolveum.midpoint.repo.sql;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;

import com.evolveum.midpoint.prism.Objectable;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.query.*;
import com.evolveum.midpoint.repo.sql.data.common.*;
import com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString;
import com.evolveum.midpoint.repo.sql.data.common.type.RObjectExtensionType;
import com.evolveum.midpoint.repo.sql.query.QueryDefinitionRegistry;
import com.evolveum.midpoint.repo.sql.query.restriction.Restriction;
import com.evolveum.midpoint.repo.sql.type.XMLGregorianCalendarType;

import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.*;
import org.hibernate.sql.JoinType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.testng.AssertJUnit;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;

import com.evolveum.midpoint.prism.PrismObjectDefinition;
import com.evolveum.midpoint.prism.match.PolyStringNormMatchingRule;
import com.evolveum.midpoint.prism.match.PolyStringOrigMatchingRule;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.prism.query.*;
import com.evolveum.midpoint.prism.schema.SchemaRegistry;
import com.evolveum.midpoint.prism.util.PrismTestUtil;
import com.evolveum.midpoint.prism.xml.XmlTypeConverter;
import com.evolveum.midpoint.repo.sql.data.common.*;
import com.evolveum.midpoint.repo.sql.data.common.enums.RActivationStatus;
import com.evolveum.midpoint.repo.sql.data.common.enums.RTaskExecutionStatus;
import com.evolveum.midpoint.repo.sql.data.common.other.RAssignmentOwner;
import com.evolveum.midpoint.repo.sql.data.common.other.RObjectType;
import com.evolveum.midpoint.repo.sql.data.common.type.RAssignmentExtensionType;
import com.evolveum.midpoint.repo.sql.data.common.type.RObjectExtensionType;
import com.evolveum.midpoint.repo.sql.query.QueryException;
import com.evolveum.midpoint.repo.sql.type.XMLGregorianCalendarType;
import com.evolveum.midpoint.repo.sql.util.HibernateToSqlTranslator;
import com.evolveum.midpoint.schema.MidPointPrismContextFactory;
import com.evolveum.midpoint.schema.constants.MidPointConstants;
Expand All @@ -75,6 +45,30 @@
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.*;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.*;
import org.hibernate.sql.JoinType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.testng.AssertJUnit;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;

import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
* @author lazyman
Expand Down Expand Up @@ -947,8 +941,8 @@ public void queryOrgTreeFindOrgs() throws Exception {
try {
Query query = session.createQuery(
"select o.fullObject,o.stringsCount,o.longsCount,o.datesCount,o.referencesCount,o.polysCount from " +
"ROrg as o where o.oid in (select distinct p.ownerOid from RParentOrgRef p where p.targetOid=:oid)"
);
"ROrg as o where o.oid in (select distinct p.ownerOid from RParentOrgRef p where p.targetOid=:oid)"
);
query.setString("oid", "1234");

String expected = HibernateToSqlTranslator.toSql(factory, query.getQueryString());
Expand Down Expand Up @@ -1160,8 +1154,8 @@ public void test310QueryNameAndOrg() throws Exception {

Conjunction c = Restrictions.conjunction();
c.add(Restrictions.and(
Restrictions.eq("u.name.orig", "cpt. Jack Sparrow"),
Restrictions.eq("u.name.norm", "cpt jack sparrow")));
Restrictions.eq("u.name.orig", "cpt. Jack Sparrow"),
Restrictions.eq("u.name.norm", "cpt jack sparrow")));
c.add(Subqueries.propertyIn(mainAlias + ".oid", detached));
main.add(c);

Expand All @@ -1187,7 +1181,7 @@ public void test310QueryNameAndOrg() throws Exception {
}
}

@Test(enabled=false)
@Test(enabled = false)
public void test320QueryEmployeeTypeAndOrgType() throws Exception {
Session session = open();

Expand All @@ -1204,7 +1198,7 @@ public void test320QueryEmployeeTypeAndOrgType() throws Exception {
addFullObjectProjectionList("o", list, false);
main.setProjection(list);

List l= main.list();
List l = main.list();
l.size();
String expected = HibernateToSqlTranslator.toSql(main);
LOGGER.info("expected query>\n{}", new Object[]{expected});
Expand Down Expand Up @@ -1280,7 +1274,7 @@ public void test340queryObjectClassTypeUser() throws Exception {
main.add(Restrictions.eq("o." + RObject.F_OBJECT_TYPE_CLASS, RObjectType.USER));
String expected = HibernateToSqlTranslator.toSql(main);

TypeFilter type = new TypeFilter(UserType.COMPLEX_TYPE);
TypeFilter type = TypeFilter.createType(UserType.COMPLEX_TYPE, null);
String real = getInterpretedQuery(session, ObjectType.class, ObjectQuery.createObjectQuery(type));

LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[]{expected, real});
Expand All @@ -1307,7 +1301,7 @@ public void test350queryObjectClassTypeAbstractRole() throws Exception {
main.add(Restrictions.in("o." + RObject.F_OBJECT_TYPE_CLASS, list));
String expected = HibernateToSqlTranslator.toSql(main);

TypeFilter type = new TypeFilter(AbstractRoleType.COMPLEX_TYPE);
TypeFilter type = TypeFilter.createType(AbstractRoleType.COMPLEX_TYPE, null);
String real = getInterpretedQuery(session, ObjectType.class, ObjectQuery.createObjectQuery(type));

LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[]{expected, real});
Expand Down Expand Up @@ -1346,7 +1340,7 @@ public void test360queryMetadataTimestamp() throws Exception {
}

@Test
public void test370queryObjectypeByTypeAndLocality() throws Exception {
public void test370queryObjectypeByTypeUserAndLocality() throws Exception {
Session session = open();
try {
Criteria main = session.createCriteria(RObject.class, "o");
Expand All @@ -1357,20 +1351,82 @@ public void test370queryObjectypeByTypeAndLocality() throws Exception {
Conjunction c = Restrictions.conjunction();
main.add(c);
c.add(Restrictions.eq("o." + RObject.F_OBJECT_TYPE_CLASS, RObjectType.USER));
c.add(Restrictions.and(Restrictions.eq("o.localityUser.orig", "Caribbean"),
Restrictions.eq("o.localityUser.norm", "caribbean")));

String expected = HibernateToSqlTranslator.toSql(main);

EqualsFilter eq = EqualsFilter.createEqual(new ItemPath(UserType.F_LOCALITY), UserType.class, prismContext,
new PolyString("Caribbean", "caribbean"));
TypeFilter type = TypeFilter.createType(UserType.COMPLEX_TYPE, eq);

String real = getInterpretedQuery(session, ObjectType.class, ObjectQuery.createObjectQuery(type));

LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[]{expected, real});
AssertJUnit.assertEquals(expected, real);

checkQueryTypeAlias(real, "m_user", "locality_orig", "locality_norm");
} finally {
close(session);
}
}

/**
* This checks aliases, if they were generated correctly for query. Alias for table as "table" parameter
* must be used for columns in "properties" parameter.
*
* @param query
* @param table
* @param properties
*/
private void checkQueryTypeAlias(String query, String table, String... properties) {
String[] array = query.split(" ");
String alias = null;
for (int i = 0; i < array.length; i++) {
if (table.equals(array[i])) {
alias = array[i + 1];
break;
}
}
AssertJUnit.assertNotNull(alias);

for (String property : properties) {
for (String token : array) {
if (token.endsWith(property + "=?") && !token.startsWith("(" + alias + ".")) {
AssertJUnit.fail("Property '" + property + "' doesn't have proper alias '"
+ alias + "' in token '" + token + "'");
}
}
}
}

@Test
public void test375queryObjectypeByTypeOrgAndLocality() throws Exception {
Session session = open();
try {
Criteria main = session.createCriteria(RObject.class, "o");
ProjectionList projections = Projections.projectionList();
addFullObjectProjectionList("o", projections, false);
main.setProjection(projections);

Conjunction c = Restrictions.conjunction();
main.add(c);
c.add(Restrictions.eq("o." + RObject.F_OBJECT_TYPE_CLASS, RObjectType.ORG));
c.add(Restrictions.and(Restrictions.eq("o.locality.orig", "Caribbean"),
Restrictions.eq("o.locality.norm", "caribbean")));

String expected = HibernateToSqlTranslator.toSql(main);

TypeFilter type = TypeFilter.createType(UserType.COMPLEX_TYPE);
EqualsFilter eq = EqualsFilter.createEqual(new ItemPath(OrgType.F_LOCALITY), OrgType.class, prismContext,
new PolyString("Caribbean", "caribbean"));
AndFilter and = AndFilter.createAnd(type, eq);
TypeFilter type = TypeFilter.createType(OrgType.COMPLEX_TYPE, eq);

String real = getInterpretedQuery(session, ObjectType.class, ObjectQuery.createObjectQuery(and));
String real = getInterpretedQuery(session, ObjectType.class, ObjectQuery.createObjectQuery(type));

LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[]{expected, real});
AssertJUnit.assertEquals(expected, real);

checkQueryTypeAlias(real, "m_org", "locality_orig", "locality_norm");
} finally {
close(session);
}
Expand Down Expand Up @@ -1398,13 +1454,12 @@ public void test380queryObjectypeByTypeAndExtensionAttribute() throws Exception

String expected = HibernateToSqlTranslator.toSql(main);

TypeFilter type = TypeFilter.createType(UserType.COMPLEX_TYPE);
EqualsFilter eq = EqualsFilter.createEqual(
new ItemPath(ObjectType.F_EXTENSION, new QName("http://example.com/p", "weapon")),
UserType.class, prismContext, "some weapon name");
AndFilter and = AndFilter.createAnd(type, eq);
TypeFilter type = TypeFilter.createType(UserType.COMPLEX_TYPE, eq);

String real = getInterpretedQuery(session, ObjectType.class, ObjectQuery.createObjectQuery(and));
String real = getInterpretedQuery(session, ObjectType.class, ObjectQuery.createObjectQuery(type));

LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[]{expected, real});
AssertJUnit.assertEquals(expected, real);
Expand All @@ -1431,11 +1486,10 @@ public void test390queryObjectypeByTypeAndReference() throws Exception {

String expected = HibernateToSqlTranslator.toSql(main);

TypeFilter type = TypeFilter.createType(UserType.COMPLEX_TYPE);
RefFilter ref = RefFilter.createReferenceEqual(UserType.F_LINK_REF, UserType.class, prismContext, "123");
AndFilter and = AndFilter.createAnd(type, ref);
TypeFilter type = TypeFilter.createType(UserType.COMPLEX_TYPE, ref);

String real = getInterpretedQuery(session, ObjectType.class, ObjectQuery.createObjectQuery(and));
String real = getInterpretedQuery(session, ObjectType.class, ObjectQuery.createObjectQuery(type));

LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[]{expected, real});
AssertJUnit.assertEquals(expected, real);
Expand Down

0 comments on commit e14f56f

Please sign in to comment.