--- /dev/null
+package org.simantics.document.linking.report.evaluator;\r
+\r
+import java.util.Collection;\r
+\r
+import org.simantics.annotation.ontology.AnnotationResource;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+/**\r
+ * Copy-paste from org.simantics.diagram.function.PredefinedVariables (removed diagram specific stuff)\r
+ * \r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ *\r
+ */\r
+public class PredefinedVariables {\r
+ public static final String root = "root";\r
+ public static final String project = "project";\r
+ public static final String model = "model";\r
+ public static final String template = "template";\r
+ public static final String current = ".";\r
+\r
+\r
+ private static PredefinedVariables factory = new PredefinedVariables();\r
+ \r
+ public static PredefinedVariables getInstance(){\r
+ return factory;\r
+ }\r
+ \r
+ public static void setFactory(PredefinedVariables factory){\r
+ PredefinedVariables.factory = factory;\r
+ }\r
+ \r
+ Resource connectedComponent = null;\r
+ \r
+\r
+\r
+\r
+\r
+ public static Resource getAnnotation(ReadGraph graph, Resource res) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ Collection<Resource> children = graph.getObjects(res, L0.ConsistsOf);\r
+ Resource anno = null;\r
+ AnnotationResource ANNO = AnnotationResource.getInstance(graph);\r
+ for (Resource child:children){\r
+ if (graph.isInstanceOf(child, ANNO.Annotation)){\r
+ anno = child;\r
+ break;\r
+ }\r
+ }\r
+ return anno;\r
+ } \r
+\r
+\r
+\r
+ public Resource getPredefinedResource(ReadGraph graph, Variable selection, String id) throws DatabaseException {\r
+ Resource predefined = null;\r
+ if (id.equals(root))\r
+ predefined = graph.getRootLibrary();\r
+ else if (id.equals(project))\r
+ predefined = SimanticsUI.getProject().get();\r
+ else if (id.equals(model))\r
+ predefined = Variables.getModel(graph, selection);\r
+ else if (id.equals(template)){\r
+// Resource composite = selection.getRepresents(graph);\r
+// if (composite == null)\r
+// return null;\r
+// ModelingResources MOD = ModelingResources.getInstance(graph);\r
+// Template2dResource TEPLATE2D = Template2dResource.getInstance(graph);\r
+// Resource diagram = graph.getPossibleObject(composite, MOD.CompositeToDiagram);\r
+// if (diagram == null)\r
+// return null;\r
+// predefined = graph.getPossibleObject(diagram, TEPLATE2D.HasDrawingTemplate);\r
+ }\r
+ else if (id.equals(current))\r
+ predefined = selection.getRepresents(graph);\r
+ return predefined;\r
+ }\r
+ \r
+ public Variable getPredefinedVariable(ReadGraph graph, Variable selection, String id) throws DatabaseException {\r
+ Resource predefined = this.getPredefinedResource(graph, selection, id);\r
+ if (predefined == null)\r
+ return null;\r
+ \r
+ Variable v = selection;\r
+ //Variable test = Variables.getPossibleVariable(graph, "http://Projects/Development%20Project/Model/Configuration/NewAutomationDiagram2/__CONTAINER__/__DIAGRAM__/4");\r
+ if (predefined != null)\r
+ v = graph.adapt(predefined, Variable.class);\r
+ return v;\r
+ }\r
+ \r
+ public Variable getVariable(ReadGraph graph, String path, Resource converter, Variable selection) throws DatabaseException {\r
+ if (path == null)\r
+ return null;\r
+ int colonInx = path.indexOf(':');\r
+ int firstSlash = path.indexOf('/');\r
+ int firstHash = path.indexOf('#');\r
+ int firstFlashInx = firstSlash;\r
+ if(firstFlashInx == -1) firstFlashInx = firstHash;\r
+ else {\r
+ if(firstHash > -1 && firstHash < firstFlashInx) firstFlashInx = firstHash;\r
+ }\r
+\r
+ String predefined = null;\r
+ String relativePath = null;\r
+\r
+ // if scheme is followed by absolute uri\r
+ // for examples:\r
+ // pre:/model/Library/logo.svg#data -- get under model\r
+ // pre:/./Library/logo.svg#data-- get under composite which corresponds the diagram\r
+ // pre:/project/Library/FORTUM_LOGO_CMYK.svg#data -- get under project\r
+ // pre:/root/Library/FORTUM_LOGO_CMYK.svg#data -- get under root\r
+ \r
+ if (colonInx != -1 && firstFlashInx != -1 && colonInx+1 == firstFlashInx && path.length() > firstFlashInx+1){\r
+ String scheme = path.substring(0, colonInx);\r
+ String absPath = path.substring(firstFlashInx+1);\r
+ if (scheme.equals("pre")){\r
+ int endOfPredefined1 = absPath.indexOf('/');\r
+ int endOfPredefined2 = absPath.indexOf('#');\r
+ if (endOfPredefined1 == -1 && endOfPredefined2 == -1)\r
+ predefined = absPath;\r
+ if (endOfPredefined1 == -1 && endOfPredefined2 != -1)\r
+ predefined = absPath.substring(0, endOfPredefined2);\r
+ if (endOfPredefined1 != -1 && endOfPredefined2 == -1)\r
+ predefined = absPath.substring(0, endOfPredefined1);\r
+ if (endOfPredefined1 != -1 && endOfPredefined2 != -1){\r
+ if (endOfPredefined2 < endOfPredefined1)\r
+ endOfPredefined1 = endOfPredefined2;\r
+ predefined = absPath.substring(0, endOfPredefined1);\r
+ }\r
+ relativePath = absPath.substring(predefined.length());\r
+ }\r
+ }\r
+\r
+ //Variable activeVariable = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);\r
+ if (selection == null)\r
+ return null;\r
+ \r
+ Variable v = selection;\r
+ if (predefined != null)\r
+ v = getPredefinedVariable(graph, selection, predefined);\r
+\r
+ if (v == null)\r
+ return null;\r
+\r
+ Variable property = null;\r
+ if (relativePath != null){\r
+ if (relativePath.startsWith("/."))\r
+ relativePath = relativePath.substring(1);\r
+ property = v.browsePossible(graph, relativePath);\r
+ }\r
+ else\r
+ property = v.browsePossible(graph, path);\r
+ return property;\r
+ }\r
+\r
+}\r