]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.common/src/org/simantics/db/common/request/Queries.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / Queries.java
diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/request/Queries.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/request/Queries.java
new file mode 100644 (file)
index 0000000..f332a6a
--- /dev/null
@@ -0,0 +1,348 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.common.request;\r
+\r
+import java.util.Collection;\r
+\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.uri.ResourceToPossibleURI;\r
+import org.simantics.db.common.uri.ResourceToURI;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.AsyncRead;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.layer0.Layer0;\r
+\r
+/**\r
+ * Facade class that aggregates the most common queries.\r
+ * \r
+ * Example:\r
+ * \r
+ *   System.out.println(  session.read( Queries.adapt(r, String.class) )  );\r
+ *\r
+ * @author Tuukka Lehtonen\r
+ * @author Toni Kalajainen\r
+ */\r
+public class Queries {\r
+\r
+    public static AsyncRead<Resource> resource(String uri) {\r
+        return new org.simantics.db.common.primitiverequest.Resource(uri);\r
+    }\r
+\r
+    public static Read<String> name(final Resource resource) {\r
+        return new ResourceRead<String>(resource) {\r
+            @Override\r
+            public String perform(ReadGraph graph) throws DatabaseException {\r
+                return graph.getRelatedValue(resource, Layer0.getInstance(graph).HasName);\r
+            }\r
+        };\r
+    }\r
+\r
+    public static Read<String> safeName(final Resource resource) {\r
+        return new ResourceRead<String>(resource) {\r
+            @Override\r
+            public String perform(ReadGraph graph) throws DatabaseException {\r
+                return NameUtils.getSafeName(graph, resource);\r
+            }\r
+        };\r
+    }\r
+\r
+    public static Read<String> name(final String uri) {\r
+        return new UnaryRead<String, String>(uri) {\r
+            @Override\r
+            public String perform(ReadGraph graph) throws DatabaseException {\r
+                Resource resource = graph.getResource(uri);\r
+                return graph.getRelatedValue(resource, Layer0.getInstance(graph).HasName);\r
+            }\r
+        };\r
+    }\r
+\r
+    public static Read<String> uri(Resource resource) {\r
+        return new ResourceToURI(resource);\r
+    }\r
+\r
+    public static Read<String> possibleUri(Resource resource) {\r
+        return new ResourceToPossibleURI(resource);\r
+    }\r
+\r
+    public static <T> Read<T> adapt(Resource resource, Class<T> target)\r
+    {\r
+        return new Adapt<T>(resource, target);\r
+    }\r
+\r
+    public static <T> Read<T> adapt(Resource resource, Class<T> target, boolean allowNull)\r
+    {\r
+        return new Adapt<T>(resource, target, allowNull);\r
+    }\r
+\r
+    public static <T> Read<T> adapt(Resource resource, Class<T> target, boolean allowNull, boolean uniqueResult)\r
+    {\r
+        return new Adapt<T>(resource, target, allowNull, uniqueResult);\r
+    }\r
+\r
+    public static Read<Boolean> isInstanceOf(Resource resource, Resource type)\r
+    {\r
+        return new IsInstanceOfQuery(resource, type);\r
+    }\r
+\r
+    public static Read<Boolean> isInstanceOf(Resource resource, String type)\r
+    {\r
+        return new IsInstanceOfQuery2(resource, type);\r
+    }\r
+\r
+    public static <T> Read<T> getRelatedValue(Resource subject, Resource relation, Binding binding)\r
+    {\r
+        return new ReadRelatedValue<T>(subject, relation, binding);\r
+    }\r
+\r
+    public static <T> Read<T> getRelatedValue(Resource subject, String relation, Binding binding)\r
+    {\r
+        return new ReadRelatedValue2<T>(subject, relation, binding);\r
+    }\r
+\r
+    public static <T> Read<T> getPossibleRelatedValue(Resource subject, Resource relation, Binding binding)\r
+    {\r
+        return new ReadPossibleRelatedValue<T>(subject, relation, binding);\r
+    }\r
+    \r
+    public static Read<Boolean> hasTag(Resource subject, Resource tag) {\r
+        return new HasTag(subject, tag);\r
+    }\r
+\r
+    public static ObjectsWithType objectsWithType(Resource subject, Resource subrelationOf, Resource instanceOf) {\r
+        return new ObjectsWithType(subject, subrelationOf, instanceOf);\r
+    }\r
+\r
+       public static Read<Resource> possibleObjectWithType(final Resource resource, final Resource predicate, final Resource type) {\r
+               return new PossibleObjectWithType(resource, predicate, type);\r
+       }\r
+       \r
+       public static Read<Resource> possibleObject(final Resource subject, final Resource predicate) {\r
+               return new Read<Resource>() {\r
+                       @Override\r
+                       public Resource perform(ReadGraph graph) throws DatabaseException {\r
+                               return graph.getPossibleObject(subject, predicate);\r
+                       }\r
+               };              \r
+       }\r
+       \r
+       public static Read<Collection<Resource>> objects(final Resource subject, final Resource predicate) {\r
+               return new Read<Collection<Resource>>() {\r
+                       @Override\r
+                       public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {\r
+                               return graph.getObjects(subject, predicate);\r
+                       }\r
+               };              \r
+       }       \r
+       \r
+    \r
+}\r
+\r
+\r
+class IsInstanceOfQuery implements Read<Boolean> {\r
+\r
+    final protected Resource resource;\r
+    final protected Resource type;\r
+\r
+    public IsInstanceOfQuery(Resource resource, Resource type) {\r
+        this.resource = resource;\r
+        this.type = type;\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return resource.hashCode() + 13 * type.hashCode();\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object object) {\r
+        if (this == object)\r
+            return true;\r
+        else if (object == null)\r
+            return false;\r
+        else if (getClass() != object.getClass())\r
+            return false;\r
+        IsInstanceOfQuery other = (IsInstanceOfQuery) object;\r
+        return resource.equals(other.resource) && type.equals(other.type);\r
+    }\r
+\r
+    @Override\r
+    public Boolean perform(ReadGraph graph) throws DatabaseException {\r
+        return graph.isInstanceOf(resource, type);\r
+    }\r
+\r
+}\r
+\r
+class IsInstanceOfQuery2 implements Read<Boolean> {\r
+\r
+    final protected Resource resource;\r
+    final protected String type;\r
+\r
+    public IsInstanceOfQuery2(Resource resource, String type) {\r
+        this.resource = resource;\r
+        this.type = type;\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return resource.hashCode() + 13 * type.hashCode();\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object object) {\r
+        if (this == object)\r
+            return true;\r
+        else if (object == null)\r
+            return false;\r
+        else if (getClass() != object.getClass())\r
+            return false;\r
+        IsInstanceOfQuery other = (IsInstanceOfQuery) object;\r
+        return resource.equals(other.resource) && type.equals(other.type);\r
+    }\r
+\r
+    @Override\r
+    public Boolean perform(ReadGraph graph) throws DatabaseException {\r
+        Resource typeResource = graph.getResource(type);\r
+        return graph.isInstanceOf(resource, typeResource);\r
+    }\r
+\r
+}\r
+\r
+\r
+class ReadRelatedValue<T> implements Read<T> {\r
+\r
+    Resource subject;\r
+    Resource relation;\r
+    Binding binding;\r
+\r
+    public ReadRelatedValue(Resource subject, Resource relation, Binding binding) {\r
+        this.subject = subject;\r
+        this.relation = relation;\r
+        this.binding = binding;\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return subject.hashCode() + 13 * relation.hashCode() + 7*binding.hashCode();\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object object) {\r
+        if (this == object)\r
+            return true;\r
+        else if (object == null)\r
+            return false;\r
+        else if (getClass() != object.getClass())\r
+            return false;\r
+        ReadRelatedValue<?> other = (ReadRelatedValue<?>) object;\r
+        return subject.equals(other.subject) &&\r
+        relation.equals(other.relation) &&\r
+        binding.equals(other.binding);\r
+    }\r
+\r
+    @SuppressWarnings("unchecked")\r
+    @Override\r
+    public T perform(ReadGraph graph) throws DatabaseException {\r
+        return (T) graph.getRelatedValue(subject, relation, binding);\r
+    }\r
+}\r
+\r
+\r
+class ReadRelatedValue2<T> implements Read<T> {\r
+\r
+    Resource subject;\r
+    String relation;\r
+    Binding binding;\r
+\r
+    public ReadRelatedValue2(Resource subject, String relation, Binding binding) {\r
+        this.subject = subject;\r
+        this.relation = relation;\r
+        this.binding = binding;\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return subject.hashCode() + 13 * relation.hashCode() + 7*binding.hashCode();\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object object) {\r
+        if (this == object)\r
+            return true;\r
+        else if (object == null)\r
+            return false;\r
+        else if (getClass() != object.getClass())\r
+            return false;\r
+        ReadRelatedValue2<?> other = (ReadRelatedValue2<?>) object;\r
+        return subject.equals(other.subject) &&\r
+        relation.equals(other.relation) &&\r
+        binding.equals(other.binding);\r
+    }\r
+\r
+    @SuppressWarnings("unchecked")\r
+    @Override\r
+    public T perform(ReadGraph graph) throws DatabaseException {\r
+        Resource relationResource = graph.getResource(relation);\r
+        return (T) graph.getRelatedValue(subject, relationResource, binding);\r
+    }\r
+}\r
+\r
+class ReadPossibleRelatedValue<T> implements Read<T> {\r
+\r
+    Resource subject;\r
+    Resource relation;\r
+    Binding binding;\r
+\r
+    public ReadPossibleRelatedValue(Resource subject, Resource relation, Binding binding) {\r
+        this.subject = subject;\r
+        this.relation = relation;\r
+        this.binding = binding;\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return subject.hashCode() + 13 * relation.hashCode() + 7*binding.hashCode() + 5453;\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object object) {\r
+        if (this == object)\r
+            return true;\r
+        else if (object == null)\r
+            return false;\r
+        else if (getClass() != object.getClass())\r
+            return false;\r
+        ReadPossibleRelatedValue<?> other = (ReadPossibleRelatedValue<?>) object;\r
+        return subject.equals(other.subject) &&\r
+        relation.equals(other.relation) &&\r
+        binding.equals(other.binding);\r
+    }\r
+\r
+    @SuppressWarnings("unchecked")\r
+    @Override\r
+    public T perform(ReadGraph graph) throws DatabaseException {\r
+        return (T) graph.getPossibleRelatedValue(subject, relation, binding);\r
+    }\r
+}\r
+\r
+class HasTag extends ResourceRead2<Boolean> {\r
+    public HasTag(Resource subject, Resource tag) {\r
+        super(subject, tag);\r
+    }\r
+\r
+    @Override\r
+    public Boolean perform(ReadGraph graph) throws DatabaseException {\r
+        return Boolean.valueOf(graph.hasStatement(resource, resource2, resource));\r
+    }\r
+}
\ No newline at end of file