diff --git a/thingifier/src/main/java/uk/co/compendiumdev/thingifier/application/DefaultGUI.java b/thingifier/src/main/java/uk/co/compendiumdev/thingifier/application/DefaultGUI.java index 54a10c73..260dc5d3 100644 --- a/thingifier/src/main/java/uk/co/compendiumdev/thingifier/application/DefaultGUI.java +++ b/thingifier/src/main/java/uk/co/compendiumdev/thingifier/application/DefaultGUI.java @@ -1,12 +1,21 @@ package uk.co.compendiumdev.thingifier.application; +import com.google.gson.GsonBuilder; +import uk.co.compendiumdev.thingifier.Thing; import uk.co.compendiumdev.thingifier.Thingifier; +import uk.co.compendiumdev.thingifier.generic.definitions.RelationshipVector; +import uk.co.compendiumdev.thingifier.generic.definitions.ThingDefinition; import uk.co.compendiumdev.thingifier.generic.instances.ThingInstance; +import uk.co.compendiumdev.thingifier.reporting.JsonThing; + +import java.util.Collection; import static spark.Spark.get; public class DefaultGUI { + // TODO: templates or tidier way to create the default GUI pages with styling + private final Thingifier thingifier; public DefaultGUI(final Thingifier thingifier) { @@ -26,11 +35,7 @@ public void setupDefaultGUI(){ response.status(200); StringBuilder html = new StringBuilder(); html.append("GUI"); - html.append(""); + html.append(getInstancesRootMenuHtml()); html.append(""); return html.toString(); }); @@ -42,25 +47,20 @@ public void setupDefaultGUI(){ String entityName = request.queryParams("entity"); - html.append("GUI"); - html.append(""); - html.append(""); - html.append(""); - for(String field : thingifier.getThingNamed(entityName).definition().getFieldNames()) { - if (!field.equals("guid")) { - html.append(String.format("", field)); - } - } - html.append(""); - for(ThingInstance instance : thingifier.getThingNamed(entityName).getInstances()){ - html.append(""); - html.append(String.format("",entityName,instance.getGUID())); - for(String field : thingifier.getThingNamed(entityName).definition().getFieldNames()) { - if (!field.equals("guid")) { - html.append(String.format("", instance.getValue(field))); - } - } - html.append(""); + html.append("GUI"); + html.append(getInstancesRootMenuHtml()); + html.append(getHTMLTableStylingCSS()); + + + final Thing thing = thingifier.getThingNamed(entityName); + final ThingDefinition definition = thing.definition(); + + html.append("

" + definition.getPlural() + "

"); + + html.append(startHtmlTableFor(definition)); + + for(ThingInstance instance : thing.getInstances()){ + html.append(htmlTableRowFor(instance)); } html.append("
guid%s
%2$s%s
"); html.append(""); @@ -75,20 +75,115 @@ public void setupDefaultGUI(){ String entityName = request.queryParams("entity"); String guid = request.queryParams("guid"); + final Thing thing = thingifier.getThingNamed(entityName); + final ThingDefinition definition = thing.definition(); + ThingInstance instance = thing.findInstanceByGUID(guid); + html.append("GUI"); + html.append(getInstancesRootMenuHtml()); + + html.append("

" + definition.getName() + "

"); + html.append(""); + + if(instance.hasAnyRelationshipInstances()) { + + html.append("

Relationships

"); + html.append(getHTMLTableStylingCSS()); + + for (RelationshipVector relationship : definition.getRelationships()) { + final Collection relatedItems = instance.connectedItems(relationship.getName()); + html.append("

" + relationship.getName() + "

"); + if(relatedItems.size() > 0) { + boolean header=true; + + for (ThingInstance relatedInstance : relatedItems) { + if(header){ + html.append(startHtmlTableFor(relatedInstance.getEntity())); + header=false; + } + html.append(htmlTableRowFor(relatedInstance)); + } + html.append(""); + + }else{ + html.append(""); + } + } + } + + html.append("

JSON Example

"); + html.append("
");
+            // pretty print the json
+            html.append(new GsonBuilder().setPrettyPrinting().create().toJson(JsonThing.asJsonObject(instance)));
+            html.append("
"); html.append(""); return html.toString(); }); } + + private String getInstancesRootMenuHtml() { + StringBuilder html = new StringBuilder(); + html.append("

Menu:

"); + html.append(""); + html.append("
"); + return html.toString(); + } + + private String getHTMLTableStylingCSS() { + StringBuilder html = new StringBuilder(); + html.append(""); + return html.toString(); + } + + private String htmlTableRowFor(final ThingInstance instance) { + StringBuilder html = new StringBuilder(); + final ThingDefinition definition = instance.getEntity(); + + html.append(""); + html.append(String.format("%2$s", + definition.getName(),instance.getGUID())); + + for(String field : definition.getFieldNames()) { + if (!field.equals("guid")) { + html.append(String.format("%s", instance.getValue(field))); + } + } + html.append(""); + + return html.toString(); + } + + private String startHtmlTableFor(final ThingDefinition definition) { + StringBuilder html = new StringBuilder(); + + html.append(""); + html.append(""); + html.append(""); + for(String field : definition.getFieldNames()) { + if (!field.equals("guid")) { + html.append(String.format("", field)); + } + } + html.append(""); + + return html.toString(); + } }
guid%s