QueryGenUtil { public static Query constructQuery(Model model, String path) throws ClassNotFoundException { validatePath(path, model); int posOfOrder = path.indexOf(" ORDER BY "); String order = null; if (posOfOrder != -1) { order = path.substring(posOfOrder + 10); path = path.substring(0, posOfOrder); } String[] queryBits = path.split("[ \t]"); Query q = new Query(); q.setDistinct(false); QueryClass qcStart = new QueryClass(Class.forName(model.getPackageName() + "." + queryBits[0])); List<QueryClass> qcs = new ArrayList<QueryClass>(); qcs.add(qcStart); for (int i = 0; i + 2 < queryBits.length; i += 2) { String refName = queryBits[i + 1]; int dotPos = refName.indexOf('.'); if (dotPos != -1) { String prefix = refName.substring(0, dotPos); if (!("reverse".equals(prefix))) { int replacementStartNo; try { replacementStartNo = Integer.parseInt(prefix) - 1; } catch (NumberFormatException e) { throw new IllegalArgumentException("Prefix " + prefix + " is not a number " + "or \"reverse\"", e); } qcStart = qcs.get(replacementStartNo); refName = refName.substring(dotPos + 1); } } QueryClass qcEnd = new QueryClass(Class.forName(model.getPackageName() + "." + queryBits[i + 2])); addReferenceConstraint(model, q, qcStart, refName, qcEnd, (i == 0)); qcs.add(qcEnd); qcStart = qcEnd; } if (order != null) { String[] orderBits = order.split("[ \t]"); for (String orderBit : orderBits) { int posOfDot = orderBit.indexOf('.'); int classNo = Integer.parseInt(orderBit.substring(0, posOfDot)) - 1; String fieldName = orderBit.substring(posOfDot + 1); QueryField qf = new QueryField(qcs.get(classNo), fieldName); q.addToSelect(qf); q.addToOrderBy(qf); } } return q; } 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 testConstructQuerySingleRef() throws Exception { Query actual = QueryGenUtil.constructQuery(os.getModel(), "Employee department Department"); Query q = new Query(); q.setDistinct(false); QueryClass qcEmpl = new QueryClass(Employee.class); q.addToSelect(qcEmpl); q.addFrom(qcEmpl); QueryClass qcDept = new QueryClass(Department.class); q.addToSelect(qcDept); q.addFrom(qcDept); QueryObjectReference ref = new QueryObjectReference(qcEmpl, "department"); ContainsConstraint cc = new ContainsConstraint(ref, ConstraintOp.CONTAINS, qcDept); q.setConstraint(cc); QueryAssert.assertEquals(q, actual); }
@Test public void testConstructQueryTwoRefs() throws Exception { Query actual = QueryGenUtil.constructQuery(os.getModel(), "Company departments Department manager Manager"); Query q = new Query(); q.setDistinct(false); QueryClass qcCom = new QueryClass(Company.class); q.addToSelect(qcCom); q.addFrom(qcCom); QueryClass qcDept = new QueryClass(Department.class); q.addToSelect(qcDept); q.addFrom(qcDept); QueryClass qcMan = new QueryClass(Manager.class); q.addToSelect(qcMan); q.addFrom(qcMan); ConstraintSet cs = new ConstraintSet(ConstraintOp.AND); QueryCollectionReference col = new QueryCollectionReference(qcCom, "departments"); ContainsConstraint cc1 = new ContainsConstraint(col, ConstraintOp.CONTAINS, qcDept); cs.addConstraint(cc1); QueryObjectReference ref = new QueryObjectReference(qcDept, "manager"); ContainsConstraint cc2 = new ContainsConstraint(ref, ConstraintOp.CONTAINS, qcMan); cs.addConstraint(cc2); q.setConstraint(cs); QueryAssert.assertEquals(q, actual); }
@Test public void testConstructQueryTwoRefsWithOrder() throws Exception { Query actual = QueryGenUtil.constructQuery(os.getModel(), "Company departments Department manager Manager ORDER BY 1.name 2.name 3.name"); Query q = new Query(); q.setDistinct(false); QueryClass qcCom = new QueryClass(Company.class); q.addToSelect(qcCom); q.addFrom(qcCom); QueryClass qcDept = new QueryClass(Department.class); q.addToSelect(qcDept); q.addFrom(qcDept); QueryClass qcMan = new QueryClass(Manager.class); q.addToSelect(qcMan); q.addFrom(qcMan); QueryField comName = new QueryField(qcCom, "name"); q.addToSelect(comName); q.addToOrderBy(comName); QueryField deptName = new QueryField(qcDept, "name"); q.addToSelect(deptName); q.addToOrderBy(deptName); QueryField manName = new QueryField(qcMan, "name"); q.addToSelect(manName); q.addToOrderBy(manName); ConstraintSet cs = new ConstraintSet(ConstraintOp.AND); QueryCollectionReference col = new QueryCollectionReference(qcCom, "departments"); ContainsConstraint cc1 = new ContainsConstraint(col, ConstraintOp.CONTAINS, qcDept); cs.addConstraint(cc1); QueryObjectReference ref = new QueryObjectReference(qcDept, "manager"); ContainsConstraint cc2 = new ContainsConstraint(ref, ConstraintOp.CONTAINS, qcMan); cs.addConstraint(cc2); q.setConstraint(cs); QueryAssert.assertEquals(q, actual); }
@Test public void testConstructQueryReverse() throws Exception { Query actual = QueryGenUtil.constructQuery(os.getModel(), "Company reverse.company Department"); Query q = new IqlQuery("SELECT a1_, a2_ FROM Company AS a1_, Department AS a2_ WHERE a2_.company CONTAINS a1_", "org.intermine.model.testmodel").toQuery(); QueryAssert.assertEquals(q, actual); }
@Test public void testConstructQueryNumber() throws Exception { Query actual = QueryGenUtil.constructQuery(os.getModel(), "Company departments Department 1.address Address"); Query q = new IqlQuery("SELECT a1_, a2_, a3_ FROM Company AS a1_, Department AS a2_, Address AS a3_ WHERE a1_.departments CONTAINS a2_ AND a1_.address CONTAINS a3_", "org.intermine.model.testmodel").toQuery(); QueryAssert.assertEquals(q, actual); }