+/*******************************************************************************\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
+\r
+\r
+import java.util.Stack;\r
+\r
+import gnu.trove.map.hash.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.exceptions.MappingException;\r
+import org.simantics.objmap.graph.schema.ILinkType;\r
+import org.simantics.objmap.graph.schema.IMappingSchema;\r
+import org.simantics.objmap.structural.IStructuralObject;\r
+import org.simantics.objmap.structural.StructuralResource;\r
+\r
+\r
+/**\r
+ * \r
+ */\r
+public class DefaultSchema implements IMappingSchema<StructuralResource, IStructuralObject> {\r
+\r
+ THashMap<Resource, ILinkType<StructuralResource, IStructuralObject>> domainLinkTypes = \r
+ new THashMap<Resource, ILinkType<StructuralResource, IStructuralObject>>();\r
+ THashMap<Class<?>, ILinkType<StructuralResource, IStructuralObject>> rangeLinkTypes = \r
+ new THashMap<Class<?>, ILinkType<StructuralResource, IStructuralObject>>();\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<StructuralResource, IStructuralObject> linkTypeOfDomainElement(ReadGraph g, StructuralResource element) throws MappingException { \r
+ try {\r
+ \r
+ for(Resource type : g.getTypes(element.getResource())) {\r
+\r
+ ILinkType<StructuralResource, IStructuralObject> 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.getResource()) + ".");\r
+ \r
+ } catch (DatabaseException e) {\r
+ throw new MappingException(e);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public ILinkType<StructuralResource, IStructuralObject> linkTypeOfRangeElement(IStructuralObject element) throws MappingException {\r
+ ILinkType<StructuralResource, IStructuralObject> type = rangeLinkTypes.get(element.getClass());\r
+ if(type == null) {\r
+ Stack<Class<?>> clazzes = new Stack<Class<?>>();\r
+ for (Class<?> clazz : element.getClass().getInterfaces()) {\r
+ clazzes.add(clazz);\r
+ }\r
+ clazzes.add(element.getClass().getSuperclass());\r
+ \r
+ while (!clazzes.isEmpty()) {\r
+ Class<?> clazz = clazzes.pop();\r
+ \r
+ type = rangeLinkTypes.get(clazz);\r
+ if (type != null)\r
+ return type;\r
+ for (Class<?> c : clazz.getInterfaces())\r
+ clazzes.add(c);\r
+ \r
+ }\r
+ throw new MappingException("Didn't find a link type for " + element + ".");\r
+ }\r
+ return type;\r
+ }\r
+\r
+ \r
+ public ILinkType<StructuralResource, IStructuralObject> linkTypeOfDomainType(ReadGraph g, Resource type) { \r
+ return domainLinkTypes.get(type);\r
+ }\r
+ \r
+ public ILinkType<StructuralResource, IStructuralObject> linkTypeOfRangeType(Class<?> clazz) {\r
+ ILinkType<StructuralResource, IStructuralObject> type = rangeLinkTypes.get(clazz);\r
+ if(type == null) {\r
+ Stack<Class<?>> clazzes = new Stack<Class<?>>();\r
+ for (Class<?> c : clazz.getInterfaces()) {\r
+ clazzes.add(c);\r
+ }\r
+ clazzes.add(clazz.getSuperclass());\r
+ \r
+ while (!clazzes.isEmpty()) {\r
+ Class<?> c = clazzes.pop();\r
+ \r
+ type = rangeLinkTypes.get(c);\r
+ if (type != null)\r
+ return type;\r
+ for (Class<?> c2 : c.getInterfaces())\r
+ clazzes.add(c2);\r
+ \r
+ }\r
+ \r
+ }\r
+ return null;\r
+ }\r
+}\r