Commits

Sam Adams committed b1976b6

Initial commit of client api

Comments (0)

Files changed (6)

chempound-client/pom.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>net.chempound</groupId>
+        <artifactId>chempound</artifactId>
+        <version>0.1-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>chempound-client</artifactId>
+    <name>Chempound : Client API</name>
+
+    <dependencies>
+
+        <dependency>
+            <groupId>net.chempound</groupId>
+            <artifactId>chempound-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.swordapp</groupId>
+            <artifactId>sword-client</artifactId>
+            <version>2.0-r339</version>
+        </dependency>
+
+    </dependencies>
+
+</project>

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

+package net.chempound.client;
+
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
+import com.hp.hpl.jena.vocabulary.RDF;
+import net.chempound.rdf.CPTerms;
+import net.chempound.rdf.RdfIO;
+import org.apache.abdera.Abdera;
+import org.apache.abdera.model.Document;
+import org.swordapp.client.*;
+
+import java.io.ByteArrayInputStream;
+import java.net.URI;
+
+/**
+ * @author Sam Adams
+ */
+public class ChempoundClient {
+
+    private final SWORDClient swordClient;
+
+    public ChempoundClient() {
+        this(new SWORDClient());
+    }
+
+    public ChempoundClient(final SWORDClient swordClient) {
+        this.swordClient = swordClient;
+    }
+
+    public CreateCollectionResponse createCollection(final CreateCollectionRequest request) throws Exception {
+        final DepositReceipt receipt = swordClient.deposit(request.getURI().toString(), mapCreateCollectionRequest(request));
+        return mapCreateCollectionResponse(receipt);
+    }
+
+    private Deposit mapCreateCollectionRequest(final CreateCollectionRequest request) {
+        final EntryPart entryPart = new EntryPart();
+        if (request.getTitle() != null) {
+            entryPart.getEntry().setTitle(request.getTitle());
+        }
+
+        final Model metadataModel = ModelFactory.createDefaultModel();
+        metadataModel.getResource("").addProperty(RDF.type, CPTerms.Collection);
+
+        attachMetadata(entryPart, metadataModel);
+
+        final Deposit deposit = new DepositFactory().newMetadataOnly(entryPart);
+        if (request.getSlug() != null) {
+            deposit.setSlug(request.getSlug());
+        }
+        return deposit;
+    }
+
+    private 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());
+    }
+
+    private CreateCollectionResponse mapCreateCollectionResponse(final DepositReceipt receipt) {
+        return new CreateCollectionResponse(URI.create(receipt.getSplashPageIRI().toString()), null);
+    }
+
+}

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

+package net.chempound.client;
+
+import java.net.URI;
+
+/**
+ * @author Sam Adams
+ */
+public class CreateCollectionRequest {
+
+    private URI uri;
+    private String title;
+    private String slug;
+    
+    public URI getURI() {
+        return uri;
+    }
+
+    public CreateCollectionRequest withUri(final URI uri) {
+        this.uri = uri;
+        return this;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public CreateCollectionRequest withTitle(final String title) {
+        this.title = title;
+        return this;
+    }
+
+    public String getSlug() {
+        return slug;
+    }
+
+    public CreateCollectionRequest withSlug(final String slug) {
+        this.slug = slug;
+        return this;
+    }
+}

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

+package net.chempound.client;
+
+import java.net.URI;
+
+/**
+ * @author Sam Adams
+ */
+public class CreateCollectionResponse {
+
+    private final URI uri;
+    private final String title;
+
+    public CreateCollectionResponse(final URI uri, final String title) {
+        this.uri = uri;
+        this.title = title;
+    }
+
+    public URI getUri() {
+        return uri;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+}

chempound-webapi/pom.xml

         <dependency>
             <groupId>org.freemarker</groupId>
             <artifactId>freemarker</artifactId>
-            <version>2.3.9</version>
         </dependency>
         <dependency>
             <groupId>com.google.inject</groupId>
             <artifactId>guice-multibindings</artifactId>
         </dependency>
 
-        <dependency>
-            <groupId>javax.inject</groupId>
-            <artifactId>javax.inject</artifactId>
-            <version>1</version>
-        </dependency>
-
     </dependencies>
 
 </project>
     <modules>
         <module>chempound-api</module>
         <module>chempound-app</module>
+        <module>chempound-client</module>
         <module>chempound-plugins</module>
         <module>chempound-rdf</module>
         <module>chempound-webapi</module>