private Set<CollectionDescriptor> getCollections() { Set<CollectionDescriptor> collections = new HashSet<CollectionDescriptor>(); CollectionDescriptor cld1 = new CollectionDescriptor("cld1", "String", "reverse1"); CollectionDescriptor cld2 = new CollectionDescriptor("cld2", "Integer", "reverse2"); collections.add(cld1); collections.add(cld2); return collections; }
/** * 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; } }
/** * Creates a column name for the "outward" key of a many-to-many collection descriptor. * * @param col CollectionDescriptor * @param version the database version number * @return a valid column name */ public static String getOutwardIndirectionColumnName(CollectionDescriptor col, int version) { if (FieldDescriptor.M_N_RELATION != col.relationType()) { throw new IllegalArgumentException("Argument must be a CollectionDescriptor for a " + "many-to-many relation"); } if (version == 0) { ReferenceDescriptor rd = col.getReverseReferenceDescriptor(); String colName = (rd == null ? TypeUtil.unqualifiedName(col.getClassDescriptor().getName()) : rd.getName()); return StringUtil.capitalise(generateSqlCompatibleName(colName)); } else if (version == 1) { return StringUtil.capitalise(generateSqlCompatibleName(col.getName())); } else { throw new IllegalArgumentException("Database version number " + version + " not recognised"); } }
private static CollectionDescriptor cloneCollectionDescriptor(CollectionDescriptor ref) { return new CollectionDescriptor(ref.getName(), ref.getReferencedClassName(), ref.getReverseReferenceFieldName()); }
/** * Generates code for a single collection. * * @param col the CollectionDescriptor * @param field true if the class should have the associated field, or false if the field is in * the superclass * @return java code */ protected String generate(CollectionDescriptor col, boolean field) { String type = "java.util.Set<" + col.getReferencedClassName() + ">"; String impl = "java.util.HashSet<" + col.getReferencedClassName() + ">"; StringBuffer sb = new StringBuffer(); if (field) { sb.append(INDENT + "// Col: " + col.getClassDescriptor().getName() + "." + col.getName() + ENDL) .append(INDENT) .append("protected ") .append(type) .append(" ") .append(col.getName()) .append(" = new ") .append(impl) .append("();" + ENDL); } sb.append(generateGetSet(col, field)) .append(ENDL); return sb.toString(); }
for (CollectionDescriptor merg : merge.getCollectionDescriptors()) { CollectionDescriptor orig = original.getCollectionDescriptorByName(merg.getName()); if (merg.getReverseReferenceFieldName() != null && orig.getReverseReferenceFieldName() == null) { removeFieldDescriptor(newSet, orig.getName()); newSet.add(cloneCollectionDescriptor(merg)); continue; if (!StringUtils.equals(merg.getReverseReferenceFieldName(), orig.getReverseReferenceFieldName())) { String fldName = original.getName() + "." + orig.getName(); throw new ModelMergerException("mismatch between reverse reference field name: " + fldName + "<-" + merg.getReverseReferenceFieldName() + " != " + fldName + "<-" + orig.getReverseReferenceFieldName()); if (!merg.getReferencedClassName().equals(orig.getReferencedClassName())) { String fldName = original.getName() + "." + orig.getName(); throw new ModelMergerException("type mismatch between collection types: " + fldName + ":" + merg.getReferencedClassName() + " != " + fldName + ":" + orig.getReferencedClassName());
public void testReferencedClassNotSet() throws Exception { CollectionDescriptor cod1 = new CollectionDescriptor("cod1", "Class2", null); Set<CollectionDescriptor> collections = Collections.singleton(cod1); // cld1 has a CollectionDescriptor that contains objects of type Class2 new ClassDescriptor("Class1", null, false, noAttrs, noRefs, collections); new ClassDescriptor("Class2", null, false, noAttrs, noRefs, noColls); try { cod1.getReferencedClassDescriptor(); fail("Expected IllegalStateException, model has not yet been set"); } catch (IllegalStateException e) { } }
if (coll.relationType() == FieldDescriptor.ONE_N_RELATION) { QueryClass qc = new QueryClass(coll .getReferencedClassDescriptor().getType()); subQ.addFrom(qc); QueryForeignKey qfk = new QueryForeignKey(qc, coll.getReverseReferenceFieldName()); subQ.addToSelect(qfk); subQ.addToSelect(qc); QueryClass qc1 = new QueryClass(clazz); QueryClass qc2 = new QueryClass(coll .getReferencedClassDescriptor().getType()); subQ.addFrom(qc1); subQ.addFrom(qc2); ConstraintOp.LESS_THAN_EQUALS, new QueryValue(new Integer(highestId)))); if (coll.relationType() == FieldDescriptor.ONE_N_RELATION) { QueryForeignKey reverseIdField = new QueryForeignKey(qc2, coll.getReverseReferenceFieldName()); cs.addConstraint(new SimpleConstraint(reverseIdField, ConstraintOp.GREATER_THAN_EQUALS, QueryClass qc1 = new QueryClass(clazz); QueryClass qc2 = new QueryClass(coll .getReferencedClassDescriptor().getType()); subQ.addFrom(qc1); subQ.addFrom(qc2);
public void testRelationTypeManyToMany() throws Exception { CollectionDescriptor col1 = new CollectionDescriptor("col1", "package.name.Class1", "col2"); CollectionDescriptor col2 = new CollectionDescriptor("col2", "package.name.Class1", null); Set<CollectionDescriptor> cols = new HashSet<CollectionDescriptor>(Arrays.asList(col1, col2)); ClassDescriptor cld = new ClassDescriptor("package.name.Class1", null, false, noAttrs, noRefs, cols); new Model("model1", "package.name", Collections.singleton(cld)); assertEquals(FieldDescriptor.M_N_RELATION, col1.relationType()); }
if (FieldDescriptor.M_N_RELATION == collection.relationType()) { if (!indirections.contains(collection.getReverseReferenceDescriptor())) { indirections.add(collection);
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"); } }
.append(StringUtil.reverseCapitalisation(name)) .append("(final ") .append(((CollectionDescriptor) field).getReferencedClassDescriptor().getName()) .append(" arg)"); if (fieldPresent) {
public void testEquals() throws Exception { CollectionDescriptor cod1 = new CollectionDescriptor("cod1", "Class1", "cod1"); CollectionDescriptor cod2 = new CollectionDescriptor("cod1", "Class1", "cod1"); CollectionDescriptor cod3 = new CollectionDescriptor("cod2", "Class1", "cod1"); CollectionDescriptor cod5 = new CollectionDescriptor("cod1", "Class2", "cod1"); CollectionDescriptor cod6 = new CollectionDescriptor("cod1", "Class1", "cod2"); assertEquals(cod1, cod2); assertEquals(cod1.hashCode(), cod2.hashCode()); assertFalse(cod1.equals(cod3)); assertFalse(cod1.equals(cod5)); assertFalse(cod1.equals(cod6)); }
.append("if (\"" + field.getName() + "\".equals(fieldName)) {\n") .append(INDENT + INDENT + INDENT) .append("return " + ((CollectionDescriptor) field).getReferencedClassName() + ".class;\n") .append(INDENT + INDENT)
CollectionDescriptor cold = citer.next(); CollectionDescriptor scdDescriptor = scd.getCollectionDescriptorByName(cold.getName()); if (scd.getCollectionDescriptorByName(cold.getName()) != null) { LOG.info("removing collection " + cold.getName() + " redefinition in " + cd.getName() + " (is now defined in " + scd.getName() + ")"); String revName = cold.getReverseReferenceFieldName(); String scdRevFieldName = scdDescriptor.getReverseReferenceFieldName(); if (StringUtils.equals(revName, scdRevFieldName)) { citer.remove(); } else { String message = "replacing the \"" + sup + "." + cold.getName() + "\" collection with " + cd.getName() + "." + cold.getName() + " failed because the reverse references differ"; throw new ModelMergerException(message);
public void testToString() throws Exception { CollectionDescriptor col = new CollectionDescriptor("ref", "package.name.Class1", null); String expected = "<collection name=\"ref\" referenced-type=\"Class1\"/>"; assertEquals(col.toString(), expected); col = new CollectionDescriptor("ref", "package.name.Class1", "reverseRef"); expected = "<collection name=\"ref\" referenced-type=\"Class1\" reverse-reference=\"reverseRef\"/>"; assertEquals(col.toString(), expected); } }
public void testSetClassDescriptor() throws Exception { ClassDescriptor cld = new ClassDescriptor("Class1", null, false, noAttrs, noRefs, noColls); CollectionDescriptor cod = new CollectionDescriptor("name", "String", null); try { cod.setClassDescriptor(cld); } catch (IllegalStateException e) { fail("should have been able set ClassDescriptor"); } try { cod.setClassDescriptor(cld); fail("Expected: IllegalStateException, ClassDescriptor already set"); } catch (IllegalStateException e) { } }
@SuppressWarnings("unchecked") Collection<InterMineObject> coll = (Collection<InterMineObject>) ((InterMineObject) o) .getFieldValue(collection.getName()); boolean needToStoreCollection = true; if (collection.relationType() == FieldDescriptor.M_N_RELATION) { String indirectTableName = DatabaseUtil.getIndirectionTableName(collection);
/** * {@inheritDoc} */ @Override public int relationType() { ReferenceDescriptor rd = getReverseReferenceDescriptor(); if (rd == null || rd instanceof CollectionDescriptor) { return M_N_RELATION; } else { return ONE_N_RELATION; } }
/** * Given a ClassDescriptor find names of all related indirection tables. * @param cld class to find tables for * @return a set of all indirection table names */ public static Set<String> getIndirectionTableNames(ClassDescriptor cld) { Set<String> tables = new HashSet<String>(); for (CollectionDescriptor col : cld.getAllCollectionDescriptors()) { if (FieldDescriptor.M_N_RELATION == col.relationType()) { tables.add(getIndirectionTableName(col)); } } return tables; }