]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.export.core/src/org/simantics/export/core/util/ExportQueries.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / util / ExportQueries.java
1 package org.simantics.export.core.util;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 import java.util.Collections;\r
6 import java.util.HashMap;\r
7 import java.util.List;\r
8 import java.util.Map;\r
9 \r
10 import org.simantics.NameLabelMode;\r
11 import org.simantics.NameLabelUtil;\r
12 import org.simantics.databoard.util.URIStringUtils;\r
13 import org.simantics.db.ReadGraph;\r
14 import org.simantics.db.Resource;\r
15 import org.simantics.db.common.request.ParametrizedRead;\r
16 import org.simantics.db.common.request.UniqueRead;\r
17 import org.simantics.db.common.utils.traverser.TraverseQueryBuilder;\r
18 import org.simantics.db.common.utils.traverser.TraverseResult;\r
19 import org.simantics.db.exception.AssumptionException;\r
20 import org.simantics.db.exception.DatabaseException;\r
21 import org.simantics.db.exception.ResourceNotFoundException;\r
22 import org.simantics.db.exception.ServiceException;\r
23 import org.simantics.db.exception.ValidationException;\r
24 import org.simantics.db.layer0.adapter.Instances;\r
25 import org.simantics.db.layer0.request.PossibleModel;\r
26 import org.simantics.db.request.Read;\r
27 import org.simantics.export.core.manager.Content;\r
28 import org.simantics.layer0.Layer0;\r
29 \r
30 /**\r
31  * Utility queries used by Exporting applications and user interfaces.\r
32  *\r
33  * @author toni.kalajainen@semantum.fi\r
34  */\r
35 public class ExportQueries {\r
36                 \r
37         public static boolean USE_INDEXER = true;\r
38         \r
39         public static Read<List<String>> toUris(final Collection<Resource> resources) {\r
40                 return new UniqueRead<List<String>>() {\r
41                         @Override\r
42                         public List<String> perform(ReadGraph graph) throws DatabaseException {\r
43                                 List<String> result = new ArrayList<String>();\r
44                                 for ( Resource r : resources ) {\r
45                                         result.add( graph.getURI(r) );\r
46                                 }\r
47                                 return result;\r
48                         }\r
49                 };\r
50         }\r
51         \r
52         public static Read<List<Resource>> toResources(final Collection<String> uris) {\r
53                 return new UniqueRead<List<Resource>>() {\r
54                         @Override\r
55                         public List<Resource> perform(ReadGraph graph) throws DatabaseException {\r
56                                 List<Resource> result = new ArrayList<Resource>();\r
57                                 for ( String uri : uris ) {\r
58                                         result.add( graph.getResource(uri) );\r
59                                 }\r
60                                 \r
61                                 return result;\r
62                         }\r
63                 };\r
64         }\r
65         \r
66         public static Read<Resource> toResource(final String uri) {\r
67                 return new UniqueRead<Resource>() {\r
68                         @Override\r
69                         public Resource perform(ReadGraph graph) throws DatabaseException {\r
70                                 return graph.getResource(uri);\r
71                         }\r
72                 };\r
73         }\r
74         \r
75         public static Read<List<Resource>> toResources2(final Collection<Content> contents) {\r
76                 return new UniqueRead<List<Resource>>() {\r
77                         @Override\r
78                         public List<Resource> perform(ReadGraph graph) throws DatabaseException {\r
79                                 List<Resource> result = new ArrayList<Resource>();\r
80                                 for ( Content content : contents ) {\r
81                                         result.add( graph.getResource( content.url ) );\r
82                                 }\r
83                                 \r
84                                 return result;\r
85                         }\r
86                 };\r
87         }\r
88         \r
89         /**\r
90          * Get query that returns labels for a collection of uris\r
91          * @param uris\r
92          * @return labels\r
93          */\r
94         public static Read<Map<String, String>> labels(final Collection<String> uris) {\r
95                 return new UniqueRead<Map<String, String>>() {\r
96                         @Override\r
97                         public Map<String, String> perform(ReadGraph graph) throws DatabaseException {\r
98                                 Map<String, String> result = new HashMap<String, String>();\r
99 \r
100                                 NameLabelMode mode = NameLabelUtil.getNameLabelMode(graph);\r
101 \r
102                                 for ( String uri : uris ) {\r
103                                         String label = null;\r
104                         try { \r
105                                         Resource r = graph.getResource( uri );\r
106                                         label = NameLabelUtil.modalName(graph, r, mode);\r
107                         } catch (AssumptionException e) {\r
108                         } catch (ValidationException e) {\r
109                         } catch (ServiceException e) {\r
110                         }\r
111                         \r
112                         if ( label == null ) {\r
113                                 int c = uri.lastIndexOf('/');\r
114                                 if ( c>=0 ) label = uri.substring(c+1);\r
115                         }\r
116                         \r
117                         if ( label==null || label.isEmpty() ) {\r
118                                 label = uri;\r
119                         }\r
120                         \r
121                                 result.put(uri, label);\r
122                                 }\r
123                                 \r
124                                 return result;\r
125                         }\r
126                 };\r
127         }\r
128 \r
129         /**\r
130          * Get query that returns a label for a uri\r
131          * @param uris\r
132          * @return labels\r
133          */\r
134         public static Read<String> label(final String uri) {\r
135                 return new UniqueRead<String>() {\r
136                         @Override\r
137                         public String perform(ReadGraph graph) throws DatabaseException {\r
138                                 String label = null;\r
139                         try { \r
140                                 Resource r = graph.getResource( uri );\r
141                                 label = NameLabelUtil.modalName(graph, r);\r
142                         } catch (AssumptionException e) {\r
143                         } catch (ValidationException e) {\r
144                         } catch (ServiceException e) {\r
145                         }\r
146                         \r
147                         if ( label == null ) {\r
148                                 int c = uri.lastIndexOf('/');\r
149                                 if ( c>=0 ) label = uri.substring(c+1);\r
150                         }\r
151                         \r
152                         if ( label==null || label.isEmpty() ) {\r
153                                 label = uri;\r
154                         }\r
155                         \r
156                         return label;\r
157                         }\r
158                 };\r
159         }\r
160 \r
161         public static Read<String> label(final Resource r ) {\r
162                 return new UniqueRead<String>() {\r
163                         public String perform(ReadGraph graph) throws DatabaseException {                               \r
164                                 try {\r
165                                 return NameLabelUtil.modalName(graph, r);\r
166                         } catch (AssumptionException e) {\r
167                         } catch (ValidationException e) {\r
168                         } catch (ServiceException e) {\r
169                         }\r
170                                 return null;\r
171                         }\r
172                 };\r
173         }\r
174         \r
175         /**\r
176          * Get model resources from contents uris\r
177          * @param contentUris\r
178          * @return models\r
179          */\r
180         public static Read<List<Resource>> toModels(final Collection<String> contentUris) {\r
181                 return new UniqueRead<List<Resource>>() {\r
182                         @Override\r
183                         public List<Resource> perform(ReadGraph graph) throws DatabaseException {\r
184                                 List<Resource> result = new ArrayList<Resource>();\r
185                                 for ( String uri : contentUris ) {\r
186                                         Resource r = graph.getResource( uri );\r
187                                         Resource possibleModel = graph.syncRequest( new PossibleModel(r) );\r
188                                         if ( !result.contains( possibleModel ) ) result.add( possibleModel );\r
189                                 }\r
190                                 \r
191                                 return result;\r
192                         }\r
193                 };              \r
194         }       \r
195 \r
196         /**\r
197          * Get model resources of a content\r
198          * @param contentUris\r
199          * @return models\r
200          */\r
201         public static Read<String> toModelUri(final String contentUri) {\r
202                 return new UniqueRead<String>() {\r
203                         @Override\r
204                         public String perform(ReadGraph graph) throws DatabaseException {\r
205                                 Resource r = graph.getResource( contentUri );\r
206                                 Resource possibleModel = graph.syncRequest( new PossibleModel(r) );\r
207                                 return possibleModel == null ? null : graph.getURI(possibleModel);\r
208                         }\r
209                 };              \r
210         }       \r
211 \r
212         public static ParametrizedRead<Collection<String>, Collection<String>> parametrizedInstancesOf(final String typeUri) {\r
213                 return new ParametrizedRead<Collection<String>, Collection<String>>() {\r
214                         @Override\r
215                         public Read<Collection<String>> get(Collection<String> parameter) {\r
216                                 return instancesOf(parameter, typeUri);\r
217                         }\r
218                 };\r
219         }\r
220 \r
221         /**\r
222          * Query instances of a type\r
223          * @param startingLocations\r
224          * @param type\r
225          * @return list or uris\r
226          */\r
227         public static Read<Collection<String>> instancesOf(final Collection<String> startingLocations, final String typeUri)\r
228         {\r
229                 return new UniqueRead<Collection<String>>() {\r
230                         public List<String> perform(ReadGraph graph) throws DatabaseException {\r
231                                 if ( USE_INDEXER ) {                                    \r
232                                         ArrayList<String> result = new ArrayList<String>();\r
233                                         try {\r
234                                                 Resource type = graph.getResource( typeUri );\r
235                                                 Instances instances = graph.adapt(type, Instances.class);\r
236                                                 for ( String slUri : startingLocations ) {\r
237                                                         Resource sl = graph.getResource( slUri );\r
238                                                         \r
239                                                         Resource model = graph.syncRequest( new PossibleModel(sl) );\r
240                                                         if ( model == null ) model = sl;\r
241                                                         \r
242                                                         for ( Resource r : instances.find(graph, model) ) {\r
243                                                                 String uri = graph.getPossibleURI( r );\r
244                                                                 if ( uri != null\r
245                                                                                 && uri.startsWith(slUri)\r
246                                                                                 && uri.length() > slUri.length()\r
247                                                                                 && uri.charAt(slUri.length()) == URIStringUtils.NAMESPACE_PATH_SEPARATOR)\r
248                                                                 {\r
249                                                                         result.add( uri );\r
250                                                                 }\r
251                                                         }\r
252                                                 }\r
253                                         } catch ( ResourceNotFoundException e ) {\r
254                                                 // If the type is not installed in the database, there sure are no instances.\r
255                                                 // This is not an error. The ontology just may not have been installed. The code must accept this.\r
256                                         }\r
257                                         return result;\r
258                                 }                               \r
259                                 \r
260                                 else \r
261                                         \r
262                                 // Query implementation \r
263                                 {\r
264                                         try {\r
265                                                 Layer0 L0 = Layer0.getInstance(graph);                                  \r
266                                                 Resource type = graph.getResource( typeUri );\r
267                                                 TraverseQueryBuilder builder = new TraverseQueryBuilder();\r
268                                                 builder.setStartResources( graph.syncRequest( ExportQueries.toResources(startingLocations) ) );\r
269                                                 builder.followRelation( L0.ConsistsOf );\r
270                                                 builder.followInstanceOf( L0.Library );\r
271                                                 \r
272                                                 try {\r
273                                                         builder.followInstanceOf( graph.getResource("http://www.apros.fi/Apros-6.1/Folder") );\r
274                                                 } catch ( ResourceNotFoundException e ) {}\r
275                                                 \r
276                                                 builder.followAndReturnInstanceOf( type );\r
277                                                 TraverseResult traverseResult = graph.syncRequest( builder.build() );\r
278                                                 return graph.syncRequest( ExportQueries.toUris( traverseResult.result ) );\r
279                                         } catch ( ResourceNotFoundException e ) {\r
280                                                 // If the type is not installed in the database, there sure are no instances.\r
281                                                 // This is not an error. The ontology just may not have been installed. The code must accept this.\r
282                                                 return Collections.emptyList();\r
283                                         }\r
284                                 }\r
285                                                                 \r
286                         }\r
287                 };\r
288         }\r
289         \r
290         /**\r
291          * Query instances of a type\r
292          * @param startingLocations\r
293          * @param type\r
294          * @return list or uris\r
295          */\r
296         public static Read<List<Resource>> instancesOf(final Collection<Resource> startingLocations, final Resource type)\r
297         {\r
298                 return new UniqueRead<List<Resource>>() {\r
299                         public List<Resource> perform(ReadGraph graph) throws DatabaseException {\r
300                                 Instances instances = graph.adapt(type, Instances.class);\r
301                                 ArrayList<Resource> result = new ArrayList<Resource>();\r
302                                 for ( Resource sl : startingLocations ) {\r
303                                         result.addAll( instances.find(graph, sl) );\r
304                                 }\r
305                                 return result;\r
306                         }\r
307                 };\r
308         }\r
309         \r
310         \r
311 }\r