--- /dev/null
+package org.simantics.annotation.ui;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.util.Map;\r
+\r
+import org.eclipse.core.runtime.NullProgressMonitor;\r
+import org.simantics.Simantics;\r
+import org.simantics.annotation.ui.wizard.AnnotationTypeExporter;\r
+import org.simantics.annotation.ui.wizard.AnnotationTypeImportWizard;\r
+import org.simantics.databoard.serialization.SerializationException;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.Statement;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.request.PropertyInfo;\r
+import org.simantics.db.layer0.request.UnescapedPropertyMapOfResource;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.userComponent.ComponentTypeCommands;\r
+import org.simantics.scl.runtime.tuple.Tuple2;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+public class SCLAnnotation {\r
+ \r
+\r
+ public static Resource newAnnotationType(WriteGraph graph, Resource parent) throws DatabaseException {\r
+ \r
+ Pair<Resource, Resource> annotation = AnnotationUtils.newAnnotationType(graph, parent);\r
+ Resource type = annotation.second;\r
+ \r
+ return type;\r
+ }\r
+ \r
+ public static Tuple2 newAnnotationType1(WriteGraph graph, Resource parent) throws DatabaseException {\r
+ \r
+ Pair<Resource, Resource> annotation = AnnotationUtils.newAnnotationType(graph, parent);\r
+ Tuple2 tuple = new Tuple2(annotation.first, annotation.second);\r
+ \r
+ return tuple;\r
+ }\r
+ \r
+ public static Resource newAnnotationValue(WriteGraph graph, Resource parent, Resource property) throws DatabaseException {\r
+ graph.markUndoPoint();\r
+ Resource annotationValue = AnnotationUtils.newAnnotationInstance(graph, parent, property);\r
+ return annotationValue;\r
+ }\r
+\r
+ public static Resource newAnnotationValueWithName(WriteGraph graph, Resource parent, String name, Resource property) throws DatabaseException {\r
+ Resource existing = Layer0Utils.getPossibleChild(graph, parent, name);\r
+ if(existing != null) return existing;\r
+ Resource annotationValue = AnnotationUtils.newAnnotationInstance(graph, parent, name, property);\r
+ return annotationValue;\r
+ }\r
+\r
+ public static Resource newAnnotationProperty(WriteGraph graph, Resource annotationType) throws DatabaseException {\r
+ \r
+ Resource annotationProperty = ComponentTypeCommands.createPropertyWithDefaults(graph, annotationType);\r
+ \r
+ return annotationProperty;\r
+ }\r
+ \r
+ public static Resource advancedAnnotationProperty(WriteGraph graph, Resource annotationType, String name, String value, String unit, String range, String label, String description) throws DatabaseException {\r
+ \r
+ Resource property = ComponentTypeCommands.createProperty(graph, annotationType, name, value, unit, range, label, description);\r
+ \r
+ return property;\r
+ }\r
+ \r
+ public static void removeAnnotationProperty(WriteGraph graph, Resource property) throws DatabaseException {\r
+ \r
+ Resource annotationType = getAnnotationType(graph, property);\r
+ ComponentTypeCommands.removeProperty(graph, annotationType, property);\r
+ }\r
+ \r
+ public static void setPropertyType(WriteGraph graph, Resource property, String type) throws DatabaseException {\r
+ \r
+ Resource annotationType = getAnnotationType(graph, property);\r
+ ComponentTypeCommands.setRequiredType(graph, property, type);\r
+ ComponentTypeCommands.convertDefaultValue(graph, annotationType, property, type);\r
+ \r
+ }\r
+ \r
+ public static void setPropertyUnit(WriteGraph graph, Resource property, String unit) throws DatabaseException {\r
+ \r
+ Resource annotationType = getAnnotationType(graph, property);\r
+ ComponentTypeCommands.setUnit(graph, property, annotationType, unit);\r
+ \r
+ }\r
+ \r
+ public static void setPropertyLabel(WriteGraph graph, Resource property, String label) throws DatabaseException {\r
+ \r
+ ComponentTypeCommands.setLabel(graph, property, label);\r
+ }\r
+ \r
+ public static void setPropertyRange(WriteGraph graph, Resource property, String range) throws DatabaseException {\r
+ \r
+ Resource annotationType = getAnnotationType(graph, property);\r
+ ComponentTypeCommands.setRange(graph, property, annotationType, range);\r
+ }\r
+ \r
+ public static void setPropertyDescription(WriteGraph graph, Resource property, String description) throws DatabaseException {\r
+ \r
+ ComponentTypeCommands.setDescription(graph, property, description);\r
+ }\r
+ \r
+ public static void setDefaultValue(WriteGraph graph, Resource property, String value) throws DatabaseException {\r
+ \r
+ Resource annotationType = getAnnotationType(graph, property);\r
+ ComponentTypeCommands.setDefaultValue(graph, annotationType, property, value);\r
+ }\r
+ \r
+ public static Resource attachAnnotation(WriteGraph graph, Resource parent, Resource annotationType) throws DatabaseException {\r
+\r
+ Resource annotation = AnnotationUtils.newAnnotation(graph, parent, annotationType);\r
+ \r
+ return annotation;\r
+ }\r
+ \r
+ public static Resource getAnnotationType(ReadGraph graph, Resource property) throws DatabaseException {\r
+ \r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ Resource annotationType = graph.getPossibleObject(property, L0.PartOf);\r
+ return annotationType;\r
+ \r
+ }\r
+ \r
+ public static Resource saveAnnotation(WriteGraph graph, Resource annotation, Resource library, String name) throws DatabaseException {\r
+ return SCL.SaveModifier.saveAnnotation(graph, annotation, library, name);\r
+ }\r
+ \r
+ public static void importAnnotationFromFile(File importLocation, Resource target) throws SerializationException, IOException, DatabaseException {\r
+ Session session = Simantics.getSession();\r
+ AnnotationTypeImportWizard.doImport(new NullProgressMonitor(), importLocation, session, target);\r
+ }\r
+ \r
+ public static void exportAnnotationToFile(File location, Resource annotationType) throws DatabaseException, IOException {\r
+ AnnotationTypeExporter.doExport(location, annotationType);\r
+ }\r
+ \r
+ public static void rename(WriteGraph graph, Resource resource, String newName) throws DatabaseException {\r
+ ComponentTypeCommands.rename(graph, resource, newName);\r
+ }\r
+\r
+ public static void copyAnnotationData(WriteGraph graph, Resource source, Resource target) throws DatabaseException {\r
+ // source and target are expected to be of the same type\r
+ Map<String, PropertyInfo> sps = graph.syncRequest(new UnescapedPropertyMapOfResource(source));\r
+ Map<String, PropertyInfo> tps = graph.syncRequest(new UnescapedPropertyMapOfResource(target));\r
+\r
+ for (PropertyInfo sp : sps.values()) {\r
+ PropertyInfo tp = tps.get(sp.name);\r
+ if (tp != null) {\r
+ for (Statement srcstm : graph.getStatements(source, sp.predicate)) {\r
+ if (srcstm.isAsserted(source)) {\r
+ // Make sure the target value is asserted as well.\r
+ graph.deny(target, sp.predicate);\r
+ } else {\r
+ Resource property = srcstm.getObject();\r
+ // Remove existing property from source and link the target property\r
+ for (Statement tgtstm : graph.getStatements(target, tp.predicate)) {\r
+ if (!tgtstm.isAsserted(target))\r
+ graph.deny(tgtstm);\r
+ }\r
+ // Re-link the source property to the target entity.\r
+ graph.deny(source, sp.predicate, property);\r
+ graph.claim(target, tp.predicate, property);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+}
\ No newline at end of file