[MOBY-guts] biomoby commit

Paul Gordon gordonp at dev.open-bio.org
Sun Apr 8 17:29:29 UTC 2007


gordonp
Sun Apr  8 13:29:29 EDT 2007
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data
In directory dev.open-bio.org:/tmp/cvs-serv21088/src/main/org/biomoby/shared/data

Modified Files:
	MobyDataComposite.java 
Log Message:
Made member data type enforcement more consistent, now you can't manually add field of the wrong type either (with the put() method)
moby-live/Java/src/main/org/biomoby/shared/data MobyDataComposite.java,1.12,1.13
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataComposite.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataComposite.java	2007/03/12 17:02:30	1.12
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataComposite.java	2007/04/08 17:29:29	1.13
@@ -54,12 +54,16 @@
 		// will be a problem for member association (i.e. it's an anonymous variable)
 		String fieldName = getName(child);
 		if(fieldName == null || fieldName.length() == 0){
-		    throw new IllegalArgumentException("The subelement with index " + i +
-						       " does not have an article " +
-						       "name, which is required");
+		    throw new MobyException("The subelement with index " + i +
+					    " does not have an article " +
+					    "name, which is required");
 		}
-
-		put(fieldName, (MobyDataObject) createInstanceFromDOM(child));
+		MobyDataObject childObject = (MobyDataObject) createInstanceFromDOM(child);
+		if(childObject == null){
+		    throw new MobyException("The object member '" + fieldName + "' for object '"+ getName() +
+					    " could not be properly parsed into a MOBY object");
+		}
+		put(fieldName, childObject);
 	    }
 	}
     }
@@ -251,8 +255,7 @@
 
 	    // Print out the sorted fields' XML
 	    for(int i = 0; i < fieldNames.length; i++){
-		//MobyDataObject mdsi = members.get(fieldNames[i]); //Java1.5
-		MobyDataObject mdsi = (MobyDataObject) members.get(fieldNames[i]);
+		MobyDataObject mdsi = members.get(fieldNames[i]);
 		// ensure the articleName is set correctly for the requirements of this object
 		if(!fieldNames[i].toString().equals(mdsi.getName())){
 		    mdsi.setName(fieldNames[i].toString());
@@ -378,11 +381,28 @@
      * e.g. creating a GenericSequence involves putting a 'length' member (MobyDataInt), 
      * then putting a 'sequence' member (MobyDataString).
      *
+     * This method will throw an IllegalArgumentException if the value does not inherit from the type defined by Moby Central  
+     *
      * NOTE: if the field already exists and is defined with the HAS relationship in the DataType ontology, 
      * the previous value is NOT overwritten, rather the MobyDataObject is switched into a MobyDataObjectVector.
      * Replace mode will be used unless the data type and relationship are properly defined.
      */
     public MobyDataObject put(String fieldName, MobyDataObject value){
+	if(value == null){
+	    return null;
+	}
+
+	MobyRelationship relationship = getDataType().getChild(fieldName);
+	MobyDataType childDataType = MobyDataType.getDataType(relationship.getDataTypeName());
+	if(!value.getDataType().inheritsFrom(childDataType)){
+	    // Incompatible types
+	    throw new IllegalArgumentException("The object member '" + fieldName + "' for object '"+ getName() +
+					       "' does not inherit from the required data type " +
+					       "(found data type " + value.getDataType().getName()+
+					       ", but require subclass of " + 
+					       childDataType.getName() +")");
+	}
+
 	// If the field already exists, see if the HAS (1-to-many) relationship exists
 	if(members.containsKey(fieldName)){
 	    MobyDataObject existingMember = members.get(fieldName);
@@ -393,12 +413,11 @@
 		return existingMember;
 	    }
 	    
-	    // Use replace mode if the data type or relatioship is unavailable
+	    // Use replace mode if the data type or relationship is unavailable
 	    MobyDataType dt = getDataType();
 	    if(dt == null){
 		return members.put(fieldName, value);
 	    }
-	    MobyRelationship relationship = getDataType().getChild(fieldName);
 	    if(relationship == null){
 		return members.put(fieldName, value);
 	    }




More information about the MOBY-guts mailing list