Sam Adams avatar Sam Adams committed eb1fd4d

Refactoring chemistry importers

Comments (0)

Files changed (7)

crystallography-importer/pom.xml

             <groupId>net.chempound</groupId>
             <artifactId>chempound-rdf-cml</artifactId>
         </dependency>
+        <dependency>
+            <groupId>net.chempound.client</groupId>
+            <artifactId>client-utils</artifactId>
+        </dependency>
 
         <dependency>
             <groupId>cml</groupId>
             <version>1.0-SNAPSHOT</version>
         </dependency>
 
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>net.chempound.chemistry</groupId>
+            <artifactId>chemistry-import-testutils</artifactId>
+            <scope>test</scope>
+        </dependency>
+
     </dependencies>
 
 </project>

crystallography-importer/src/main/java/net/chempound/crystal/importer/CrystalStructureImporter.java

 import com.hp.hpl.jena.rdf.model.Resource;
 import com.hp.hpl.jena.rdf.model.Statement;
 import com.hp.hpl.jena.rdf.model.StmtIterator;
-import com.hp.hpl.jena.sparql.vocabulary.FOAF;
 import com.hp.hpl.jena.vocabulary.RDFS;
 import net.chempound.chemistry.CmlImporter;
+import net.chempound.chemistry.ImageGenerator;
+import net.chempound.chemistry.cml.ChemicalMime;
 import net.chempound.chemistry.inchi.InchiTool;
 import net.chempound.content.DefaultDepositRequest;
 import net.chempound.content.DepositRequest;
 import net.chempound.rdf.chempound.ChempoundAggregation;
 import net.chempound.storage.InMemoryResource;
 import net.chempound.storage.LocalResource;
-import net.chempound.util.MimeType;
 import nu.xom.Element;
 import nu.xom.Elements;
 import nu.xom.Node;
 import org.xmlcml.cml.element.CMLMolecule;
 import org.xmlcml.cml.tools.ConnectionTableTool;
 
+import javax.inject.Inject;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
 
     private static final Logger LOG = LoggerFactory.getLogger(CrystalStructureImporter.class);
 
-
-    public static final MimeType CHEMICAL_CIF = new MimeType("chemical/x-cif", "Crystallographic Information Framework");
+    private final ImageGenerator imageGenerator;
 
     private boolean createImages = true;
 
