/** * Returns whether this primary key can be fetched now. * * @param pk the PrimaryKey * @param cld the ClassDescriptor that the PrimaryKey is in * @param pksNotDone a Map of pks not yet fetched * @return a boolean */ protected boolean canDoPkNow(PrimaryKey pk, ClassDescriptor cld, Map<PrimaryKey, ClassDescriptor> pksNotDone) { boolean canDoPkNow = true; Iterator<String> fieldNameIter = pk.getFieldNames().iterator(); while (fieldNameIter.hasNext() && canDoPkNow) { String fieldName = fieldNameIter.next(); FieldDescriptor fd = cld.getFieldDescriptorByName(fieldName); if (fd.isReference()) { Iterator<ClassDescriptor> otherCldIter = pksNotDone.values().iterator(); while (otherCldIter.hasNext() && canDoPkNow) { ClassDescriptor otherCld = otherCldIter.next(); Class<? extends FastPathObject> fieldClass = ((ReferenceDescriptor) fd) .getReferencedClassDescriptor().getType(); if (otherCld.getType().isAssignableFrom(fieldClass) || fieldClass.isAssignableFrom(otherCld.getType())) { canDoPkNow = false; } } } } return canDoPkNow; }
/** * Returns whether this primary key can be fetched now. * * @param pk the PrimaryKey * @param cld the ClassDescriptor that the PrimaryKey is in * @param pksNotDone a Map of pks not yet fetched * @return a boolean */ protected boolean canDoPkNow(PrimaryKey pk, ClassDescriptor cld, Map<PrimaryKey, ClassDescriptor> pksNotDone) { boolean canDoPkNow = true; Iterator<String> fieldNameIter = pk.getFieldNames().iterator(); while (fieldNameIter.hasNext() && canDoPkNow) { String fieldName = fieldNameIter.next(); FieldDescriptor fd = cld.getFieldDescriptorByName(fieldName); if (fd.isReference()) { Iterator<ClassDescriptor> otherCldIter = pksNotDone.values().iterator(); while (otherCldIter.hasNext() && canDoPkNow) { ClassDescriptor otherCld = otherCldIter.next(); Class<? extends FastPathObject> fieldClass = ((ReferenceDescriptor) fd) .getReferencedClassDescriptor().getType(); if (otherCld.getType().isAssignableFrom(fieldClass) || fieldClass.isAssignableFrom(otherCld.getType())) { canDoPkNow = false; } } } } return canDoPkNow; }
/** * Return the java type of a particular field. * @param field descriptor for the field in question * @return the java type */ protected String getType(FieldDescriptor field) { String type = null; if (field instanceof AttributeDescriptor) { type = ((AttributeDescriptor) field).getType(); } else if (field instanceof CollectionDescriptor) { type = "java.util.Set<" + ((CollectionDescriptor) field).getReferencedClassName() + ">"; } else { type = ((ReferenceDescriptor) field).getReferencedClassDescriptor().getName(); } return type; }
/** * Return the java type of a particular field. * @param field descriptor for the field in question * @return the java type */ protected String getType(FieldDescriptor field) { String type = null; if (field instanceof AttributeDescriptor) { type = ((AttributeDescriptor) field).getType(); } else if (field instanceof CollectionDescriptor) { type = "java.util.Set<" + ((CollectionDescriptor) field).getReferencedClassName() + ">"; } else { type = ((ReferenceDescriptor) field).getReferencedClassDescriptor().getName(); } return type; }
cld = ((ReferenceDescriptor) fld).getReferencedClassDescriptor(); } else { String qualifiedClassName = model.getPackageName() + "."
public void testReferencedClassNotSet() throws Exception { ReferenceDescriptor rfd1 = new ReferenceDescriptor("rfd1", "Class2", null); Set<ReferenceDescriptor> references = Collections.singleton(rfd1); // cld1 has a ReferenceDescriptor that points to Class2 new ClassDescriptor("Class1", null, false, ClassDescriptorFactory.NO_ATTRS, references, ClassDescriptorFactory.NO_COLLS); new ClassDescriptor("Class2", null, false, ClassDescriptorFactory.NO_ATTRS, ClassDescriptorFactory.NO_REFS, ClassDescriptorFactory.NO_COLLS); try { rfd1.getReferencedClassDescriptor(); fail("Expected IllegalStateException, model has not yet been set"); } catch (IllegalStateException e) { } }
} else if (cc.getQueryClass() == null) { tablenames.add(DatabaseUtil.getTableName(schema.getTableMaster( refDesc.getReferencedClassDescriptor())));
} else if (cc.getQueryClass() == null) { tablenames.add(DatabaseUtil.getTableName(schema.getTableMaster( refDesc.getReferencedClassDescriptor())));
public void testGetReferencedClass() throws Exception { ReferenceDescriptor rfd1 = new ReferenceDescriptor("rfd1", "package.name.Class2", null); Set<ReferenceDescriptor> references = Collections.singleton(rfd1); // cld1 has a ReferenceDescriptor that points to Class2 ClassDescriptor cld1 = new ClassDescriptor("package.name.Class1", null, false, ClassDescriptorFactory.NO_ATTRS, references, ClassDescriptorFactory.NO_COLLS); ClassDescriptor cld2 = cldFac.makeClass("Class2"); new Model("model", "package.name", Arrays.asList(cld1, cld2)); try { ClassDescriptor refCld = rfd1.getReferencedClassDescriptor(); assertTrue("ClassDescriptor was null", refCld != null); assertTrue("Expected ClassDescriptor to be Class2", refCld.getName().equals("package.name.Class2")); } catch (IllegalStateException e) { fail("Should have returned a ClassDescriptor"); } }
/** * Attach the model. Throws IllegalArgumentExceptions if node doesn't map onto the model. * * @param model model to attach * @throws IllegalArgumentException if class or field are not found in the model */ public void setModel(Model model) { ClassDescriptor cld = model.getClassDescriptorByName(getParentType()); if (cld == null) { throw new IllegalArgumentException("No class '" + getParentType() + "' found in model '" + model.getName() + "'."); } fd = cld.getFieldDescriptorByName(fieldName); if (fd == null) { throw new IllegalArgumentException("Class '" + cld.getName() + "' does not have field '" + fieldName + "'."); } type = TypeUtil.unqualifiedName(fd.isAttribute() ? ((AttributeDescriptor) fd).getType() : ((ReferenceDescriptor) fd) .getReferencedClassDescriptor().getName()); attribute = fd.isAttribute(); reference = fd.isReference(); collection = fd.isCollection(); this.model = model; }
/** * Attach the model. Throws IllegalArgumentExceptions if node doesn't map onto the model. * * @param model model to attach * @throws IllegalArgumentException if class or field are not found in the model */ public void setModel(Model model) { ClassDescriptor cld = model.getClassDescriptorByName(getParentType()); if (cld == null) { throw new IllegalArgumentException("No class '" + getParentType() + "' found in model '" + model.getName() + "'."); } fd = cld.getFieldDescriptorByName(fieldName); if (fd == null) { throw new IllegalArgumentException("Class '" + cld.getName() + "' does not have field '" + fieldName + "'."); } type = TypeUtil.unqualifiedName(fd.isAttribute() ? ((AttributeDescriptor) fd).getType() : ((ReferenceDescriptor) fd) .getReferencedClassDescriptor().getName()); attribute = fd.isAttribute(); reference = fd.isReference(); collection = fd.isCollection(); this.model = model; }
public void testReverseReferenceValid() throws Exception { // codd1 in Class1 points to Class2, cod2 in Class2 points to Class1 CollectionDescriptor cod1 = new CollectionDescriptor("cod1", "package.name.Class2", "cod2"); CollectionDescriptor cod2 = new CollectionDescriptor("cod2", "package.name.Class1", "cod1"); Set<CollectionDescriptor> cols1 = Collections.singleton(cod1); Set<CollectionDescriptor> cols2 = Collections.singleton(cod2); ClassDescriptor cld1 = new ClassDescriptor("package.name.Class1", null, false, noAttrs, noRefs, cols1); ClassDescriptor cld2 = new ClassDescriptor("package.name.Class2", null, false, noAttrs, noRefs, cols2); new Model("model", "package.name", Arrays.asList(cld1, cld2)); try { ReferenceDescriptor rfdReverse = cod1.getReverseReferenceDescriptor(); assertEquals(cod2, rfdReverse); assertEquals(cld1, rfdReverse.getReferencedClassDescriptor()); } catch (IllegalStateException e) { fail("Should have returned reverse ReferenceDescriptor"); } }
public void testReverseReferenceValid() throws Exception { // rfd1 in Class1 points to Class2, rfd2 in Class2 points to Class1 ReferenceDescriptor rfd1 = new ReferenceDescriptor("rfd1", "package.name.Class2", "rfd2"); ReferenceDescriptor rfd2 = new ReferenceDescriptor("rfd2", "package.name.Class1", "rfd1"); Set<ReferenceDescriptor> refs1 = Collections.singleton(rfd1); Set<ReferenceDescriptor> refs2 = Collections.singleton(rfd2); ClassDescriptor cld1 = new ClassDescriptor("package.name.Class1", null, false, ClassDescriptorFactory.NO_ATTRS, refs1, ClassDescriptorFactory.NO_COLLS); ClassDescriptor cld2 = new ClassDescriptor("package.name.Class2", null, false, ClassDescriptorFactory.NO_ATTRS, refs2, ClassDescriptorFactory.NO_COLLS); new Model("model", "package.name", Arrays.asList(cld1, cld2)); try { ReferenceDescriptor rfdReverse = rfd1.getReverseReferenceDescriptor(); assertEquals(rfd2, rfdReverse); assertEquals(cld1, rfdReverse.getReferencedClassDescriptor()); } catch (IllegalStateException e) { fail("Should have returned reverse ReferenceDescriptor"); } }
QueryClass qc2 = new QueryClass(ref.getReferencedClassDescriptor().getType());
QueryClass qc2 = new QueryClass(ref.getReferencedClassDescriptor().getType());
/** * Do the work of processItem() by setting DataSet and DataSource references and collections * on the given Item. * @param model the data model * @param item the Item to process * @param dataSetId the item id of the DataSet to add * @param dataSourceId the item id of the DataSource to add */ public static void setDataSets(Model model, Item item, String dataSetId, String dataSourceId) { String className = item.getClassName(); ClassDescriptor cd = model.getClassDescriptorByName(className); ReferenceDescriptor rd = cd.getReferenceDescriptorByName("source"); String dataSourceClassName = "org.intermine.model.bio.DataSource"; if (rd != null && rd.getReferencedClassDescriptor().getName().equals(dataSourceClassName) && !item.hasReference("source")) { item.setReference("source", dataSourceId); } if (item.canHaveReference("dataSource") && !item.hasReference("dataSource")) { item.setReference("dataSource", dataSourceId); } if (item.canHaveReference("dataSet") && !item.hasReference("dataSet")) { item.setReference("dataSet", dataSetId); } if (item.canHaveCollection("dataSets")) { item.addToCollection("dataSets", dataSetId); } }
} else if (fd instanceof ReferenceDescriptor) { ReferenceDescriptor ref = (ReferenceDescriptor) fd; QueryClass subQc = new QueryClass(ref.getReferencedClassDescriptor().getType()); q.addFrom(subQc); QueryObjectReference qor = new QueryObjectReference(qc, fieldName);
/** * If the last element in the path is a reference or collection return the ClassDescriptor that * the reference or collection references. If the path has one element (eg. "Employee"), * return its ClassDescriptor. If the last element in the path is an attribute, return null. * @return the ClassDescriptor */ public ClassDescriptor getEndClassDescriptor() { if (getEndFieldDescriptor() == null) { return getStartClassDescriptor(); } if (!getEndFieldDescriptor().isAttribute()) { if (getEndFieldDescriptor().isCollection()) { CollectionDescriptor collDesc = (CollectionDescriptor) getEndFieldDescriptor(); return collDesc.getReferencedClassDescriptor(); } if (getEndFieldDescriptor().isReference()) { ReferenceDescriptor refDesc = (ReferenceDescriptor) getEndFieldDescriptor(); return refDesc.getReferencedClassDescriptor(); } } return null; }
} else if (fd instanceof ReferenceDescriptor) { ReferenceDescriptor ref = (ReferenceDescriptor) fd; QueryClass subQc = new QueryClass(ref.getReferencedClassDescriptor().getType()); q.addFrom(subQc); QueryObjectReference qor = new QueryObjectReference(qc, fieldName);
/** * If the last element in the path is a reference or collection return the ClassDescriptor that * the reference or collection references. If the path has one element (eg. "Employee"), * return its ClassDescriptor. If the last element in the path is an attribute, return null. * @return the ClassDescriptor */ public ClassDescriptor getEndClassDescriptor() { if (getEndFieldDescriptor() == null) { return getStartClassDescriptor(); } if (!getEndFieldDescriptor().isAttribute()) { if (getEndFieldDescriptor().isCollection()) { CollectionDescriptor collDesc = (CollectionDescriptor) getEndFieldDescriptor(); return collDesc.getReferencedClassDescriptor(); } if (getEndFieldDescriptor().isReference()) { ReferenceDescriptor refDesc = (ReferenceDescriptor) getEndFieldDescriptor(); return refDesc.getReferencedClassDescriptor(); } } return null; }