+++ /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
-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
-\r
-\r
-/**\r
- * An implementation of IMappingSchema that contains\r
- * only SimpleLinkTypes. The link type of any domain\r
- * element is based solely on its type in database and\r
- * the link type of any range element is based on its class.\r
- * @author Hannu Niemistö\r
- */\r
-public class SimpleSchema 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 " + 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
- 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
- \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.getClass() + ".");\r
- }\r
- return type;\r
- }\r
-\r
-}\r