--- /dev/null
+package org.simantics.document.linking.report.evaluator;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import org.simantics.Simantics;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.NamedResource;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.document.linking.ontology.DocumentLink;\r
+import org.simantics.document.linking.report.templates.CustomizableContentProvider;\r
+import org.simantics.document.linking.report.templates.custom.CustomizableContent;\r
+import org.simantics.document.linking.report.templates.custom.EvaluatorCustomizableContent;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.objmap.graph.IMapping;\r
+import org.simantics.objmap.graph.Mappings;\r
+import org.simantics.objmap.graph.schema.IMappingSchema;\r
+import org.simantics.objmap.graph.schema.MappingSchemas;\r
+import org.simantics.objmap.graph.schema.SimpleSchema;\r
+\r
+public class DBUtil {\r
+ \r
+ /**\r
+ * Loads single evaluator tree from graph.\r
+ * @param graph\r
+ * @param res\r
+ * @return\r
+ * @throws DatabaseException\r
+ */\r
+ public static EvaluatorItem load(ReadGraph graph, Resource res) throws DatabaseException{\r
+ IMappingSchema<Resource, Object> schema = getSchema(graph); \r
+ IMapping<Resource,Object> mapping = Mappings.createWithoutListening(schema);\r
+ EvaluatorItem item = (EvaluatorItem)mapping.map(graph, res);\r
+ return item;\r
+ }\r
+ \r
+ /**\r
+ * Saves a single evaluator tree to graph.\r
+ * @param graph\r
+ * @param item\r
+ * @return\r
+ * @throws DatabaseException\r
+ */\r
+ public static Resource save(WriteGraph graph, EvaluatorItem item) throws DatabaseException{\r
+ IMappingSchema<Resource, Object> schema = getSchema(graph); \r
+ IMapping<Resource,Object> mapping = Mappings.createWithoutListening(schema);\r
+ Resource resource = mapping.inverseMap(graph, item);\r
+ return resource;\r
+ }\r
+ \r
+ /**\r
+ * Creates mapping schema for Evaluator tree.\r
+ * @param g\r
+ * @return\r
+ * @throws DatabaseException\r
+ */\r
+ public static IMappingSchema<Resource,Object> getSchema(ReadGraph g) throws DatabaseException{\r
+ try {\r
+ SimpleSchema schema = new SimpleSchema();\r
+ schema.addLinkType(MappingSchemas.fromAnnotations(g, AlignmentHint.class));\r
+ schema.addLinkType(MappingSchemas.fromAnnotations(g, And.class));\r
+ schema.addLinkType(MappingSchemas.fromAnnotations(g, Constant.class));\r
+ schema.addLinkType(MappingSchemas.fromAnnotations(g, Date.class));\r
+ schema.addLinkType(MappingSchemas.fromAnnotations(g, EvaluatorRoot.class));\r
+ schema.addLinkType(MappingSchemas.fromAnnotations(g, If.class));\r
+ schema.addLinkType(MappingSchemas.fromAnnotations(g, Lines.class));\r
+ schema.addLinkType(MappingSchemas.fromAnnotations(g, Or.class));\r
+ schema.addLinkType(MappingSchemas.fromAnnotations(g, TextSizeHint.class));\r
+ schema.addLinkType(MappingSchemas.fromAnnotations(g, Variable.class));\r
+ schema.addLinkType(MappingSchemas.fromAnnotations(g, Path.class));\r
+ \r
+ return schema;\r
+ } catch (IllegalAccessException e) {\r
+ throw new DatabaseException(e);\r
+ } catch (InstantiationException e) {\r
+ throw new DatabaseException(e);\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Saves evaluator tree configurations of content provider.\r
+ * @param graph\r
+ * @param provider\r
+ * @param template\r
+ * @throws DatabaseException\r
+ */\r
+ public static void save(WriteGraph graph, CustomizableContentProvider provider, Resource template) throws DatabaseException{\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ DocumentLink sl = DocumentLink.getInstance(graph);\r
+ \r
+ Map<String, Resource> existingEvaluatorTemplates = new HashMap<String, Resource>();\r
+ for (Resource r : graph.getObjects(template, l0.ConsistsOf)) {\r
+ if (graph.isInstanceOf(r, sl.EvaluatorTree_Template)) {\r
+ String id = graph.getRelatedValue(r, l0.HasName);\r
+ existingEvaluatorTemplates.put(id, r);\r
+ }\r
+ }\r
+ \r
+ for (String id : provider.getContentIds()) {\r
+ CustomizableContent content = provider.getContent(id);\r
+ if (content instanceof EvaluatorCustomizableContent) {\r
+ EvaluatorCustomizableContent ecc = (EvaluatorCustomizableContent)content;\r
+ Resource item = save(graph, ecc.getItem());\r
+ \r
+ Resource treeTemplate = existingEvaluatorTemplates.get(id);\r
+ if (treeTemplate == null) {\r
+ treeTemplate = graph.newResource();\r
+ graph.claim(treeTemplate, l0.InstanceOf, sl.EvaluatorTree_Template);\r
+ graph.claimLiteral(treeTemplate, l0.HasName, id);\r
+ graph.claim(template, l0.ConsistsOf, treeTemplate);\r
+ } else {\r
+ graph.deny(treeTemplate,l0.ConsistsOf);\r
+ }\r
+ graph.claim(treeTemplate, l0.ConsistsOf, item);\r
+ }\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Loads evaluator tree configurations from a graph and applies them to content provider.\r
+ * @param graph\r
+ * @param template\r
+ * @param provider\r
+ * @throws DatabaseException\r
+ */\r
+ public static void load(ReadGraph graph, Resource template, CustomizableContentProvider provider) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ DocumentLink sl = DocumentLink.getInstance(graph);\r
+ \r
+ Map<String, Resource> existingEvaluatorTemplates = new HashMap<String, Resource>();\r
+ for (Resource r : graph.getObjects(template, l0.ConsistsOf)) {\r
+ if (graph.isInstanceOf(r, sl.EvaluatorTree_Template)) {\r
+ String id = graph.getRelatedValue(r, l0.HasName);\r
+ existingEvaluatorTemplates.put(id, r);\r
+ }\r
+ }\r
+ \r
+ for (String id : provider.getContentIds()) {\r
+ Resource treeTemplate = existingEvaluatorTemplates.get(id);\r
+ if (treeTemplate == null)\r
+ continue;\r
+ \r
+ Resource itemRes = graph.getSingleObject(treeTemplate, l0.ConsistsOf);\r
+ EvaluatorItem item = load(graph, itemRes);\r
+ EvaluatorCustomizableContent ecc = new EvaluatorCustomizableContent(provider.getContent(id).getCustomizationDescription());\r
+ ecc.setItem(item);\r
+ provider.setContent(id, ecc);\r
+ \r
+ } \r
+ \r
+ }\r
+ \r
+ public static List<NamedResource> getTemplates(final Resource library) throws DatabaseException{\r
+ if (library == null)\r
+ throw new IllegalArgumentException("Library cannot be null");\r
+ List<NamedResource> templates = Simantics.getSession().syncRequest(new Read<List<NamedResource>>() {\r
+ @Override\r
+ public List<NamedResource> perform(ReadGraph graph) throws DatabaseException{\r
+ return getTemplates(graph, library);\r
+ }\r
+ });\r
+ return templates;\r
+ }\r
+ \r
+ public static List<NamedResource> getTemplates(ReadGraph graph, final Resource library) throws DatabaseException{\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ DocumentLink sl = DocumentLink.getInstance(graph);\r
+ Collection<Resource> templates = graph.syncRequest(new ObjectsWithType(library, l0.ConsistsOf, sl.ReportTemplate));\r
+ List<NamedResource> result = new ArrayList<NamedResource>(templates.size());\r
+ for (Resource template : templates) {\r
+ String name = graph.getRelatedValue(template, l0.HasName);\r
+ result.add(new NamedResource(name, template));\r
+ }\r
+\r
+ Collections.sort(result);\r
+ return result;\r
+ }\r
+ \r
+ public static void save(final Resource library, final String name,final CustomizableContentProvider provider) throws DatabaseException{\r
+ Simantics.getSession().syncRequest(new WriteRequest() {\r
+ \r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ final List<NamedResource> templates = DBUtil.getTemplates(graph,library);\r
+ Resource template = null;\r
+ for (NamedResource nr : templates) {\r
+ if (name.equals(nr.getName())) {\r
+ template = nr.getResource();\r
+ break;\r
+ }\r
+ }\r
+ if (template == null) {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ DocumentLink sl = DocumentLink.getInstance(graph);\r
+ template = graph.newResource();\r
+ graph.claim(template, l0.InstanceOf, sl.ReportTemplate);\r
+ graph.claim(library, l0.ConsistsOf, template);\r
+ graph.claimLiteral(template, l0.HasName, name);\r
+ }\r
+ DBUtil.save(graph, provider, template);\r
+ }\r
+ });\r
+ }\r
+\r
+}\r