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