Commits

Sam Adams committed 912368d

Generate search forms dynamically

Comments (0)

Files changed (2)

chemistry-search-structure/src/main/java/net/chempound/webapp/search/chemistry/StructureSearchProvider.java

 import net.chempound.chemistry.inchi.InchiTool;
 import net.chempound.rdf.cml.CmlRdf;
 import net.chempound.webapp.search.AbstractSearchProvider;
+import net.chempound.webapp.search.SearchField;
 import net.chempound.webapp.search.SparqlQueryBuilder;
 import nu.xom.Document;
-import org.apache.commons.io.IOUtils;
 import org.apache.http.HttpResponse;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpGet;
 import org.xmlcml.cml.element.CMLMolecule;
 import org.xmlcml.cml.tools.SMILESTool;
 
-import java.io.IOException;
-import java.io.InputStream;
 import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 import java.util.Map;
 
+import static net.chempound.webapp.search.FieldType.TEXT;
+
 /**
  * @author Sam Adams
  */
 public class StructureSearchProvider extends AbstractSearchProvider {
 
+    private static final String SYSTEMATIC_NAME = "sys_name";
+    private static final String SMILES = "smiles";
+    private static final String INCHI = "inchi";
+    private static final String INCHI_KEY = "inchi_key";
+
+    private final List<SearchField> fieldList;
+
+    {
+        final List<SearchField> fields = new ArrayList<SearchField>();
+        fields.add(new SearchField(SYSTEMATIC_NAME, TEXT, "Systematic name:"));
+        fields.add(new SearchField(SMILES, TEXT, "SMILES:"));
+        fields.add(new SearchField(INCHI, TEXT, "InChI:"));
+        fields.add(new SearchField(INCHI_KEY, TEXT, "InChIKey:"));
+        fieldList = Collections.unmodifiableList(fields);
+    }
+
     @Override
     public String getId() {
         return "structure";
     }
 
     @Override
-    public String getSearchForm() {
-        final InputStream in = getClass().getResourceAsStream("search-structure.html");
-        try {
-            return IOUtils.toString(in, "UTF-8");
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        } finally {
-            IOUtils.closeQuietly(in);
-        }
+    public List<SearchField> getFields() {
+        return fieldList;
     }
 
     @Override
     }
 
     private void processSmiles(final Map<String, String> form, final StructureSparqlQuery queryString) {
-        final String value = form.get("smiles");
+        final String value = form.get(SMILES);
         if (value != null && value.length() > 0) {
             final CMLMolecule molecule = SMILESTool.createMolecule(value);
             final String inchi = InchiTool.generateInchi(molecule, "");
     }
 
     private void processName(final Map<String, String> form, final StructureSparqlQuery queryString) {
-        final String value = form.get("sys_name");
+        final String value = form.get(SYSTEMATIC_NAME);
         try {
             if (value != null && value.length() > 0) {
                 final HttpClient client = new DefaultHttpClient();
                 final String name = URLEncoder.encode(value, "UTF-8");
-                final HttpGet get = new HttpGet("http://opsin.ch.cam.ac.uk/opsin/"+name);
+                final HttpGet get = new HttpGet("http://opsin.ch.cam.ac.uk/opsin/" + name);
                 final HttpResponse response = client.execute(get);
                 final CMLBuilder builder = new CMLBuilder();
                 final Document doc = builder.build(response.getEntity().getContent());
         }
     }
 
-    private void processInchi(final Map<String,String> form, final StructureSparqlQuery queryString) {
-        final String value = form.get("inchi");
+    private void processInchi(final Map<String, String> form, final StructureSparqlQuery queryString) {
+        final String value = form.get(INCHI);
         if (value != null && value.length() > 0) {
             queryString.addInchi(value);
         }
     }
 
-    private void processInchiKey(final Map<String,String> form, final StructureSparqlQuery queryString) {
-        final String value = form.get("inchi_key");
+    private void processInchiKey(final Map<String, String> form, final StructureSparqlQuery queryString) {
+        final String value = form.get(INCHI_KEY);
         if (value != null && value.length() > 0) {
             queryString.addInchiKey(value);
         }
     static class StructureSparqlQuery extends SparqlQueryBuilder {
 
         public void addInchiKey(final String value) {
-            statements.add("  ?item <"+ CmlRdf.inchiKey +"> ?id"+i+ " .\n");
-            statements.add("  ?id"+i+" <"+ RDF.value +"> ?ik"+i+ " .\n");
-            statements.add("  FILTER regex(?ik"+i+", \"^"+ value +"\")\n");
+            statements.add("  ?item <" + CmlRdf.inchiKey + "> ?id" + i + " .\n");
+            statements.add("  ?id" + i + " <" + RDF.value + "> ?ik" + i + " .\n");
+            statements.add("  FILTER regex(?ik" + i + ", \"^" + value + "\")\n");
             i++;
         }
 
         public void addInchi(final String value) {
-            statements.add("  ?item <"+CmlRdf.inchi+"> [<"+RDFS.label+"> ?val"+i+ "] .\n");
+            statements.add("  ?item <" + CmlRdf.inchi + "> [<" + RDFS.label + "> ?val" + i + "] .\n");
             statements.add("  FILTER (?val" + i + " = ?q" + i + ") \n");
             queryBindings.put("q+i", ResourceFactory.createPlainLiteral(value));
             i++;
         }
 
-
     }
 
 }

chemistry-search-structure/src/main/resources/net/chempound/webapp/search/chemistry/search-structure.html

-
-<div class="search search-structure">
-    <p>
-        <label>Systematic name:</label>
-        <input name="sys_name" type="text" />
-    </p>
-    <p>
-        <label>SMILES:</label>
-        <input name="smiles" type="text" />
-    </p>
-    <p>
-        <label>InChI:</label>
-        <input name="inchi" type="text" />
-    </p>
-    <p>
-        <label>InChIKey:</label>
-        <input name="inchi_key" type="text" />
-    </p>
-</div>