diff --git a/src/edu/ucsb/nceas/metacat/dataone/MNodeService.java b/src/edu/ucsb/nceas/metacat/dataone/MNodeService.java index d3b5e6fd2..3f6c7b7ac 100644 --- a/src/edu/ucsb/nceas/metacat/dataone/MNodeService.java +++ b/src/edu/ucsb/nceas/metacat/dataone/MNodeService.java @@ -2190,7 +2190,7 @@ public Identifier publish(Session session, Identifier originalIdentifier) throws Map obsoletedBys = new HashMap(); obsoletedBys.put(originalIdentifier, newIdentifier); ByteArrayOutputStream out = new ByteArrayOutputStream(); - modifier.replaceObsoletedIds(obsoletedBys, out); + modifier.replaceObsoletedIds(obsoletedBys, out, session.getSubject()); String resourceMapString = out.toString("UTF-8"); // get the original ORE SM and update the values diff --git a/src/edu/ucsb/nceas/metacat/dataone/resourcemap/ResourceMapModifier.java b/src/edu/ucsb/nceas/metacat/dataone/resourcemap/ResourceMapModifier.java index c71e8fed3..f476f3176 100644 --- a/src/edu/ucsb/nceas/metacat/dataone/resourcemap/ResourceMapModifier.java +++ b/src/edu/ucsb/nceas/metacat/dataone/resourcemap/ResourceMapModifier.java @@ -36,10 +36,12 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.dataone.service.types.v1.Identifier; +import org.dataone.service.types.v1.Subject; import org.dataone.vocabulary.CITO; import org.dataone.vocabulary.DC_TERMS; import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; +import com.hp.hpl.jena.rdf.model.AnonId; import com.hp.hpl.jena.rdf.model.Literal; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; @@ -105,9 +107,10 @@ public ResourceMapModifier(Identifier oldResourceMapId, InputStream originalReso * Create new resource map by replacing obsoleted ids by new ids. * @param obsoletedBys a map represents the ids' with the obsoletedBy relationship - the keys are the one need to be obsoleted (replaced); value are the new ones need to be used. They are all science metadata objects * @param newResourceMap the place where the created new resource map will be written + * @param subject the subject who generates the resource map * @throws UnsupportedEncodingException */ - public void replaceObsoletedIds(MapobsoletedBys, OutputStream newResourceMap ) throws UnsupportedEncodingException { + public void replaceObsoletedIds(MapobsoletedBys, OutputStream newResourceMap, Subject subject) throws UnsupportedEncodingException { //replace ids Vector oldURIs = new Vector(); //those uris (resource) shouldn't be aggregated into the new ore since they are obsoleted Vector newURIs = new Vector(); //those uris (resource) should be added into the new aggregation @@ -148,12 +151,12 @@ public void replaceObsoletedIds(MapobsoletedBys, Output iterator = model.listStatements(selector); while (iterator.hasNext()) { Statement statement = iterator.nextStatement(); - Resource subject = statement.getSubject(); + Resource subj = statement.getSubject(); //handle the case - oldId isDocumentBy oldId - if(subject.getURI().equals(oldResource.getURI())) { - subject = newResource; + if(subj.getURI().equals(oldResource.getURI())) { + subj = newResource; } - Statement newStatement = ResourceFactory.createStatement(subject, CITO.isDocumentedBy, newResource); + Statement newStatement = ResourceFactory.createStatement(subj, CITO.isDocumentedBy, newResource); needToRemove.add(statement); model.add(newStatement); } @@ -166,20 +169,23 @@ public void replaceObsoletedIds(MapobsoletedBys, Output } //generate a new resource for the new resource map identifier - Resource newOreResource = generateNewOREResource(model); + Resource newOreResource = generateNewOREResource(model, subject); Resource oldOreResource = getResource(model,oldResourceMapId.getValue()); replaceAggregations(model, oldOreResource, newOreResource, oldURIs, newURIs); //write it to standard out model.write(newResourceMap); } + + /** * This method generates a Resource object for the new ore id in the given model * @param model the model where the new generated Resource object will be attached + * @param subject name of the creator of this resource map * @return the generated new ORE Resource object * @throws UnsupportedEncodingException */ - private Resource generateNewOREResource(Model model) throws UnsupportedEncodingException { + private Resource generateNewOREResource(Model model, Subject subject) throws UnsupportedEncodingException { String escapedNewOreId = URLEncoder.encode(newResourceMapId.getValue(), "UTF-8"); String uri = baseURI + escapedNewOreId; Resource resource = model.createResource(uri); @@ -206,9 +212,35 @@ private Resource generateNewOREResource(Model model) throws UnsupportedEncodingE Resource typeObj = ResourceFactory.createResource("http://www.openarchives.org/ore/terms/ResourceMap"); Statement state4 = ResourceFactory.createStatement(resource, typePred, typeObj); model.add(state4); - //TODO: create a creator statement + //create a creator statement + Property creator = ResourceFactory.createProperty("http://purl.org/dc/elements/1.1/", "creator"); + Resource agent = generateAgentResource(subject); + Statement creatorState = ResourceFactory.createStatement(resource, creator, agent); + model.add(creatorState); return resource; } + + /** + * Generate an agent resource + * @param subject the name of the agent resource + * @return the agent resource + */ + private Resource generateAgentResource(Subject subject) { + String name = "Metacat"; + if (subject != null && subject.getValue() != null && !subject.getValue().trim().equals("")) { + name = subject.getValue(); + } + Resource creator = model.createResource(AnonId.create()); + Property type = ResourceFactory.createProperty(RDF_NAMESPACE, "type"); + Resource typeObj = ResourceFactory.createResource("http://purl.org/dc/terms/Agent"); + Statement statement = ResourceFactory.createStatement(creator, type, typeObj); + model.add(statement); + Property namePred = ResourceFactory.createProperty("http://xmlns.com/foaf/0.1/", "name"); + Literal nameObj = ResourceFactory.createPlainLiteral(name); + Statement nameState = ResourceFactory.createStatement(creator, namePred, nameObj); + model.add(nameState); + return creator; + } /** * Replace the old aggregation relationship by the new ore id. diff --git a/test/edu/ucsb/nceas/metacat/dataone/resourcemap/ResourceMapModifierTest.java b/test/edu/ucsb/nceas/metacat/dataone/resourcemap/ResourceMapModifierTest.java index b3e409d67..6d806689c 100644 --- a/test/edu/ucsb/nceas/metacat/dataone/resourcemap/ResourceMapModifierTest.java +++ b/test/edu/ucsb/nceas/metacat/dataone/resourcemap/ResourceMapModifierTest.java @@ -28,6 +28,7 @@ import java.util.Vector; import org.dataone.service.types.v1.Identifier; +import org.dataone.service.types.v1.Subject; import org.dataone.vocabulary.CITO; import com.hp.hpl.jena.rdf.model.Model; @@ -121,7 +122,9 @@ public void testReplaceObsoletedIds() throws Exception { new_metadata_id.setValue(NEW_METADATA_PID); obsoletedBys.put(origin_metadata_id, new_metadata_id); ByteArrayOutputStream out = new ByteArrayOutputStream(); - modifier.replaceObsoletedIds(obsoletedBys, out); + Subject subj = new Subject(); + subj.setValue("foo"); + modifier.replaceObsoletedIds(obsoletedBys, out, subj); String outStr = out.toString("UTF-8"); System.out.println(outStr); ByteArrayInputStream in = new ByteArrayInputStream(outStr.getBytes("UTF-8"));