-/*******************************************************************************\r
- * Copyright (c) 2007, 2013 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.objmap.structural.rules.domain;\r
-\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-\r
-import org.apache.log4j.Logger;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.objmap.exceptions.MappingException;\r
-import org.simantics.objmap.graph.rules.domain.IDomainAccessor;\r
-import org.simantics.objmap.structural.StructuralResource;\r
-\r
-\r
-public class StructuralRelatedObjectAccessor implements IDomainAccessor<StructuralResource,StructuralResource> {\r
-\r
- static Logger LOGGER = Logger.getLogger("org.simantics.objmap");\r
- \r
- Resource relation;\r
- boolean useTypeResource;\r
- \r
- private boolean preventStructuralChanges = true;\r
- private boolean preventStructuralRootChanges = true;\r
- \r
- public StructuralRelatedObjectAccessor(Resource relation, boolean useTypeResource) {\r
- this.relation = relation;\r
- this.useTypeResource = useTypeResource;\r
- }\r
- \r
- public StructuralRelatedObjectAccessor(Resource relation, boolean useTypeResource, boolean preventStructuralChanges, boolean preventStructuralRootChanges) {\r
- this.relation = relation;\r
- this.useTypeResource = useTypeResource;\r
- this.preventStructuralChanges = preventStructuralChanges;\r
- this.preventStructuralRootChanges = preventStructuralRootChanges;\r
- }\r
- \r
- private boolean preventChange(StructuralResource element) {\r
- return preventStructuralChanges && element.isStructural() && (!element.isStructuralRoot()||preventStructuralRootChanges); \r
- }\r
-\r
- @Override\r
- public StructuralResource get(ReadGraph g, StructuralResource element) throws MappingException {\r
- try {\r
- \r
- LOGGER.info(" RelatedObjectAccessor.get");\r
- \r
- if (!element.isStructural())\r
- return null;\r
- Resource instance = StructuralUtils.getContainingInstance(element);\r
- \r
- Resource publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);\r
-\r
- if (publicRelation == null)\r
- return null;\r
- Resource r = g.getPossibleObject(instance, publicRelation);\r
- if (r == null)\r
- return null;\r
- List<Resource> context = new ArrayList<Resource>();\r
- for (int i = 0; i < element.getContext().size()-1; i++)\r
- context.add(element.getContext().get(i));\r
- if (StructuralUtils.isStructuralInstance(g, r)) {\r
- return new StructuralResource(g, r, context,r);\r
- } else {\r
- return new StructuralResource(g, r, context);\r
- }\r
- } catch (DatabaseException e) {\r
- throw new MappingException(e);\r
- }\r
- }\r
- \r
- @Override\r
- public boolean set(WriteGraph g, StructuralResource element, StructuralResource value)\r
- throws MappingException {\r
- try {\r
- LOGGER.info(" RelatedObjectAccessor.set");\r
- Resource instance = StructuralUtils.getContainingInstance(element);\r
- Resource publicRelation = null;\r
- if (instance == null)\r
- return false;\r
- publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);\r
- if (value == null) {\r
- if (publicRelation == null)\r
- return false;\r
- if (preventChange(element))\r
- return false;\r
- g.deny(instance, publicRelation);\r
- return true;\r
- } else {\r
- if (publicRelation == null) {\r
- if (preventChange(element))\r
- return false;\r
- publicRelation = StructuralUtils.getOrCreatePublishedRelation(g, element, relation);\r
- g.claim(instance, publicRelation, value.getResource());\r
- return true;\r
- } else {\r
- Resource r = g.getPossibleObject(instance, publicRelation);\r
- if (r == null) {\r
- if (preventChange(element))\r
- return false;\r
- g.claim(instance, publicRelation, value.getResource());\r
- return true;\r
- } else {\r
- if (r.equals(value.getResource()))\r
- return false;\r
- if (preventChange(element))\r
- return false;\r
- g.deny(instance, publicRelation);\r
- g.claim(instance, publicRelation, value.getResource());\r
- return true;\r
- }\r
- }\r
- }\r
- } catch (DatabaseException e) {\r
- throw new MappingException(e);\r
- }\r
- \r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2013 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.objmap.structural.rules.domain;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.objmap.exceptions.MappingException;
+import org.simantics.objmap.graph.rules.domain.IDomainAccessor;
+import org.simantics.objmap.structural.StructuralResource;
+
+
+public class StructuralRelatedObjectAccessor implements IDomainAccessor<StructuralResource,StructuralResource> {
+
+ static Logger LOGGER = LoggerFactory.getLogger(StructuralRelatedObjectAccessor.class);
+
+ Resource relation;
+ boolean useTypeResource;
+
+ private boolean preventStructuralChanges = true;
+ private boolean preventStructuralRootChanges = true;
+
+ public StructuralRelatedObjectAccessor(Resource relation, boolean useTypeResource) {
+ this.relation = relation;
+ this.useTypeResource = useTypeResource;
+ }
+
+ public StructuralRelatedObjectAccessor(Resource relation, boolean useTypeResource, boolean preventStructuralChanges, boolean preventStructuralRootChanges) {
+ this.relation = relation;
+ this.useTypeResource = useTypeResource;
+ this.preventStructuralChanges = preventStructuralChanges;
+ this.preventStructuralRootChanges = preventStructuralRootChanges;
+ }
+
+ private boolean preventChange(StructuralResource element) {
+ return preventStructuralChanges && element.isStructural() && (!element.isStructuralRoot()||preventStructuralRootChanges);
+ }
+
+ @Override
+ public StructuralResource get(ReadGraph g, StructuralResource element) throws MappingException {
+ try {
+
+ LOGGER.trace(" RelatedObjectAccessor.get");
+
+ if (!element.isStructural())
+ return null;
+ Resource instance = StructuralUtils.getContainingInstance(element);
+
+ Resource publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);
+
+ if (publicRelation == null)
+ return null;
+ Resource r = g.getPossibleObject(instance, publicRelation);
+ if (r == null)
+ return null;
+ List<Resource> context = new ArrayList<Resource>();
+ for (int i = 0; i < element.getContext().size()-1; i++)
+ context.add(element.getContext().get(i));
+ if (StructuralUtils.isStructuralInstance(g, r)) {
+ return new StructuralResource(g, r, context,r);
+ } else {
+ return new StructuralResource(g, r, context);
+ }
+ } catch (DatabaseException e) {
+ throw new MappingException(e);
+ }
+ }
+
+ @Override
+ public boolean set(WriteGraph g, StructuralResource element, StructuralResource value)
+ throws MappingException {
+ try {
+ LOGGER.trace(" RelatedObjectAccessor.set");
+ Resource instance = StructuralUtils.getContainingInstance(element);
+ Resource publicRelation = null;
+ if (instance == null)
+ return false;
+ publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);
+ if (value == null) {
+ if (publicRelation == null)
+ return false;
+ if (preventChange(element))
+ return false;
+ g.deny(instance, publicRelation);
+ return true;
+ } else {
+ if (publicRelation == null) {
+ if (preventChange(element))
+ return false;
+ publicRelation = StructuralUtils.getOrCreatePublishedRelation(g, element, relation);
+ g.claim(instance, publicRelation, value.getResource());
+ return true;
+ } else {
+ Resource r = g.getPossibleObject(instance, publicRelation);
+ if (r == null) {
+ if (preventChange(element))
+ return false;
+ g.claim(instance, publicRelation, value.getResource());
+ return true;
+ } else {
+ if (r.equals(value.getResource()))
+ return false;
+ if (preventChange(element))
+ return false;
+ g.deny(instance, publicRelation);
+ g.claim(instance, publicRelation, value.getResource());
+ return true;
+ }
+ }
+ }
+ } catch (DatabaseException e) {
+ throw new MappingException(e);
+ }
+
+ }
+
+}