--- /dev/null
+/*******************************************************************************\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.Collection;\r
+import java.util.Collections;\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.graph.rules.domain.MappingUtils;\r
+import org.simantics.objmap.structural.StructuralResource;\r
+\r
+\r
+public class StructuralRelatedObjectsAccessor implements IDomainAccessor<StructuralResource,Collection<StructuralResource>> {\r
+\r
+ static Logger LOGGER = Logger.getLogger("org.simantics.objmap");\r
+ \r
+ Resource relation;\r
+ boolean deleteExtraObjects;\r
+\r
+ public StructuralRelatedObjectsAccessor(Resource relation, boolean deleteExtraObjects) {\r
+ super();\r
+ this.relation = relation;\r
+ this.deleteExtraObjects = deleteExtraObjects;\r
+ }\r
+\r
+ @Override\r
+ public Collection<StructuralResource> get(ReadGraph g, StructuralResource element) throws MappingException {\r
+ try {\r
+ LOGGER.info(" RelatedObjectsAccessor.get");\r
+ \r
+ if (!element.isStructural())\r
+ return Collections.emptyList();\r
+ \r
+ // Structural instance\r
+ Resource instance = StructuralUtils.getContainingInstance(element);\r
+ \r
+ Resource publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);\r
+ \r
+ if (publicRelation == null)\r
+ return Collections.emptyList();\r
+ \r
+ Collection<Resource> coll = g.getObjects(instance, publicRelation);\r
+ List<StructuralResource> result = new ArrayList<StructuralResource>(coll.size());\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
+ for (Resource r : coll) {\r
+ if (StructuralUtils.isStructuralInstance(g, r)) {\r
+ result.add(new StructuralResource(g, r, context,r));\r
+ } else {\r
+ result.add(new StructuralResource(g, r, context));\r
+ }\r
+ }\r
+ return result;\r
+ } catch (DatabaseException e) {\r
+ throw new MappingException(e);\r
+ }\r
+ }\r
+ \r
+ @Override\r
+ public boolean set(WriteGraph g, StructuralResource element, Collection<StructuralResource> value)\r
+ throws MappingException {\r
+ try {\r
+ LOGGER.info(" RelatedObjectsAccessor.set");\r
+ \r
+ if (!element.isStructural())\r
+ return false;\r
+ \r
+ Resource instance = StructuralUtils.getContainingInstance(element);\r
+ Resource publicRelation = null;\r
+ if (value.size() == 0) {\r
+ publicRelation = StructuralUtils.getPublishedRelation(g, element, relation);\r
+ if (publicRelation == null)\r
+ return false;\r
+ else {\r
+ return MappingUtils.synchronizeStatements(g, instance, publicRelation, new Resource[0], deleteExtraObjects);\r
+ }\r
+ } else {\r
+ publicRelation = StructuralUtils.getOrCreatePublishedRelation(g, element, relation);\r
+ if (publicRelation == null)\r
+ throw new MappingException("Structural Resource " + element + " cannot contain structural elements, the Resource is not published.");\r
+ Resource[] arr = new Resource[value.size()];\r
+ int i = 0; \r
+ for (StructuralResource sr : value) {\r
+ arr[i++] = sr.getResource();\r
+ }\r
+ return MappingUtils.synchronizeStatements(g, instance, publicRelation, arr, deleteExtraObjects);\r
+ }\r
+ \r
+ \r
+ \r
+ } catch (DatabaseException e) {\r
+ throw new MappingException(e);\r
+ }\r
+ \r
+ }\r
+\r
+ \r
+}\r