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