From 8777c3b17827b29aa42c78b288a3a20a421e0ffb Mon Sep 17 00:00:00 2001 From: luukkainen Date: Tue, 24 Sep 2013 12:47:29 +0000 Subject: [PATCH] Java object to ordered set mapping fixes #4439 git-svn-id: https://www.simantics.org/svn/simantics/3d/trunk@27883 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../graph/annotations/OrderedSetType.java | 23 ++++++++++ .../objmap/graph/schema/MappingSchemas.java | 8 ++++ .../schema/OrderedSetSimpleLinkType.java | 34 ++++++++++++++ .../structural/schema/MappingSchemas.java | 46 +++++++++++-------- .../schema/OrderedSetSimpleLinkType.java | 28 +++++++++++ .../structural/schema/SimpleLinkType.java | 12 +++-- 6 files changed, 128 insertions(+), 23 deletions(-) create mode 100644 org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OrderedSetType.java create mode 100644 org.simantics.objmap2/src/org/simantics/objmap/graph/schema/OrderedSetSimpleLinkType.java create mode 100644 org.simantics.objmap2/src/org/simantics/objmap/structural/schema/OrderedSetSimpleLinkType.java diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OrderedSetType.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OrderedSetType.java new file mode 100644 index 00000000..0ae08afa --- /dev/null +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OrderedSetType.java @@ -0,0 +1,23 @@ +/******************************************************************************* + * Copyright (c) 2012, 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.graph.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface OrderedSetType { + String value(); +} diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java index 0007d2ba..b9192849 100644 --- a/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java @@ -28,6 +28,7 @@ import org.simantics.objmap.graph.annotations.HasCollectionAdder; import org.simantics.objmap.graph.annotations.HasCollectionRemover; import org.simantics.objmap.graph.annotations.HasSetter; import org.simantics.objmap.graph.annotations.OptionalRelatedElements; +import org.simantics.objmap.graph.annotations.OrderedSetType; import org.simantics.objmap.graph.annotations.RelatedElements; import org.simantics.objmap.graph.annotations.RelatedElementsGet; import org.simantics.objmap.graph.annotations.RelatedGetObj; @@ -80,6 +81,13 @@ public class MappingSchemas { return new DynamicSimpleLinkType(g.getResource(dynamicType.value()), clazz, rules); } + OrderedSetType orderedSetType = clazz.getAnnotation(OrderedSetType.class); + if (orderedSetType != null) { + ArrayList> rules = new ArrayList>(); + collectRulesFromAnnotations(g, clazz, rules); + + return new OrderedSetSimpleLinkType(g.getResource(orderedSetType.value()), clazz, rules); + } throw new IllegalArgumentException("Class " + clazz.toString() + " does not contain annotations."); } diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/OrderedSetSimpleLinkType.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/OrderedSetSimpleLinkType.java new file mode 100644 index 00000000..b9127458 --- /dev/null +++ b/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/OrderedSetSimpleLinkType.java @@ -0,0 +1,34 @@ +package org.simantics.objmap.graph.schema; + +import java.util.ArrayList; + +import org.simantics.db.Resource; +import org.simantics.db.common.utils.OrderedSetUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.objmap.bidirectional.IBidirectionalMappingRule; +import org.simantics.objmap.exceptions.MappingException; + +public class OrderedSetSimpleLinkType extends SimpleLinkType { + + public OrderedSetSimpleLinkType(Resource domainType, Class rangeType, ArrayList> rules) { + super(domainType, rangeType, rules); + } + + public OrderedSetSimpleLinkType(Resource domainType, Class rangeType) { + super(domainType, rangeType); + } + + public Resource createDomainElement(org.simantics.db.WriteGraph g, Range rangeElement) throws org.simantics.objmap.exceptions.MappingException { + try { + if(LOGGER.isInfoEnabled()) + LOGGER.info("SimpleLinkType.createDomainElement " + + rangeElement.toString() + ); + Resource result = OrderedSetUtils.create(g, domainType); + return result; + } catch(DatabaseException e) { + throw new MappingException(e); + } + }; + +} diff --git a/org.simantics.objmap2/src/org/simantics/objmap/structural/schema/MappingSchemas.java b/org.simantics.objmap2/src/org/simantics/objmap/structural/schema/MappingSchemas.java index 758db0e0..3f9169ec 100644 --- a/org.simantics.objmap2/src/org/simantics/objmap/structural/schema/MappingSchemas.java +++ b/org.simantics.objmap2/src/org/simantics/objmap/structural/schema/MappingSchemas.java @@ -18,7 +18,6 @@ import java.util.ArrayList; import java.util.Collection; import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.objmap.bidirectional.IBidirectionalMappingRule; import org.simantics.objmap.graph.annotations.GraphType; @@ -26,6 +25,7 @@ import org.simantics.objmap.graph.annotations.HasCollectionAdder; import org.simantics.objmap.graph.annotations.HasCollectionRemover; import org.simantics.objmap.graph.annotations.HasSetter; import org.simantics.objmap.graph.annotations.OptionalRelatedElements; +import org.simantics.objmap.graph.annotations.OrderedSetType; import org.simantics.objmap.graph.annotations.RelatedElements; import org.simantics.objmap.graph.annotations.RelatedElementsGet; import org.simantics.objmap.graph.annotations.RelatedGetObj; @@ -33,6 +33,18 @@ import org.simantics.objmap.graph.annotations.RelatedGetValue; import org.simantics.objmap.graph.annotations.RelatedOrderedSetElements; import org.simantics.objmap.graph.annotations.RelatedValue; import org.simantics.objmap.graph.annotations.UpdateMethod; +import org.simantics.objmap.graph.annotations.meta.IsClassRule; +import org.simantics.objmap.graph.annotations.meta.IsCollectionRule; +import org.simantics.objmap.graph.annotations.meta.IsFieldRule; +import org.simantics.objmap.graph.annotations.meta.IsGetSetRule; +import org.simantics.objmap.graph.annotations.meta.IsMethodRule; +import org.simantics.objmap.graph.rules.factory.IClassRuleFactory; +import org.simantics.objmap.graph.rules.factory.ICollectionRuleFactory; +import org.simantics.objmap.graph.rules.factory.IFieldRuleFactory; +import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory; +import org.simantics.objmap.graph.rules.factory.IMethodRuleFactory; +import org.simantics.objmap.structural.IStructuralObject; +import org.simantics.objmap.structural.StructuralResource; import org.simantics.objmap.structural.annotations.StructuralRelatedElementsGet; import org.simantics.objmap.structural.annotations.StructuralRelatedGetObj; import org.simantics.objmap.structural.annotations.TypeRelatedElementsGet; @@ -51,18 +63,6 @@ import org.simantics.objmap.structural.annotations.factories.TypeRelatedElements import org.simantics.objmap.structural.annotations.factories.TypeRelatedGetSetObjRuleFactory; import org.simantics.objmap.structural.annotations.factories.TypeRelatedGetSetValueRuleFactory; import org.simantics.objmap.structural.annotations.factories.UpdateMethodFactory; -import org.simantics.objmap.graph.annotations.meta.IsClassRule; -import org.simantics.objmap.graph.annotations.meta.IsCollectionRule; -import org.simantics.objmap.graph.annotations.meta.IsFieldRule; -import org.simantics.objmap.graph.annotations.meta.IsGetSetRule; -import org.simantics.objmap.graph.annotations.meta.IsMethodRule; -import org.simantics.objmap.graph.rules.factory.IClassRuleFactory; -import org.simantics.objmap.graph.rules.factory.ICollectionRuleFactory; -import org.simantics.objmap.graph.rules.factory.IFieldRuleFactory; -import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory; -import org.simantics.objmap.graph.rules.factory.IMethodRuleFactory; -import org.simantics.objmap.structural.IStructuralObject; -import org.simantics.objmap.structural.StructuralResource; public class MappingSchemas { @@ -76,12 +76,20 @@ public class MappingSchemas { public static SimpleLinkType fromAnnotations(ReadGraph g, Class clazz) throws DatabaseException, InstantiationException, IllegalAccessException { GraphType graphType = clazz.getAnnotation(GraphType.class); - ArrayList> rules = new ArrayList>(); - collectRulesFromAnnotations(g, clazz, rules); - - return new SimpleLinkType( - g.getResource(graphType.value()), - clazz, rules); + if (graphType != null) { + ArrayList> rules = new ArrayList>(); + collectRulesFromAnnotations(g, clazz, rules); + + return new SimpleLinkType(g.getResource(graphType.value()), clazz, rules); + } + OrderedSetType orderedSetType = clazz.getAnnotation(OrderedSetType.class); + if (orderedSetType != null) { + ArrayList> rules = new ArrayList>(); + collectRulesFromAnnotations(g, clazz, rules); + + return new OrderedSetSimpleLinkType(g.getResource(orderedSetType.value()), clazz, rules); + } + throw new IllegalArgumentException("Class " + clazz.toString() + " does not contain annotations."); } public static void collectRulesFromAnnotations(ReadGraph g, Class clazz, Collection> rules) throws DatabaseException, InstantiationException, IllegalAccessException { diff --git a/org.simantics.objmap2/src/org/simantics/objmap/structural/schema/OrderedSetSimpleLinkType.java b/org.simantics.objmap2/src/org/simantics/objmap/structural/schema/OrderedSetSimpleLinkType.java new file mode 100644 index 00000000..6fd6a016 --- /dev/null +++ b/org.simantics.objmap2/src/org/simantics/objmap/structural/schema/OrderedSetSimpleLinkType.java @@ -0,0 +1,28 @@ +package org.simantics.objmap.structural.schema; + +import java.util.ArrayList; + +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.utils.OrderedSetUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.objmap.bidirectional.IBidirectionalMappingRule; +import org.simantics.objmap.structural.IStructuralObject; +import org.simantics.objmap.structural.StructuralResource; + +public class OrderedSetSimpleLinkType extends SimpleLinkType { + + public OrderedSetSimpleLinkType(Resource domainType, Class rangeType, ArrayList> rules) { + super(domainType, rangeType, rules); + } + + public OrderedSetSimpleLinkType(Resource domainType, Class rangeType) { + super(domainType, rangeType); + } + + @Override + protected Resource newResource(WriteGraph g, Resource type) throws DatabaseException { + return OrderedSetUtils.create(g, type); + } + +} diff --git a/org.simantics.objmap2/src/org/simantics/objmap/structural/schema/SimpleLinkType.java b/org.simantics.objmap2/src/org/simantics/objmap/structural/schema/SimpleLinkType.java index eb9d57c4..7ad6b6d0 100644 --- a/org.simantics.objmap2/src/org/simantics/objmap/structural/schema/SimpleLinkType.java +++ b/org.simantics.objmap2/src/org/simantics/objmap/structural/schema/SimpleLinkType.java @@ -69,15 +69,13 @@ public class SimpleLinkType implements ILinkType