+/*******************************************************************************\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.schema;\r
+\r
+import java.util.ArrayList;\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.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.objmap.backward.IBackwardMapping;\r
+import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;\r
+import org.simantics.objmap.exceptions.MappingException;\r
+import org.simantics.objmap.forward.IForwardMapping;\r
+import org.simantics.objmap.graph.schema.ILinkType;\r
+import org.simantics.objmap.structural.IStructuralObject;\r
+import org.simantics.objmap.structural.StructuralResource;\r
+\r
+\r
+\r
+public class SimpleLinkType implements ILinkType<StructuralResource,IStructuralObject> {\r
+ \r
+ static Logger LOGGER = Logger.getLogger("org.simantics.objmap");\r
+ \r
+ public Resource domainType;\r
+ public Class<?> rangeType;\r
+ ArrayList<IBidirectionalMappingRule<StructuralResource,IStructuralObject>> rules;\r
+ \r
+ public SimpleLinkType(Resource domainType, Class<?> rangeType,\r
+ ArrayList<IBidirectionalMappingRule<StructuralResource,IStructuralObject>> rules) {\r
+ this.domainType = domainType;\r
+ this.rangeType = rangeType;\r
+ this.rules = rules;\r
+ }\r
+\r
+ public SimpleLinkType(Resource domainType, Class<?> rangeType) {\r
+ this(domainType, rangeType, new ArrayList<IBidirectionalMappingRule<StructuralResource,IStructuralObject>>());\r
+ }\r
+\r
+ /**\r
+ * Adds a new rule to this link type that is enforced\r
+ * during updates.\r
+ */\r
+ public void addRule(IBidirectionalMappingRule<StructuralResource,IStructuralObject> rule) {\r
+ rules.add(rule);\r
+ }\r
+ \r
+ @Override\r
+ public StructuralResource createDomainElement(WriteGraph g, IStructuralObject rangeElement)\r
+ throws MappingException {\r
+ try {\r
+ if(LOGGER.isInfoEnabled())\r
+ LOGGER.info("SimpleLinkType.createDomainElement " +\r
+ rangeElement.toString()\r
+ );\r
+ if (rangeElement.getContext().size() == 0) {\r
+ // there is no context, this not a structural resource / object.\r
+ Resource result = newResource(g, domainType);\r
+ return new StructuralResource(g,result);\r
+ } else {\r
+ if (rangeElement.getContext().size() == 1 && rangeElement.getContext().get(0).equals(rangeElement)) {\r
+ // Structural object's context is itself, we are instantiating a new structural model.\r
+ Resource type = rangeElement.getType();\r
+ Resource result = newResource(g, type);\r
+ return new StructuralResource(g,result,result);\r
+ } else {\r
+ // Structural object's context is not itself, which means that the object is inside of a structural model.\r
+ // At the moment we do not support modifying instantiated structural models.\r
+ throw new MappingException("Cannot create a new StucturalObject " + rangeElement + " " + rangeElement.getClass());\r
+ }\r
+ }\r
+ } catch(DatabaseException e) {\r
+ throw new MappingException(e);\r
+ }\r
+ }\r
+ \r
+ protected Resource newResource(WriteGraph g, Resource type) throws DatabaseException {\r
+ Resource result = g.newResource();\r
+ g.claim(result, Layer0.getInstance(g).InstanceOf, null, type);\r
+ return result;\r
+ }\r
+ @Override\r
+ public IStructuralObject createRangeElement(ReadGraph g, StructuralResource domainElement)\r
+ throws MappingException {\r
+ try {\r
+ if(LOGGER.isInfoEnabled())\r
+ try { \r
+ LOGGER.info("SimpleLinkType.createRangeElement " + NameUtils.getSafeName(g, domainElement.getResource()));\r
+ } catch(DatabaseException e) {\r
+ throw new MappingException(e);\r
+ }\r
+ IStructuralObject result = (IStructuralObject)rangeType.newInstance();\r
+ if (domainElement.getContext().size() == 1) {\r
+ if (domainElement.getContext().get(0).equals(domainElement.getResource()))\r
+ result.setContext(Collections.singletonList(result));\r
+ else {\r
+ //result.setContext(result); \r
+ }\r
+ }\r
+ return result;\r
+ } catch (InstantiationException e) {\r
+ throw new MappingException(e);\r
+ } catch (IllegalAccessException e) {\r
+ throw new MappingException(e);\r
+ }\r
+ }\r
+ \r
+ @SuppressWarnings("unchecked")\r
+ public void createDomain(WriteGraph graph, IBackwardMapping<StructuralResource,IStructuralObject> mapping, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {\r
+ if (domainElement.isStructuralRoot())\r
+ // FIXME: this is nasty, but when a structural model is instantiated by creating new IStructuralObject, its related objects must be read from the graph first, or otherwise the objects would be deleted from the graph.\r
+ // as a side effect, if the new IStructuralObject has any properties set, those properties are set to null (because the graph does not contain those values).\r
+ // \r
+ updateRange(graph, (IForwardMapping<StructuralResource, IStructuralObject>)mapping, domainElement, rangeElement);\r
+ updateDomain(graph, mapping, domainElement, rangeElement);\r
+ };\r
+ \r
+ \r
+ @Override\r
+ public void createRange(ReadGraph graph, IForwardMapping<StructuralResource, IStructuralObject> mapping, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {\r
+ if (rangeElement.getContext().size() == 0 && domainElement.getContext().size() > 0) {\r
+ List<IStructuralObject> ctx = new ArrayList<IStructuralObject>(domainElement.getContext().size());\r
+ try {\r
+ List<Resource> context = new ArrayList<Resource>();\r
+ for (int i = 0; i <domainElement.getContext().size(); i++) {\r
+ context.add(domainElement.getContext().get(i));\r
+ IStructuralObject ctxObj = mapping.get(new StructuralResource(graph,context.get(context.size()-1),context));\r
+ if (ctxObj == null) throw new MappingException("Cannot resolve range context for domain element " + domainElement);\r
+ ctx.add(ctxObj);\r
+ }\r
+ //ctx = mapping.get(new StructuralResource(graph,context.get(context.size()-1),context));\r
+ } catch (DatabaseException e) {\r
+ throw new MappingException(e);\r
+ }\r
+ if (ctx.size() == 0)\r
+ throw new MappingException("Cannot find context for structural object, " + domainElement);\r
+ rangeElement.setContext(ctx);\r
+ }\r
+ updateRange(graph, mapping, domainElement, rangeElement);\r
+ }\r
+ \r
+ public boolean updateDomain(WriteGraph g, IBackwardMapping<StructuralResource,IStructuralObject> map, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {\r
+ if(LOGGER.isInfoEnabled())\r
+ try { \r
+ LOGGER.info("SimpleLinkType.updateDomain " +\r
+ NameUtils.getSafeName(g, domainElement.getResource()) + " " +\r
+ rangeElement.toString()\r
+ );\r
+ } catch(DatabaseException e) {\r
+ throw new MappingException(e);\r
+ }\r
+ \r
+ boolean updated = false;\r
+ for(IBidirectionalMappingRule<StructuralResource, IStructuralObject> rule : rules)\r
+ updated |= rule.updateDomain(g, map, domainElement, rangeElement);\r
+ return updated;\r
+ }\r
+ \r
+ public boolean updateRange(ReadGraph g, IForwardMapping<StructuralResource, IStructuralObject> map, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {\r
+ \r
+ if(LOGGER.isInfoEnabled())\r
+ try { \r
+ LOGGER.info("SimpleLinkType.updateRange " +\r
+ NameUtils.getSafeName(g, domainElement.getResource()) + " " +\r
+ rangeElement.toString()\r
+ );\r
+ } catch(DatabaseException e) {\r
+ throw new MappingException(e);\r
+ }\r
+ \r
+ boolean updated = false;\r
+ for(IBidirectionalMappingRule<StructuralResource, IStructuralObject> rule : rules)\r
+ updated |= rule.updateRange(g, map, domainElement, rangeElement);\r
+ return updated;\r
+ }\r
+}\r