]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
added new objmap annotation OptionalRelatedElements that allows field to be null
authorniemisto <niemisto@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 6 Apr 2011 07:18:05 +0000 (07:18 +0000)
committerniemisto <niemisto@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 6 Apr 2011 07:18:05 +0000 (07:18 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@20350 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.objmap/src/org/simantics/objmap/annotations/OptionalRelatedElements.java [new file with mode: 0644]
org.simantics.objmap/src/org/simantics/objmap/annotations/factories/OptionalRelatedElementsRuleFactory.java [new file with mode: 0644]
org.simantics.objmap/src/org/simantics/objmap/rules/range/FieldAccessorWithDefault.java [new file with mode: 0644]

diff --git a/org.simantics.objmap/src/org/simantics/objmap/annotations/OptionalRelatedElements.java b/org.simantics.objmap/src/org/simantics/objmap/annotations/OptionalRelatedElements.java
new file mode 100644 (file)
index 0000000..da74792
--- /dev/null
@@ -0,0 +1,28 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 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.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.annotations.factories.OptionalRelatedElementsRuleFactory;\r
+import org.simantics.objmap.annotations.meta.HasFieldRuleFactory;\r
+\r
+@Retention(RetentionPolicy.RUNTIME)\r
+@Target(ElementType.FIELD)\r
+@HasFieldRuleFactory(OptionalRelatedElementsRuleFactory.class)\r
+public @interface OptionalRelatedElements {\r
+       String value();\r
+       boolean composition() default false;\r
+}\r
diff --git a/org.simantics.objmap/src/org/simantics/objmap/annotations/factories/OptionalRelatedElementsRuleFactory.java b/org.simantics.objmap/src/org/simantics/objmap/annotations/factories/OptionalRelatedElementsRuleFactory.java
new file mode 100644 (file)
index 0000000..a338448
--- /dev/null
@@ -0,0 +1,43 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 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.annotations.factories;\r
+\r
+import java.lang.annotation.Annotation;\r
+import java.lang.reflect.Field;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.exception.ResourceNotFoundException;\r
+import org.simantics.db.exception.ServiceException;\r
+import org.simantics.db.exception.ValidationException;\r
+import org.simantics.objmap.IMappingRule;\r
+import org.simantics.objmap.annotations.RelatedElements;\r
+import org.simantics.objmap.rules.MappedElementsRule;\r
+import org.simantics.objmap.rules.domain.RelatedObjectsAccessor;\r
+import org.simantics.objmap.rules.factory.IFieldRuleFactory;\r
+import org.simantics.objmap.rules.range.FieldAccessor;\r
+import org.simantics.objmap.rules.range.FieldAccessorWithDefault;\r
+\r
+public class OptionalRelatedElementsRuleFactory implements IFieldRuleFactory {\r
+\r
+    @Override\r
+    public IMappingRule create(ReadGraph g, Annotation _annotation, Field field) throws ResourceNotFoundException, ValidationException, ServiceException {\r
+        RelatedElements annotation = (RelatedElements)_annotation;\r
+        return new MappedElementsRule(\r
+                new RelatedObjectsAccessor(g.getResource(annotation.value()),\r
+                        annotation.composition()),\r
+                new FieldAccessorWithDefault(field, Collections.emptyList())\r
+                );\r
+    }\r
+\r
+}\r
diff --git a/org.simantics.objmap/src/org/simantics/objmap/rules/range/FieldAccessorWithDefault.java b/org.simantics.objmap/src/org/simantics/objmap/rules/range/FieldAccessorWithDefault.java
new file mode 100644 (file)
index 0000000..688de95
--- /dev/null
@@ -0,0 +1,39 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 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.rules.range;\r
+\r
+import java.lang.reflect.Field;\r
+\r
+import org.simantics.objmap.MappingException;\r
+\r
+/**\r
+ * Accesses the given field of the element.\r
+ * @author Hannu Niemist�\r
+ */\r
+public class FieldAccessorWithDefault<T> extends FieldAccessor<T> {\r
+    \r
+       T defaultValue;\r
+\r
+       public FieldAccessorWithDefault(Field field, T defaultValue) {\r
+        super(field);\r
+        this.defaultValue = defaultValue;\r
+    }\r
+\r
+    @Override\r
+       public T get(Object element) throws MappingException {\r
+               T value = super.get(element);\r
+               if(value == null)\r
+                   return defaultValue;\r
+               else\r
+                   return value;\r
+       }\r
+}\r