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