*******************************************************************************/\r
package org.simantics.objmap.graph;\r
\r
-import org.simantics.db.Resource;\r
import org.simantics.objmap.graph.impl.Mapping;\r
import org.simantics.objmap.graph.schema.IMappingSchema;\r
/**\r
import java.lang.annotation.Target;\r
\r
import org.simantics.objmap.graph.annotations.meta.IsGetSetRule;\r
-import org.simantics.objmap.graph.rules.adapters.IdentityAdapter;\r
-import org.simantics.objmap.graph.rules.adapters.ValueAdapter;\r
-\r
\r
\r
@Retention(RetentionPolicy.RUNTIME)\r
String objRelation();\r
String objType();\r
String valRelation();\r
-// Class<? extends ValueAdapter> adapter() default IdentityAdapter.class;\r
}\r
\r
@Retention(RetentionPolicy.RUNTIME)\r
@Target(ElementType.METHOD)\r
-//@HasGetSetRuleFactory(RelatedGetSetValueRuleFactory.class)\r
public @interface GetType {\r
String value();\r
}\r
\r
@Retention(RetentionPolicy.RUNTIME)\r
@Target(ElementType.FIELD)\r
-//@HasFieldRuleFactory(OptionalRelatedElementsRuleFactory.class)\r
@IsFieldRule\r
public @interface OptionalRelatedElements {\r
String value();\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
+ * 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.graph.annotations;\r
+\r
+import java.lang.annotation.ElementType;\r
+import java.lang.annotation.Retention;\r
+import java.lang.annotation.RetentionPolicy;\r
+import java.lang.annotation.Target;\r
+\r
+@Retention(RetentionPolicy.RUNTIME)\r
+@Target(ElementType.METHOD)\r
+public @interface OrderedElementsAdd {\r
+}\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
+ * 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.graph.annotations;\r
+\r
+import java.lang.annotation.ElementType;\r
+import java.lang.annotation.Retention;\r
+import java.lang.annotation.RetentionPolicy;\r
+import java.lang.annotation.Target;\r
+\r
+import org.simantics.objmap.graph.annotations.meta.IsCollectionRule;\r
+\r
+\r
+\r
+\r
+@Retention(RetentionPolicy.RUNTIME)\r
+@Target(ElementType.METHOD)\r
+@IsCollectionRule\r
+@HasCollectionAdder(OrderedElementsAdd.class)\r
+@HasCollectionRemover(OrderedElementsRem.class)\r
+public @interface OrderedElementsGet {\r
+ boolean composition() default false;\r
+}\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
+ * 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.graph.annotations;\r
+\r
+import java.lang.annotation.ElementType;\r
+import java.lang.annotation.Retention;\r
+import java.lang.annotation.RetentionPolicy;\r
+import java.lang.annotation.Target;\r
+\r
+\r
+@Retention(RetentionPolicy.RUNTIME)\r
+@Target(ElementType.METHOD)\r
+public @interface OrderedElementsRem {\r
+}\r
\r
@Retention(RetentionPolicy.RUNTIME)\r
@Target(ElementType.FIELD)\r
-//HasFieldRuleFactory(RelatedElementRuleFactory.class)\r
@IsFieldRule\r
public @interface RelatedElement {\r
String value();\r
\r
@Retention(RetentionPolicy.RUNTIME)\r
@Target(ElementType.FIELD)\r
-//@HasFieldRuleFactory(RelatedElementsRuleFactory.class)\r
@IsFieldRule\r
public @interface RelatedElements {\r
String value();\r
\r
@Retention(RetentionPolicy.RUNTIME)\r
@Target(ElementType.METHOD)\r
-//@HasCollectionRuleFactory(RelatedElementsRuleFactory2.class)\r
@IsCollectionRule\r
@HasCollectionAdder(RelatedElementsAdd.class)\r
@HasCollectionRemover(RelatedElementsRem.class)\r
\r
@Retention(RetentionPolicy.RUNTIME)\r
@Target(ElementType.FIELD)\r
-//@HasFieldRuleFactory(RelatedOrderedSetElementsRuleFactory.class)\r
@IsFieldRule\r
public @interface RelatedOrderedSetElements {\r
boolean composition() default false;\r
*/\r
@Retention(RetentionPolicy.RUNTIME)\r
@Target(ElementType.FIELD)\r
-//@HasFieldRuleFactory(RelatedValueRuleFactory.class)\r
@IsFieldRule\r
public @interface RelatedValue {\r
String value();\r
\r
@Retention(RetentionPolicy.RUNTIME)\r
@Target(ElementType.METHOD)\r
-//@HasGetSetRuleFactory(RelatedGetSetValueRuleFactory.class)\r
public @interface SetType {\r
String value();\r
}\r
*/\r
@Retention(RetentionPolicy.RUNTIME)\r
@Target(ElementType.METHOD)\r
-//@HasMethodRuleFactory(UpdateMethodFactory.class)\r
@IsFieldRule\r
public @interface UpdateMethod {\r
}\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
+ * 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.graph.annotations.factories;\r
+\r
+import java.lang.annotation.Annotation;\r
+import java.lang.reflect.Method;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;\r
+import org.simantics.objmap.graph.annotations.OrderedElementsAdd;\r
+import org.simantics.objmap.graph.annotations.OrderedElementsGet;\r
+import org.simantics.objmap.graph.annotations.OrderedElementsRem;\r
+import org.simantics.objmap.graph.rules.MappedElementsRule;\r
+import org.simantics.objmap.graph.rules.domain.RelatedOrderedSetElementsAccessor;\r
+import org.simantics.objmap.graph.rules.factory.ICollectionRuleFactory;\r
+import org.simantics.objmap.graph.rules.range.ListAccessor;\r
+\r
+\r
+public class OrderedElementsRuleFactory<Range> implements ICollectionRuleFactory<Resource,Range> {\r
+ \r
+ @Override\r
+ public IBidirectionalMappingRule<Resource, Range> create(ReadGraph g, Annotation annotation,\r
+ Method getter, Method adder, Method remover)\r
+ throws DatabaseException {\r
+ OrderedElementsGet getterAnn = (OrderedElementsGet)annotation;\r
+ return new MappedElementsRule<Resource,Range>(new RelatedOrderedSetElementsAccessor(getterAnn.composition()),\r
+ new ListAccessor<Range,Range>(getter, adder, remover));\r
+ }\r
+ \r
+ @Override\r
+ public boolean isAdder(Annotation getterAnnotation, Annotation annotation) {\r
+ OrderedElementsGet getterAnn = (OrderedElementsGet)getterAnnotation;\r
+ OrderedElementsAdd adderAnn = (OrderedElementsAdd)annotation;\r
+ return true;\r
+ }\r
+ \r
+ @Override\r
+ public boolean isRemover(Annotation getterAnnotation, Annotation annotation) {\r
+ OrderedElementsGet getterAnn = (OrderedElementsGet)getterAnnotation;\r
+ OrderedElementsRem adderAnn = (OrderedElementsRem)annotation;\r
+ return true;\r
+ }\r
+\r
+}\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
+ * 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.graph.rules.range;\r
+\r
+import java.lang.reflect.InvocationTargetException;\r
+import java.lang.reflect.Method;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.simantics.objmap.exceptions.MappingException;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+\r
+/**\r
+ * Accessor for mapped collections. \r
+ * Uses three methods:\r
+ * - Getter: returns the collection.\r
+ * - Adder: adds one item into the collection.\r
+ * - Remover: removes one item from the collection. \r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ *\r
+ * @param <T>\r
+ */\r
+public class ListAccessor<Range,T> implements IRangeAccessor<Range,Collection<T>> {\r
+ \r
+ private Method getter;\r
+ private Method adder;\r
+ private Method remover;\r
+ \r
+ public ListAccessor(Method getter, Method adder, Method remover) {\r
+ this.getter = getter;\r
+ this.adder = adder;\r
+ this.remover = remover;\r
+ }\r
+ \r
+ @SuppressWarnings("unchecked")\r
+ public java.util.Collection<T> get(Object element) throws MappingException {\r
+ try {\r
+ return (Collection<T>) getter.invoke(element);\r
+ } catch (IllegalArgumentException e) {\r
+ throw new MappingException(e);\r
+ } catch (IllegalAccessException e) {\r
+ throw new MappingException(e);\r
+ } catch (InvocationTargetException e) {\r
+ throw new MappingException(e);\r
+ }\r
+ };\r
+\r
+ @Override\r
+ public boolean set(Range element, Collection<T> value)\r
+ throws MappingException {\r
+ java.util.Collection<T> current = get(element);\r
+ Collection<Pair<Integer, T>> adding = new ArrayList<Pair<Integer, T>>();\r
+ Collection<T> removing = new ArrayList<T>();\r
+ for (T e : current) {\r
+ if (!value.contains(e))\r
+ removing.add(e);\r
+ }\r
+ int i = 0;\r
+ for (T e : value) {\r
+ if (!current.contains(e))\r
+ adding.add(new Pair<Integer, T>(i, e));\r
+ i++;\r
+ }\r
+ \r
+ try {\r
+ for (T e : removing) {\r
+ remover.invoke(element, e);\r
+ }\r
+ \r
+ for (Pair<Integer,T> e : adding) {\r
+ adder.invoke(element, e.first,e.second);\r
+ }\r
+ } catch (IllegalArgumentException e) {\r
+ throw new MappingException(e);\r
+ } catch (IllegalAccessException e) {\r
+ throw new MappingException(e);\r
+ } catch (InvocationTargetException e) {\r
+ throw new MappingException(e);\r
+ }\r
+ return removing.size() > 0 || adding.size() > 0;\r
+ \r
+ }\r
+}\r
\r
}\r
\r
- @Override\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
public Range createRangeElement(ReadGraph g, Resource domainElement)\r
throws MappingException {\r
try {\r
}\r
}\r
\r
- @Override\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
public Range createRangeElement(ReadGraph g, Resource domainElement)\r
throws MappingException {\r
try {\r
import org.simantics.objmap.graph.annotations.HasCollectionRemover;\r
import org.simantics.objmap.graph.annotations.HasSetter;\r
import org.simantics.objmap.graph.annotations.OptionalRelatedElements;\r
+import org.simantics.objmap.graph.annotations.OrderedElementsGet;\r
import org.simantics.objmap.graph.annotations.OrderedSetType;\r
import org.simantics.objmap.graph.annotations.RelatedElements;\r
import org.simantics.objmap.graph.annotations.RelatedElementsGet;\r
import org.simantics.objmap.graph.annotations.UpdateMethod;\r
import org.simantics.objmap.graph.annotations.factories.CompoundRelatedGetSetValueRuleFactory;\r
import org.simantics.objmap.graph.annotations.factories.OptionalRelatedElementsRuleFactory;\r
+import org.simantics.objmap.graph.annotations.factories.OrderedElementsRuleFactory;\r
import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory;\r
import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory2;\r
import org.simantics.objmap.graph.annotations.factories.RelatedGetSetObjRuleFactory;\r
public static ICollectionRuleFactory<Resource,Object> createCollectionRuleFactory(ReadGraph g, Annotation annotation, Method getter) {\r
if (annotation.annotationType().equals(RelatedElementsGet.class))\r
return new RelatedElementsRuleFactory2<Object>();\r
+ if (annotation.annotationType().equals(OrderedElementsGet.class))\r
+ return new OrderedElementsRuleFactory<Object>();\r
return null;\r
}\r
\r
throw new MappingException(e);\r
}\r
}\r
- @Override\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
public Range createRangeElement(ReadGraph g, Resource domainElement)\r
throws MappingException {\r
try {\r