QueryGenUtil { public static Set<String> expandPath(ObjectStore os, String path) throws ObjectStoreException { int posOfOrder = path.indexOf(" ORDER BY "); String order = ""; if (posOfOrder != -1) { order = path.substring(posOfOrder); path = path.substring(0, posOfOrder); } Set<String> paths = new LinkedHashSet<String>(); String clsName; String refName = ""; int refEnd = 0; if (path.indexOf(' ') != -1) { int clsEnd = path.indexOf(' '); clsName = path.substring(0, clsEnd); refEnd = path.indexOf(' ', clsEnd + 1); refName = path.substring(clsEnd, refEnd); } else { clsName = path; } Set<String> subs = getClassNames(os, clsName); for (String subName : subs) { Set<String> nextPaths = new LinkedHashSet<String>(); if (!("".equals(refName))) { nextPaths.addAll(expandPath(os, path.substring(refEnd + 1).trim())); } else { nextPaths.addAll(subs); return nextPaths; } for (String nextPath : nextPaths) { paths.add((subName + refName + " " + nextPath).trim() + order); } } return paths; } 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 testExpandPathStart() throws Exception { String original = "+Employee department Department"; String exp1 = "Employee department Department"; String exp2 = "CEO department Department"; String exp3 = "Manager department Department"; Set expected = new LinkedHashSet(Arrays.asList(new Object[] {exp1, exp2, exp3})); Assert.assertEquals(expected, QueryGenUtil.expandPath(os, original)); }
@Test public void testExpandPathEnd() throws Exception { String original = "Company departments Department employees +Employee"; String exp1 = "Company departments Department employees Employee"; String exp2 = "Company departments Department employees Manager"; String exp3 = "Company departments Department employees CEO"; Set expected = new LinkedHashSet(Arrays.asList(new Object[] {exp1, exp2, exp3})); Assert.assertEquals(expected, QueryGenUtil.expandPath(os, original)); }
@Test public void testExpandPathMultiple() throws Exception { String original = "+Employee department Department employees +Employee"; String exp1 = "Employee department Department employees Employee"; String exp2 = "Employee department Department employees Manager"; String exp3 = "Employee department Department employees CEO"; String exp4 = "Manager department Department employees Employee"; String exp5 = "Manager department Department employees Manager"; String exp6 = "Manager department Department employees CEO"; String exp7 = "CEO department Department employees Employee"; String exp8 = "CEO department Department employees Manager"; String exp9 = "CEO department Department employees CEO"; Assert.assertEquals(new LinkedHashSet(Arrays.asList(exp1, exp2, exp3, exp4, exp5, exp6, exp7, exp8, exp9)), QueryGenUtil.expandPath(os, original)); }
@Test public void testExpandPathMultipleWithOrder() throws Exception { String original = "+Employee department Department employees +Employee ORDER BY 1.name 2.name 3.name"; String exp1 = "Employee department Department employees Employee ORDER BY 1.name 2.name 3.name"; String exp2 = "Employee department Department employees Manager ORDER BY 1.name 2.name 3.name"; String exp3 = "Employee department Department employees CEO ORDER BY 1.name 2.name 3.name"; String exp4 = "Manager department Department employees Employee ORDER BY 1.name 2.name 3.name"; String exp5 = "Manager department Department employees Manager ORDER BY 1.name 2.name 3.name"; String exp6 = "Manager department Department employees CEO ORDER BY 1.name 2.name 3.name"; String exp7 = "CEO department Department employees Employee ORDER BY 1.name 2.name 3.name"; String exp8 = "CEO department Department employees Manager ORDER BY 1.name 2.name 3.name"; String exp9 = "CEO department Department employees CEO ORDER BY 1.name 2.name 3.name"; Assert.assertEquals(new LinkedHashSet(Arrays.asList(exp1, exp2, exp3, exp4, exp5, exp6, exp7, exp8, exp9)), QueryGenUtil.expandPath(os, original)); }