]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/SCLAnnotation.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.annotation.ui / src / org / simantics / annotation / ui / SCLAnnotation.java
1 package org.simantics.annotation.ui;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Map;
6
7 import org.eclipse.core.runtime.NullProgressMonitor;
8 import org.simantics.Simantics;
9 import org.simantics.annotation.ui.wizard.AnnotationTypeExporter;
10 import org.simantics.annotation.ui.wizard.AnnotationTypeImportWizard;
11 import org.simantics.databoard.serialization.SerializationException;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.Session;
15 import org.simantics.db.Statement;
16 import org.simantics.db.WriteGraph;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.request.PropertyInfo;
19 import org.simantics.db.layer0.request.UnescapedPropertyMapOfResource;
20 import org.simantics.db.layer0.util.Layer0Utils;
21 import org.simantics.layer0.Layer0;
22 import org.simantics.modeling.userComponent.ComponentTypeCommands;
23 import org.simantics.scl.runtime.tuple.Tuple2;
24 import org.simantics.utils.datastructures.Pair;
25
26 public class SCLAnnotation {
27         
28
29         public static Resource newAnnotationType(WriteGraph graph, Resource parent) throws DatabaseException {
30                 
31                 Pair<Resource, Resource> annotation = AnnotationUtils.newAnnotationType(graph, parent);
32                 Resource type = annotation.second;
33                 
34                 return type;
35         }
36         
37         public static Tuple2 newAnnotationType1(WriteGraph graph, Resource parent) throws DatabaseException {
38                 
39                 Pair<Resource, Resource> annotation = AnnotationUtils.newAnnotationType(graph, parent);
40                 Tuple2 tuple = new Tuple2(annotation.first, annotation.second);
41                 
42                 return tuple;
43         }
44         
45         public static Resource newAnnotationValue(WriteGraph graph, Resource parent, Resource property) throws DatabaseException {
46                 graph.markUndoPoint();
47                 Resource annotationValue = AnnotationUtils.newAnnotationInstance(graph, parent, property);
48                 return annotationValue;
49         }
50
51         public static Resource newAnnotationValueWithName(WriteGraph graph, Resource parent, String name, Resource property) throws DatabaseException {
52                 Resource existing = Layer0Utils.getPossibleChild(graph, parent, name);
53                 if(existing != null) return existing;
54                 Resource annotationValue = AnnotationUtils.newAnnotationInstance(graph, parent, name, property);
55                 return annotationValue;
56         }
57
58         public static Resource newAnnotationProperty(WriteGraph graph, Resource annotationType) throws DatabaseException {
59                 
60                 Resource annotationProperty = ComponentTypeCommands.createPropertyWithDefaults(graph, annotationType);
61                 
62                 return annotationProperty;
63         }
64         
65         public static Resource advancedAnnotationProperty(WriteGraph graph, Resource annotationType, String name, String value, String unit, String range, String label, String description) throws DatabaseException {
66                 
67                 Resource property = ComponentTypeCommands.createProperty(graph, annotationType, name, value, unit, range, label, description);
68                 
69                 return property;
70         }
71         
72         public static void removeAnnotationProperty(WriteGraph graph, Resource property) throws DatabaseException {
73                 
74                 Resource annotationType = getAnnotationType(graph, property);
75                 ComponentTypeCommands.removeProperty(graph, annotationType, property);
76         }
77         
78         public static void setPropertyType(WriteGraph graph, Resource property, String type) throws DatabaseException {
79                 
80                 Resource annotationType = getAnnotationType(graph, property);
81                 ComponentTypeCommands.setRequiredType(graph, property, type);
82                 ComponentTypeCommands.convertDefaultValue(graph, annotationType, property, type);
83                 
84         }
85         
86         public static void setPropertyUnit(WriteGraph graph, Resource property, String unit) throws DatabaseException {
87                 
88                 Resource annotationType = getAnnotationType(graph, property);
89                 ComponentTypeCommands.setUnit(graph, property, annotationType, unit);
90                 
91         }
92         
93         public static void setPropertyLabel(WriteGraph graph, Resource property, String label) throws DatabaseException {
94                 
95                 ComponentTypeCommands.setLabel(graph, property, label);
96         }
97         
98         public static void setPropertyRange(WriteGraph graph, Resource property, String range) throws DatabaseException {
99                 
100                 Resource annotationType = getAnnotationType(graph, property);
101                 ComponentTypeCommands.setRange(graph, property, annotationType, range);
102         }
103         
104         public static void setPropertyDescription(WriteGraph graph, Resource property, String description) throws DatabaseException {
105                 
106                 ComponentTypeCommands.setDescription(graph, property, description);
107         }
108         
109         public static void setDefaultValue(WriteGraph graph, Resource property, String value) throws DatabaseException {
110                 
111                 Resource annotationType = getAnnotationType(graph, property);
112                 ComponentTypeCommands.setDefaultValue(graph, annotationType, property, value);
113         }
114         
115         public static Resource attachAnnotation(WriteGraph graph, Resource parent, Resource annotationType) throws DatabaseException {
116
117                 Resource annotation = AnnotationUtils.newAnnotation(graph, parent, annotationType);
118                 
119                 return annotation;
120         }
121         
122         public static Resource getAnnotationType(ReadGraph graph, Resource property) throws DatabaseException {
123                 
124                 Layer0 L0 = Layer0.getInstance(graph);
125                 Resource annotationType = graph.getPossibleObject(property, L0.PartOf);
126                 return annotationType;
127                 
128         }
129         
130         public static Resource saveAnnotation(WriteGraph graph, Resource annotation, Resource library, String name) throws DatabaseException {
131                 return SCL.SaveModifier.saveAnnotation(graph, annotation, library, name);
132         }
133         
134         public static void importAnnotationFromFile(File importLocation, Resource target) throws SerializationException, IOException, DatabaseException {
135                 Session session = Simantics.getSession();
136                 AnnotationTypeImportWizard.doImport(new NullProgressMonitor(), importLocation, session, target);
137         }
138         
139         public static void exportAnnotationToFile(File location, Resource annotationType) throws DatabaseException, IOException {
140                 AnnotationTypeExporter.doExport(location, annotationType);
141         }
142         
143         public static void rename(WriteGraph graph, Resource resource, String newName) throws DatabaseException {
144                 ComponentTypeCommands.rename(graph, resource, newName);
145         }
146
147     public static void copyAnnotationData(WriteGraph graph, Resource source, Resource target) throws DatabaseException {
148         // source and target are expected to be of the same type
149         Map<String, PropertyInfo> sps = graph.syncRequest(new UnescapedPropertyMapOfResource(source));
150         Map<String, PropertyInfo> tps = graph.syncRequest(new UnescapedPropertyMapOfResource(target));
151
152         for (PropertyInfo sp : sps.values()) {
153             PropertyInfo tp = tps.get(sp.name);
154             if (tp != null) {
155                 for (Statement srcstm : graph.getStatements(source, sp.predicate)) {
156                     if (srcstm.isAsserted(source)) {
157                         // Make sure the target value is asserted as well.
158                         graph.deny(target, sp.predicate);
159                     } else {
160                         Resource property = srcstm.getObject();
161                         // Remove existing property from source and link the target property
162                         for (Statement tgtstm : graph.getStatements(target, tp.predicate)) {
163                             if (!tgtstm.isAsserted(target))
164                                 graph.deny(tgtstm);
165                         }
166                         // Re-link the source property to the target entity.
167                         graph.deny(source, sp.predicate, property);
168                         graph.claim(target, tp.predicate, property);
169                     }
170                 }
171             }
172         }
173     }
174
175 }