]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/SCLAnnotation.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.annotation.ui / src / org / simantics / annotation / ui / SCLAnnotation.java
1 package org.simantics.annotation.ui;\r
2 \r
3 import java.io.File;\r
4 import java.io.IOException;\r
5 import java.util.Map;\r
6 \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
25 \r
26 public class SCLAnnotation {\r
27         \r
28 \r
29         public static Resource newAnnotationType(WriteGraph graph, Resource parent) throws DatabaseException {\r
30                 \r
31                 Pair<Resource, Resource> annotation = AnnotationUtils.newAnnotationType(graph, parent);\r
32                 Resource type = annotation.second;\r
33                 \r
34                 return type;\r
35         }\r
36         \r
37         public static Tuple2 newAnnotationType1(WriteGraph graph, Resource parent) throws DatabaseException {\r
38                 \r
39                 Pair<Resource, Resource> annotation = AnnotationUtils.newAnnotationType(graph, parent);\r
40                 Tuple2 tuple = new Tuple2(annotation.first, annotation.second);\r
41                 \r
42                 return tuple;\r
43         }\r
44         \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
49         }\r
50 \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
56         }\r
57 \r
58         public static Resource newAnnotationProperty(WriteGraph graph, Resource annotationType) throws DatabaseException {\r
59                 \r
60                 Resource annotationProperty = ComponentTypeCommands.createPropertyWithDefaults(graph, annotationType);\r
61                 \r
62                 return annotationProperty;\r
63         }\r
64         \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
66                 \r
67                 Resource property = ComponentTypeCommands.createProperty(graph, annotationType, name, value, unit, range, label, description);\r
68                 \r
69                 return property;\r
70         }\r
71         \r
72         public static void removeAnnotationProperty(WriteGraph graph, Resource property) throws DatabaseException {\r
73                 \r
74                 Resource annotationType = getAnnotationType(graph, property);\r
75                 ComponentTypeCommands.removeProperty(graph, annotationType, property);\r
76         }\r
77         \r
78         public static void setPropertyType(WriteGraph graph, Resource property, String type) throws DatabaseException {\r
79                 \r
80                 Resource annotationType = getAnnotationType(graph, property);\r
81                 ComponentTypeCommands.setRequiredType(graph, property, type);\r
82                 ComponentTypeCommands.convertDefaultValue(graph, annotationType, property, type);\r
83                 \r
84         }\r
85         \r
86         public static void setPropertyUnit(WriteGraph graph, Resource property, String unit) throws DatabaseException {\r
87                 \r
88                 Resource annotationType = getAnnotationType(graph, property);\r
89                 ComponentTypeCommands.setUnit(graph, property, annotationType, unit);\r
90                 \r
91         }\r
92         \r
93         public static void setPropertyLabel(WriteGraph graph, Resource property, String label) throws DatabaseException {\r
94                 \r
95                 ComponentTypeCommands.setLabel(graph, property, label);\r
96         }\r
97         \r
98         public static void setPropertyRange(WriteGraph graph, Resource property, String range) throws DatabaseException {\r
99                 \r
100                 Resource annotationType = getAnnotationType(graph, property);\r
101                 ComponentTypeCommands.setRange(graph, property, annotationType, range);\r
102         }\r
103         \r
104         public static void setPropertyDescription(WriteGraph graph, Resource property, String description) throws DatabaseException {\r
105                 \r
106                 ComponentTypeCommands.setDescription(graph, property, description);\r
107         }\r
108         \r
109         public static void setDefaultValue(WriteGraph graph, Resource property, String value) throws DatabaseException {\r
110                 \r
111                 Resource annotationType = getAnnotationType(graph, property);\r
112                 ComponentTypeCommands.setDefaultValue(graph, annotationType, property, value);\r
113         }\r
114         \r
115         public static Resource attachAnnotation(WriteGraph graph, Resource parent, Resource annotationType) throws DatabaseException {\r
116 \r
117                 Resource annotation = AnnotationUtils.newAnnotation(graph, parent, annotationType);\r
118                 \r
119                 return annotation;\r
120         }\r
121         \r
122         public static Resource getAnnotationType(ReadGraph graph, Resource property) throws DatabaseException {\r
123                 \r
124                 Layer0 L0 = Layer0.getInstance(graph);\r
125                 Resource annotationType = graph.getPossibleObject(property, L0.PartOf);\r
126                 return annotationType;\r
127                 \r
128         }\r
129         \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
132         }\r
133         \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
137         }\r
138         \r
139         public static void exportAnnotationToFile(File location, Resource annotationType) throws DatabaseException, IOException {\r
140                 AnnotationTypeExporter.doExport(location, annotationType);\r
141         }\r
142         \r
143         public static void rename(WriteGraph graph, Resource resource, String newName) throws DatabaseException {\r
144                 ComponentTypeCommands.rename(graph, resource, newName);\r
145         }\r
146 \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
151 \r
152         for (PropertyInfo sp : sps.values()) {\r
153             PropertyInfo tp = tps.get(sp.name);\r
154             if (tp != null) {\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
159                     } else {\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
165                         }\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
169                     }\r
170                 }\r
171             }\r
172         }\r
173     }\r
174 \r
175 }