/** {@inheritDoc} */ public ActionForward execute( @SuppressWarnings("unused") ActionMapping mapping, @SuppressWarnings("unused") ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String type = request.getParameter("type"); if (type == null) { type = "webapp"; } if ("webapp".equals(type)) { response.getOutputStream().print("OK"); } else if ("query".equals(type)) { final InterMineAPI im = SessionMethods.getInterMineAPI(request.getSession()); ObjectStore os = im.getObjectStore(); Query q = new Query(); QueryClass c = new QueryClass(InterMineObject.class); q.addFrom(c); q.addToSelect(c); // Add a unique value to the select to avoid caching the query QueryValue token = new QueryValue(System.currentTimeMillis()); q.addToSelect(token); Results r = os.execute(q, 1, false, false, false); if (r.get(0) != null) { response.getOutputStream().print("OK"); } else { response.getOutputStream().print("NO RESULTS"); } } return null; }
public void tearDown() throws Exception { LOG.info("in tear down"); ObjectStoreWriter osw = ObjectStoreWriterFactory.getObjectStoreWriter("osw.unittest"); if (osw.isInTransaction()) { osw.abortTransaction(); } Query q = new Query(); QueryClass qc = new QueryClass(InterMineObject.class); q.addFrom(qc); q.addToSelect(qc); SingletonResults res = osw.getObjectStore().executeSingleton(q); LOG.info("created results"); Iterator resIter = res.iterator(); osw.beginTransaction(); while (resIter.hasNext()) { InterMineObject o = (InterMineObject) resIter.next(); LOG.info("deleting: " + o.getId()); osw.delete(o); } osw.commitTransaction(); LOG.info("committed transaction"); osw.close(); LOG.info("closed objectstore"); }
public Query translateQuery(Query query) throws ObjectStoreException { Query q = new Query(); QueryClass qc = new QueryClass(Company.class); q.addToSelect(qc); q.addFrom(qc); return q; }
/** * Retrieve the publications to be updated * * @param os The ObjectStore to read from * @return a List of publications */ protected List<Publication> getPublications(ObjectStore os) { Query q = new Query(); QueryClass qc = new QueryClass(Publication.class); q.addFrom(qc); q.addToSelect(qc); ConstraintSet cs = new ConstraintSet(ConstraintOp.OR); SimpleConstraint scTitle = new SimpleConstraint(new QueryField(qc, "title"), ConstraintOp.IS_NULL); cs.addConstraint(scTitle); SimpleConstraint scYear = new SimpleConstraint(new QueryField(qc, "year"), ConstraintOp.IS_NULL); cs.addConstraint(scYear); SimpleConstraint scFirstAuthor = new SimpleConstraint(new QueryField(qc, "firstAuthor"), ConstraintOp.IS_NULL); cs.addConstraint(scFirstAuthor); q.setConstraint(cs); @SuppressWarnings("unchecked") List<Publication> retval = (List<Publication>) ((List) os.executeSingleton(q)); return retval; }
private Query getCheckQuery() { Query cq = new Query(); QueryClass qc = new QueryClass(Types.class); cq.addFrom(qc); cq.addToSelect(new QueryFunction()); cq.setConstraint( new SimpleConstraint( new QueryField(qc, "name"), ConstraintOp.MATCHES, new QueryValue("histo%"))); return cq; }
private Query internalGetQuery() { Query q = new Query(); QueryClass qc1 = new QueryClass(clazz); q.addFrom(qc1); q.addToSelect(qc1); q.setConstraint( new ContainsConstraint( new QueryCollectionReference(o, fieldName), ConstraintOp.CONTAINS, qc1)); q.setDistinct(false); return q; }
/** Delete all InterMineObjects from the objectstore. */ private void cleanObjects(ObjectStoreWriter osw) throws ObjectStoreException { Query q = new Query(); QueryClass queryClass = new QueryClass(InterMineObject.class); q.addToSelect(queryClass); q.addFrom(queryClass); SingletonResults r = osw.getObjectStore().executeSingleton(q); for (InterMineObject o : (List<InterMineObject>) ((List) r)) { osw.delete(o); } }
public void testTranslateQueryInvalid() throws Exception { Query original = new Query(); QueryClass qc2 = new QueryClass(Company.class); original.addFrom(qc2); original.addToSelect(qc2); try { translator.translateQuery(original); fail("Expected ObjectStoreException"); } catch (ObjectStoreException e) { } }
public void testTranslateFromDbObject() throws Exception { Item dbItem = new Item(); dbItem.setClassName("Department"); dbItem.setImplementations("Broke"); dbItem.setIdentifier("fish_1"); Attribute dbAttr1 = new Attribute(); dbAttr1.setName("name"); dbAttr1.setValue("Department1"); dbAttr1.setItem(dbItem); dbItem.addAttributes(dbAttr1); Attribute dbAttr2 = new Attribute(); dbAttr2.setName("debt"); dbAttr2.setValue("10"); dbAttr2.setItem(dbItem); dbItem.addAttributes(dbAttr2); Reference dbRef1 = new Reference(); dbRef1.setName("company"); dbRef1.setRefId("2"); dbRef1.setItem(dbItem); dbItem.addReferences(dbRef1); ReferenceList dbCol1 = new ReferenceList(); dbCol1.setName("employees"); dbCol1.setRefIds("3 4"); dbCol1.setItem(dbItem); dbItem.addCollections(dbCol1); InterMineObject result = (InterMineObject) translator.translateFromDbObject(dbItem); assertTrue(result instanceof Department); assertTrue(result instanceof Broke); assertEquals("Department1", ((Department) result).getName()); assertEquals(10, ((Broke) result).getDebt()); Map fieldMap = ((DynamicBean) ((net.sf.cglib.proxy.Factory) result).getCallback(0)).getMap(); ProxyReference o = (ProxyReference) fieldMap.get("Company"); assertNotNull(o); assertEquals(new Integer(2), o.getId()); Collection c = ((Department) result).getEmployees(); assertTrue(c instanceof SingletonResults); Query expected = new Query(); QueryClass qc = new QueryClass(InterMineObject.class); expected.addFrom(qc); expected.addToSelect(qc); QueryField qf = new QueryField(qc, "id"); Set ids = new HashSet(); ids.add(new Integer(3)); ids.add(new Integer(4)); BagConstraint bc = new BagConstraint(qf, ConstraintOp.IN, ids); expected.setConstraint(bc); assertEquals(expected, ((SingletonResults) c).getQuery()); }
public void testTranslateQueryBagConstraint() throws Exception { Query expected = new Query(); QueryClass qc = new QueryClass(Item.class); expected.addFrom(qc); expected.addToSelect(qc); QueryField qf = new QueryField(qc, "identifier"); BagConstraint bc = new BagConstraint( qf, ConstraintOp.IN, Arrays.asList(new Object[] {"fish_12", "fish_15", "fish_19"})); expected.setConstraint(bc); Query original = new Query(); QueryClass qc2 = new QueryClass(InterMineObject.class); original.addFrom(qc2); original.addToSelect(qc2); QueryField qf2 = new QueryField(qc2, "id"); BagConstraint bc2 = new BagConstraint( qf2, ConstraintOp.IN, Arrays.asList(new Object[] {new Integer(12), new Integer(15), new Integer(19)})); original.setConstraint(bc2); assertEquals(expected, translator.translateQuery(original)); }
public void testTranslateQuerySpecificClassPlus() throws Exception { Query expected = new Query(); QueryClass qc = new QueryClass(Item.class); expected.addFrom(qc); expected.addToSelect(qc); ConstraintSet cs = new ConstraintSet(ConstraintOp.AND); QueryField qf2 = new QueryField(qc, "identifier"); SimpleConstraint sc2 = new SimpleConstraint(qf2, ConstraintOp.EQUALS, new QueryValue("fish_42")); cs.addConstraint(sc2); QueryField qf1 = new QueryField(qc, "className"); SimpleConstraint sc1 = new SimpleConstraint(qf1, ConstraintOp.EQUALS, new QueryValue("Department")); cs.addConstraint(sc1); expected.setConstraint(cs); Query original = new Query(); QueryClass qc2 = new QueryClass(Department.class); original.addFrom(qc2); original.addToSelect(qc2); QueryField qf3 = new QueryField(qc2, "id"); SimpleConstraint sc3 = new SimpleConstraint(qf3, ConstraintOp.EQUALS, new QueryValue(new Integer(42))); original.setConstraint(sc3); assertEquals(expected, translator.translateQuery(original)); }
public void testFailFast() throws Exception { Query q1 = new Query(); QueryClass qc1 = new QueryClass(Employee.class); q1.addFrom(qc1); q1.addToSelect(qc1); Results r1 = writer.execute(q1); writer.store(data.get("EmployeeA1")); try { r1.iterator().hasNext(); fail("Expected: ConcurrentModificationException"); } catch (ConcurrentModificationException e) { } }
public void test() throws Exception { IqlQuery fq = new IqlQuery( "SELECT DISTINCT a1_, a3_, a4_ FROM org.intermine.model.testmodel.Department AS a1_," + " org.intermine.model.testmodel.CEO AS a2_, org.intermine.model.testmodel.Company " + "AS a3_, org.intermine.model.testmodel.Manager AS a4_ " + "WHERE (a1_.manager CONTAINS a2_ AND a2_.company CONTAINS a3_ " + "AND a1_.employees CONTAINS a4_)", "org.intermine.model.testmodel"); Query query = fq.toQuery(); Results results = osd.execute(query); PathQuery pathQuery = new PathQuery(model); pathQuery.addViews( "Department.name", "Department.manager.company.name", "Department.manager.company.vatNumber", "Department.employees.seniority"); pathQuery.addConstraint(new PathConstraintSubclass("Department.manager", "CEO")); pathQuery.addConstraint(new PathConstraintSubclass("Department.employees", "Manager")); Map<String, QuerySelectable> pathToQueryNode = new HashMap<String, QuerySelectable>(); QueryClass deptQC = (QueryClass) query.getSelect().get(0); pathToQueryNode.put("Department", deptQC); QueryField deptNameQF = new QueryField(deptQC, "name"); pathToQueryNode.put("Department.name", deptNameQF); QueryClass compQC = (QueryClass) query.getSelect().get(1); pathToQueryNode.put("Department.manager.company", compQC); QueryField compNameQF = new QueryField(compQC, "name"); pathToQueryNode.put("Department.manager.company.name", compNameQF); QueryField compVatNumQF = new QueryField(compQC, "vatNumber"); pathToQueryNode.put("Department.manager.company.vatNumber", compVatNumQF); QueryClass manQC = (QueryClass) query.getSelect().get(2); pathToQueryNode.put("Department.employees", manQC); QueryField manSeniority = new QueryField(manQC, "seniority"); pathToQueryNode.put("Department.employees.seniority", manSeniority); WebResults webResults = new WebResults(im, pathQuery, results, pathToQueryNode, null); assertEquals("Department1", webResults.get(0).get(0).get(0).getValue().getField()); assertEquals("Company1", webResults.get(0).get(0).get(1).getValue().getField()); assertEquals(new Integer(101), webResults.get(0).get(0).get(2).getValue().getField()); assertEquals("Department2", webResults.get(1).get(0).get(0).getValue().getField()); assertEquals("Company2", webResults.get(1).get(0).get(1).getValue().getField()); assertEquals(new Integer(102), webResults.get(1).get(0).get(2).getValue().getField()); assertEquals("Department3", webResults.get(2).get(0).get(0).getValue().getField()); assertEquals("Company3", webResults.get(2).get(0).get(1).getValue().getField()); assertEquals(new Integer(103), webResults.get(2).get(0).get(2).getValue().getField()); }
public void testTranslation() throws Exception { ObjectStore os2 = new ObjectStoreTranslatingImpl( Model.getInstanceByName("testmodel"), ObjectStoreFactory.getObjectStore("os.unittest"), new CompanyTranslator()); Query q = new Query(); QueryClass qc = new QueryClass(InterMineObject.class); q.addToSelect(qc); q.addFrom(qc); Results res = os2.execute(q); assertEquals(2, res.size()); assertEquals("CompanyA", ((Bank) ((ResultsRow) res.get(0)).get(0)).getName()); assertEquals("CompanyB", ((Bank) ((ResultsRow) res.get(1)).get(0)).getName()); }
/** * Add a contains constraint to Query (q) built with the query class and attribute given in input * * @param query The query to add a reference to. * @param qc The class the reference belongs to. * @param attribute the name of the field of the class. * @param attributePath Another similarly named field - I wish it had been documented! * @return The query class of the attributePath. */ protected QueryClass addReference( final Query query, final QueryClass qc, String attribute, String attributePath) { ConstraintSet cs = (ConstraintSet) query.getConstraint(); QueryReference qr = null; String type = ""; boolean useSubClass = false; if (WidgetConfigUtil.isPathContainingSubClass(os.getModel(), attribute)) { useSubClass = true; type = attribute.substring(attribute.indexOf("[") + 1, attribute.indexOf("]")); attribute = attribute.substring(0, attribute.indexOf("[")); } QueryClass qcTmp = null; try { qr = new QueryObjectReference(qc, attribute); if (useSubClass) { try { qcTmp = new QueryClass(Class.forName(os.getModel().getPackageName() + "." + type)); } catch (ClassNotFoundException cnfe) { LOG.error("The type " + type + " doesn't exist in the model."); } } else { qcTmp = new QueryClass(qr.getType()); } } catch (IllegalArgumentException e) { // Not a reference - try collection instead qr = new QueryCollectionReference(qc, attribute); if (useSubClass) { try { qcTmp = new QueryClass(Class.forName(os.getModel().getPackageName() + "." + type)); } catch (ClassNotFoundException cnfe) { LOG.error("The type " + type + " doesn't exist in the model."); } } else { qcTmp = new QueryClass(TypeUtil.getElementType(qc.getType(), attribute)); } } QueryClass ret; if (!queryClassInQuery.containsKey(attributePath)) { ret = qcTmp; query.addFrom(ret); cs.addConstraint(new ContainsConstraint(qr, ConstraintOp.CONTAINS, ret)); queryClassInQuery.put(attributePath, ret); } else { ret = queryClassInQuery.get(attributePath); } return ret; }
/** Test that transactions can be aborted */ public void testAbortTransactions() throws Exception { Address address1 = new Address(); address1.setAddress("Address 3"); Address address2 = new Address(); address2.setAddress("Address 4"); Query q = new Query(); QueryClass qcAddress = new QueryClass(Address.class); QueryField qf = new QueryField(qcAddress, "address"); ConstraintSet cs1 = new ConstraintSet(ConstraintOp.OR); cs1.addConstraint(new SimpleConstraint(qf, ConstraintOp.MATCHES, new QueryValue("Address%"))); q.addToSelect(qcAddress); q.addFrom(qcAddress); q.addToOrderBy(qf); q.setConstraint(cs1); Results res = writer.execute(q); assertEquals(res.toString(), 0, res.size()); res = realOs.execute(q); assertEquals(res.toString(), 0, res.size()); writer.beginTransaction(); assertTrue(writer.isInTransaction()); writer.store(address1); writer.store(address2); // TODO: These lines now fail, because we do not allow querying on writers with uncommitted // data. The writer should relax this restriction. res = writer.execute(q); assertEquals(2, res.size()); res = realOs.execute(q); assertEquals(res.toString(), 0, res.size()); writer.abortTransaction(); assertFalse(writer.isInTransaction()); // Should be nothing there unless we commit res = writer.execute(q); assertEquals(res.toString(), 0, res.size()); res = realOs.execute(q); assertEquals(res.toString(), 0, res.size()); }
/** Test that transactions do actually commit and that isInTransaction() works. */ public void testCommitTransactions() throws Exception { Address address1 = new Address(); address1.setAddress("Address 1"); Address address2 = new Address(); address2.setAddress("Address 2"); Query q = new Query(); QueryClass qcAddress = new QueryClass(Address.class); QueryField qf = new QueryField(qcAddress, "address"); ConstraintSet cs1 = new ConstraintSet(ConstraintOp.OR); cs1.addConstraint(new SimpleConstraint(qf, ConstraintOp.MATCHES, new QueryValue("Address%"))); q.addToSelect(qcAddress); q.addFrom(qcAddress); q.addToOrderBy(qf); q.setConstraint(cs1); try { writer.beginTransaction(); assertTrue(writer.isInTransaction()); writer.store(address1); writer.store(address2); // Should be nothing in OS until we commit Results res = realOs.execute(q); assertEquals(0, res.size()); // However, they should be in the WRITER. // TODO: These lines now fail, because we do not allow querying on writers with uncommitted // data. The writer should relax this restriction. res = writer.execute(q); assertEquals(2, res.size()); writer.commitTransaction(); assertFalse(writer.isInTransaction()); res = realOs.execute(q); assertEquals(2, res.size()); assertEquals(address1, (Address) ((ResultsRow) res.get(0)).get(0)); assertEquals(address2, (Address) ((ResultsRow) res.get(1)).get(0)); } finally { writer.delete(address1); writer.delete(address2); } }
/** * @param os object store * @return map of name to so term * @throws ObjectStoreException if something goes wrong */ protected Map<String, SOTerm> populateSOTermMap(ObjectStore os) throws ObjectStoreException { Map<String, SOTerm> soTerms = new HashMap<String, SOTerm>(); Query q = new Query(); q.setDistinct(false); QueryClass qcSOTerm = new QueryClass(SOTerm.class); q.addToSelect(qcSOTerm); q.addFrom(qcSOTerm); q.addToOrderBy(qcSOTerm); Results res = os.execute(q); Iterator it = res.iterator(); while (it.hasNext()) { ResultsRow<InterMineObject> rr = (ResultsRow<InterMineObject>) it.next(); SOTerm soTerm = (SOTerm) rr.get(0); soTerms.put(soTerm.getName(), soTerm); } return soTerms; }
/** * Create a queryField starting from the path given in input and add the contain constraints * between all the queryclass composing the path. If addToSelect is true, add the queryFiled as * select, group by and order by element. * * @param path the path used to create the queryField. The path doesn't containt the startClass * @param query the query to modify * @param addToSelect if true add the queryFiled as select, group by and order by element * @return the queryField created */ protected QueryField createQueryFieldByPath(String path, Query query, boolean addToSelect) { QueryField queryField = null; String[] splittedPath = path.split("\\."); QueryClass qc = startClass; String attribute; String attributePath = ""; for (int i = 0; i < splittedPath.length; i++) { attribute = splittedPath[i]; if (i == splittedPath.length - 1) { queryField = new QueryField(qc, attribute); if (addToSelect) { query.addToSelect(queryField); query.addToGroupBy(queryField); query.addToOrderBy(queryField); } } else { attributePath = createAttributePath(splittedPath, i); qc = addReference(query, qc, attribute, attributePath); } } return queryField; }
private int removeOrphanTags() { // Basically: // select tag.id // from tag, (select array(select id from userprofile) as ids) as sq // where tag.userprofileid IS NULL OR tag.userprofileid <> ALL (sq.ids) // order by tag.id ObjectStoreWriter osw = getUserProfile(); Query q = new Query(); QueryClass qc = new QueryClass(Tag.class); q.setConstraint(new SimpleConstraint(new QueryField(qc, "userProfile"), ConstraintOp.IS_NULL)); Set<Object> res; try { res = osw.executeSingleton(q); } catch (Exception e) { throw new BuildException("Could not retrieve any tags", e); } try { osw.beginTransaction(); for (Object o : res) { osw.delete((Tag) o); } osw.commitTransaction(); } catch (Exception e) { throw new BuildException("Error deleting tags", e); } finally { if (osw != null) { try { if (osw.isInTransaction()) { osw.abortTransaction(); } } catch (ObjectStoreException e) { throw new BuildException("Could not even manage transactions here...", e); } } } return res.size(); }
/** * Search for the classes in a collection for a given InterMineObject, for example find all of the * sub-classes of Employee in the Department.employees collection of a given Department. If there * are no subclasses or the collection is empty a list with the type of the collection is * returned. * * @param object an InterMineObject to inspect * @param field the name if the collection to check * @param os the ObjectStore in which to execute the query * @return a list of classes in the collection */ public static List<Class<?>> queryForTypesInCollection( InterMineObject object, String field, ObjectStore os) { List<Class<?>> typesInCollection = new ArrayList<Class<?>>(); // if there are no subclasses there can only be one type in the collection Model model = os.getModel(); ClassDescriptor startCld = model.getClassDescriptorByName(DynamicUtil.getSimpleClassName(object)); CollectionDescriptor col = startCld.getCollectionDescriptorByName(field, true); ClassDescriptor colCld = col.getReferencedClassDescriptor(); if (model.getAllSubs(colCld).isEmpty()) { // there aren't any subclasses, so no need to do a query typesInCollection.add(colCld.getType()); } else { // there may be multiple subclasses in the collection, need to run a query Query query = new Query(); QueryClass qc = new QueryClass(colCld.getType()); query.addFrom(qc); query.addToSelect(new QueryField(qc, "class")); query.setDistinct(true); query.setConstraint( new ContainsConstraint( new QueryCollectionReference(object, field), ConstraintOp.CONTAINS, qc)); for (Object o : os.executeSingleton(query)) { typesInCollection.add((Class<?>) o); } // Collection was empty but add collection type to be consistent with collection types // without subclasses. if (typesInCollection.isEmpty()) { typesInCollection.add(colCld.getType()); } } return typesInCollection; }
public void testTranslateQueryNoConstraint() throws Exception { Query expected = new Query(); QueryClass qc = new QueryClass(Item.class); expected.addFrom(qc); expected.addToSelect(qc); Query original = new Query(); QueryClass qc2 = new QueryClass(InterMineObject.class); original.addFrom(qc2); original.addToSelect(qc2); assertEquals(expected, translator.translateQuery(original)); }
public void testTranslateQuerySimpleConstraint() throws Exception { Query expected = new Query(); QueryClass qc = new QueryClass(Item.class); expected.addFrom(qc); expected.addToSelect(qc); QueryField qf = new QueryField(qc, "identifier"); SimpleConstraint sc = new SimpleConstraint(qf, ConstraintOp.EQUALS, new QueryValue("fish_42")); expected.setConstraint(sc); Query original = new Query(); QueryClass qc2 = new QueryClass(InterMineObject.class); original.addFrom(qc2); original.addToSelect(qc2); QueryField qf2 = new QueryField(qc2, "id"); SimpleConstraint sc2 = new SimpleConstraint(qf2, ConstraintOp.EQUALS, new QueryValue(new Integer(42))); original.setConstraint(sc2); assertEquals(expected, translator.translateQuery(original)); }
public void testTranslateQuerySpecificClass() throws Exception { Query expected = new Query(); QueryClass qc = new QueryClass(Item.class); expected.addFrom(qc); expected.addToSelect(qc); QueryField qf = new QueryField(qc, "className"); SimpleConstraint sc = new SimpleConstraint(qf, ConstraintOp.EQUALS, new QueryValue("Department")); expected.setConstraint(sc); Query original = new Query(); QueryClass qc2 = new QueryClass(Department.class); original.addFrom(qc2); original.addToSelect(qc2); assertEquals(expected, translator.translateQuery(original)); }
/** @return query to get all parent so terms */ protected Query getAllParents() { Query q = new Query(); q.setDistinct(false); QueryClass qcFeature = new QueryClass(model.getClassDescriptorByName("SequenceFeature").getType()); q.addToSelect(qcFeature); q.addFrom(qcFeature); QueryClass qcSOTerm = new QueryClass(OntologyTerm.class); q.addToSelect(qcSOTerm); q.addFrom(qcSOTerm); q.addToOrderBy(qcSOTerm); ConstraintSet cs = new ConstraintSet(ConstraintOp.AND); QueryObjectReference ref1 = new QueryObjectReference(qcFeature, "sequenceOntologyTerm"); cs.addConstraint(new ContainsConstraint(ref1, ConstraintOp.CONTAINS, qcSOTerm)); // Set the constraint of the query q.setConstraint(cs); return q; }
private static void runSpanValidationQuery(InterMineAPI im) { // a Map contains orgName and its chrInfo accordingly // e.g. <D.Melanogaster, (D.Melanogaster, X, 5000)...> chrInfoMap = new HashMap<String, List<ChromosomeInfo>>(); try { Query q = new Query(); QueryClass qcOrg = new QueryClass(Organism.class); QueryClass qcChr = new QueryClass(Chromosome.class); // Result columns QueryField qfOrgName = new QueryField(qcOrg, "shortName"); QueryField qfChrPID = new QueryField(qcChr, "primaryIdentifier"); QueryField qfChrLength = new QueryField(qcChr, "length"); // As in SQL SELECT ?,?,? q.addToSelect(qfOrgName); q.addToSelect(qfChrPID); q.addToSelect(qfChrLength); // As in SQL FROM ?,? q.addFrom(qcChr); q.addFrom(qcOrg); // As in SQL WHERE ? QueryObjectReference organism = new QueryObjectReference(qcChr, "organism"); ContainsConstraint ccOrg = new ContainsConstraint(organism, ConstraintOp.CONTAINS, qcOrg); q.setConstraint(ccOrg); Results results = im.getObjectStore().execute(q); // a List contains all the chrInfo (organism, chrPID, length) List<ChromosomeInfo> chrInfoList = new ArrayList<ChromosomeInfo>(); // a Set contains all the orgName Set<String> orgSet = new HashSet<String>(); // Handle results for (Iterator<?> iter = results.iterator(); iter.hasNext(); ) { ResultsRow<?> row = (ResultsRow<?>) iter.next(); String org = (String) row.get(0); String chrPID = (String) row.get(1); Integer chrLength = (Integer) row.get(2); // Add orgName to HashSet to filter out duplication orgSet.add(org); if (chrLength != null) { ChromosomeInfo chrInfo = new ChromosomeInfo(); chrInfo.setOrgName(org); chrInfo.setChrPID(chrPID); chrInfo.setChrLength(chrLength); // Add ChromosomeInfo to Arraylist chrInfoList.add(chrInfo); } } // Iterate orgSet and chrInfoList to put data in chrInfoMap which has the key as the // orgName and value as a ArrayList containing a list of chrInfo which has the same // orgName for (String o : orgSet) { // a List to store chrInfo for the same organism List<ChromosomeInfo> chrInfoSubList = new ArrayList<ChromosomeInfo>(); for (ChromosomeInfo chrInfo : chrInfoList) { if (o.equals(chrInfo.getOrgName())) { chrInfoSubList.add(chrInfo); chrInfoMap.put(o, chrInfoSubList); } } } } catch (Exception e) { e.printStackTrace(); } }
/** The method to run all the queries. */ @SuppressWarnings("rawtypes") private void queryExecutor() { // Use spanOverlapFullResultMap to store the data in the session @SuppressWarnings("unchecked") Map<String, Map<GenomicRegion, List<SpanQueryResultRow>>> spanOverlapFullResultMap = (Map<String, Map<GenomicRegion, List<SpanQueryResultRow>>>) request.getSession().getAttribute("spanOverlapFullResultMap"); if (spanOverlapFullResultMap == null) { spanOverlapFullResultMap = new HashMap<String, Map<GenomicRegion, List<SpanQueryResultRow>>>(); } Map<GenomicRegion, List<SpanQueryResultRow>> spanOverlapResultDisplayMap = Collections.synchronizedMap(new LinkedHashMap<GenomicRegion, List<SpanQueryResultRow>>()); // GBrowse track @SuppressWarnings("unchecked") Map<String, Map<GenomicRegion, LinkedHashMap<String, LinkedHashSet<GBrowseTrackInfo>>>> gbrowseFullTrackMap = (HashMap< String, Map<GenomicRegion, LinkedHashMap<String, LinkedHashSet<GBrowseTrackInfo>>>>) request.getSession().getAttribute("gbrowseFullTrackMap"); if (gbrowseFullTrackMap == null) { gbrowseFullTrackMap = new HashMap< String, Map<GenomicRegion, LinkedHashMap<String, LinkedHashSet<GBrowseTrackInfo>>>>(); } Map<GenomicRegion, LinkedHashMap<String, LinkedHashSet<GBrowseTrackInfo>>> gbrowseTrackMap = Collections.synchronizedMap( new LinkedHashMap< GenomicRegion, LinkedHashMap<String, LinkedHashSet<GBrowseTrackInfo>>>()); if (!spanOverlapFullResultMap.containsKey(spanUUIDString)) { spanOverlapFullResultMap.put(spanUUIDString, spanOverlapResultDisplayMap); request.getSession().setAttribute("spanOverlapFullResultMap", spanOverlapFullResultMap); gbrowseFullTrackMap.put(spanUUIDString, gbrowseTrackMap); request.getSession().setAttribute("gbrowseFullTrackMap", gbrowseFullTrackMap); try { Query q; for (GenomicRegion aSpan : spanList) { q = new Query(); q.setDistinct(true); String chrPID = aSpan.getChr(); Integer start = aSpan.getStart(); Integer end = aSpan.getEnd(); /* >>>>> TEST CODE <<<<< LOG.info("OrgName: " + orgName); LOG.info("chrPID: " + chrPID); LOG.info("start: " + start); LOG.info("end: " + end); LOG.info("FeatureTypes: " + ftKeys); LOG.info("Submissions: " + subKeys); >>>>> TEST CODE <<<<< */ // DB tables QueryClass qcOrg = new QueryClass(Organism.class); QueryClass qcChr = new QueryClass(Chromosome.class); QueryClass qcFeature = new QueryClass(SequenceFeature.class); QueryClass qcLoc = new QueryClass(Location.class); QueryClass qcSubmission = new QueryClass(Submission.class); QueryField qfOrgName = new QueryField(qcOrg, "shortName"); QueryField qfChrPID = new QueryField(qcChr, "primaryIdentifier"); QueryField qfFeaturePID = new QueryField(qcFeature, "primaryIdentifier"); QueryField qfFeatureId = new QueryField(qcFeature, "id"); QueryField qfFeatureClass = new QueryField(qcFeature, "class"); QueryField qfSubmissionTitle = new QueryField(qcSubmission, "title"); QueryField qfSubmissionDCCid = new QueryField(qcSubmission, "DCCid"); QueryField qfChr = new QueryField(qcChr, "primaryIdentifier"); QueryField qfLocStart = new QueryField(qcLoc, "start"); QueryField qfLocEnd = new QueryField(qcLoc, "end"); q.addToSelect(qfFeatureId); q.addToSelect(qfFeaturePID); q.addToSelect(qfFeatureClass); q.addToSelect(qfChr); q.addToSelect(qfLocStart); q.addToSelect(qfLocEnd); q.addToSelect(qfSubmissionDCCid); q.addToSelect(qfSubmissionTitle); q.addFrom(qcChr); q.addFrom(qcOrg); q.addFrom(qcFeature); q.addFrom(qcLoc); q.addFrom(qcSubmission); q.addToOrderBy(qfLocStart, "ascending"); ConstraintSet constraints = new ConstraintSet(ConstraintOp.AND); q.setConstraint(constraints); // SequenceFeature.organism = Organism QueryObjectReference organism = new QueryObjectReference(qcFeature, "organism"); ContainsConstraint ccOrg = new ContainsConstraint(organism, ConstraintOp.CONTAINS, qcOrg); constraints.addConstraint(ccOrg); // Organism.name = orgName SimpleConstraint scOrg = new SimpleConstraint(qfOrgName, ConstraintOp.EQUALS, new QueryValue(orgName)); constraints.addConstraint(scOrg); // Location.feature = SequenceFeature QueryObjectReference locSubject = new QueryObjectReference(qcLoc, "feature"); ContainsConstraint ccLocSubject = new ContainsConstraint(locSubject, ConstraintOp.CONTAINS, qcFeature); constraints.addConstraint(ccLocSubject); // Location.locatedOn = Chromosome QueryObjectReference locObject = new QueryObjectReference(qcLoc, "locatedOn"); ContainsConstraint ccLocObject = new ContainsConstraint(locObject, ConstraintOp.CONTAINS, qcChr); constraints.addConstraint(ccLocObject); // Chromosome.primaryIdentifier = chrPID SimpleConstraint scChr = new SimpleConstraint(qfChrPID, ConstraintOp.EQUALS, new QueryValue(chrPID)); constraints.addConstraint(scChr); // SequenceFeature.submissions = Submission QueryCollectionReference submission = new QueryCollectionReference(qcFeature, "submissions"); ContainsConstraint ccSubmission = new ContainsConstraint(submission, ConstraintOp.CONTAINS, qcSubmission); constraints.addConstraint(ccSubmission); // SequenceFeature.class in a list constraints.addConstraint(new BagConstraint(qfFeatureClass, ConstraintOp.IN, ftKeys)); // Submission.CCDid in a list constraints.addConstraint(new BagConstraint(qfSubmissionDCCid, ConstraintOp.IN, subKeys)); OverlapRange overlapInput = new OverlapRange(new QueryValue(start), new QueryValue(end), locObject); OverlapRange overlapFeature = new OverlapRange( new QueryField(qcLoc, "start"), new QueryField(qcLoc, "end"), locObject); OverlapConstraint oc = new OverlapConstraint(overlapInput, ConstraintOp.OVERLAPS, overlapFeature); constraints.addConstraint(oc); Results results = im.getObjectStore().execute(q); /* >>>>> TEST CODE <<<<< LOG.info("Query: " + q.toString()); LOG.info("Result Size: " + results.size()); LOG.info("Result >>>>> " + results); >>>>> TEST CODE <<<<< */ List<SpanQueryResultRow> spanResults = new ArrayList<SpanQueryResultRow>(); if (results == null || results.isEmpty()) { spanOverlapResultDisplayMap.put(aSpan, null); gbrowseTrackMap.put(aSpan, null); } else { for (Iterator<?> iter = results.iterator(); iter.hasNext(); ) { ResultsRow<?> row = (ResultsRow<?>) iter.next(); SpanQueryResultRow aRow = new SpanQueryResultRow(); aRow.setFeatureId((Integer) row.get(0)); aRow.setFeaturePID((String) row.get(1)); aRow.setFeatureClass(((Class) row.get(2)).getSimpleName()); aRow.setChr((String) row.get(3)); aRow.setStart((Integer) row.get(4)); aRow.setEnd((Integer) row.get(5)); aRow.setSubDCCid((String) row.get(6)); aRow.setSubTitle((String) row.get(7)); spanResults.add(aRow); } spanOverlapResultDisplayMap.put(aSpan, spanResults); gbrowseTrackMap.put(aSpan, getSubGbrowseTrack(spanResults)); // Gbrowse } } } catch (Exception e) { e.printStackTrace(); } } }
/** * Fetches equivalent objects for a particular primary key. * * @param pk the PrimaryKey * @param cld the ClassDescriptor of the PrimaryKey * @param results a Map to hold results that are to be added to the cache * @param objectsForCld a List of objects relevant to this PrimaryKey * @param fetchedObjectIds a Set to hold ids of objects that are fetched, to prefetch from the * data tracker later * @throws ObjectStoreException if something goes wrong */ protected void doPk( PrimaryKey pk, ClassDescriptor cld, Map<InterMineObject, Set<InterMineObject>> results, List<InterMineObject> objectsForCld, Set<Integer> fetchedObjectIds) throws ObjectStoreException { Iterator<InterMineObject> objectsForCldIter = objectsForCld.iterator(); while (objectsForCldIter.hasNext()) { int objCount = 0; int origObjCount = 0; Query q = new Query(); QueryClass qc = new QueryClass(cld.getType()); q.addFrom(qc); q.addToSelect(qc); ConstraintSet cs = new ConstraintSet(ConstraintOp.AND); q.setConstraint(cs); Map<String, Set<Object>> fieldNameToValues = new HashMap<String, Set<Object>>(); for (String fieldName : pk.getFieldNames()) { try { QueryField qf = new QueryField(qc, fieldName); q.addToSelect(qf); Set<Object> values = new HashSet<Object>(); fieldNameToValues.put(fieldName, values); cs.addConstraint(new BagConstraint(qf, ConstraintOp.IN, values)); } catch (IllegalArgumentException e) { QueryForeignKey qf = new QueryForeignKey(qc, fieldName); q.addToSelect(qf); Set<Object> values = new HashSet<Object>(); fieldNameToValues.put(fieldName, values); cs.addConstraint(new BagConstraint(qf, ConstraintOp.IN, values)); } } // Now make a map from the primary key values to source objects Map<List<Object>, InterMineObject> keysToSourceObjects = new HashMap<List<Object>, InterMineObject>(); while (objectsForCldIter.hasNext() && (objCount < 500)) { InterMineObject object = objectsForCldIter.next(); origObjCount++; try { if (DataLoaderHelper.objectPrimaryKeyNotNull(model, object, cld, pk, source, idMap)) { List<Collection<Object>> values = new ArrayList<Collection<Object>>(); boolean skipObject = false; Map<String, Set<Object>> fieldsValues = new HashMap<String, Set<Object>>(); for (String fieldName : pk.getFieldNames()) { try { Object value = object.getFieldProxy(fieldName); Set<Object> fieldValues; if (value instanceof InterMineObject) { Integer id = idMap.get(((InterMineObject) value).getId()); if (id == null) { Set<InterMineObject> eqs = results.get(value); if (eqs == null) { value = object.getFieldValue(fieldName); eqs = queryEquivalentObjects((InterMineObject) value, source); } fieldValues = new HashSet<Object>(); for (InterMineObject obj : eqs) { fieldValues.add(obj.getId()); } } else { fieldValues = Collections.singleton((Object) id); } } else { fieldValues = Collections.singleton(value); } values.add(fieldValues); fieldsValues.put(fieldName, fieldValues); for (Object fieldValue : fieldValues) { long time = System.currentTimeMillis(); boolean pkQueryFruitless = hints.pkQueryFruitless(cld.getType(), fieldName, fieldValue); String summaryName = Util.getFriendlyName(cld.getType()) + "." + fieldName; if (!savedTimes.containsKey(summaryName)) { savedTimes.put(summaryName, new Long(System.currentTimeMillis() - time)); savedCounts.put(summaryName, new Integer(0)); } if (pkQueryFruitless) { skipObject = true; } } } catch (IllegalAccessException e) { throw new RuntimeException(e); } } if (!skipObject) { objCount++; for (String fieldName : pk.getFieldNames()) { fieldNameToValues.get(fieldName).addAll(fieldsValues.get(fieldName)); } for (List<Object> valueSet : CollectionUtil.fanOutCombinations(values)) { if (keysToSourceObjects.containsKey(valueSet)) { throw new ObjectStoreException( "Duplicate objects found for pk " + cld.getName() + "." + pk.getName() + ": " + object); } keysToSourceObjects.put(valueSet, object); } } } } catch (MetaDataException e) { throw new ObjectStoreException(e); } } // Prune BagConstraints using the hints system. // boolean emptyQuery = false; // Iterator<String> fieldNameIter = pk.getFieldNames().iterator(); // while (fieldNameIter.hasNext() && (!emptyQuery)) { // String fieldName = fieldNameIter.next(); // Set values = fieldNameToValues.get(fieldName); // Iterator valueIter = values.iterator(); // while (valueIter.hasNext()) { // if (hints.pkQueryFruitless(cld.getType(), fieldName, valueIter.next())) { // valueIter.remove(); // } // } // if (values.isEmpty()) { // emptyQuery = true; // } // } if (objCount > 0) { // Iterate through query, and add objects to results // long time = System.currentTimeMillis(); int matches = 0; Results res = lookupOs.execute(q, 2000, false, false, false); @SuppressWarnings("unchecked") List<ResultsRow<Object>> tmpRes = (List) res; for (ResultsRow<Object> row : tmpRes) { List<Object> values = new ArrayList<Object>(); for (int i = 1; i <= pk.getFieldNames().size(); i++) { values.add(row.get(i)); } Set<InterMineObject> set = results.get(keysToSourceObjects.get(values)); if (set != null) { set.add((InterMineObject) row.get(0)); matches++; } fetchedObjectIds.add(((InterMineObject) row.get(0)).getId()); } // LOG.info("Fetched " + res.size() + " equivalent objects for " + objCount // + " objects in " + (System.currentTimeMillis() - time) + " ms for " // + cld.getName() + "." + pk.getName()); } } }
@Override protected void execute() throws Exception { String pathString = request.getParameter("path"); Map<String, Object> attributes = getHeaderAttributes(); output.setHeaderAttributes(attributes); if (isEmpty(pathString)) { throw new BadRequestException("No path provided"); } attributes.put("path", pathString); kvPairs.put("path", pathString); String typeConstraintStr = request.getParameter("typeConstraints"); Map<String, String> typeMap = new HashMap<String, String>(); if (!isEmpty(typeConstraintStr)) { logger.debug(typeConstraintStr); JSONObject typeJO = new JSONObject(typeConstraintStr); Iterator<String> it = (Iterator<String>) typeJO.keys(); while (it.hasNext()) { String name = it.next(); String subType = typeJO.getString(name); typeMap.put(name, subType); } } Model model = im.getModel(); Path path; try { if (typeMap.isEmpty()) { path = new Path(model, pathString); } else { path = new Path(model, pathString, typeMap); } } catch (PathException e) { throw new BadRequestException("Bad path given: " + pathString, e); } Query q = new Query(); attributes.put("class", path.getLastClassDescriptor().getUnqualifiedName()); attributes.put("field", path.getLastElement()); String type = ((AttributeDescriptor) path.getEndFieldDescriptor()).getType(); String[] parts = split(type, '.'); reverse(parts); attributes.put("type", parts[0]); QueryClass qc = new QueryClass(path.getPrefix().getEndType()); q.addFrom(qc); QueryField qf1 = new QueryField(qc, path.getLastElement()); q.addToSelect(qf1); QueryFunction qf = new QueryFunction(); q.addToSelect(qf); q.addToGroupBy(qf1); int count = im.getObjectStore().count(q, ObjectStore.SEQUENCE_IGNORE); if (formatIsCount()) { output.addResultItem(Arrays.asList(String.valueOf(count))); } else { attributes.put("count", count); Results results = im.getObjectStore().execute(q, DEFAULT_BATCH_SIZE, true, true, false); Iterator<Object> iter = results.iterator(); while (iter.hasNext()) { @SuppressWarnings("rawtypes") List row = (List) iter.next(); Map<String, Object> jsonMap = new HashMap<String, Object>(); jsonMap.put("value", row.get(0)); jsonMap.put("count", row.get(1)); JSONObject jo = new JSONObject(jsonMap); List<String> forOutput = new ArrayList<String>(); forOutput.add(jo.toString()); if (iter.hasNext()) { // Standard hack to ensure commas forOutput.add(""); } output.addResultItem(forOutput); } } }
public void testLoad() throws Exception { TSVFileReaderTask tsvTask = new TSVFileReaderTask(); tsvTask.setIgnoreDuplicates(true); tsvTask.setIntegrationWriterAlias("integration.unittestmulti"); tsvTask.setSourceName("testsource"); cleanObjects(tsvTask.getDirectDataLoader().getIntegrationWriter()); File tempFile = File.createTempFile("TSVFileReaderTaskTest", "tmp"); FileWriter fw = new FileWriter(tempFile); InputStream is = getClass().getClassLoader().getResourceAsStream("TSVFileReaderTaskTest.tsv"); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = null; while ((line = br.readLine()) != null) { fw.write(line + "\n"); } fw.close(); FileSet fileSet = new FileSet(); fileSet.setFile(tempFile); tsvTask.addFileSet(fileSet); InputStream confInputStream = getClass().getClassLoader().getResourceAsStream("TSVFileReaderTaskTest.properties"); DelimitedFileConfiguration dfc = new DelimitedFileConfiguration(model, confInputStream); tsvTask.executeInternal(dfc, tempFile); // Check the results to see if we have some data... ObjectStore os = tsvTask.getDirectDataLoader().getIntegrationWriter().getObjectStore(); Query q = new Query(); QueryClass empQueryClass = new QueryClass(Employee.class); QueryField qf0 = new QueryField(empQueryClass, "age"); QueryField qf1 = new QueryField(empQueryClass, "name"); QueryField qf2 = new QueryField(empQueryClass, "fullTime"); q.addToSelect(qf0); q.addToSelect(qf1); q.addToSelect(qf2); q.addFrom(empQueryClass); q.addToOrderBy(qf1); Results r = os.execute(q); if (r.size() != 3) { for (List<Object> rr : (List<List<Object>>) ((List) r)) { System.err.print("row: "); for (Object obj : rr) { System.err.print("{" + obj + "} "); } System.err.println(); } } assertEquals(3, r.size()); List expectedRow0 = Arrays.asList(new Object[] {new Integer(10), "EmployeeA1", Boolean.FALSE}); assertEquals(expectedRow0, r.get(0)); List expectedRow1 = Arrays.asList(new Object[] {new Integer(20), "EmployeeA2", Boolean.TRUE}); assertEquals(expectedRow1, r.get(1)); List expectedRow2 = Arrays.asList(new Object[] {new Integer(0), "EmployeeA3", Boolean.FALSE}); assertEquals(expectedRow2, r.get(2)); }