1 package org.simantics.export.core.util;
\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
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
31 * Utility queries used by Exporting applications and user interfaces.
\r
33 * @author toni.kalajainen@semantum.fi
\r
35 public class ExportQueries {
\r
37 public static boolean USE_INDEXER = true;
\r
39 public static Read<List<String>> toUris(final Collection<Resource> resources) {
\r
40 return new UniqueRead<List<String>>() {
\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
52 public static Read<List<Resource>> toResources(final Collection<String> uris) {
\r
53 return new UniqueRead<List<Resource>>() {
\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
66 public static Read<Resource> toResource(final String uri) {
\r
67 return new UniqueRead<Resource>() {
\r
69 public Resource perform(ReadGraph graph) throws DatabaseException {
\r
70 return graph.getResource(uri);
\r
75 public static Read<List<Resource>> toResources2(final Collection<Content> contents) {
\r
76 return new UniqueRead<List<Resource>>() {
\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
90 * Get query that returns labels for a collection of uris
\r
94 public static Read<Map<String, String>> labels(final Collection<String> uris) {
\r
95 return new UniqueRead<Map<String, String>>() {
\r
97 public Map<String, String> perform(ReadGraph graph) throws DatabaseException {
\r
98 Map<String, String> result = new HashMap<String, String>();
\r
100 NameLabelMode mode = NameLabelUtil.getNameLabelMode(graph);
\r
102 for ( String uri : uris ) {
\r
103 String label = null;
\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
112 if ( label == null ) {
\r
113 int c = uri.lastIndexOf('/');
\r
114 if ( c>=0 ) label = uri.substring(c+1);
\r
117 if ( label==null || label.isEmpty() ) {
\r
121 result.put(uri, label);
\r
130 * Get query that returns a label for a uri
\r
134 public static Read<String> label(final String uri) {
\r
135 return new UniqueRead<String>() {
\r
137 public String perform(ReadGraph graph) throws DatabaseException {
\r
138 String label = null;
\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
147 if ( label == null ) {
\r
148 int c = uri.lastIndexOf('/');
\r
149 if ( c>=0 ) label = uri.substring(c+1);
\r
152 if ( label==null || label.isEmpty() ) {
\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
165 return NameLabelUtil.modalName(graph, r);
\r
166 } catch (AssumptionException e) {
\r
167 } catch (ValidationException e) {
\r
168 } catch (ServiceException e) {
\r
176 * Get model resources from contents uris
\r
177 * @param contentUris
\r
180 public static Read<List<Resource>> toModels(final Collection<String> contentUris) {
\r
181 return new UniqueRead<List<Resource>>() {
\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
197 * Get model resources of a content
\r
198 * @param contentUris
\r
201 public static Read<String> toModelUri(final String contentUri) {
\r
202 return new UniqueRead<String>() {
\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
212 public static ParametrizedRead<Collection<String>, Collection<String>> parametrizedInstancesOf(final String typeUri) {
\r
213 return new ParametrizedRead<Collection<String>, Collection<String>>() {
\r
215 public Read<Collection<String>> get(Collection<String> parameter) {
\r
216 return instancesOf(parameter, typeUri);
\r
222 * Query instances of a type
\r
223 * @param startingLocations
\r
225 * @return list or uris
\r
227 public static Read<Collection<String>> instancesOf(final Collection<String> startingLocations, final String typeUri)
\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
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
239 Resource model = graph.syncRequest( new PossibleModel(sl) );
\r
240 if ( model == null ) model = sl;
\r
242 for ( Resource r : instances.find(graph, model) ) {
\r
243 String uri = graph.getPossibleURI( r );
\r
245 && uri.startsWith(slUri)
\r
246 && uri.length() > slUri.length()
\r
247 && uri.charAt(slUri.length()) == URIStringUtils.NAMESPACE_PATH_SEPARATOR)
\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
262 // Query implementation
\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
273 builder.followInstanceOf( graph.getResource("http://www.apros.fi/Apros-6.1/Folder") );
\r
274 } catch ( ResourceNotFoundException e ) {}
\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
291 * Query instances of a type
\r
292 * @param startingLocations
\r
294 * @return list or uris
\r
296 public static Read<List<Resource>> instancesOf(final Collection<Resource> startingLocations, final Resource type)
\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