-/*******************************************************************************\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 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 RelatedObjectAccessor 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 RelatedObjectAccessor(Resource relation, boolean useTypeResource) {\r
- this.relation = relation;\r
- this.useTypeResource = useTypeResource;\r
- }\r
- \r
- public RelatedObjectAccessor(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
- LOGGER.info(" RelatedObjectAccessor.get");\r
- Resource res = getServiceResource(g, element);\r
- if (res == null)\r
- return null;\r
- Resource r = g.getPossibleObject(res, relation);\r
- if (r == null)\r
- return null;\r
- if (StructuralUtils.isStructuralInstance(g, r)) {\r
- return new StructuralResource(g, r, element.getContext(),r);\r
- } else {\r
- return new StructuralResource(g, r, element.getContext());\r
- }\r
- } catch (DatabaseException e) {\r
- throw new MappingException(e);\r
- }\r
- }\r
- \r
- @Override\r
- public boolean set(WriteGraph g, StructuralResource selement, StructuralResource value)\r
- throws MappingException {\r
- try {\r
- LOGGER.info(" RelatedObjectAccessor.set");\r
- Resource element = getServiceResource(g, selement);\r
- if (element == null)\r
- return false;\r
- Resource resource = g.getPossibleObject(element, relation);\r
- if(resource == null) {\r
- if(value == null)\r
- return false;\r
- if (preventChange(selement))\r
- return false;\r
- g.claim(element, relation, value.getResource());\r
- return true;\r
- }\r
- else if(resource.equals(value))\r
- return false;\r
- else {\r
- if (preventChange(selement))\r
- return false;\r
- g.deny(element, relation);\r
- if(value != null)\r
- g.claim(element, relation, value.getResource());\r
- return true;\r
- }\r
- } catch (DatabaseException e) {\r
- throw new MappingException(e);\r
- }\r
- \r
- }\r
- \r
- \r
- \r
- private Resource getServiceResource(ReadGraph g, StructuralResource element) {\r
- if(!useTypeResource)\r
- return element.getResource();\r
- return element.getTypeResource();\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 org.apache.log4j.Logger;
+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 RelatedObjectAccessor implements IDomainAccessor<StructuralResource,StructuralResource> {
+
+ static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
+
+ Resource relation;
+ boolean useTypeResource;
+
+ private boolean preventStructuralChanges = true;
+ private boolean preventStructuralRootChanges = true;
+
+ public RelatedObjectAccessor(Resource relation, boolean useTypeResource) {
+ this.relation = relation;
+ this.useTypeResource = useTypeResource;
+ }
+
+ public RelatedObjectAccessor(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.info(" RelatedObjectAccessor.get");
+ Resource res = getServiceResource(g, element);
+ if (res == null)
+ return null;
+ Resource r = g.getPossibleObject(res, relation);
+ if (r == null)
+ return null;
+ if (StructuralUtils.isStructuralInstance(g, r)) {
+ return new StructuralResource(g, r, element.getContext(),r);
+ } else {
+ return new StructuralResource(g, r, element.getContext());
+ }
+ } catch (DatabaseException e) {
+ throw new MappingException(e);
+ }
+ }
+
+ @Override
+ public boolean set(WriteGraph g, StructuralResource selement, StructuralResource value)
+ throws MappingException {
+ try {
+ LOGGER.info(" RelatedObjectAccessor.set");
+ Resource element = getServiceResource(g, selement);
+ if (element == null)
+ return false;
+ Resource resource = g.getPossibleObject(element, relation);
+ if(resource == null) {
+ if(value == null)
+ return false;
+ if (preventChange(selement))
+ return false;
+ g.claim(element, relation, value.getResource());
+ return true;
+ }
+ else if(resource.equals(value.getResource()))
+ return false;
+ else {
+ if (preventChange(selement))
+ return false;
+ g.deny(element, relation);
+ if(value != null)
+ g.claim(element, relation, value.getResource());
+ return true;
+ }
+ } catch (DatabaseException e) {
+ throw new MappingException(e);
+ }
+
+ }
+
+
+
+ private Resource getServiceResource(ReadGraph g, StructuralResource element) {
+ if(!useTypeResource)
+ return element.getResource();
+ return element.getTypeResource();
+ }
+
+}