--- /dev/null
+/*******************************************************************************\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.graph.schema;\r
+\r
+\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
+\r
+\r
+/**\r
+ * \r
+ */\r
+public class DefaultSchema implements IMappingSchema<Resource,Object> {\r
+\r
+ THashMap<Resource, ILinkType<Resource,Object>> domainLinkTypes = \r
+ new THashMap<Resource, ILinkType<Resource,Object>>();\r
+ THashMap<Class<?>, ILinkType<Resource,Object>> rangeLinkTypes = \r
+ new THashMap<Class<?>, ILinkType<Resource,Object>>();\r
+ \r
+ public void addLinkType(SimpleLinkType<Object> linkType) {\r
+ domainLinkTypes.put(linkType.domainType, linkType);\r
+ rangeLinkTypes.put(linkType.rangeType, linkType);\r
+ }\r
+ \r
+ public void addLinkType(AdaptedLinkType<Object> linkType) {\r
+ domainLinkTypes.put(linkType.domainType, linkType);\r
+ rangeLinkTypes.put(linkType.rangeType, linkType);\r
+ }\r
+ \r
+ @Override\r
+ public ILinkType<Resource,Object> linkTypeOfDomainElement(ReadGraph g, Resource element) throws MappingException { \r
+ try {\r
+ \r
+ for(Resource type : g.getTypes(element)) {\r
+\r
+ ILinkType<Resource,Object> 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<Resource,Object> linkTypeOfRangeElement(Object element) throws MappingException {\r
+ ILinkType<Resource,Object> 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<Resource,Object> linkTypeOfDomainType(ReadGraph g, Resource type) { \r
+ return domainLinkTypes.get(type);\r
+ }\r
+ \r
+ public ILinkType<Resource,Object> linkTypeOfRangeType(Class<?> clazz) {\r
+ ILinkType<Resource,Object> type = rangeLinkTypes.get(clazz);\r
+ if(type == null) {\r
+ // FIXME: c is not referenced at all, should it be?\r
+ // TODO: should this method take inheritance into account ?\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