Commits

Sam Adams committed 67d0acb

Include metadata on deposit

Comments (0)

Files changed (3)

chempound-client/src/main/java/net/chempound/client/DefaultChempoundClient.java

             final String mimetype = "application/zip";
             final String packaging = UriRegistry.PACKAGE_SIMPLE_ZIP;
             final InputStream file = createZipFile(request);
-            
+
             if (request.hasMetadata()) {
-                deposit = depositFactory.newMultipart(createEntryPart(request), file, filename, mimetype, packaging);
+                final EntryPart entryPart = createEntryPart(request);
+                attachMetadata(entryPart, request.getMetadata());
+                deposit = depositFactory.newMultipart(entryPart, file, filename, mimetype, packaging);
             } else {
                 deposit = depositFactory.newBinaryOnly(file, filename, mimetype, packaging);
             }
         } else {
-            deposit = depositFactory.newMetadataOnly(createEntryPart(request));
+            final EntryPart entryPart = createEntryPart(request);
+            attachMetadata(entryPart, request.getMetadata());
+            deposit = depositFactory.newMetadataOnly(entryPart);
         }
 
         if (request.getSlug() != null) {
     }
 
     private static void attachMetadata(final EntryPart entryPart, final Model metadataModel) {
-        final byte[] bytes = RdfIO.writeRdf(metadataModel);
-        final Document document = Abdera.getNewParser().parse(new ByteArrayInputStream(bytes));
-        entryPart.getEntry().addExtension(document.getRoot());
+        if (metadataModel != null) {
+            final byte[] bytes = RdfIO.writeRdf(metadataModel);
+            final Document document = Abdera.getNewParser().parse(new ByteArrayInputStream(bytes));
+            entryPart.getEntry().addExtension(document.getRoot());
+        }
     }
 
 }

chempound-client/src/main/java/net/chempound/client/DepositItemRequest.java

 package net.chempound.client;
 
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
 import net.chempound.storage.LocalResource;
 
 import java.net.URI;
     private URI uri;
     private String title;
     private String slug;
+    private Model metadata;
 
     public URI getURI() {
         return uri;
         return this;
     }
 
-    public void addResource(final LocalResource resource) {
+    public DepositItemRequest withMetadata(final Model metadata) {
+        this.metadata = ModelFactory.createDefaultModel().add(metadata);
+        return this;
+    }
+
+    public Model getMetadata() {
+        return metadata;
+    }
+
+    public DepositItemRequest withFile(final LocalResource resource) {
         resources.add(resource);
+        return this;
     }
 
     public boolean hasMetadata() {
-        return title != null;
+        return title != null || metadata != null;
     }
 
     public boolean hasResources() {

chempound-client/src/main/java/net/chempound/client/DepositRequestMapper.java

  * @author Sam Adams
  */
 public class DepositRequestMapper {
+
     public static DepositItemRequest map(final DepositRequest deposit) {
         final DepositItemRequest request = new DepositItemRequest()
                                             .withTitle(deposit.getTitle())
-                                            .withSlug(deposit.getSlug());
+                                            .withSlug(deposit.getSlug())
+                                            .withMetadata(deposit.getMetadataModel());
         for (final DepositResource resource : deposit.getAggregatedResources()) {
-            request.addResource((LocalResource) resource);
+            request.withFile((LocalResource) resource);
         }
 
         return request;
     }
+
 }