Sam Adams avatar Sam Adams committed 01a7522

Refactoring chemical importers to share more code

Comments (0)

Files changed (4)

chemistry-common/src/main/java/net/chempound/chemistry/cml/CmlUtils.java

 import org.xmlcml.cml.element.CMLModule;
 
 import javax.xml.namespace.QName;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * @author Sam Adams
  */
 public class CmlUtils {
 
-    public static CMLModule findModuleByConvention(final CMLElement cml, final QName convention) {
+    public static List<CMLModule> findModulesWithConvention(final CMLElement cml, final QName convention) {
+        final Elements elements = cml.getChildCMLElements("module");
+        final List<CMLModule> result = new ArrayList<CMLModule>();
+        for (int i = 0; i < elements.size(); i++) {
+            final CMLModule module = (CMLModule) elements.get(i);
+            if (convention.equals(getConvention(module))) {
+                result.add(module);
+            }
+        }
+        return result;
+    }
+
+    public static CMLModule findFirstModuleByConvention(final CMLElement cml, final QName convention) {
         final Elements elements = cml.getChildCMLElements("module");
         for (int i = 0; i < elements.size(); i++) {
             final CMLModule module = (CMLModule) elements.get(i);
-            if (convention.equals(CmlUtils.getConvention(module))) {
+            if (convention.equals(getConvention(module))) {
                 return module;
             }
         }

chemistry-import-testutils/src/main/java/net/chempound/chemistry/testutil/ImageGeneratorStubber.java

 package net.chempound.chemistry.testutil;
 
 import net.chempound.chemistry.ImageGenerator;
+import org.apache.commons.io.FileUtils;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 import org.xmlcml.cml.element.CMLMolecule;
  */
 public class ImageGeneratorStubber {
 
+    private static final byte[] STUB_IMAGE = new byte[]{0};
+
     public static ImageGenerator stubImageGenerator() throws IOException {
         final ImageGenerator imageGenerator = mock(ImageGenerator.class);
 
             public Object answer(final InvocationOnMock invocation) throws Throwable {
                 final File imageFile = (File) invocation.getArguments()[1];
                 final File thumbnailFile = (File) invocation.getArguments()[2];
-                imageFile.createNewFile();
-                thumbnailFile.createNewFile();
+                FileUtils.writeByteArrayToFile(imageFile, STUB_IMAGE);
+                FileUtils.writeByteArrayToFile(thumbnailFile, STUB_IMAGE);
                 return null;
             }
         }).when(imageGenerator).createImages(any(CMLMolecule.class), any(File.class), any(File.class));
+
         return imageGenerator;
     }
 

chemistry-importer/src/main/java/net/chempound/chemistry/CmlImporter.java

 
 import net.chempound.ContentImporter;
 import net.chempound.chemistry.cml.ChemicalMime;
+import net.chempound.content.DepositRequest;
 import net.chempound.storage.InMemoryResource;
 import net.chempound.storage.LocalResource;
 import net.chempound.util.MimeType;
-import nu.xom.Document;
-import nu.xom.Element;
-import nu.xom.ParsingException;
-import nu.xom.Serializer;
+import nu.xom.*;
 import org.apache.commons.io.FileUtils;
 import org.xmlcml.cml.base.CMLBuilder;
+import org.xmlcml.cml.base.CMLUtil;
+import org.xmlcml.cml.element.CMLMolecule;
+import org.xmlcml.cml.tools.ConnectionTableTool;
 
+import javax.inject.Inject;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
+import java.util.List;
+
+import static org.xmlcml.cml.base.CMLConstants.CML_XPATH;
 
 /**
  * @author Sam Adams
  */
 public abstract class CmlImporter extends ContentImporter {
 
+    private final ImageGenerator imageGenerator;
+    private final InChIGenerator inchiGenerator;
+
     protected LocalResource imageResource;
     protected LocalResource thumbnailResource;
 
+    private boolean createImages = true;
+
+    @Inject
+    protected CmlImporter(final ImageGenerator imageGenerator, final InChIGenerator inchiGenerator) {
+        this.imageGenerator = imageGenerator;
+        this.inchiGenerator = inchiGenerator;
+    }
+
+    public boolean isCreateImages() {
+        return createImages;
+    }
+
+    public void setCreateImages(final boolean createImages) {
+        this.createImages = createImages;
+    }
+
+    protected void attachIdentifiers(final Element element) {
+
+        final List<Node> nodes = CMLUtil.getQueryNodes(element, ".//cml:molecule", CML_XPATH);
+        if (nodes.isEmpty()) {
+            throw new IllegalArgumentException("Unable to find molecule!");
+        }
+
+        attachIdentifiers((CMLMolecule) nodes.get(0));
+    }
+
+    protected void attachIdentifiers(final CMLMolecule molecule) {
+        final ConnectionTableTool connectionTableTool = new ConnectionTableTool(molecule);
+        connectionTableTool.partitionIntoMolecules();
+
+        inchiGenerator.attachIdentifiers(molecule);
+
+        final Elements mols = molecule.getChildCMLElements("molecule");
+        for (int i = 0; i < mols.size(); i++) {
+            inchiGenerator.attachIdentifiers((CMLMolecule) mols.get(i));
+        }
+    }
+
+    protected void createImages(final LocalResource resource, final String id, final String ext) throws IOException {
+        if (createImages) {
+            final File imageFile = File.createTempFile("image", "png");
+            try {
+                final File thumbnailFile = File.createTempFile("image_tn", "png");
+                try {
+                    imageGenerator.createImages(resource, ext, imageFile, thumbnailFile);
+                    imageResource = createImageResource(imageFile, id+".png");
+                    thumbnailResource = createImageResource(thumbnailFile, id+"_tn.png");
+                } finally {
+                    FileUtils.deleteQuietly(thumbnailFile);
+                }
+            } finally {
+                FileUtils.deleteQuietly(imageFile);
+            }
+        }
+    }
+
+    protected void createImages(final CMLMolecule molecule, final String id) throws IOException {
+        if (createImages) {
+            final File imageFile = File.createTempFile("image", "png");
+            try {
+                final File thumbnailFile = File.createTempFile("image_tn", "png");
+                try {
+                    imageGenerator.createImages(molecule, imageFile, thumbnailFile);
+                    imageResource = createImageResource(imageFile, id+".png");
+                    thumbnailResource = createImageResource(thumbnailFile, id+"_tn.png");
+                } finally {
+                    FileUtils.deleteQuietly(thumbnailFile);
+                }
+            } finally {
+                FileUtils.deleteQuietly(imageFile);
+            }
+        }
+    }
+
+    protected void attachImages(final DepositRequest depositRequest) {
+        if (imageResource != null) {
+            depositRequest.setImage(imageResource);
+        }
+        if (thumbnailResource != null) {
+            depositRequest.setThumbnail(thumbnailResource);
+        }
+    }
+
+
     protected static LocalResource createCmlResource(final Element cml, final String path) throws IOException {
         final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
         final Serializer ser = new Serializer(buffer);

chemistry-importer/src/main/java/net/chempound/chemistry/ImageGenerator.java

         final File tempFile = File.createTempFile("mol", "cml");
         try {
             CmlIO.write(molecule, tempFile);
-
             createImages(tempFile, imageFile, thumbnailFile);
         } finally {
             FileUtils.deleteQuietly(tempFile);
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.