]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.g3d/src/org/simantics/g3d/objmap/schema/DefaultSchema.java
3D framework (Simca 2012)
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / objmap / schema / DefaultSchema.java
diff --git a/org.simantics.g3d/src/org/simantics/g3d/objmap/schema/DefaultSchema.java b/org.simantics.g3d/src/org/simantics/g3d/objmap/schema/DefaultSchema.java
new file mode 100644 (file)
index 0000000..43ed22f
--- /dev/null
@@ -0,0 +1,96 @@
+/*******************************************************************************\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.g3d.objmap.schema;\r
+\r
+import gnu.trove.THashMap;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.objmap.ILinkType;\r
+import org.simantics.objmap.IMappingSchema;\r
+import org.simantics.objmap.MappingException;\r
+import org.simantics.objmap.schema.SimpleLinkType;\r
+\r
+/**\r
+ * \r
+ */\r
+public class DefaultSchema implements IMappingSchema {\r
+\r
+    THashMap<Resource, ILinkType> domainLinkTypes = \r
+        new THashMap<Resource, ILinkType>();\r
+    THashMap<Class<?>, ILinkType> rangeLinkTypes = \r
+        new THashMap<Class<?>, ILinkType>();\r
+    \r
+    public void addLinkType(SimpleLinkType linkType) {\r
+        domainLinkTypes.put(linkType.domainType, linkType);\r
+        rangeLinkTypes.put(linkType.rangeType, linkType);\r
+    }\r
+    \r
+    public void addLinkType(AdaptedLinkType linkType) {\r
+        domainLinkTypes.put(linkType.domainType, linkType);\r
+        rangeLinkTypes.put(linkType.rangeType, linkType);\r
+    }\r
+    \r
+    @Override\r
+    public ILinkType linkTypeOfDomainElement(ReadGraph g, Resource element) throws MappingException {        \r
+        try {\r
+               \r
+               for(Resource type : g.getTypes(element)) {\r
+\r
+                       ILinkType linkType = domainLinkTypes.get(type);\r
+                       if(linkType != null) return linkType;\r
+                       \r
+               }\r
+               \r
+               throw new MappingException("Didn't find a link type for " +\r
+                               NameUtils.getSafeName(g, element) + ".");\r
+                               \r
+        } catch (DatabaseException e) {\r
+            throw new MappingException(e);\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public ILinkType linkTypeOfRangeElement(Object element) throws MappingException {\r
+       ILinkType type = rangeLinkTypes.get(element.getClass());\r
+               if(type == null)  {\r
+                       for (Class<?> clazz : element.getClass().getInterfaces()) {\r
+                               type = rangeLinkTypes.get(clazz);\r
+                               if (type != null)\r
+                                       return type;\r
+                               \r
+                       }\r
+                       throw new MappingException("Didn't find a link type for " +     element + ".");\r
+               }\r
+               return type;\r
+    }\r
+\r
+    \r
+    public ILinkType linkTypeOfDomainType(ReadGraph g, Resource type)  {        \r
+       return domainLinkTypes.get(type);\r
+    }\r
+    \r
+    public ILinkType linkTypeOfRangeType(Class<?> clazz) {\r
+       ILinkType type = rangeLinkTypes.get(clazz);\r
+       if(type == null)  {\r
+                       for (Class<?> c : clazz.getInterfaces()) {\r
+                               type = rangeLinkTypes.get(clazz);\r
+                               if (type != null)\r
+                                       return type;\r
+                               \r
+                       }\r
+               }\r
+       return null;\r
+    }\r
+}\r