+    public CrystalStructureImporter() {
+        this(new ImageGenerator());
+    }
+
+    @Inject
+    public CrystalStructureImporter(final ImageGenerator imageGenerator) {
+        this.imageGenerator = imageGenerator;
+    }
+
     public void load(final CIF originalCif) throws Exception {
 
         final List<CIF> splitCifs = CifSplitter.split(originalCif);
             try {
                 final File thumbnailFile = File.createTempFile("image_tn", "png");
                 try {
-                    createImages(cifResource, "cif", imageFile, thumbnailFile);
+                    imageGenerator.createImages(cifResource, "cif", imageFile, thumbnailFile);
                     imageResource = createImageResource(imageFile, id+".png");
                     thumbnailResource = createImageResource(thumbnailFile, id+"_tn.png");
                 } finally {
         final CrystalCml2RdfConverter conv = new CrystalCml2RdfConverter();
         final Model model = conv.createModel(completeCml, URI.create(""));
 
+        final DepositRequest depositRequest = new DefaultDepositRequest();
+        depositRequest.setSlug(id);
 
-        final DepositRequest loadRequest = new DefaultDepositRequest();
-        loadRequest.setSlug(id);
-        final ChempoundAggregation item = loadRequest.getResourceMetadata();
+        final ChempoundAggregation item = depositRequest.getResourceMetadata();
         addType(item);
 
         final LocalResource rdfResource = writeRdf(id+".rdf", model);
 
-        loadRequest.addResource(cifResource);
-        loadRequest.addResource(cmlResource);
-        loadRequest.addResource(rdfResource);
+        depositRequest.addResource(cifResource);
+        depositRequest.addResource(cmlResource);
+        depositRequest.addResource(rdfResource);
 
         if (imageResource != null) {
-            item.addProperty(FOAF.img, imageResource);
-            loadRequest.addResource(imageResource);
+            depositRequest.setImage(imageResource);
         }
         if (thumbnailResource != null) {
-            item.addProperty(FOAF.thumbnail, thumbnailResource);
-            loadRequest.addResource(thumbnailResource);
+            depositRequest.setThumbnail(thumbnailResource);
         }
 
+        final String title = getTitle(model);
+        if (title != null) {
+            depositRequest.setTitle(title);
+        }
+
+        addAuthors(model);
+
+        return depositRequest;
+    }
+
+    private void addAuthors(final Model model) {
+        final Resource root = model.getResource("");
+        StmtIterator it = root.listProperties(CifDict.publAuthorName);
+        List<String> names = new ArrayList<String>();
+        while (it.hasNext()) {
+            Statement s = it.next();
+            names.add(s.getObject().asLiteral().getString());
+        }
+        for (String name : names) {
+            root.addProperty(DCTerms.contributor, name);
+        }
+    }
+
+    private String getTitle(final Model model) {
         final Resource root = model.getResource("");
         if (root.hasProperty(CifDict.chemicalNameSystematic)) {
-            String title = root.getProperty(CifDict.chemicalNameSystematic).getString();
-            item.setTitle(title);
+            return root.getProperty(CifDict.chemicalNameSystematic).getString();
         } else if (root.hasProperty(CifDict.publSectionTitle)) {
-            String title = root.getProperty(CifDict.publSectionTitle).getString();
-            item.setTitle(title);
+            return root.getProperty(CifDict.publSectionTitle).getString();
         }
-
-        // chemical_formula_moiety
-
-        if (root.hasProperty(CifDict.publAuthorName)) {
-            StmtIterator it = root.listProperties(CifDict.publAuthorName);
-            List<String> names = new ArrayList<String>();
-            while (it.hasNext()) {
-                Statement s = it.next();
-                names.add(s.getObject().asLiteral().getString());
-            }
-            for (String name : names) {
-                root.addProperty(DCTerms.contributor, name);
-            }
-        }
-
-        return loadRequest;
+        return null;
     }
 
     public static void attachIdentifiers(final CMLCml completeCml) {
         final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
         CifIO.writeCif(cif, buffer, "UTF-8");
         final byte[] bytes = buffer.toByteArray();
-        return new InMemoryResource(path, CHEMICAL_CIF, bytes);
+        return new InMemoryResource(path, ChemicalMime.CHEMICAL_CIF, bytes);
     }
 
 

crystallography-importer/src/test/java/net/chempound/crystal/importer/CrystalStructureImporterTest.java

 import com.hp.hpl.jena.rdf.model.Model;
 import com.hp.hpl.jena.rdf.model.ModelFactory;
 import com.hp.hpl.jena.rdf.model.RDFReader;
+import net.chempound.chemistry.ImageGenerator;
 import net.chempound.rdf.IgnoringEmptyBaseUriErrorHandler;
 import net.chempound.storage.DepositResource;
 import net.chempound.util.MimeType;
 import nu.xom.ParsingException;
 import org.apache.commons.io.IOUtils;
+import org.junit.Before;
 import org.junit.Test;
 import org.xmlcml.cif.CIF;
 import org.xmlcml.cif.CIFException;
 import java.io.IOException;
 import java.io.InputStream;
 
+import static net.chempound.chemistry.testutil.ImageGeneratorMocker.mockImageGenerator;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
  */
 public class CrystalStructureImporterTest {
 
+    private ImageGenerator imageGenerator;
+
     static {
         CrystalPersonalities.init();
     }
 
+    @Before
+    public void setUp() throws Exception {
+        imageGenerator = mockImageGenerator();
+    }
+
     @Test
     public void testCifImporter() throws IOException, CIFException, ParsingException {
         CIF cif = loadCif("/wn2432.cif");
 
-        CrystalStructureImporter importer = new CrystalStructureImporter();
+        CrystalStructureImporter importer = new CrystalStructureImporter(imageGenerator);
         DepositRequest request = importer.generateDepositRequest(cif, "wn2432");
 
         Model model = getRdfModel(request);

crystallography-test-harness/pom.xml

             <scope>test</scope>
         </dependency>
 
+        <dependency>
+            <groupId>net.chempound.chemistry</groupId>
+            <artifactId>chemistry-import-testutils</artifactId>
+            <scope>test</scope>
+        </dependency>
+
     </dependencies>
 
 </project>

crystallography-test-harness/src/test/java/net/chempound/crystal/CrystalIntegrationTest.java

 import com.hp.hpl.jena.rdf.model.Statement;
 import com.hp.hpl.jena.xmloutput.RDFXMLWriterI;
 import com.hp.hpl.jena.xmloutput.impl.Abbreviated;
+import net.chempound.DefaultChempoundModule;
+import net.chempound.chemistry.ImageGenerator;
+import net.chempound.config.BaseUri;
+import net.chempound.config.ChempoundConfiguration;
+import net.chempound.config.DefaultChempoundConfiguration;
+import net.chempound.content.DepositRequest;
+import net.chempound.crystal.importer.CrystalStructureImporter;
+import net.chempound.crystal.importer.utils.CifIO;
+import net.chempound.rdf.DCTerms;
+import net.chempound.storage.DepositResource;
+import net.chempound.webapp.DefaultChempoundWebModule;
 import nu.xom.ParsingException;
 import org.apache.abdera.Abdera;
 import org.apache.abdera.model.Document;
 import org.restlet.data.Status;
 import org.swordapp.client.*;
 import org.xmlcml.cif.CIF;
-import net.chempound.config.BaseUri;
-import net.chempound.config.ChempoundConfiguration;
-import net.chempound.DefaultChempoundModule;
-import net.chempound.config.DefaultChempoundConfiguration;
-import net.chempound.content.DepositRequest;
-import net.chempound.crystal.importer.CrystalStructureImporter;
-import net.chempound.crystal.importer.utils.CifIO;
-import net.chempound.rdf.DCTerms;
-import net.chempound.storage.DepositResource;
-import net.chempound.webapp.DefaultChempoundWebModule;
 
 import java.io.*;
 import java.net.URI;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import static net.chempound.chemistry.testutil.ImageGeneratorMocker.mockImageGenerator;
+import static org.junit.Assert.*;
 
 /**
  * @author sea36
 
     private File workspace;
 
+    private ImageGenerator imageGenerator;
+
     @Before
     public void init() throws Exception {
 
+        imageGenerator = mockImageGenerator();
+
         workspace = createWorkspace();
 
         Injector injector = Guice.createInjector(
 
 
     private DepositReceipt performDeposit(CIF cif, String id) throws IOException, ParsingException, SWORDClientException, SWORDError, ProtocolViolationException {
-        CrystalStructureImporter importer = new CrystalStructureImporter();
+        CrystalStructureImporter importer = new CrystalStructureImporter(imageGenerator);
         DepositRequest request = importer.generateDepositRequest(cif, id);
         Deposit deposit = createDeposit(id, request);
 

crystallography-test-harness/src/test/java/net/chempound/crystal/CrystalSearchIntegrationTest.java

 import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.google.inject.Module;
+import net.chempound.chemistry.ImageGenerator;
 import net.chempound.client.SwordUtil;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import java.net.URI;
 import java.util.UUID;
 
+import static net.chempound.chemistry.testutil.ImageGeneratorMocker.mockImageGenerator;
 import static org.junit.Assert.*;
 
 /**
  */
 public class CrystalSearchIntegrationTest {
 
+    public static final String SWORD_ENDPOINT = "http://localhost:8715/sword/collection/";
+
+    private static ImageGenerator imageGenerator;
     private static Component component;
-
     private static File workspace;
-    public static final String SWORD_ENDPOINT = "http://localhost:8715/sword/collection/";
 
     @BeforeClass
     public static void init() throws Exception {
 
+        imageGenerator = mockImageGenerator();
+
         workspace = createWorkspace();
 
         Injector injector = Guice.createInjector(
     }
 
     private static void loadData() throws Exception {
-
         loadData("ds2101sup1");
-
-        Thread.sleep(120000);
-
         loadData("ff2005sup1");
         loadData("jj2086");
         loadData("wn2432");
-
     }
 
     private static void loadData(String id) throws Exception {
         InputStream in = CrystalSearchIntegrationTest.class.getResourceAsStream("/"+id+".cif");
         try {
-            CrystalStructureImporter importer = new CrystalStructureImporter();
+            CrystalStructureImporter importer = new CrystalStructureImporter(imageGenerator);
             importer.setCreateImages(false);
             DepositRequest request = importer.generateDepositRequest(in, id);
             Deposit deposit = SwordUtil.createSwordDeposit(request);
             </dependency>
 
             <dependency>
+                <groupId>net.chempound.chemistry</groupId>
+                <artifactId>chemistry-import-testutils</artifactId>
+                <version>0.1-SNAPSHOT</version>
+            </dependency>
+
+            <dependency>
+                <groupId>net.chempound.client</groupId>
+                <artifactId>client-utils</artifactId>
+                <version>0.1-SNAPSHOT</version>
+            </dependency>
+
+            <dependency>
                 <groupId>cml</groupId>
                 <artifactId>jumbo-converters-cif</artifactId>
                 <version>0.4-SNAPSHOT</version>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.