1 package org.simantics.annotation.ui;
\r
4 import java.io.IOException;
\r
5 import java.util.Map;
\r
7 import org.eclipse.core.runtime.NullProgressMonitor;
\r
8 import org.simantics.Simantics;
\r
9 import org.simantics.annotation.ui.wizard.AnnotationTypeExporter;
\r
10 import org.simantics.annotation.ui.wizard.AnnotationTypeImportWizard;
\r
11 import org.simantics.databoard.serialization.SerializationException;
\r
12 import org.simantics.db.ReadGraph;
\r
13 import org.simantics.db.Resource;
\r
14 import org.simantics.db.Session;
\r
15 import org.simantics.db.Statement;
\r
16 import org.simantics.db.WriteGraph;
\r
17 import org.simantics.db.exception.DatabaseException;
\r
18 import org.simantics.db.layer0.request.PropertyInfo;
\r
19 import org.simantics.db.layer0.request.UnescapedPropertyMapOfResource;
\r
20 import org.simantics.db.layer0.util.Layer0Utils;
\r
21 import org.simantics.layer0.Layer0;
\r
22 import org.simantics.modeling.userComponent.ComponentTypeCommands;
\r
23 import org.simantics.scl.runtime.tuple.Tuple2;
\r
24 import org.simantics.utils.datastructures.Pair;
\r
26 public class SCLAnnotation {
\r
29 public static Resource newAnnotationType(WriteGraph graph, Resource parent) throws DatabaseException {
\r
31 Pair<Resource, Resource> annotation = AnnotationUtils.newAnnotationType(graph, parent);
\r
32 Resource type = annotation.second;
\r
37 public static Tuple2 newAnnotationType1(WriteGraph graph, Resource parent) throws DatabaseException {
\r
39 Pair<Resource, Resource> annotation = AnnotationUtils.newAnnotationType(graph, parent);
\r
40 Tuple2 tuple = new Tuple2(annotation.first, annotation.second);
\r
45 public static Resource newAnnotationValue(WriteGraph graph, Resource parent, Resource property) throws DatabaseException {
\r
46 graph.markUndoPoint();
\r
47 Resource annotationValue = AnnotationUtils.newAnnotationInstance(graph, parent, property);
\r
48 return annotationValue;
\r
51 public static Resource newAnnotationValueWithName(WriteGraph graph, Resource parent, String name, Resource property) throws DatabaseException {
\r
52 Resource existing = Layer0Utils.getPossibleChild(graph, parent, name);
\r
53 if(existing != null) return existing;
\r
54 Resource annotationValue = AnnotationUtils.newAnnotationInstance(graph, parent, name, property);
\r
55 return annotationValue;
\r
58 public static Resource newAnnotationProperty(WriteGraph graph, Resource annotationType) throws DatabaseException {
\r
60 Resource annotationProperty = ComponentTypeCommands.createPropertyWithDefaults(graph, annotationType);
\r
62 return annotationProperty;
\r
65 public static Resource advancedAnnotationProperty(WriteGraph graph, Resource annotationType, String name, String value, String unit, String range, String label, String description) throws DatabaseException {
\r
67 Resource property = ComponentTypeCommands.createProperty(graph, annotationType, name, value, unit, range, label, description);
\r
72 public static void removeAnnotationProperty(WriteGraph graph, Resource property) throws DatabaseException {
\r
74 Resource annotationType = getAnnotationType(graph, property);
\r
75 ComponentTypeCommands.removeProperty(graph, annotationType, property);
\r
78 public static void setPropertyType(WriteGraph graph, Resource property, String type) throws DatabaseException {
\r
80 Resource annotationType = getAnnotationType(graph, property);
\r
81 ComponentTypeCommands.setRequiredType(graph, property, type);
\r
82 ComponentTypeCommands.convertDefaultValue(graph, annotationType, property, type);
\r
86 public static void setPropertyUnit(WriteGraph graph, Resource property, String unit) throws DatabaseException {
\r
88 Resource annotationType = getAnnotationType(graph, property);
\r
89 ComponentTypeCommands.setUnit(graph, property, annotationType, unit);
\r
93 public static void setPropertyLabel(WriteGraph graph, Resource property, String label) throws DatabaseException {
\r
95 ComponentTypeCommands.setLabel(graph, property, label);
\r
98 public static void setPropertyRange(WriteGraph graph, Resource property, String range) throws DatabaseException {
\r
100 Resource annotationType = getAnnotationType(graph, property);
\r
101 ComponentTypeCommands.setRange(graph, property, annotationType, range);
\r
104 public static void setPropertyDescription(WriteGraph graph, Resource property, String description) throws DatabaseException {
\r
106 ComponentTypeCommands.setDescription(graph, property, description);
\r
109 public static void setDefaultValue(WriteGraph graph, Resource property, String value) throws DatabaseException {
\r
111 Resource annotationType = getAnnotationType(graph, property);
\r
112 ComponentTypeCommands.setDefaultValue(graph, annotationType, property, value);
\r
115 public static Resource attachAnnotation(WriteGraph graph, Resource parent, Resource annotationType) throws DatabaseException {
\r
117 Resource annotation = AnnotationUtils.newAnnotation(graph, parent, annotationType);
\r
122 public static Resource getAnnotationType(ReadGraph graph, Resource property) throws DatabaseException {
\r
124 Layer0 L0 = Layer0.getInstance(graph);
\r
125 Resource annotationType = graph.getPossibleObject(property, L0.PartOf);
\r
126 return annotationType;
\r
130 public static Resource saveAnnotation(WriteGraph graph, Resource annotation, Resource library, String name) throws DatabaseException {
\r
131 return SCL.SaveModifier.saveAnnotation(graph, annotation, library, name);
\r
134 public static void importAnnotationFromFile(File importLocation, Resource target) throws SerializationException, IOException, DatabaseException {
\r
135 Session session = Simantics.getSession();
\r
136 AnnotationTypeImportWizard.doImport(new NullProgressMonitor(), importLocation, session, target);
\r
139 public static void exportAnnotationToFile(File location, Resource annotationType) throws DatabaseException, IOException {
\r
140 AnnotationTypeExporter.doExport(location, annotationType);
\r
143 public static void rename(WriteGraph graph, Resource resource, String newName) throws DatabaseException {
\r
144 ComponentTypeCommands.rename(graph, resource, newName);
\r
147 public static void copyAnnotationData(WriteGraph graph, Resource source, Resource target) throws DatabaseException {
\r
148 // source and target are expected to be of the same type
\r
149 Map<String, PropertyInfo> sps = graph.syncRequest(new UnescapedPropertyMapOfResource(source));
\r
150 Map<String, PropertyInfo> tps = graph.syncRequest(new UnescapedPropertyMapOfResource(target));
\r
152 for (PropertyInfo sp : sps.values()) {
\r
153 PropertyInfo tp = tps.get(sp.name);
\r
155 for (Statement srcstm : graph.getStatements(source, sp.predicate)) {
\r
156 if (srcstm.isAsserted(source)) {
\r
157 // Make sure the target value is asserted as well.
\r
158 graph.deny(target, sp.predicate);
\r
160 Resource property = srcstm.getObject();
\r
161 // Remove existing property from source and link the target property
\r
162 for (Statement tgtstm : graph.getStatements(target, tp.predicate)) {
\r
163 if (!tgtstm.isAsserted(target))
\r
164 graph.deny(tgtstm);
\r
166 // Re-link the source property to the target entity.
\r
167 graph.deny(source, sp.predicate, property);
\r
168 graph.claim(target, tp.predicate, property);
\r