QueryGenUtil { protected static QueryAndClass createClassFindingQuery(Model model, String part) { try { String[] paths = part.split("\\."); Query q = new Query(); QueryClass qc = new QueryClass(Class.forName(model.getPackageName() + "." + paths[0])); q.addFrom(qc); ConstraintSet cs = new ConstraintSet(ConstraintOp.AND); for (int i = 1; i < paths.length; i++) { if (i == paths.length - 1) { QueryField qf = new QueryField(qc, paths[i]); q.addToSelect(qf); } else { try { QueryObjectReference qor = new QueryObjectReference(qc, paths[i]); qc = new QueryClass(qor.getType()); q.addFrom(qc); cs.addConstraint(new ContainsConstraint(qor, ConstraintOp.CONTAINS, qc)); } catch (IllegalArgumentException e) { QueryCollectionReference qcr = new QueryCollectionReference(qc, paths[i]); qc = new QueryClass(TypeUtil.getElementType(qc.getType(), paths[i])); q.addFrom(qc); cs.addConstraint(new ContainsConstraint(qcr, ConstraintOp.CONTAINS, qc)); } } } q.setDistinct(true); int csSize = cs.getConstraints().size(); if (csSize == 1) { q.setConstraint(cs.getConstraints().iterator().next()); } else if (csSize > 1) { q.setConstraint(cs); } return new QueryAndClass(q, qc.getType()); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("Class not found while processing " + part, e); } } private  QueryGenUtil(); static void validatePath(String path, Model model); static Set<String> expandPath(ObjectStore os, String path); static Query constructQuery(Model model, String path);  }
@Test public void testCreateQuery() throws Exception { String clsName = "Department.employees.class"; Assert.assertEquals("SELECT DISTINCT a2_.class AS a3_ FROM org.intermine.model.testmodel.Department AS a1_, org.intermine.model.testmodel.Employee AS a2_ WHERE a1_.employees CONTAINS a2_", QueryGenUtil.createClassFindingQuery(os.getModel(), clsName).getQuery().toString()); }