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