Commits

Anonymous committed eeb41f9

Include splash page templates in layout, rather than separately generating output and embedding it

  • Participants
  • Parent commits 617b78c

Comments (0)

Files changed (6)

File chempound-webapi/src/main/java/net/chempound/webapp/output/AbstractSplashPageWriter.java

 
 import com.hp.hpl.jena.rdf.model.Model;
 import com.hp.hpl.jena.rdf.model.Resource;
-import freemarker.template.Template;
 import freemarker.template.TemplateException;
 import net.chempound.services.FreemarkerService;
 
 import java.io.IOException;
-import java.io.StringWriter;
 import java.net.URI;
-import java.util.HashMap;
 import java.util.Map;
 
 /**
 
     protected abstract String getTemplateName();
 
-    public String write(final Model model, final URI aggregationUri, final Map<String, Object> map) throws IOException, TemplateException {
+    public void write(final Model model, final URI aggregationUri, final Map<String, Object> map, final Page page) throws IOException, TemplateException {
 
-        final Map<String,Object> o = new HashMap<String, Object>(map);
+        page.setTemplatePath(getTemplateName());
 
         final Class<? extends Resource> type = getResourceType();
         final Resource item = model.getResource(aggregationUri.toString()).as(type);
-        o.put("aggregation", item);
-        o.put("_", item);
-
-        final StringWriter buffer = new StringWriter();
-
-        final Template template = freemarker.getTemplate(getTemplateName());
-        template.process(o, buffer);
-
-        return buffer.toString();
+        page.addParam("aggregation", item);
+        page.addParam("_", item);
     }
 
 }

File chempound-webapi/src/main/java/net/chempound/webapp/output/Page.java

+package net.chempound.webapp.output;
+
+import java.util.Map;
+
+public class Page {
+
+    private final Map<String, Object> model;
+
+    private String templatePath;
+
+    public Page(final Map<String, Object> model) {
+        this.model = model;
+    }
+
+    public void setTemplatePath(final String templatePath) {
+        this.templatePath = templatePath;
+    }
+
+    public void addParam(final String name, final Object value) {
+        model.put(name, value);
+    }
+
+    public String getTemplatePath() {
+        return templatePath;
+    }
+}

File chempound-webapi/src/main/java/net/chempound/webapp/output/SplashPageWriter.java

  */
 public interface SplashPageWriter {
 
-    String write(Model model, URI aggregationUri, Map<String,Object> map) throws Exception;
+    void write(Model model, URI aggregationUri, Map<String, Object> map, final Page page) throws Exception;
 
 }

File chempound-webapp/src/main/java/net/chempound/webapp/splashpage/DefaultCollectionSplashPageWriter.java

 package net.chempound.webapp.splashpage;
 
 import com.hp.hpl.jena.rdf.model.Model;
-import freemarker.template.Template;
 import freemarker.template.TemplateException;
 import net.chempound.bean.ItemInfo;
 import net.chempound.config.BaseUri;
 import net.chempound.datastore.TripleStore;
 import net.chempound.rdf.chempound.Collection;
 import net.chempound.services.FreemarkerService;
+import net.chempound.webapp.output.Page;
 import net.chempound.webapp.output.SplashPageWriter;
 
 import javax.inject.Inject;
 import java.io.IOException;
-import java.io.StringWriter;
 import java.net.URI;
 import java.util.HashMap;
 import java.util.List;
     }
 
     @Override
-    public String write(final Model model, final URI collectionUri, final Map<String, Object> map) throws IOException, TemplateException {
-        final Map<String,Object> o = new HashMap<String, Object>(map);
+    public void write(final Model model, final URI collectionUri, final Map<String, Object> map, final Page page) throws IOException, TemplateException {
 
         final List<ItemInfo> itemList = tripleStore.getItems(collectionUri);
         final Map<String,ItemInfo> itemMap = buildMap(itemList);
 
         final Collection aggregation = model.getResource(collectionUri.toString()).as(Collection.class);
-        o.put("aggregation", aggregation);
-        o.put("items", itemMap);
-        o.put("title", aggregation.getTitle());
+        page.addParam("aggregation", aggregation);
+        page.addParam("items", itemMap);
+        page.addParam("title", aggregation.getTitle());
 
-        final StringWriter buffer = new StringWriter();
-
-        final Template template = freemarker.getTemplate("default/collection.ftl");
-        template.process(o, buffer);
+        page.setTemplatePath("default/collection.ftl");
 
         final StringBuilder headers = new StringBuilder();
         if (map.containsKey("headers")) {
             headers.append(map.get("headers")).append('\n');
         }
         headers.append("<link rel='http://purl.org/net/sword/terms/deposit' href='" + getSwordUri(collectionUri) + "' />");
-        map.put("headers", headers.toString());
-
-        return buffer.toString();
+        page.addParam("headers", headers.toString());
     }
 
     private URI getSwordUri(final URI collectionUri) {

File chempound-webapp/src/main/java/net/chempound/webapp/splashpage/SplashPageGenerator.java

 import net.chempound.services.FreemarkerService;
 import net.chempound.util.MimeType;
 import net.chempound.webapp.output.ContentTypeRegistry;
+import net.chempound.webapp.output.Page;
 import net.chempound.webapp.output.SplashPageWriter;
 import org.restlet.data.MediaType;
 import org.restlet.ext.freemarker.TemplateRepresentation;
                 writer = getDefaultItemWriter();
             }
         }
+
         final Map<String,Object> map = new HashMap<String, Object>();
+        final Page page = new Page(map);
         map.put("uri", aggregationUri);
 
-        final String content = writer.write(model, aggregationUri, map);
-        map.put("content", content);
+        writer.write(model, aggregationUri, map, page);
+
         map.put("uri", aggregationUri);
+        map.put("template", page.getTemplatePath());
+
         final Template template = freemarker.getTemplate("splash.ftl");
         return new TemplateRepresentation(template, map, MediaType.TEXT_HTML);
     }

File chempound-webapp/src/main/resources/net/chempound/templates/splash.ftl

     title=title!uri
 >
 
-${content}
+<#include template>
 
 </@layout.basic>