/** * Returns the name of the table in which to store a row for the given ClassDescriptor. * * @param cld the ClassDescriptor from the Model * @return the ClassDescriptor that masters the table */ public synchronized ClassDescriptor getTableMaster(ClassDescriptor cld) { ClassDescriptor retval = classDescriptorToTableClassDescriptor.get(cld); if (retval == null) { Iterator<ClassDescriptor> truncIter = truncated.iterator(); while (truncIter.hasNext() && (retval == null)) { ClassDescriptor truncCld = truncIter.next(); if (truncCld.getType().isAssignableFrom(cld.getType())) { retval = truncCld; } } if (retval == null) { retval = cld; } classDescriptorToTableClassDescriptor.put(cld, retval); } return retval; }
/** * Returns the name of the table in which to store a row for the given ClassDescriptor. * * @param cld the ClassDescriptor from the Model * @return the ClassDescriptor that masters the table */ public synchronized ClassDescriptor getTableMaster(ClassDescriptor cld) { ClassDescriptor retval = classDescriptorToTableClassDescriptor.get(cld); if (retval == null) { Iterator<ClassDescriptor> truncIter = truncated.iterator(); while (truncIter.hasNext() && (retval == null)) { ClassDescriptor truncCld = truncIter.next(); if (truncCld.getType().isAssignableFrom(cld.getType())) { retval = truncCld; } } if (retval == null) { retval = cld; } classDescriptorToTableClassDescriptor.put(cld, retval); } return retval; }
private static void addToIgnoredFields( Map<Class<? extends InterMineObject>, Set<String>> ignoredFieldMap, ClassDescriptor cld, String fieldName) { if (cld == null) { LOG.error("ClassDesriptor was null when attempting to add an ignored field."); } else if (InterMineObject.class.isAssignableFrom(cld.getType())) { Set<ClassDescriptor> clds = new HashSet<ClassDescriptor>(); clds.add(cld); for (ClassDescriptor subCld : cld.getSubDescriptors()) { clds.add(subCld); } for (ClassDescriptor ignoreCld : clds) { Set<String> fields = ignoredFieldMap.get(ignoreCld.getType()); @SuppressWarnings("unchecked") Class<? extends InterMineObject> cls = (Class<? extends InterMineObject>) ignoreCld.getType(); if (fields == null) { fields = new HashSet<String>(); ignoredFieldMap.put(cls, fields); } fields.add(fieldName); } } else { LOG.error("cld " + cld + " is not IMO!"); } }
private static void addToIgnoredFields( Map<Class<? extends InterMineObject>, Set<String>> ignoredFieldMap, ClassDescriptor cld, String fieldName) { if (cld == null) { LOG.error("ClassDesriptor was null when attempting to add an ignored field."); } else if (InterMineObject.class.isAssignableFrom(cld.getType())) { Set<ClassDescriptor> clds = new HashSet<ClassDescriptor>(); clds.add(cld); for (ClassDescriptor subCld : cld.getSubDescriptors()) { clds.add(subCld); } for (ClassDescriptor ignoreCld : clds) { Set<String> fields = ignoredFieldMap.get(ignoreCld.getType()); @SuppressWarnings("unchecked") Class<? extends InterMineObject> cls = (Class<? extends InterMineObject>) ignoreCld.getType(); if (fields == null) { fields = new HashSet<String>(); ignoredFieldMap.put(cls, fields); } fields.add(fieldName); } } else { LOG.error("cld " + cld + " is not IMO!"); } }
/** * {@inheritDoc} */ @Override public Set<Query> createPKQueriesForClass(InterMineObject obj, Source source, boolean queryNulls, ClassDescriptor cld) throws MetaDataException { if (hints.classNotExists(cld.getType())) { return Collections.emptySet(); } return super.createPKQueriesForClass(obj, source, queryNulls, cld); }
/** * recurse into class descriptor and add all subclasses to ignoredClasses * @param ignoredClassMap * set of classes * @param cld * super class descriptor */ @SuppressWarnings("unchecked") private void addCldToIgnored(Set<Class<? extends InterMineObject>> ignoredClassMap, ClassDescriptor cld) { if (cld == null) { LOG.error("cld is null!"); } else if (InterMineObject.class.isAssignableFrom(cld.getType())) { ignoredClassMap.add((Class<? extends InterMineObject>) cld.getType()); for (ClassDescriptor subCld : cld.getSubDescriptors()) { addCldToIgnored(ignoredClassMap, subCld); } } else { LOG.error("cld " + cld + " is not IMO!"); } }
/** * recurse into class descriptor and add all subclasses to ignoredClasses * @param ignoredClassMap * set of classes * @param cld * super class descriptor */ @SuppressWarnings("unchecked") private void addCldToIgnored(Set<Class<? extends InterMineObject>> ignoredClassMap, ClassDescriptor cld) { if (cld == null) { LOG.error("cld is null!"); } else if (InterMineObject.class.isAssignableFrom(cld.getType())) { ignoredClassMap.add((Class<? extends InterMineObject>) cld.getType()); for (ClassDescriptor subCld : cld.getSubDescriptors()) { addCldToIgnored(ignoredClassMap, subCld); } } else { LOG.error("cld " + cld + " is not IMO!"); } }
/** * {@inheritDoc} */ @Override public Set<Query> createPKQueriesForClass(InterMineObject obj, Source source, boolean queryNulls, ClassDescriptor cld) throws MetaDataException { if (hints.classNotExists(cld.getType())) { return Collections.emptySet(); } return super.createPKQueriesForClass(obj, source, queryNulls, cld); }
/** * Takes a Class, and generates a Map of all the collections that are in the Class or any of its * parents. The Class may be a dynamic class - ie not in the model, although at least one of its * parents are in the model. * * @param c a Class * @return a Map from String collection name to Class element type */ public Map<String, Class<?>> getCollectionsForClass(Class<?> c) { synchronized (classToCollectionsMap) { Map<String, Class<?>> retval = classToCollectionsMap.get(c); if (retval == null) { retval = new LinkedHashMap<String, Class<?>>(); for (FieldDescriptor fd : getFieldDescriptorsForClass(c).values()) { if (fd instanceof CollectionDescriptor) { CollectionDescriptor cd = (CollectionDescriptor) fd; retval.put(cd.getName(), cd.getReferencedClassDescriptor().getType()); } } classToCollectionsMap.put(c, retval); } return retval; } }
/** * Takes a Class, and generates a Map of all the collections that are in the Class or any of its * parents. The Class may be a dynamic class - ie not in the model, although at least one of its * parents are in the model. * * @param c a Class * @return a Map from String collection name to Class element type */ public Map<String, Class<?>> getCollectionsForClass(Class<?> c) { synchronized (classToCollectionsMap) { Map<String, Class<?>> retval = classToCollectionsMap.get(c); if (retval == null) { retval = new LinkedHashMap<String, Class<?>>(); for (FieldDescriptor fd : getFieldDescriptorsForClass(c).values()) { if (fd instanceof CollectionDescriptor) { CollectionDescriptor cd = (CollectionDescriptor) fd; retval.put(cd.getName(), cd.getReferencedClassDescriptor().getType()); } } classToCollectionsMap.put(c, retval); } return retval; } }
private void countAndStore(ObjectStore os, Model model, ClassDescriptor cld) throws ObjectStoreException { if (!classCountsMap.containsKey(cld.getName())) { int classCount = countClass(os, cld.getType()); LOG.info("Adding class count: " + cld.getUnqualifiedName() + " = " + classCount); classCountsMap.put(cld.getName(), new Integer(classCount)); // if this class is empty all subclasses MUST be empty as well if (classCount == 0) { for (ClassDescriptor subCld : model.getAllSubs(cld)) { if (!classCountsMap.containsKey(subCld.getName())) { classCountsMap.put(subCld.getName(), new Integer(classCount)); } } } } }
private void countAndStore(ObjectStore os, Model model, ClassDescriptor cld) throws ObjectStoreException { if (!classCountsMap.containsKey(cld.getName())) { int classCount = countClass(os, cld.getType()); LOG.info("Adding class count: " + cld.getUnqualifiedName() + " = " + classCount); classCountsMap.put(cld.getName(), new Integer(classCount)); // if this class is empty all subclasses MUST be empty as well if (classCount == 0) { for (ClassDescriptor subCld : model.getAllSubs(cld)) { if (!classCountsMap.containsKey(subCld.getName())) { classCountsMap.put(subCld.getName(), new Integer(classCount)); } } } } }
/** * Method to set up chromosome fields for helping */ public ChromosomeLocationHelper() { Model model = Model.getInstanceByName("genomic"); if (model == null) { throw new RuntimeException("No genomic model is available"); } ClassDescriptor chr = model.getClassDescriptorByName("Chromosome"); if (chr == null) { throw new RuntimeException("This genomic model does not contain Chromosomes"); } ClassDescriptor org = model.getClassDescriptorByName("Organism"); if (org == null) { throw new RuntimeException("This genomic model does not contain Organisms"); } AttributeDescriptor taxonDesc = org.getAttributeDescriptorByName("taxonId"); taxonIdsAreStrings = "java.lang.String".equals(taxonDesc.getType()); chromosome = new QueryClass(chr.getType()); organism = new QueryClass(org.getType()); chrIdField = new QueryField(chromosome, "primaryIdentifier"); taxonIdField = new QueryField(organism, "taxonId"); }
/** * Convenience method to test whether an InterMineObject is an instance of the given class name * without needing to import the class itself. This is used to refer to classes that may not * be present in particular data models. * @param model the data model * @param obj an object to test * @param clsName test whether obj is an instance of this class * @return true if obj is an instance of clsName */ public static boolean isInstance(Model model, InterMineObject obj, String clsName) { if (model.hasClassDescriptor(clsName)) { Class<? extends FastPathObject> cls = model.getClassDescriptorByName(clsName).getType(); if (DynamicUtil.isInstance(obj, cls)) { return true; } } return false; }
/** * Return the type of the last element in the path, regardless of whether it is an attribute * or a class. * @return the Class of the last element */ public Class<?> getEndType() { Class<?> retval = null; if (endFld != null && endFld.isAttribute()) { retval = Util.getClassFromString(((AttributeDescriptor) endFld).getType()); } else { retval = getLastClassDescriptor().getType(); } return retval; }
/** * Return the type of the last element in the path, regardless of whether it is an attribute * or a class. * @return the Class of the last element */ public Class<?> getEndType() { Class<?> retval = null; if (endFld != null && endFld.isAttribute()) { retval = Util.getClassFromString(((AttributeDescriptor) endFld).getType()); } else { retval = getLastClassDescriptor().getType(); } return retval; }
/** * Test whether the given type can be added to this bag, type can be a * qualified or un-qualified string. * @param testType type to check * @return true if type can be added to the bag */ public boolean isOfType(String testType) { Model model = os.getModel(); // this method works with qualified and unqualified class names ClassDescriptor testCld = model.getClassDescriptorByName(testType); if (testCld == null) { throw new IllegalArgumentException("Class not found in model: " + testType); } Set<ClassDescriptor> clds = model.getClassDescriptorsForClass(testCld .getType()); for (ClassDescriptor cld : clds) { String className = cld.getName(); if (TypeUtil.unqualifiedName(className).equals(getType())) { return true; } } return false; }
/** * Test whether the given type can be added to this bag, type can be a * qualified or un-qualified string. * @param testType type to check * @return true if type can be added to the bag */ public boolean isOfType(String testType) { Model model = os.getModel(); // this method works with qualified and unqualified class names ClassDescriptor testCld = model.getClassDescriptorByName(testType); if (testCld == null) { throw new IllegalArgumentException("Class not found in model: " + testType); } Set<ClassDescriptor> clds = model.getClassDescriptorsForClass(testCld .getType()); for (ClassDescriptor cld : clds) { String className = cld.getName(); if (TypeUtil.unqualifiedName(className).equals(getType())) { return true; } } return false; }
private Results getFieldSummary(ClassDescriptor cld, String fieldName, ObjectStore os) { Query q = new Query(); q.setDistinct(true); QueryClass qc = new QueryClass(cld.getType()); q.addToSelect(new QueryField(qc, fieldName)); q.addFrom(qc); Results results = os.execute(q); return results; }
private Results getFieldSummary(ClassDescriptor cld, String fieldName, ObjectStore os) { Query q = new Query(); q.setDistinct(true); QueryClass qc = new QueryClass(cld.getType()); q.addToSelect(new QueryField(qc, fieldName)); q.addFrom(qc); Results results = os.execute(q); return results; }