--- /dev/null
+/*******************************************************************************\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.ui.workbench.editor.input;\r
+\r
+import java.util.Arrays;\r
+\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.Queries;\r
+import org.simantics.db.common.request.UnaryRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.request.combinations.Combinators;\r
+import org.simantics.db.layer0.request.combinations.Combinators.SynchronizationProcedure;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.ui.workbench.IResourceEditorInput;\r
+\r
+/**\r
+ * Composable database read operations designed to be used for defining\r
+ * validation strategies for resource editor inputs through composition.\r
+ * \r
+ * <p>\r
+ * For example one validation criterion might be that the editor input or one of\r
+ * its neighboring resources must have a proper URI.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public final class InputValidationCombinators {\r
+\r
+ /**\r
+ * Returns a function:\r
+ * <pre>\r
+ * = subject -> exists(URI(subject))\r
+ * | null -> false\r
+ * </pre>.\r
+ */\r
+ public static ParametrizedRead<Resource, Boolean> hasURI() {\r
+ return HAS_URI;\r
+ }\r
+\r
+ /**\r
+ * Returns a function:\r
+ * <pre>\r
+ * = input -> resource(input)\r
+ * | null -> null\r
+ * </pre>.\r
+ */\r
+ public static ParametrizedRead<IResourceEditorInput, Resource> extractInputResource() {\r
+ return EXTRACT_INPUT_RESOURCE;\r
+ }\r
+\r
+ /**\r
+ * Returns a function:\r
+ * <pre>\r
+ * = subject -> singleObject(subject, resource(relationURI))\r
+ * | null -> null\r
+ * </pre>.\r
+ */\r
+ public static ParametrizedRead<Resource,Resource> completeFunction(String relationURI) {\r
+ return new CompleteFunctionURI(relationURI);\r
+ }\r
+\r
+ /**\r
+ * Returns a function:\r
+ * <pre>\r
+ * = subject -> possibleObject(subject, resource(relationURI))\r
+ * | null -> null\r
+ * </pre>.\r
+ */\r
+ public static ParametrizedRead<Resource,Resource> partialFunction(String relationURI) {\r
+ return new PartialFunctionURI(relationURI);\r
+ }\r
+\r
+ /**\r
+ * Returns a function:\r
+ * <pre>\r
+ * = subject -> true if any of conditions is true for subject, false otherwise\r
+ * | null -> true\r
+ * </pre>.\r
+ */\r
+ @SuppressWarnings("unchecked")\r
+ public static ParametrizedRead<Resource, Boolean> or(ParametrizedRead<Resource, Boolean> c1,\r
+ ParametrizedRead<Resource, Boolean> c2) {\r
+ return new FunctionOr(new ParametrizedRead[] { c1, c2 });\r
+ }\r
+\r
+ // ------------------------------------------------------------------------\r
+\r
+ private static class Or extends UnaryRead<Resource, Boolean> {\r
+ ParametrizedRead<Resource, Boolean>[] reads;\r
+ public Or(Resource resource, ParametrizedRead<Resource, Boolean>... reads) {\r
+ super(resource);\r
+ this.reads = reads;\r
+ }\r
+ @Override\r
+ public Boolean perform(ReadGraph graph) throws DatabaseException {\r
+ for (ParametrizedRead<Resource, Boolean> r : reads) {\r
+ Read<Boolean> read = r.get(parameter);\r
+ Boolean value = graph.syncRequest( read );\r
+ if (value)\r
+ return Boolean.TRUE;\r
+ }\r
+ return Boolean.FALSE;\r
+ }\r
+ @Override\r
+ public int hashCode() {\r
+ return super.hashCode() * 31 + Arrays.hashCode(reads);\r
+ }\r
+ @Override\r
+ public boolean equals(Object object) {\r
+ if (this == object) return true;\r
+ else if (object == null || getClass() != object.getClass()) return false;\r
+ Or other = (Or) object;\r
+ return super.equals(object) && Arrays.equals(reads, other.reads);\r
+ }\r
+ }\r
+\r
+ // ------------------------------------------------------------------------\r
+\r
+ private static class FunctionOr implements ParametrizedRead<Resource, Boolean> {\r
+ ParametrizedRead<Resource, Boolean>[] reads;\r
+ public FunctionOr(ParametrizedRead<Resource, Boolean>... reads) {\r
+ this.reads = reads;\r
+ }\r
+ @Override\r
+ public Read<Boolean> get(Resource subject) {\r
+ if (subject == null || reads.length == 0)\r
+ return Combinators.constant(Boolean.TRUE);\r
+ return new Or(subject, reads);\r
+ }\r
+ @Override\r
+ public int hashCode() {\r
+ return getClass().hashCode() + 31 * Arrays.hashCode(reads);\r
+ }\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if(obj == this) return true;\r
+ if(obj == null || obj.getClass() != getClass()) return false;\r
+ FunctionOr other = (FunctionOr)obj;\r
+ return Arrays.equals(reads, other.reads);\r
+ }\r
+ }\r
+\r
+ // ------------------------------------------------------------------------\r
+\r
+ private static class HasURI extends UnaryRead<Resource, Boolean> {\r
+ public HasURI(Resource resource) {\r
+ super(resource);\r
+ }\r
+ @Override\r
+ public Boolean perform(ReadGraph graph) throws DatabaseException {\r
+ if (parameter == null)\r
+ return Boolean.FALSE;\r
+\r
+ String uri = graph.syncRequest(Queries.possibleUri(parameter));\r
+ //System.out.println("uri(" + parameter + "): " + uri);\r
+ if (uri == null)\r
+ return Boolean.FALSE;\r
+\r
+ // FIXME: URI request will return invalid URIs, like\r
+ // null/Configuration/MyComposite after deleting the parenting model\r
+ // For this reason we try to reverse lookup the URI back into a\r
+ // resource which must be equal to the original parameter resource.\r
+\r
+ Resource reverseLookup = graph.getPossibleResource(uri);\r
+ //System.out.println("resource(" + uri + "): " + reverseLookup);\r
+ return parameter.equals(reverseLookup);\r
+ }\r
+ }\r
+\r
+ public static Read<Boolean> hasURI(Resource resource) {\r
+ return new HasURI(resource);\r
+ }\r
+\r
+ // ------------------------------------------------------------------------\r
+\r
+ private static class ExtractResource extends UnaryRead<IResourceEditorInput, Resource> {\r
+ public ExtractResource(IResourceEditorInput input) {\r
+ super(input);\r
+ }\r
+ @Override\r
+ public Resource perform(ReadGraph graph) throws DatabaseException {\r
+ return parameter != null ? parameter.getResource() : null;\r
+ }\r
+ }\r
+\r
+ public static Read<Resource> extractResource(IResourceEditorInput input) {\r
+ return new ExtractResource(input);\r
+ }\r
+\r
+ // ------------------------------------------------------------------------\r
+\r
+ private static class PossibleObjectURI implements Read<Resource> {\r
+ Resource subject;\r
+ String relationURI;\r
+ public PossibleObjectURI(Resource subject, String relationURI) {\r
+ this.subject = subject;\r
+ this.relationURI = relationURI;\r
+ }\r
+ @Override\r
+ public Resource perform(ReadGraph graph) throws DatabaseException {\r
+ SynchronizationProcedure<Resource> procedure = new SynchronizationProcedure<Resource>();\r
+ Resource relation = graph.getResource(relationURI);\r
+ graph.forPossibleObject(subject, relation, procedure);\r
+ return procedure.getResult();\r
+ }\r
+ @Override\r
+ public int hashCode() {\r
+ return subject.hashCode() + 31 * relationURI.hashCode();\r
+ }\r
+ @Override\r
+ public boolean equals(Object object) {\r
+ if (this == object) return true;\r
+ else if (object == null || getClass() != object.getClass()) return false;\r
+ PossibleObjectURI other = (PossibleObjectURI)object;\r
+ return subject.equals(other.subject) && relationURI.equals(other.relationURI);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Returns a read request that reads an object possibly connected to the subject by the relation.\r
+ */\r
+ public static Read<Resource> possibleObject(Resource subject, String relationURI) {\r
+ return new PossibleObjectURI(subject, relationURI);\r
+ }\r
+\r
+ // ------------------------------------------------------------------------\r
+\r
+ private static class PartialFunctionURI implements ParametrizedRead<Resource, Resource> {\r
+ String relationURI;\r
+ public PartialFunctionURI(String relationURI) {\r
+ this.relationURI = relationURI;\r
+ }\r
+ @Override\r
+ public Read<Resource> get(Resource subject) {\r
+ if (subject == null)\r
+ return Combinators.constant(null);\r
+ return possibleObject(subject, relationURI);\r
+ }\r
+ @Override\r
+ public int hashCode() {\r
+ return getClass().hashCode() + 31 * relationURI.hashCode();\r
+ }\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if(obj == this) return true;\r
+ if(obj == null || obj.getClass() != getClass()) return false;\r
+ PartialFunctionURI other = (PartialFunctionURI)obj;\r
+ return relationURI.equals(other.relationURI);\r
+ }\r
+ }\r
+\r
+ // ------------------------------------------------------------------------\r
+\r
+ private static class SingleObjectURI implements Read<Resource> {\r
+ Resource subject;\r
+ String relationURI;\r
+ public SingleObjectURI(Resource subject, String relationURI) {\r
+ this.subject = subject;\r
+ this.relationURI = relationURI;\r
+ }\r
+ @Override\r
+ public Resource perform(ReadGraph graph) throws DatabaseException {\r
+ SynchronizationProcedure<Resource> procedure = new SynchronizationProcedure<Resource>();\r
+ Resource relation = graph.getResource(relationURI);\r
+ graph.forSingleObject(subject, relation, procedure);\r
+ return procedure.getResult();\r
+ }\r
+ @Override\r
+ public int hashCode() {\r
+ return subject.hashCode() + 31 * relationURI.hashCode();\r
+ }\r
+ @Override\r
+ public boolean equals(Object object) {\r
+ if (this == object) return true;\r
+ else if (object == null || getClass() != object.getClass()) return false;\r
+ SingleObjectURI other = (SingleObjectURI) object;\r
+ return subject.equals(other.subject) && relationURI.equals(other.relationURI);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Returns a read request that reads a single object connected to the\r
+ * subject by the relation.\r
+ */\r
+ public static Read<Resource> singleObject(Resource subject, String relationURI) {\r
+ return new SingleObjectURI(subject, relationURI);\r
+ }\r
+\r
+ // ------------------------------------------------------------------------\r
+\r
+ private static class CompleteFunctionURI implements ParametrizedRead<Resource, Resource> {\r
+ String relationURI;\r
+ public CompleteFunctionURI(String relationURI) {\r
+ this.relationURI = relationURI;\r
+ }\r
+ @Override\r
+ public Read<Resource> get(Resource subject) {\r
+ if (subject == null)\r
+ return Combinators.constant(null);\r
+ return singleObject(subject, relationURI);\r
+ }\r
+ @Override\r
+ public int hashCode() {\r
+ return getClass().hashCode() + 31 * relationURI.hashCode();\r
+ }\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if(obj == this) return true;\r
+ if(obj == null || obj.getClass() != getClass()) return false;\r
+ CompleteFunctionURI other = (CompleteFunctionURI)obj;\r
+ return relationURI.equals(other.relationURI);\r
+ }\r
+ }\r
+\r
+ // ------------------------------------------------------------------------\r
+\r
+ private static final ParametrizedRead<IResourceEditorInput, Resource> EXTRACT_INPUT_RESOURCE = new ParametrizedRead<IResourceEditorInput, Resource>() {\r
+ @Override\r
+ public Read<Resource> get(IResourceEditorInput input) {\r
+ return extractResource(input);\r
+ }\r
+ };\r
+\r
+ // ------------------------------------------------------------------------\r
+\r
+ private static final ParametrizedRead<Resource, Boolean> HAS_URI = new ParametrizedRead<Resource, Boolean>() {\r
+ @Override\r
+ public Read<Boolean> get(Resource resource) {\r
+ return hasURI(resource);\r
+ }\r
+ };\r
+\r
+}\r