/** * Generates the getElementType method. * * @param cld the ClassDescriptor * @return a String with the method */ public String generateGetElementType(ClassDescriptor cld) { StringBuffer sb = new StringBuffer(); sb.append(INDENT).append("public Class<?> getElementType(final String fieldName) {\n"); for (FieldDescriptor field : cld.getAllFieldDescriptors()) { if (field.isCollection()) { sb.append(INDENT + INDENT) .append("if (\"" + field.getName() + "\".equals(fieldName)) {\n") .append(INDENT + INDENT + INDENT) .append( "return " + ((CollectionDescriptor) field).getReferencedClassName() + ".class;\n") .append(INDENT + INDENT) .append("}\n"); } } sb.append(INDENT + INDENT) .append("if (!" + cld.getName() + ".class.equals(getClass())) {\n") .append(INDENT + INDENT + INDENT) .append("return TypeUtil.getElementType(" + cld.getName() + ".class, fieldName);\n") .append(INDENT + INDENT) .append("}\n") .append(INDENT + INDENT) .append("throw new IllegalArgumentException(\"Unknown field \" + fieldName);\n") .append(INDENT) .append("}\n"); return sb.toString(); }
/** Perform the mapping. */ public void process() { for (ClassDescriptor cld : model.getClassDescriptors()) { String cldName = cld.getName(); if (!"org.intermine.model.InterMineObject".equals(cldName)) { String pkg = TypeUtil.packageName(cldName); String cls = TypeUtil.unqualifiedName(cld.getName()); String separator = File.separator; // Escape windows path seperator if ("\\".equals(separator)) { separator = "\\\\"; } File dir = new File(file, pkg.replaceAll("[.]", separator)); dir.mkdirs(); File path = new File(dir, cls + ".java"); try { path.delete(); BufferedWriter fos = new BufferedWriter(new FileWriter(path, true)); fos.write(generate(cld, false)); fos.close(); if (cld.isInterface()) { path = new File(dir, cls + "Shadow.java"); path.delete(); fos = new BufferedWriter(new FileWriter(path, true)); fos.write(generate(cld, true)); fos.close(); } } catch (IOException e) { throw new RuntimeException("Error creating java", e); } } } }
/** * Generates the getFieldType method. * * @param cld the ClassDescriptor * @return a String with the method */ public String generateGetFieldType(ClassDescriptor cld) { StringBuffer sb = new StringBuffer(); sb.append(INDENT).append("public Class<?> getFieldType(final String fieldName) {\n"); for (FieldDescriptor field : cld.getAllFieldDescriptors()) { sb.append(INDENT + INDENT) .append("if (\"" + field.getName() + "\".equals(fieldName)) {\n") .append(INDENT + INDENT + INDENT); if (field instanceof AttributeDescriptor) { AttributeDescriptor attribute = (AttributeDescriptor) field; if ("boolean".equals(attribute.getType())) { sb.append("return Boolean.TYPE;\n"); } else if ("short".equals(attribute.getType())) { sb.append("return Short.TYPE;\n"); } else if ("int".equals(attribute.getType())) { sb.append("return Integer.TYPE;\n"); } else if ("long".equals(attribute.getType())) { sb.append("return Long.TYPE;\n"); } else if ("float".equals(attribute.getType())) { sb.append("return Float.TYPE;\n"); } else if ("double".equals(attribute.getType())) { sb.append("return Double.TYPE;\n"); } else { sb.append("return " + attribute.getType() + ".class;\n"); } } else if (field.isReference()) { sb.append("return " + ((ReferenceDescriptor) field).getReferencedClassName() + ".class;\n"); } else { sb.append("return java.util.Set.class;\n"); } sb.append(INDENT + INDENT).append("}\n"); } sb.append(INDENT + INDENT) .append("if (!" + cld.getName() + ".class.equals(getClass())) {\n") .append(INDENT + INDENT + INDENT) .append("return TypeUtil.getFieldType(" + cld.getName() + ".class, fieldName);\n") .append(INDENT + INDENT) .append("}\n") .append(INDENT + INDENT) .append("throw new IllegalArgumentException(\"Unknown field \" + fieldName);\n") .append(INDENT) .append("}\n"); return sb.toString(); }
/** * Generate a .equals() method for the given class. * * @param cld descriptor for class in question * @return generated java code as string */ protected String generateEquals(ClassDescriptor cld) { if (cld.getFieldDescriptorByName("id") != null) { String unqualifiedName = TypeUtil.unqualifiedName(cld.getName()); StringBuffer sb = new StringBuffer(); sb.append(INDENT) .append("@Override public boolean equals(Object o) { return (o instanceof ") .append(unqualifiedName) .append(" && id != null) ? id.equals(((") .append(unqualifiedName) .append(")o).getId()) : this == o; }" + ENDL); return sb.toString(); } else { return ""; } }
/** * Generates the addCollectionElement method. * * @param cld the ClassDescriptor * @return a String with the method */ public String generateAddCollectionElement(ClassDescriptor cld) { StringBuffer sb = new StringBuffer(); sb.append(INDENT) .append("public void addCollectionElement(final String fieldName,") .append(" final org.intermine.model.InterMineObject element) {\n") .append(INDENT + INDENT); for (FieldDescriptor field : cld.getAllFieldDescriptors()) { if (field.isCollection()) { String fieldName = field.getName(); if ("fieldName".equals(fieldName)) { fieldName = "this.fieldName"; } else if ("element".equals(fieldName)) { fieldName = "this.element"; } sb.append("if (\"" + field.getName() + "\".equals(fieldName)) {\n") .append(INDENT + INDENT + INDENT) .append( fieldName + ".add((" + ((CollectionDescriptor) field).getReferencedClassName() + ") element);\n") .append(INDENT + INDENT) .append("} else "); } } sb.append("{\n") .append(INDENT + INDENT + INDENT) .append("if (!" + cld.getName() + ".class.equals(getClass())) {\n") .append(INDENT + INDENT + INDENT + INDENT) .append("TypeUtil.addCollectionElement(this, fieldName, element);\n") .append(INDENT + INDENT + INDENT + INDENT) .append("return;\n") .append(INDENT + INDENT + INDENT) .append("}\n") .append(INDENT + INDENT + INDENT) .append("throw new IllegalArgumentException(\"Unknown collection \" + fieldName);\n") .append(INDENT + INDENT) .append("}\n") .append(INDENT) .append("}\n"); return sb.toString(); }
/** * Generate a .toString() method for the given class . * * @param cld descriptor for the class in question * @return generated java code as a string */ protected String generateToString(ClassDescriptor cld) { String unqualifiedName = TypeUtil.unqualifiedName(cld.getName()); StringBuilder sb = new StringBuilder(); Set<FieldDescriptor> keyFields = cld.getAllFieldDescriptors(); if (keyFields.size() > 0) { sb.append(INDENT) .append("@Override public String toString() { ") .append("return \"") .append(unqualifiedName) .append(" ["); TreeMap<String, FieldDescriptor> sortedMap = new TreeMap<String, FieldDescriptor>(); for (FieldDescriptor field : keyFields) { sortedMap.put(field.getName(), field); } boolean needComma = false; for (Map.Entry<String, FieldDescriptor> entry : sortedMap.entrySet()) { FieldDescriptor field = entry.getValue(); if (!(field instanceof CollectionDescriptor)) { if (needComma) { sb.append(", "); } needComma = true; sb.append(field.getName()); if (field instanceof AttributeDescriptor) { sb.append("=\\\"\" + " + field.getName() + " + \"\\\""); } else { sb.append( "=\" + (" + field.getName() + " == null ? \"null\" : (" + field.getName() + ".getId() == null ? \"no id\" : " + field.getName() + ".getId().toString())) + \""); } } } sb.append("]\"; }" + ENDL); } return sb.toString(); }
/** * 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()); } } }
/** * Generates the setFieldValue method. * * @param cld the ClassDescriptor * @return a String with the method */ public String generateSetFieldValue(ClassDescriptor cld) { StringBuffer sb = new StringBuffer(); sb.append(INDENT) .append("public void setFieldValue(final String fieldName, final Object value) {\n") .append(INDENT + INDENT); for (FieldDescriptor field : cld.getAllFieldDescriptors()) { sb.append("if (\"" + field.getName() + "\".equals(fieldName)) {\n"); String fieldName = field.getName(); if ("value".equals(fieldName)) { fieldName = "this.value"; } else if ("fieldName".equals(fieldName)) { fieldName = "this.fieldName"; } if (field instanceof AttributeDescriptor) { AttributeDescriptor attribute = (AttributeDescriptor) field; if ("boolean".equals(attribute.getType())) { sb.append(INDENT + INDENT + INDENT) .append(fieldName + " = ((Boolean) value).booleanValue();\n"); } else if ("short".equals(attribute.getType())) { sb.append(INDENT + INDENT + INDENT) .append(fieldName + " = ((Short) value).shortValue();\n"); } else if ("int".equals(attribute.getType())) { sb.append(INDENT + INDENT + INDENT) .append(fieldName + " = ((Integer) value).intValue();\n"); } else if ("long".equals(attribute.getType())) { sb.append(INDENT + INDENT + INDENT) .append(fieldName + " = ((Long) value).longValue();\n"); } else if ("float".equals(attribute.getType())) { sb.append(INDENT + INDENT + INDENT) .append(fieldName + " = ((Float) value).floatValue();\n"); } else if ("double".equals(attribute.getType())) { sb.append(INDENT + INDENT + INDENT) .append(fieldName + " = ((Double) value).doubleValue();\n"); } else { sb.append(INDENT + INDENT + INDENT) .append(fieldName + " = (" + attribute.getType() + ") value;\n"); } } else if (field.isReference()) { sb.append(INDENT + INDENT + INDENT) .append(fieldName + " = (org.intermine.model.InterMineObject) value;\n"); } else { sb.append(INDENT + INDENT + INDENT).append(fieldName + " = (java.util.Set) value;\n"); } sb.append(INDENT + INDENT).append("} else "); } sb.append("{\n") .append(INDENT + INDENT + INDENT) .append("if (!" + cld.getName() + ".class.equals(getClass())) {\n") .append(INDENT + INDENT + INDENT + INDENT) .append("TypeUtil.setFieldValue(this, fieldName, value);\n") .append(INDENT + INDENT + INDENT + INDENT) .append("return;\n") .append(INDENT + INDENT + INDENT) .append("}\n") .append(INDENT + INDENT + INDENT) .append("throw new IllegalArgumentException(\"Unknown field \" + fieldName);\n") .append(INDENT + INDENT) .append("}\n") .append(INDENT) .append("}\n"); return sb.toString(); }
/** * Generates the getFieldValue method. * * @param cld the ClassDescriptor * @param proxy false to make the getFieldValue method, true to make the getFieldProxy method * @return a String with the method */ public String generateGetFieldValue(ClassDescriptor cld, boolean proxy) { StringBuffer sb = new StringBuffer(); sb.append(INDENT) .append( "public Object getField" + (proxy ? "Proxy" : "Value") + "(final String fieldName) throws IllegalAccessException {\n"); for (FieldDescriptor field : cld.getAllFieldDescriptors()) { sb.append(INDENT + INDENT).append("if (\"" + field.getName() + "\".equals(fieldName)) {\n"); String fieldName = field.getName(); if ("fieldName".equals(fieldName)) { fieldName = "this.fieldName"; } if (field instanceof AttributeDescriptor) { AttributeDescriptor attribute = (AttributeDescriptor) field; if ("boolean".equals(attribute.getType())) { sb.append(INDENT + INDENT + INDENT) .append("return Boolean.valueOf(" + fieldName + ");\n"); } else if ("short".equals(attribute.getType())) { sb.append(INDENT + INDENT + INDENT).append("return Short.valueOf(" + fieldName + ");\n"); } else if ("int".equals(attribute.getType())) { sb.append(INDENT + INDENT + INDENT) .append("return Integer.valueOf(" + fieldName + ");\n"); } else if ("long".equals(attribute.getType())) { sb.append(INDENT + INDENT + INDENT).append("return Long.valueOf(" + fieldName + ");\n"); } else if ("float".equals(attribute.getType())) { sb.append(INDENT + INDENT + INDENT).append("return Float.valueOf(" + fieldName + ");\n"); } else if ("double".equals(attribute.getType())) { sb.append(INDENT + INDENT + INDENT).append("return Double.valueOf(" + fieldName + ");\n"); } else { sb.append(INDENT + INDENT + INDENT).append("return " + fieldName + ";\n"); } } else if (field.isReference()) { sb.append(INDENT + INDENT + INDENT); if (proxy) { sb.append("return " + fieldName + ";\n"); } else { sb.append("if (" + fieldName + " instanceof ProxyReference) {\n") .append(INDENT + INDENT + INDENT + INDENT) .append("return ((ProxyReference) " + fieldName + ").getObject();\n") .append(INDENT + INDENT + INDENT) .append("} else {\n") .append(INDENT + INDENT + INDENT + INDENT) .append("return " + fieldName + ";\n") .append(INDENT + INDENT + INDENT) .append("}\n"); } } else { sb.append(INDENT + INDENT + INDENT).append("return " + fieldName + ";\n"); } sb.append(INDENT + INDENT).append("}\n"); } sb.append(INDENT + INDENT) .append("if (!" + cld.getName() + ".class.equals(getClass())) {\n") .append(INDENT + INDENT + INDENT) .append("return TypeUtil.getField" + (proxy ? "Proxy" : "Value") + "(this, fieldName);\n") .append(INDENT + INDENT) .append("}\n") .append(INDENT + INDENT) .append("throw new IllegalArgumentException(\"Unknown field \" + fieldName);\n") .append(INDENT) .append("}\n"); return sb.toString(); }
/** * Generates the setoBJECT method for deserialising objects. * * @param cld a ClassDescriptor * @return a String containing the method */ public String generateSetObject(ClassDescriptor cld) { StringBuffer sb = new StringBuffer(); sb.append(INDENT) .append("public void setoBJECT(String notXml, ObjectStore os) {\n") .append(INDENT + INDENT) .append("setoBJECT(NotXmlParser.SPLITTER.split(notXml), os);\n") .append(INDENT) .append("}\n") .append(INDENT) .append("public void setoBJECT(final String[] notXml, final ObjectStore os) {\n") .append(INDENT + INDENT) .append( "if (!" + cld.getName() + (cld.isInterface() ? "Shadow" : "") + ".class.equals(getClass())) {\n") .append(INDENT + INDENT + INDENT) .append( "throw new IllegalStateException(\"Class \" + getClass().getName() + \"" + " does not match code (" + cld.getName() + ")\");\n") .append(INDENT + INDENT) .append("}\n") .append(INDENT + INDENT) .append("for (int i = 2; i < notXml.length;) {\n") .append(INDENT + INDENT + INDENT) .append("int startI = i;\n"); for (FieldDescriptor field : cld.getAllFieldDescriptors()) { String fieldName = field.getName(); if ("notXml".equals(fieldName)) { fieldName = "this.notXml"; } else if ("os".equals(fieldName)) { fieldName = "this.os"; } if (field instanceof AttributeDescriptor) { AttributeDescriptor attribute = (AttributeDescriptor) field; sb.append(INDENT + INDENT + INDENT) .append("if ((i < notXml.length) && \"a" + fieldName + "\".equals(notXml[i])) {\n") .append(INDENT + INDENT + INDENT + INDENT) .append("i++;\n") .append(INDENT + INDENT + INDENT + INDENT); if ("boolean".equals(attribute.getType())) { sb.append(fieldName + " = Boolean.parseBoolean(notXml[i]);\n"); } else if ("short".equals(attribute.getType())) { sb.append(fieldName + " = Short.parseShort(notXml[i]);\n"); } else if ("int".equals(attribute.getType())) { sb.append(fieldName + " = Integer.parseInt(notXml[i]);\n"); } else if ("long".equals(attribute.getType())) { sb.append(fieldName + " = Long.parseLong(notXml[i]);\n"); } else if ("float".equals(attribute.getType())) { sb.append(fieldName + " = Float.parseFloat(notXml[i]);\n"); } else if ("double".equals(attribute.getType())) { sb.append(fieldName + " = Double.parseDouble(notXml[i]);\n"); } else if ("java.lang.Boolean".equals(attribute.getType())) { sb.append(fieldName + " = Boolean.valueOf(notXml[i]);\n"); } else if ("java.lang.Short".equals(attribute.getType())) { sb.append(fieldName + " = Short.valueOf(notXml[i]);\n"); } else if ("java.lang.Integer".equals(attribute.getType())) { sb.append(fieldName + " = Integer.valueOf(notXml[i]);\n"); } else if ("java.lang.Long".equals(attribute.getType())) { sb.append(fieldName + " = Long.valueOf(notXml[i]);\n"); } else if ("java.lang.Float".equals(attribute.getType())) { sb.append(fieldName + " = Float.valueOf(notXml[i]);\n"); } else if ("java.lang.Double".equals(attribute.getType())) { sb.append(fieldName + " = Double.valueOf(notXml[i]);\n"); } else if ("java.util.Date".equals(attribute.getType())) { sb.append(fieldName + " = new java.util.Date(Long.parseLong(notXml[i]));\n"); } else if ("java.math.BigDecimal".equals(attribute.getType())) { sb.append(fieldName + " = new java.math.BigDecimal(notXml[i]);\n"); } else if ("org.intermine.objectstore.query.ClobAccess".equals(attribute.getType())) { sb.append( fieldName + " = org.intermine.objectstore.query.ClobAccess" + ".decodeDbDescription(os, notXml[i]);\n"); } else if ("java.lang.String".equals(attribute.getType())) { sb.append("StringBuilder string = null;\n") .append(INDENT + INDENT + INDENT + INDENT) .append( "while ((i + 1 < notXml.length) && (notXml[i + 1].charAt(0) == '" + ENCODED_DELIM + "')) {\n") .append(INDENT + INDENT + INDENT + INDENT + INDENT) .append("if (string == null) string = new StringBuilder(notXml[i]);\n") .append(INDENT + INDENT + INDENT + INDENT + INDENT) .append("i++;\n") .append(INDENT + INDENT + INDENT + INDENT + INDENT) .append("string.append(\"" + DELIM + "\").append(notXml[i].substring(1));\n") .append(INDENT + INDENT + INDENT + INDENT) .append("}\n") .append(INDENT + INDENT + INDENT + INDENT) .append(fieldName + " = string == null ? notXml[i] : string.toString();\n"); } else { throw new IllegalArgumentException("Unknown type " + attribute.getType()); } sb.append(INDENT + INDENT + INDENT + INDENT) .append("i++;\n") .append(INDENT + INDENT + INDENT) .append("}\n"); } else if (field.isReference()) { ReferenceDescriptor reference = (ReferenceDescriptor) field; sb.append(INDENT + INDENT + INDENT) .append("if ((i < notXml.length) &&\"r" + fieldName + "\".equals(notXml[i])) {\n") .append(INDENT + INDENT + INDENT + INDENT) .append("i++;\n") .append(INDENT + INDENT + INDENT + INDENT) .append( fieldName + " = new ProxyReference(os, Integer.valueOf(notXml[i])" + ", " + reference.getReferencedClassName() + ".class);\n") .append(INDENT + INDENT + INDENT + INDENT) .append("i++;\n") .append(INDENT + INDENT + INDENT) .append("};\n"); } } sb.append(INDENT + INDENT + INDENT) .append("if (startI == i) {\n") .append(INDENT + INDENT + INDENT + INDENT) .append("throw new IllegalArgumentException(\"Unknown field \" + notXml[i]);\n") .append(INDENT + INDENT + INDENT) .append("}\n") .append(INDENT + INDENT) .append("}\n"); for (FieldDescriptor field : cld.getAllFieldDescriptors()) { String fieldName = field.getName(); if ("notXml".equals(fieldName)) { fieldName = "this.notXml"; } else if ("os".equals(fieldName)) { fieldName = "this.os"; } if (field instanceof CollectionDescriptor) { CollectionDescriptor coll = (CollectionDescriptor) field; sb.append(INDENT + INDENT) .append( fieldName + " = new ProxyCollection<" + coll.getReferencedClassName() + ">(os, this, \"" + fieldName + "\", " + coll.getReferencedClassName() + ".class);\n"); } } sb.append(INDENT).append("}\n"); return sb.toString(); }
/** * Generates the getoBJECT method for producing NotXml. * * @param cld the ClassDescriptor * @return generated java code as a String */ protected String generateGetObject(ClassDescriptor cld) { StringBuffer sb = new StringBuffer(); sb.append(INDENT) .append("public StringConstructor getoBJECT() {\n") .append(INDENT + INDENT) .append( "if (!" + cld.getName() + (cld.isInterface() ? "Shadow" : "") + ".class.equals(getClass())) {\n") .append(INDENT + INDENT + INDENT) .append("return NotXmlRenderer.render(this);\n") .append(INDENT + INDENT) .append("}\n") .append(INDENT + INDENT) .append("StringConstructor sb = new StringConstructor();\n") .append(INDENT + INDENT) .append("sb.append(\"" + DELIM + cld.getName() + "\");\n"); for (FieldDescriptor field : cld.getAllFieldDescriptors()) { if (field instanceof AttributeDescriptor) { AttributeDescriptor attribute = (AttributeDescriptor) field; if (attribute.getType().startsWith("java.")) { sb.append(INDENT + INDENT) .append("if (" + attribute.getName() + " != null) {\n") .append(INDENT + INDENT + INDENT) .append("sb.append(\"" + DELIM + "a" + field.getName() + DELIM + "\")"); if ("java.util.Date".equals(attribute.getType())) { sb.append(".append(" + attribute.getName() + ".getTime());\n"); } else if ("java.lang.String".equals(attribute.getType())) { sb.append(";\n") .append(INDENT + INDENT + INDENT) .append("String string = " + attribute.getName() + ";\n") .append(INDENT + INDENT + INDENT) .append("while (string != null) {\n") .append(INDENT + INDENT + INDENT + INDENT) .append("int delimPosition = string.indexOf(\"" + DELIM + "\");\n") .append(INDENT + INDENT + INDENT + INDENT) .append("if (delimPosition == -1) {\n") .append(INDENT + INDENT + INDENT + INDENT + INDENT) .append("sb.append(string);\n") .append(INDENT + INDENT + INDENT + INDENT + INDENT) .append("string = null;\n") .append(INDENT + INDENT + INDENT + INDENT) .append("} else {\n") .append(INDENT + INDENT + INDENT + INDENT + INDENT) .append("sb.append(string.substring(0, delimPosition + 3));\n") .append(INDENT + INDENT + INDENT + INDENT + INDENT) .append("sb.append(\"" + ENCODED_DELIM + "\");\n") .append(INDENT + INDENT + INDENT + INDENT + INDENT) .append("string = string.substring(delimPosition + 3);\n") .append(INDENT + INDENT + INDENT + INDENT) .append("}\n") .append(INDENT + INDENT + INDENT) .append("}\n"); } else { sb.append(".append(" + attribute.getName() + ");\n"); } sb.append(INDENT + INDENT).append("}\n"); } else if ("org.intermine.objectstore.query.ClobAccess".equals(attribute.getType())) { sb.append(INDENT + INDENT) .append("if (" + attribute.getName() + " != null) {\n") .append(INDENT + INDENT + INDENT) .append("sb.append(\"" + DELIM + "a" + field.getName() + DELIM + "\" + ") .append(attribute.getName() + ".getDbDescription());\n") .append(INDENT + INDENT) .append("}\n"); } else { sb.append(INDENT) .append(INDENT) .append("sb.append(\"" + DELIM + "a" + field.getName() + DELIM + "\")") .append(".append(" + field.getName() + ");\n"); } } else if (field.isReference()) { sb.append(INDENT + INDENT) .append("if (" + field.getName() + " != null) {\n") .append(INDENT + INDENT + INDENT) .append("sb.append(\"" + DELIM + "r" + field.getName() + DELIM + "\")") .append(".append(" + field.getName() + ".getId());\n") .append(INDENT + INDENT) .append("}\n"); } } sb.append(INDENT + INDENT).append("return sb;\n").append(INDENT).append("}\n"); return sb.toString(); }
/** * Generate the output for a ClassDescriptor. * * @param cld the ClassDescriptor * @param shadow whether to generate the shadow class of an interface * @return the relevant String representation */ protected String generate(ClassDescriptor cld, boolean shadow) { StringBuffer sb = new StringBuffer(); String packageName = TypeUtil.packageName(cld.getName()); if (packageName.length() > 0) { sb.append("package ").append(packageName).append(";" + ENDL + ENDL); } if ((!cld.isInterface()) || shadow) { boolean hasCollections = false; boolean hasReferences = false; for (FieldDescriptor fd : cld.getAllFieldDescriptors()) { if (fd instanceof CollectionDescriptor) { hasCollections = true; } else if (fd instanceof ReferenceDescriptor) { hasReferences = true; } } sb.append("import org.intermine.objectstore.ObjectStore;" + ENDL); sb.append("import org.intermine.objectstore.intermine.NotXmlParser;" + ENDL); sb.append("import org.intermine.objectstore.intermine.NotXmlRenderer;" + ENDL); if (hasCollections) { sb.append("import org.intermine.objectstore.proxy.ProxyCollection;" + ENDL); } if (hasReferences) { sb.append("import org.intermine.objectstore.proxy.ProxyReference;" + ENDL); } sb.append("import org.intermine.util.StringConstructor;" + ENDL); sb.append("import org.intermine.util.TypeUtil;" + ENDL); if (shadow) { sb.append("import org.intermine.model.ShadowClass;" + ENDL); } sb.append(ENDL); } sb.append("public ") .append((cld.isInterface() && (!shadow)) ? "interface " : "class ") .append(TypeUtil.unqualifiedName(cld.getName())) .append(shadow ? "Shadow" : ""); if (shadow) { sb.append(" implements ") .append(TypeUtil.unqualifiedName(cld.getName())) .append(", ShadowClass"); } else { if (!cld.isInterface()) { if (cld.getSuperclassDescriptor() != null) { sb.append(" extends ").append(cld.getSuperclassDescriptor().getName()); } } boolean firstTime = true; if (cld.getSuperDescriptors().size() > 0) { for (ClassDescriptor superCld : cld.getSuperDescriptors()) { if (superCld.isInterface()) { if (firstTime) { sb.append(cld.isInterface() ? " extends " : " implements "); firstTime = false; } else { sb.append(", "); } sb.append(superCld.getName()); } } } else { sb.append(" implements org.intermine.model.FastPathObject"); } } sb.append(ENDL).append("{" + ENDL); if (shadow) { sb.append(INDENT) .append("public static final Class<") .append(TypeUtil.unqualifiedName(cld.getName())) .append("> shadowOf = ") .append(TypeUtil.unqualifiedName(cld.getName())) .append(".class;" + ENDL); } // FieldDescriptors defined for this class/interface if (cld.isInterface() && (!shadow)) { sb.append(generateFieldDescriptors(cld, false)); } else { sb.append(generateFieldDescriptors(cld, true)) .append(generateEquals(cld)) .append(generateHashCode(cld)) .append(generateToString(cld)) .append(generateGetFieldValue(cld, false)) .append(generateGetFieldValue(cld, true)) .append(generateSetFieldValue(cld)) .append(generateGetFieldType(cld)); if (cld.getSuperDescriptors().size() > 0) { sb.append(generateGetObject(cld)) .append(generateSetObject(cld)) .append(generateAddCollectionElement(cld)) .append(generateGetElementType(cld)); } } sb.append("}" + ENDL); return sb.toString(); }
@Override public int hashCode() { return cld.getName().hashCode(); }
/** * Look a the values of the given primary key in the object and return true if and only if some * part of the primary key is null. If the primary key contains a reference it is sufficient for * any of the primary keys of the referenced object to be non-null (ie objectPrimaryKeyIsNull() * returning true). * * @param model the Model in which to find ClassDescriptors * @param obj the Object to check * @param cld one of the classes that obj is. Only primary keys for this classes will be checked * @param pk the primary key to check * @param source the Source database * @param idMap an IntToIntMap from source IDs to destination IDs * @return true if the the given primary key is non-null for the given object * @throws MetaDataException if anything goes wrong */ public static boolean objectPrimaryKeyNotNull( Model model, InterMineObject obj, ClassDescriptor cld, PrimaryKey pk, Source source, IntToIntMap idMap) throws MetaDataException { for (String fieldName : pk.getFieldNames()) { FieldDescriptor fd = cld.getFieldDescriptorByName(fieldName); if (fd instanceof AttributeDescriptor) { Object value; try { value = obj.getFieldValue(fieldName); } catch (IllegalAccessException e) { throw new MetaDataException( "Failed to get field " + fieldName + " for key " + pk + " from " + obj, e); } if (value == null) { return false; } } else if (fd instanceof CollectionDescriptor) { throw new MetaDataException( "Primary key " + pk.getName() + " for class " + cld.getName() + " cannot contain collection " + fd.getName() + ": collections cannot be part of a primary key. Please edit" + model.getName() + "_keyDefs.properties"); } else if (fd instanceof ReferenceDescriptor) { InterMineObject refObj; try { refObj = (InterMineObject) obj.getFieldProxy(fieldName); } catch (IllegalAccessException e) { throw new MetaDataException( "Failed to get field " + fieldName + " for key " + pk + " from " + obj, e); } if (refObj == null) { return false; } if ((refObj.getId() != null) && (idMap.get(refObj.getId()) != null)) { // We have previously loaded the object in this reference. continue; } if (refObj instanceof ProxyReference) { refObj = ((ProxyReference) refObj).getObject(); } boolean foundNonNullKey = false; boolean foundKey = false; Set<ClassDescriptor> classDescriptors = model.getClassDescriptorsForClass(refObj.getClass()); CLDS: for (ClassDescriptor refCld : classDescriptors) { Set<PrimaryKey> primaryKeys; if (source == null) { primaryKeys = new LinkedHashSet<PrimaryKey>(PrimaryKeyUtil.getPrimaryKeys(refCld).values()); } else { primaryKeys = DataLoaderHelper.getPrimaryKeys(refCld, source, null); } for (PrimaryKey refPK : primaryKeys) { foundKey = true; if (objectPrimaryKeyNotNull(model, refObj, refCld, refPK, source, idMap)) { foundNonNullKey = true; break CLDS; } } } if (foundKey && (!foundNonNullKey)) { return false; } } } return true; }
/** * Return a Set of PrimaryKeys relevant to a given Source for a ClassDescriptor. The Set contains * all the primary keys that exist on a particular class that are used by the source, without * performing any recursion. The Model.getClassDescriptorsForClass() method is recommended if you * wish for all the primary keys of the class' parents as well. * * @param cld the ClassDescriptor * @param source the Source * @param os the ObjectStore that these PrimaryKeys are used in, for creating indexes * @return a Set of PrimaryKeys */ public static Set<PrimaryKey> getPrimaryKeys(ClassDescriptor cld, Source source, ObjectStore os) { GetPrimaryKeyCacheKey key = new GetPrimaryKeyCacheKey(cld, source); synchronized (getPrimaryKeyCache) { Set<PrimaryKey> keySet = getPrimaryKeyCache.get(key); if (keySet == null) { keySet = new LinkedHashSet<PrimaryKey>(); Properties keys = getKeyProperties(source); if (keys != null) { if (!verifiedSources.contains(source)) { String packageNameWithDot = cld.getName().substring(0, cld.getName().lastIndexOf('.') + 1); LOG.info( "Verifying primary key config for source " + source + ", packageName = " + packageNameWithDot); for (Map.Entry<Object, Object> entry : keys.entrySet()) { String cldName = (String) entry.getKey(); String keyList = (String) entry.getValue(); if (!cldName.contains(".")) { ClassDescriptor iCld = cld.getModel().getClassDescriptorByName(packageNameWithDot + cldName); if (iCld != null) { Map<String, PrimaryKey> map = PrimaryKeyUtil.getPrimaryKeys(iCld); String[] tokens = keyList.split(","); for (int i = 0; i < tokens.length; i++) { String token = tokens[i].trim(); if (map.get(token) == null) { throw new IllegalArgumentException( "Primary key " + token + " for class " + cldName + " required by datasource " + source.getName() + " in " + source.getName() + "_keys.properties is not defined in " + cld.getModel().getName() + "_keyDefs.properties"); } } } else { LOG.warn( "Ignoring entry for " + cldName + " in file " + cld.getModel().getName() + "_keyDefs.properties - not in model!"); } } } verifiedSources.add(source); } Map<String, PrimaryKey> map = PrimaryKeyUtil.getPrimaryKeys(cld); String cldName = TypeUtil.unqualifiedName(cld.getName()); String keyList = (String) keys.get(cldName); if (keyList != null) { String[] tokens = keyList.split(","); for (int i = 0; i < tokens.length; i++) { String token = tokens[i].trim(); if (map.get(token) == null) { throw new IllegalArgumentException( "Primary key " + token + " for class " + cld.getName() + " required by data source " + source.getName() + " in " + source.getName() + "_keys.properties is not defined in " + cld.getModel().getName() + "_keyDefs.properties"); } else { keySet.add(map.get(token)); } } } for (Map.Entry<Object, Object> entry : keys.entrySet()) { String propKey = (String) entry.getKey(); String fieldList = (String) entry.getValue(); int posOfDot = propKey.indexOf('.'); if (posOfDot > 0) { String propCldName = propKey.substring(0, posOfDot); if (cldName.equals(propCldName)) { String keyName = propKey.substring(posOfDot + 1); PrimaryKey pk = new PrimaryKey(keyName, fieldList, cld); if (!keySet.contains(pk)) { keySet.add(pk); if (os instanceof ObjectStoreInterMineImpl) { ObjectStoreInterMineImpl osimi = (ObjectStoreInterMineImpl) os; DatabaseSchema schema = osimi.getSchema(); ClassDescriptor tableMaster = schema.getTableMaster(cld); String tableName = DatabaseUtil.getTableName(tableMaster); List<String> fields = new ArrayList<String>(); for (String field : pk.getFieldNames()) { String colName = DatabaseUtil.generateSqlCompatibleName(field); if (tableMaster.getReferenceDescriptorByName(field, true) != null) { colName += "id"; } fields.add(colName); } String sql = "CREATE INDEX " + tableName + "__" + keyName + " ON " + tableName + " (" + StringUtil.join(fields, ", ") + ")"; System.out.println("Creating index: " + sql); LOG.info("Creating index: " + sql); Connection conn = null; try { conn = osimi.getConnection(); conn.createStatement().execute(sql); } catch (SQLException e) { LOG.warn("Index creation failed", e); } finally { if (conn != null) { osimi.releaseConnection(conn); } } } } } } } } else { throw new IllegalArgumentException( "Unable to find keys for source " + source.getName() + " in file " + source.getName() + "_keys.properties"); } getPrimaryKeyCache.put(key, keySet); } return keySet; } }