--- /dev/null
+package org.simantics.export.core.util;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import org.simantics.NameLabelMode;\r
+import org.simantics.NameLabelUtil;\r
+import org.simantics.databoard.util.URIStringUtils;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ParametrizedRead;\r
+import org.simantics.db.common.request.UniqueRead;\r
+import org.simantics.db.common.utils.traverser.TraverseQueryBuilder;\r
+import org.simantics.db.common.utils.traverser.TraverseResult;\r
+import org.simantics.db.exception.AssumptionException;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ResourceNotFoundException;\r
+import org.simantics.db.exception.ServiceException;\r
+import org.simantics.db.exception.ValidationException;\r
+import org.simantics.db.layer0.adapter.Instances;\r
+import org.simantics.db.layer0.request.PossibleModel;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.export.core.manager.Content;\r
+import org.simantics.layer0.Layer0;\r
+\r
+/**\r
+ * Utility queries used by Exporting applications and user interfaces.\r
+ *\r
+ * @author toni.kalajainen@semantum.fi\r
+ */\r
+public class ExportQueries {\r
+ \r
+ public static boolean USE_INDEXER = true;\r
+ \r
+ public static Read<List<String>> toUris(final Collection<Resource> resources) {\r
+ return new UniqueRead<List<String>>() {\r
+ @Override\r
+ public List<String> perform(ReadGraph graph) throws DatabaseException {\r
+ List<String> result = new ArrayList<String>();\r
+ for ( Resource r : resources ) {\r
+ result.add( graph.getURI(r) );\r
+ }\r
+ return result;\r
+ }\r
+ };\r
+ }\r
+ \r
+ public static Read<List<Resource>> toResources(final Collection<String> uris) {\r
+ return new UniqueRead<List<Resource>>() {\r
+ @Override\r
+ public List<Resource> perform(ReadGraph graph) throws DatabaseException {\r
+ List<Resource> result = new ArrayList<Resource>();\r
+ for ( String uri : uris ) {\r
+ result.add( graph.getResource(uri) );\r
+ }\r
+ \r
+ return result;\r
+ }\r
+ };\r
+ }\r
+ \r
+ public static Read<Resource> toResource(final String uri) {\r
+ return new UniqueRead<Resource>() {\r
+ @Override\r
+ public Resource perform(ReadGraph graph) throws DatabaseException {\r
+ return graph.getResource(uri);\r
+ }\r
+ };\r
+ }\r
+ \r
+ public static Read<List<Resource>> toResources2(final Collection<Content> contents) {\r
+ return new UniqueRead<List<Resource>>() {\r
+ @Override\r
+ public List<Resource> perform(ReadGraph graph) throws DatabaseException {\r
+ List<Resource> result = new ArrayList<Resource>();\r
+ for ( Content content : contents ) {\r
+ result.add( graph.getResource( content.url ) );\r
+ }\r
+ \r
+ return result;\r
+ }\r
+ };\r
+ }\r
+ \r
+ /**\r
+ * Get query that returns labels for a collection of uris\r
+ * @param uris\r
+ * @return labels\r
+ */\r
+ public static Read<Map<String, String>> labels(final Collection<String> uris) {\r
+ return new UniqueRead<Map<String, String>>() {\r
+ @Override\r
+ public Map<String, String> perform(ReadGraph graph) throws DatabaseException {\r
+ Map<String, String> result = new HashMap<String, String>();\r
+\r
+ NameLabelMode mode = NameLabelUtil.getNameLabelMode(graph);\r
+\r
+ for ( String uri : uris ) {\r
+ String label = null;\r
+ try { \r
+ Resource r = graph.getResource( uri );\r
+ label = NameLabelUtil.modalName(graph, r, mode);\r
+ } catch (AssumptionException e) {\r
+ } catch (ValidationException e) {\r
+ } catch (ServiceException e) {\r
+ }\r
+ \r
+ if ( label == null ) {\r
+ int c = uri.lastIndexOf('/');\r
+ if ( c>=0 ) label = uri.substring(c+1);\r
+ }\r
+ \r
+ if ( label==null || label.isEmpty() ) {\r
+ label = uri;\r
+ }\r
+ \r
+ result.put(uri, label);\r
+ }\r
+ \r
+ return result;\r
+ }\r
+ };\r
+ }\r
+\r
+ /**\r
+ * Get query that returns a label for a uri\r
+ * @param uris\r
+ * @return labels\r
+ */\r
+ public static Read<String> label(final String uri) {\r
+ return new UniqueRead<String>() {\r
+ @Override\r
+ public String perform(ReadGraph graph) throws DatabaseException {\r
+ String label = null;\r
+ try { \r
+ Resource r = graph.getResource( uri );\r
+ label = NameLabelUtil.modalName(graph, r);\r
+ } catch (AssumptionException e) {\r
+ } catch (ValidationException e) {\r
+ } catch (ServiceException e) {\r
+ }\r
+ \r
+ if ( label == null ) {\r
+ int c = uri.lastIndexOf('/');\r
+ if ( c>=0 ) label = uri.substring(c+1);\r
+ }\r
+ \r
+ if ( label==null || label.isEmpty() ) {\r
+ label = uri;\r
+ }\r
+ \r
+ return label;\r
+ }\r
+ };\r
+ }\r
+\r
+ public static Read<String> label(final Resource r ) {\r
+ return new UniqueRead<String>() {\r
+ public String perform(ReadGraph graph) throws DatabaseException { \r
+ try {\r
+ return NameLabelUtil.modalName(graph, r);\r
+ } catch (AssumptionException e) {\r
+ } catch (ValidationException e) {\r
+ } catch (ServiceException e) {\r
+ }\r
+ return null;\r
+ }\r
+ };\r
+ }\r
+ \r
+ /**\r
+ * Get model resources from contents uris\r
+ * @param contentUris\r
+ * @return models\r
+ */\r
+ public static Read<List<Resource>> toModels(final Collection<String> contentUris) {\r
+ return new UniqueRead<List<Resource>>() {\r
+ @Override\r
+ public List<Resource> perform(ReadGraph graph) throws DatabaseException {\r
+ List<Resource> result = new ArrayList<Resource>();\r
+ for ( String uri : contentUris ) {\r
+ Resource r = graph.getResource( uri );\r
+ Resource possibleModel = graph.syncRequest( new PossibleModel(r) );\r
+ if ( !result.contains( possibleModel ) ) result.add( possibleModel );\r
+ }\r
+ \r
+ return result;\r
+ }\r
+ }; \r
+ } \r
+\r
+ /**\r
+ * Get model resources of a content\r
+ * @param contentUris\r
+ * @return models\r
+ */\r
+ public static Read<String> toModelUri(final String contentUri) {\r
+ return new UniqueRead<String>() {\r
+ @Override\r
+ public String perform(ReadGraph graph) throws DatabaseException {\r
+ Resource r = graph.getResource( contentUri );\r
+ Resource possibleModel = graph.syncRequest( new PossibleModel(r) );\r
+ return possibleModel == null ? null : graph.getURI(possibleModel);\r
+ }\r
+ }; \r
+ } \r
+\r
+ public static ParametrizedRead<Collection<String>, Collection<String>> parametrizedInstancesOf(final String typeUri) {\r
+ return new ParametrizedRead<Collection<String>, Collection<String>>() {\r
+ @Override\r
+ public Read<Collection<String>> get(Collection<String> parameter) {\r
+ return instancesOf(parameter, typeUri);\r
+ }\r
+ };\r
+ }\r
+\r
+ /**\r
+ * Query instances of a type\r
+ * @param startingLocations\r
+ * @param type\r
+ * @return list or uris\r
+ */\r
+ public static Read<Collection<String>> instancesOf(final Collection<String> startingLocations, final String typeUri)\r
+ {\r
+ return new UniqueRead<Collection<String>>() {\r
+ public List<String> perform(ReadGraph graph) throws DatabaseException {\r
+ if ( USE_INDEXER ) { \r
+ ArrayList<String> result = new ArrayList<String>();\r
+ try {\r
+ Resource type = graph.getResource( typeUri );\r
+ Instances instances = graph.adapt(type, Instances.class);\r
+ for ( String slUri : startingLocations ) {\r
+ Resource sl = graph.getResource( slUri );\r
+ \r
+ Resource model = graph.syncRequest( new PossibleModel(sl) );\r
+ if ( model == null ) model = sl;\r
+ \r
+ for ( Resource r : instances.find(graph, model) ) {\r
+ String uri = graph.getPossibleURI( r );\r
+ if ( uri != null\r
+ && uri.startsWith(slUri)\r
+ && uri.length() > slUri.length()\r
+ && uri.charAt(slUri.length()) == URIStringUtils.NAMESPACE_PATH_SEPARATOR)\r
+ {\r
+ result.add( uri );\r
+ }\r
+ }\r
+ }\r
+ } catch ( ResourceNotFoundException e ) {\r
+ // If the type is not installed in the database, there sure are no instances.\r
+ // This is not an error. The ontology just may not have been installed. The code must accept this.\r
+ }\r
+ return result;\r
+ } \r
+ \r
+ else \r
+ \r
+ // Query implementation \r
+ {\r
+ try {\r
+ Layer0 L0 = Layer0.getInstance(graph); \r
+ Resource type = graph.getResource( typeUri );\r
+ TraverseQueryBuilder builder = new TraverseQueryBuilder();\r
+ builder.setStartResources( graph.syncRequest( ExportQueries.toResources(startingLocations) ) );\r
+ builder.followRelation( L0.ConsistsOf );\r
+ builder.followInstanceOf( L0.Library );\r
+ \r
+ try {\r
+ builder.followInstanceOf( graph.getResource("http://www.apros.fi/Apros-6.1/Folder") );\r
+ } catch ( ResourceNotFoundException e ) {}\r
+ \r
+ builder.followAndReturnInstanceOf( type );\r
+ TraverseResult traverseResult = graph.syncRequest( builder.build() );\r
+ return graph.syncRequest( ExportQueries.toUris( traverseResult.result ) );\r
+ } catch ( ResourceNotFoundException e ) {\r
+ // If the type is not installed in the database, there sure are no instances.\r
+ // This is not an error. The ontology just may not have been installed. The code must accept this.\r
+ return Collections.emptyList();\r
+ }\r
+ }\r
+ \r
+ }\r
+ };\r
+ }\r
+ \r
+ /**\r
+ * Query instances of a type\r
+ * @param startingLocations\r
+ * @param type\r
+ * @return list or uris\r
+ */\r
+ public static Read<List<Resource>> instancesOf(final Collection<Resource> startingLocations, final Resource type)\r
+ {\r
+ return new UniqueRead<List<Resource>>() {\r
+ public List<Resource> perform(ReadGraph graph) throws DatabaseException {\r
+ Instances instances = graph.adapt(type, Instances.class);\r
+ ArrayList<Resource> result = new ArrayList<Resource>();\r
+ for ( Resource sl : startingLocations ) {\r
+ result.addAll( instances.find(graph, sl) );\r
+ }\r
+ return result;\r
+ }\r
+ };\r
+ }\r
+ \r
+ \r
+}\r