]> gerrit.simantics Code Review - simantics/platform.git/blob
d41a0616601392bfa676c313b25eaad1d9d71cb9
[simantics/platform.git] /
1 package org.simantics.document.linking.report.evaluator;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.simantics.Simantics;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.WriteGraph;
14 import org.simantics.db.common.NamedResource;
15 import org.simantics.db.common.request.ObjectsWithType;
16 import org.simantics.db.common.request.WriteRequest;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.request.Read;
19 import org.simantics.document.linking.ontology.DocumentLink;
20 import org.simantics.document.linking.report.templates.CustomizableContentProvider;
21 import org.simantics.document.linking.report.templates.custom.CustomizableContent;
22 import org.simantics.document.linking.report.templates.custom.EvaluatorCustomizableContent;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.objmap.graph.IMapping;
25 import org.simantics.objmap.graph.Mappings;
26 import org.simantics.objmap.graph.schema.IMappingSchema;
27 import org.simantics.objmap.graph.schema.MappingSchemas;
28 import org.simantics.objmap.graph.schema.SimpleSchema;
29
30 public class DBUtil {
31         
32         /**
33          * Loads single evaluator tree from graph.
34          * @param graph
35          * @param res
36          * @return
37          * @throws DatabaseException
38          */
39         public static EvaluatorItem load(ReadGraph graph, Resource res) throws DatabaseException{
40                 IMappingSchema<Resource, Object> schema = getSchema(graph); 
41                 IMapping<Resource,Object> mapping =  Mappings.createWithoutListening(schema);
42                 EvaluatorItem item = (EvaluatorItem)mapping.map(graph, res);
43                 return item;
44         }
45         
46         /**
47          * Saves a single evaluator tree to graph.
48          * @param graph
49          * @param item
50          * @return
51          * @throws DatabaseException
52          */
53         public static Resource save(WriteGraph graph, EvaluatorItem item) throws DatabaseException{
54                 IMappingSchema<Resource, Object> schema = getSchema(graph); 
55                 IMapping<Resource,Object> mapping =  Mappings.createWithoutListening(schema);
56                 Resource resource =  mapping.inverseMap(graph, item);
57                 return resource;
58         }
59         
60         /**
61          * Creates mapping schema for Evaluator tree.
62          * @param g
63          * @return
64          * @throws DatabaseException
65          */
66         public static IMappingSchema<Resource,Object> getSchema(ReadGraph g) throws DatabaseException{
67                 try {
68                         SimpleSchema schema = new SimpleSchema();
69                         schema.addLinkType(MappingSchemas.fromAnnotations(g, AlignmentHint.class));
70                         schema.addLinkType(MappingSchemas.fromAnnotations(g, And.class));
71                         schema.addLinkType(MappingSchemas.fromAnnotations(g, Constant.class));
72                         schema.addLinkType(MappingSchemas.fromAnnotations(g, Date.class));
73                         schema.addLinkType(MappingSchemas.fromAnnotations(g, EvaluatorRoot.class));
74                         schema.addLinkType(MappingSchemas.fromAnnotations(g, If.class));
75                         schema.addLinkType(MappingSchemas.fromAnnotations(g, Lines.class));
76                         schema.addLinkType(MappingSchemas.fromAnnotations(g, Or.class));
77                         schema.addLinkType(MappingSchemas.fromAnnotations(g, TextSizeHint.class));
78                         schema.addLinkType(MappingSchemas.fromAnnotations(g, Variable.class));
79                         schema.addLinkType(MappingSchemas.fromAnnotations(g, Path.class));
80                         
81                         return schema;
82                 } catch (IllegalAccessException e) {
83                         throw new DatabaseException(e);
84                 } catch (InstantiationException e) {
85                         throw new DatabaseException(e);
86                 }
87         }
88         
89         /**
90          * Saves evaluator tree configurations of content provider.
91          * @param graph
92          * @param provider
93          * @param template
94          * @throws DatabaseException
95          */
96         public static void save(WriteGraph graph, CustomizableContentProvider provider, Resource template) throws DatabaseException{
97                 Layer0 l0 = Layer0.getInstance(graph);
98                 DocumentLink sl = DocumentLink.getInstance(graph);
99                 
100                 Map<String, Resource> existingEvaluatorTemplates = new HashMap<String, Resource>();
101                 for (Resource r : graph.getObjects(template, l0.ConsistsOf)) {
102                         if (graph.isInstanceOf(r, sl.EvaluatorTree_Template)) {
103                                 String id = graph.getRelatedValue(r, l0.HasName);
104                                 existingEvaluatorTemplates.put(id, r);
105                         }
106                 }
107                 
108                 for (String id : provider.getContentIds()) {
109                         CustomizableContent content = provider.getContent(id);
110                         if (content instanceof EvaluatorCustomizableContent) {
111                                 EvaluatorCustomizableContent ecc = (EvaluatorCustomizableContent)content;
112                                 Resource item = save(graph, ecc.getItem());
113                                 
114                                 Resource treeTemplate = existingEvaluatorTemplates.get(id);
115                                 if (treeTemplate == null) {
116                                         treeTemplate = graph.newResource();
117                                         graph.claim(treeTemplate, l0.InstanceOf, sl.EvaluatorTree_Template);
118                                         graph.claimLiteral(treeTemplate, l0.HasName, id);
119                                         graph.claim(template, l0.ConsistsOf, treeTemplate);
120                                 } else {
121                                         graph.deny(treeTemplate,l0.ConsistsOf);
122                                 }
123                                 graph.claim(treeTemplate, l0.ConsistsOf, item);
124                         }
125                 }
126         }
127         
128         /**
129          * Loads evaluator tree configurations from a graph and applies them to content provider.
130          * @param graph
131          * @param template
132          * @param provider
133          * @throws DatabaseException
134          */
135         public static void load(ReadGraph graph, Resource template, CustomizableContentProvider provider) throws DatabaseException {
136                 Layer0 l0 = Layer0.getInstance(graph);
137                 DocumentLink sl = DocumentLink.getInstance(graph);
138         
139                 Map<String, Resource> existingEvaluatorTemplates = new HashMap<String, Resource>();
140                 for (Resource r : graph.getObjects(template, l0.ConsistsOf)) {
141                         if (graph.isInstanceOf(r, sl.EvaluatorTree_Template)) {
142                                 String id = graph.getRelatedValue(r, l0.HasName);
143                                 existingEvaluatorTemplates.put(id, r);
144                         }
145                 }
146                 
147                 for (String id : provider.getContentIds()) {
148                         Resource treeTemplate = existingEvaluatorTemplates.get(id);
149                         if (treeTemplate == null)
150                                 continue;
151                         
152                         Resource itemRes = graph.getSingleObject(treeTemplate, l0.ConsistsOf);
153                         EvaluatorItem item = load(graph, itemRes);
154                         EvaluatorCustomizableContent ecc = new EvaluatorCustomizableContent(provider.getContent(id).getCustomizationDescription());
155                         ecc.setItem(item);
156                         provider.setContent(id, ecc);
157                          
158                 }       
159                 
160         }
161         
162         public static List<NamedResource> getTemplates(final Resource library) throws DatabaseException{
163                 if (library == null)
164                         throw new IllegalArgumentException("Library cannot be null");
165                 List<NamedResource> templates = Simantics.getSession().syncRequest(new Read<List<NamedResource>>() {
166                 @Override
167                 public List<NamedResource> perform(ReadGraph graph) throws DatabaseException{
168                                 return getTemplates(graph, library);
169                         }
170                 });
171                 return templates;
172         }
173         
174         public static List<NamedResource> getTemplates(ReadGraph graph, final Resource library) throws DatabaseException{
175                 Layer0 l0 = Layer0.getInstance(graph);
176                 DocumentLink sl = DocumentLink.getInstance(graph);
177                 Collection<Resource> templates = graph.syncRequest(new ObjectsWithType(library, l0.ConsistsOf, sl.ReportTemplate));
178                 List<NamedResource> result = new ArrayList<NamedResource>(templates.size());
179                 for (Resource template : templates) {
180                         String name = graph.getRelatedValue(template, l0.HasName);
181                         result.add(new NamedResource(name, template));
182                 }
183
184                 Collections.sort(result);
185                 return result;
186         }
187         
188         public static void save(final Resource library, final String name,final  CustomizableContentProvider provider) throws DatabaseException{
189                 Simantics.getSession().syncRequest(new WriteRequest() {
190                         
191                         @Override
192                         public void perform(WriteGraph graph) throws DatabaseException {
193                                 final List<NamedResource> templates = DBUtil.getTemplates(graph,library);
194                                 Resource template = null;
195                                 for (NamedResource nr : templates) {
196                                         if (name.equals(nr.getName())) {
197                                                 template = nr.getResource();
198                                                 break;
199                                         }
200                                 }
201                                 if (template == null) {
202                                         Layer0 l0 = Layer0.getInstance(graph);
203                                         DocumentLink sl = DocumentLink.getInstance(graph);
204                                         template = graph.newResource();
205                                         graph.claim(template, l0.InstanceOf, sl.ReportTemplate);
206                                         graph.claim(library, l0.ConsistsOf, template);
207                                         graph.claimLiteral(template, l0.HasName, name);
208                                 }
209                                 DBUtil.save(graph, provider, template);
210                         }
211                 });
212         }
213
214 }