Commits

Sam Adams committed f826acc

Started migration to guice-based architecture

Comments (0)

Files changed (4)

crystallography-handler/pom.xml

             <artifactId>chempound-rdf-vocabs</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>com.google.inject</groupId>
+            <artifactId>guice</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.google.inject.extensions</groupId>
+            <artifactId>guice-multibindings</artifactId>
+        </dependency>
+
     </dependencies>
 
 </project>

crystallography-handler/src/main/java/uk/ac/cam/ch/wwmm/chempound/crystal/CrystallographyModule.java

+package uk.ac.cam.ch.wwmm.chempound.crystal;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.multibindings.MapBinder;
+import com.google.inject.multibindings.Multibinder;
+import uk.ac.cam.ch.wwmm.chempound.crystal.display.CrystalContentHandler;
+import uk.ac.cam.ch.wwmm.chempound.crystal.search.CrystalSearchProvider;
+import uk.ac.cam.ch.wwmm.chempound.webapp.output.ContentHandler;
+import uk.ac.cam.ch.wwmm.chempound.webapp.search.SearchProvider;
+
+/**
+ * @author sea36
+ */
+public class CrystallographyModule extends AbstractModule {
+
+    @Override
+    protected void configure() {
+        Multibinder<ContentHandler> contentBinder = Multibinder.newSetBinder(binder(), ContentHandler.class);
+        contentBinder.addBinding().to(CrystalContentHandler.class);
+
+        MapBinder<String,SearchProvider> searchBinder = MapBinder.newMapBinder(binder(), String.class, SearchProvider.class);
+        searchBinder.addBinding("crystal").to(CrystalSearchProvider.class);
+    }
+
+}

crystallography-handler/src/main/java/uk/ac/cam/ch/wwmm/chempound/crystal/display/CrystalContentHandler.java

 package uk.ac.cam.ch.wwmm.chempound.crystal.display;
 
+import com.google.inject.Injector;
 import com.hp.hpl.jena.rdf.model.Model;
 import uk.ac.cam.ch.wwmm.chempound.webapp.output.ContentHandler;
 import uk.ac.cam.ch.wwmm.chempound.webapp.output.SplashWriter;
 
+import javax.inject.Inject;
 import java.net.URI;
 
 /**
  */
 public class CrystalContentHandler extends ContentHandler {
 
-    public CrystalContentHandler() {
+    private Injector injector;
+
+    @Inject
+    public CrystalContentHandler(Injector injector) {
         super(URI.create("http://www.xmlcml.org/cryst-rdf-schema#CrystallographicExperiment"));
+        this.injector = injector;
     }
 
-
-
     @Override
     public SplashWriter getWriter(Model model, URI aggregationUri) {
-        return new CrystalWriter(model, aggregationUri, getApplication());
+        return injector.getInstance(CrystalWriter.class);
     }
 
 }

crystallography-handler/src/main/java/uk/ac/cam/ch/wwmm/chempound/crystal/display/CrystalWriter.java

 import uk.ac.cam.ch.wwmm.chempound.crystal.rdf.Cryst;
 import uk.ac.cam.ch.wwmm.chempound.rdf.DCTerms;
 import uk.ac.cam.ch.wwmm.chempound.webapp.ChempoundWebApp;
+import uk.ac.cam.ch.wwmm.chempound.webapp.FreemarkerService;
 import uk.ac.cam.ch.wwmm.chempound.webapp.output.SplashWriter;
 
+import javax.inject.Inject;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringWriter;
         }
     }
 
-    private Model model;
-    private URI aggregationUri;
-    private ChempoundWebApp app;
+    private FreemarkerService freemarker;
 
-
-
-    public CrystalWriter(Model model, URI aggregationUri, ChempoundWebApp app) {
-        this.model = model;
-        this.aggregationUri = aggregationUri;
-        this.app = app;
+    @Inject
+    public CrystalWriter(FreemarkerService freemarker) {
+        this.freemarker = freemarker;
     }
 
     @Override
-    public String write(Map<String, Object> map) throws IOException, TemplateException {
+    public String write(Model model, URI aggregationUri, Map<String, Object> map) throws IOException, TemplateException {
 
         map.put("style", style);
         map.put("headers", headers);
 
         Map<String,Object> o = new HashMap<String, Object>(map);
 
-        Resource r = this.model.getResource(aggregationUri.toString());
+        Resource r = model.getResource(aggregationUri.toString());
 
         CrystalStructure cryst = new CrystalStructure(r);
         CrystalEyeEntry entry = new CrystalEyeEntry(r);
 
         StringWriter buffer = new StringWriter();
 
-        Template template = app.getTemplate("crystal/crystal.ftl");
+        Template template = freemarker.getTemplate("crystal/crystal.ftl");
         template.process(o, buffer);
 
         return buffer.toString();