1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.common.request;
14 import java.util.Collection;
16 import org.simantics.databoard.binding.Binding;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.uri.ResourceToPossibleURI;
20 import org.simantics.db.common.uri.ResourceToURI;
21 import org.simantics.db.common.utils.NameUtils;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.request.AsyncRead;
24 import org.simantics.db.request.Read;
25 import org.simantics.layer0.Layer0;
28 * Facade class that aggregates the most common queries.
32 * System.out.println( session.read( Queries.adapt(r, String.class) ) );
34 * @author Tuukka Lehtonen
35 * @author Toni Kalajainen
37 public class Queries {
39 public static AsyncRead<Resource> resource(String uri) {
40 return new org.simantics.db.common.primitiverequest.Resource(uri);
43 public static Read<String> name(final Resource resource) {
44 return new ResourceRead<String>(resource) {
46 public String perform(ReadGraph graph) throws DatabaseException {
47 return graph.getRelatedValue(resource, Layer0.getInstance(graph).HasName);
52 public static Read<String> safeName(final Resource resource) {
53 return new ResourceRead<String>(resource) {
55 public String perform(ReadGraph graph) throws DatabaseException {
56 return NameUtils.getSafeName(graph, resource);
61 public static Read<String> name(final String uri) {
62 return new UnaryRead<String, String>(uri) {
64 public String perform(ReadGraph graph) throws DatabaseException {
65 Resource resource = graph.getResource(uri);
66 return graph.getRelatedValue(resource, Layer0.getInstance(graph).HasName);
71 public static Read<String> uri(Resource resource) {
72 return new ResourceToURI(resource);
75 public static Read<String> possibleUri(Resource resource) {
76 return new ResourceToPossibleURI(resource);
79 public static <T> Read<T> adapt(Resource resource, Class<T> target)
81 return new Adapt<T>(resource, target);
84 public static <T> Read<T> adapt(Resource resource, Class<T> target, boolean allowNull)
86 return new Adapt<T>(resource, target, allowNull);
89 public static <T> Read<T> adapt(Resource resource, Class<T> target, boolean allowNull, boolean uniqueResult)
91 return new Adapt<T>(resource, target, allowNull, uniqueResult);
94 public static Read<Boolean> isInstanceOf(Resource resource, Resource type)
96 return new IsInstanceOfQuery(resource, type);
99 public static Read<Boolean> isInstanceOf(Resource resource, String type)
101 return new IsInstanceOfQuery2(resource, type);
104 public static <T> Read<T> getRelatedValue(Resource subject, Resource relation, Binding binding)
106 return new ReadRelatedValue<T>(subject, relation, binding);
109 public static <T> Read<T> getRelatedValue(Resource subject, String relation, Binding binding)
111 return new ReadRelatedValue2<T>(subject, relation, binding);
114 public static <T> Read<T> getPossibleRelatedValue(Resource subject, Resource relation, Binding binding)
116 return new ReadPossibleRelatedValue<T>(subject, relation, binding);
119 public static Read<Boolean> hasTag(Resource subject, Resource tag) {
120 return new HasTag(subject, tag);
123 public static ObjectsWithType objectsWithType(Resource subject, Resource subrelationOf, Resource instanceOf) {
124 return new ObjectsWithType(subject, subrelationOf, instanceOf);
127 public static Read<Resource> possibleObjectWithType(final Resource resource, final Resource predicate, final Resource type) {
128 return new PossibleObjectWithType(resource, predicate, type);
131 public static Read<Resource> possibleObject(final Resource subject, final Resource predicate) {
132 return new Read<Resource>() {
134 public Resource perform(ReadGraph graph) throws DatabaseException {
135 return graph.getPossibleObject(subject, predicate);
140 public static Read<Collection<Resource>> objects(final Resource subject, final Resource predicate) {
141 return new Read<Collection<Resource>>() {
143 public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
144 return graph.getObjects(subject, predicate);
153 class IsInstanceOfQuery implements Read<Boolean> {
155 final protected Resource resource;
156 final protected Resource type;
158 public IsInstanceOfQuery(Resource resource, Resource type) {
159 this.resource = resource;
164 public int hashCode() {
165 return resource.hashCode() + 13 * type.hashCode();
169 public boolean equals(Object object) {
172 else if (object == null)
174 else if (getClass() != object.getClass())
176 IsInstanceOfQuery other = (IsInstanceOfQuery) object;
177 return resource.equals(other.resource) && type.equals(other.type);
181 public Boolean perform(ReadGraph graph) throws DatabaseException {
182 return graph.isInstanceOf(resource, type);
187 class IsInstanceOfQuery2 implements Read<Boolean> {
189 final protected Resource resource;
190 final protected String type;
192 public IsInstanceOfQuery2(Resource resource, String type) {
193 this.resource = resource;
198 public int hashCode() {
199 return resource.hashCode() + 13 * type.hashCode();
203 public boolean equals(Object object) {
206 else if (object == null)
208 else if (getClass() != object.getClass())
210 IsInstanceOfQuery2 other = (IsInstanceOfQuery2) object;
211 return resource.equals(other.resource) && type.equals(other.type);
215 public Boolean perform(ReadGraph graph) throws DatabaseException {
216 Resource typeResource = graph.getResource(type);
217 return graph.isInstanceOf(resource, typeResource);
223 class ReadRelatedValue<T> implements Read<T> {
229 public ReadRelatedValue(Resource subject, Resource relation, Binding binding) {
230 this.subject = subject;
231 this.relation = relation;
232 this.binding = binding;
236 public int hashCode() {
237 return subject.hashCode() + 13 * relation.hashCode() + 7*binding.hashCode();
241 public boolean equals(Object object) {
244 else if (object == null)
246 else if (getClass() != object.getClass())
248 ReadRelatedValue<?> other = (ReadRelatedValue<?>) object;
249 return subject.equals(other.subject) &&
250 relation.equals(other.relation) &&
251 binding.equals(other.binding);
254 @SuppressWarnings("unchecked")
256 public T perform(ReadGraph graph) throws DatabaseException {
257 return (T) graph.getRelatedValue(subject, relation, binding);
262 class ReadRelatedValue2<T> implements Read<T> {
268 public ReadRelatedValue2(Resource subject, String relation, Binding binding) {
269 this.subject = subject;
270 this.relation = relation;
271 this.binding = binding;
275 public int hashCode() {
276 return subject.hashCode() + 13 * relation.hashCode() + 7*binding.hashCode();
280 public boolean equals(Object object) {
283 else if (object == null)
285 else if (getClass() != object.getClass())
287 ReadRelatedValue2<?> other = (ReadRelatedValue2<?>) object;
288 return subject.equals(other.subject) &&
289 relation.equals(other.relation) &&
290 binding.equals(other.binding);
293 @SuppressWarnings("unchecked")
295 public T perform(ReadGraph graph) throws DatabaseException {
296 Resource relationResource = graph.getResource(relation);
297 return (T) graph.getRelatedValue(subject, relationResource, binding);
301 class ReadPossibleRelatedValue<T> implements Read<T> {
307 public ReadPossibleRelatedValue(Resource subject, Resource relation, Binding binding) {
308 this.subject = subject;
309 this.relation = relation;
310 this.binding = binding;
314 public int hashCode() {
315 return subject.hashCode() + 13 * relation.hashCode() + 7*binding.hashCode() + 5453;
319 public boolean equals(Object object) {
322 else if (object == null)
324 else if (getClass() != object.getClass())
326 ReadPossibleRelatedValue<?> other = (ReadPossibleRelatedValue<?>) object;
327 return subject.equals(other.subject) &&
328 relation.equals(other.relation) &&
329 binding.equals(other.binding);
332 @SuppressWarnings("unchecked")
334 public T perform(ReadGraph graph) throws DatabaseException {
335 return (T) graph.getPossibleRelatedValue(subject, relation, binding);
339 class HasTag extends ResourceRead2<Boolean> {
340 public HasTag(Resource subject, Resource tag) {
345 public Boolean perform(ReadGraph graph) throws DatabaseException {
346 return Boolean.valueOf(graph.hasStatement(resource, resource2, resource));