Commits

Sam Adams committed 257c907

Generate search forms dynamically

Comments (0)

Files changed (3)

crystallography-handler/src/main/java/net/chempound/crystal/search/CrystalSearchProvider.java

 import net.chempound.crystal.rdf.ont.CifDict;
 import net.chempound.crystal.rdf.ont.Cryst;
 import net.chempound.webapp.search.AbstractSearchProvider;
+import net.chempound.webapp.search.SearchField;
 import net.chempound.webapp.search.SparqlQueryBuilder;
-import org.apache.commons.io.IOUtils;
 
-import java.io.IOException;
-import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 import java.util.Map;
 
+import static net.chempound.webapp.search.FieldType.NUMBER;
+import static net.chempound.webapp.search.FieldType.TEXT;
+
 /**
  * @author Sam Adams
  */
 public class CrystalSearchProvider extends AbstractSearchProvider {
 
+    private static final String CRYSTAL_SYSTEM = "crystal_system";
+    private static final String CELL_LENGTH_A = "cell_length_a";
+    private static final String CELL_LENGTH_B = "cell_length_b";
+    private static final String CELL_LENGTH_C = "cell_length_c";
+    private static final String CELL_ANGLE_ALPHA = "cell_angle_alpha";
+    private static final String CELL_ANGLE_BETA = "cell_angle_beta";
+    private static final String CELL_ANGLE_GAMMA = "cell_angle_gamma";
+    private static final String FORMULA_WEIGHT = "formula_weight";
+    private static final String INCHI_KEY = "inchi_key";
+
+    private static final String OP = "_OP";
+
+    private final List<SearchField> fieldList;
+
+    {
+        final List<SearchField> fields = new ArrayList<SearchField>();
+        fields.add(new SearchField(CRYSTAL_SYSTEM, TEXT, "Crystal system:"));
+        fields.add(new SearchField(CELL_LENGTH_A, NUMBER, "Cell length a:"));
+        fields.add(new SearchField(CELL_LENGTH_B, NUMBER, "Cell length b:"));
+        fields.add(new SearchField(CELL_LENGTH_C, NUMBER, "Cell length c:"));
+        fields.add(new SearchField(CELL_ANGLE_ALPHA, NUMBER, "Cell angle alpha:"));
+        fields.add(new SearchField(CELL_ANGLE_BETA, NUMBER, "Cell angle beta:"));
+        fields.add(new SearchField(CELL_ANGLE_GAMMA, NUMBER, "Cell angle gamma:"));
+        fields.add(new SearchField(FORMULA_WEIGHT, NUMBER, "Formula weight:"));
+        fields.add(new SearchField(INCHI_KEY, TEXT, "InChIKey:"));
+        fieldList = Collections.unmodifiableList(fields);
+    }
+
     @Override
     public String getId() {
         return "crystal";
     }
 
     @Override
-    public String getSearchForm() {
-        final InputStream in = getClass().getResourceAsStream("search-crystal.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;
     }
 
-    public SparqlQueryBuilder createSparqlQuery(final Map<String,String> form) {
+    public SparqlQueryBuilder createSparqlQuery(final Map<String, String> form) {
 
         final CrystalSparqlBuilder queryString = new CrystalSparqlBuilder();
 
-        processUnitCell(form, "cell_length_a", "cell_length_a_type", Cryst.cellLengthA, queryString);
-        processUnitCell(form, "cell_length_b", "cell_length_b_type", Cryst.cellLengthB, queryString);
-        processUnitCell(form, "cell_length_c", "cell_length_c_type", Cryst.cellLengthC, queryString);
+        processUnitCell(form, CELL_LENGTH_A, CELL_LENGTH_A + OP, Cryst.cellLengthA, queryString);
+        processUnitCell(form, CELL_LENGTH_B, CELL_LENGTH_B + OP, Cryst.cellLengthB, queryString);
+        processUnitCell(form, CELL_LENGTH_C, CELL_LENGTH_C + OP, Cryst.cellLengthC, queryString);
 
-        processUnitCell(form, "cell_angle_alpha", "cell_angle_alpha_type", Cryst.cellAngleAlpha, queryString);
-        processUnitCell(form, "cell_angle_beta", "cell_angle_beta_type", Cryst.cellAngleBeta, queryString);
-        processUnitCell(form, "cell_angle_gamma", "cell_angle_gamma_type", Cryst.cellAngleGamma, queryString);
+        processUnitCell(form, CELL_ANGLE_ALPHA, CELL_ANGLE_ALPHA + OP, Cryst.cellAngleAlpha, queryString);
+        processUnitCell(form, CELL_ANGLE_BETA, CELL_ANGLE_BETA + OP, Cryst.cellAngleBeta, queryString);
+        processUnitCell(form, CELL_ANGLE_GAMMA, CELL_ANGLE_GAMMA + OP, Cryst.cellAngleGamma, queryString);
 
         processFormulaWeight(form, queryString);
 
     }
 
     private void processCrystalSystem(final Map<String, String> form, final CrystalSparqlBuilder queryString) {
-        final String value = form.get("crystal_system");
+        final String value = form.get(CRYSTAL_SYSTEM);
         if (value != null && value.length() > 0) {
             queryString.addProperty(CifDict.spaceGroupCrystalSystem, value);
         }
     }
 
-    private void processUnitCell(final Map<String,String> form, final String field, final String type, final Property prop, final CrystalSparqlBuilder queryString) {
+    private void processUnitCell(final Map<String, String> form, final String field, final String type, final Property prop, final CrystalSparqlBuilder queryString) {
         final String value = form.get(field);
         if (value != null && value.length() > 0) {
             final String op = form.get(type);
         }
     }
 
-    private void processFormulaWeight(final Map<String,String> form, final CrystalSparqlBuilder queryString) {
-        final String value = form.get("formula_weight");
+    private void processFormulaWeight(final Map<String, String> form, final CrystalSparqlBuilder queryString) {
+        final String value = form.get(FORMULA_WEIGHT);
         if (value != null && value.length() > 0) {
-            final String op = form.get("formula_weight_type");
+            final String op = form.get(FORMULA_WEIGHT + OP);
             queryString.addFormulaWeight(value, op);
         }
     }
 
-    private void processInchiKey(final Map<String,String> form, final CrystalSparqlBuilder queryString) {
-        final String value = form.get("inchi_key");
+    private void processInchiKey(final Map<String, String> form, final CrystalSparqlBuilder queryString) {
+        final String value = form.get(INCHI_KEY);
         if (value != null && value.length() > 0) {
             queryString.addInchiKey(value);
         }

crystallography-handler/src/main/resources/net/chempound/crystal/search/search-crystal.html

-<div class="search search-crystal">
-    <p>
-        <label>crystal system:</label>
-        <input name="crystal_system" type="text" />
-    </p>
-    <p>
-        <label>cell length a:</label>
-        <input name="cell_length_a" type="text" />
-        <select name="cell_length_a_type">
-            <option>gt</option>
-            <option>lt</option>
-            <option>eq</option>
-        </select>
-    </p>
-    <p>
-        <label>cell length b:</label>
-        <input name="cell_length_b" type="text" />
-        <select name="cell_length_b_type">
-            <option>gt</option>
-            <option>lt</option>
-            <option>eq</option>
-        </select>
-    </p>
-    <p>
-        <label>cell length c:</label>
-        <input name="cell_length_c" type="text" />
-        <select name="cell_length_c_type">
-            <option>gt</option>
-            <option>lt</option>
-            <option>eq</option>
-        </select>
-    </p>
-    <p>
-        <label>cell angle alpha:</label>
-        <input name="cell_angle_alpha" type="text" />
-        <select name="cell_angle_alpha_type">
-            <option>gt</option>
-            <option>lt</option>
-            <option>eq</option>
-        </select>
-    </p>
-    <p>
-        <label>cell angle beta:</label>
-        <input name="cell_angle_beta" type="text" />
-        <select name="cell_angle_beta_type">
-            <option>gt</option>
-            <option>lt</option>
-            <option>eq</option>
-        </select>
-    </p>
-    <p>
-        <label>cell angle gamma:</label>
-        <input name="cell_angle_gamma" type="text" />
-        <select name="cell_angle_gamma_type">
-            <option>gt</option>
-            <option>lt</option>
-            <option>eq</option>
-        </select>
-    </p>
-    <p>
-        <label>formula weight:</label>
-        <input name="formula_weight" type="text" />
-        <select name="formula_weight_type">
-            <option>gt</option>
-            <option>lt</option>
-            <option>eq</option>
-        </select>
-    </p>
-    <p>
-        <label>InChIKey:</label>
-        <input name="inchi_key" type="text" />
-    </p>
-</div>

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

         Form form = new Form();
         form.add("search_provider", "crystal");
         form.add("cell_length_a", "10");
-        form.add("cell_length_a_type", "gt");
+        form.add("cell_length_a_op", "gt");
         Request request = new Request(Method.POST, "http://localhost:8715/search/", form.getWebRepresentation());
         Response response = client.handle(request);
         try {
         Form form = new Form();
         form.add("search_provider", "crystal");
         form.add("cell_length_a", "10");
-        form.add("cell_length_a_type", "lt");
+        form.add("cell_length_a_op", "lt");
         Request request = new Request(Method.POST, "http://localhost:8715/search/", form.getWebRepresentation());
         Response response = client.handle(request);
         try {
         Form form = new Form();
         form.add("search_provider", "crystal");
         form.add("cell_length_b", "10");
-        form.add("cell_length_b_type", "gt");
+        form.add("cell_length_b_op", "gt");
         Request request = new Request(Method.POST, "http://localhost:8715/search/", form.getWebRepresentation());
         Response response = client.handle(request);
         try {
         Form form = new Form();
         form.add("search_provider", "crystal");
         form.add("cell_length_b", "10");
-        form.add("cell_length_b_type", "lt");
+        form.add("cell_length_b_op", "lt");
         Request request = new Request(Method.POST, "http://localhost:8715/search/", form.getWebRepresentation());
         Response response = client.handle(request);
         try {
         Form form = new Form();
         form.add("search_provider", "crystal");
         form.add("cell_length_c", "20");
-        form.add("cell_length_c_type", "gt");
+        form.add("cell_length_c_op", "gt");
         Request request = new Request(Method.POST, "http://localhost:8715/search/", form.getWebRepresentation());
         Response response = client.handle(request);
         try {
         Form form = new Form();
         form.add("search_provider", "crystal");
         form.add("cell_length_c", "20");
-        form.add("cell_length_c_type", "lt");
+        form.add("cell_length_c_op", "lt");
         Request request = new Request(Method.POST, "http://localhost:8715/search/", form.getWebRepresentation());
         Response response = client.handle(request);
         try {
         Form form = new Form();
         form.add("search_provider", "crystal");
         form.add("formula_weight", "300");
-        form.add("formula_weight_type", "gt");
+        form.add("formula_weight_op", "gt");
         Request request = new Request(Method.POST, "http://localhost:8715/search/", form.getWebRepresentation());
         Response response = client.handle(request);
         try {
         Form form = new Form();
         form.add("search_provider", "crystal");
         form.add("formula_weight", "300");
-        form.add("formula_weight_type", "lt");
+        form.add("formula_weight_op", "lt");
         Request request = new Request(Method.POST, "http://localhost:8715/search/", form.getWebRepresentation());
         Response response = client.handle(request);
         try {