]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/DefaultSchema.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / schema / DefaultSchema.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2013 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.objmap.graph.schema;\r
13 \r
14 \r
15 \r
16 import gnu.trove.map.hash.THashMap;\r
17 \r
18 import org.simantics.db.ReadGraph;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.db.common.utils.NameUtils;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.objmap.exceptions.MappingException;\r
23 \r
24 \r
25 /**\r
26  * \r
27  */\r
28 public class DefaultSchema implements IMappingSchema<Resource,Object> {\r
29 \r
30     THashMap<Resource, ILinkType<Resource,Object>> domainLinkTypes = \r
31         new THashMap<Resource, ILinkType<Resource,Object>>();\r
32     THashMap<Class<?>, ILinkType<Resource,Object>> rangeLinkTypes = \r
33         new THashMap<Class<?>, ILinkType<Resource,Object>>();\r
34     \r
35     public void addLinkType(SimpleLinkType<Object> linkType) {\r
36         domainLinkTypes.put(linkType.domainType, linkType);\r
37         rangeLinkTypes.put(linkType.rangeType, linkType);\r
38     }\r
39     \r
40     public void addLinkType(AdaptedLinkType<Object> linkType) {\r
41         domainLinkTypes.put(linkType.domainType, linkType);\r
42         rangeLinkTypes.put(linkType.rangeType, linkType);\r
43     }\r
44     \r
45     @Override\r
46     public ILinkType<Resource,Object> linkTypeOfDomainElement(ReadGraph g, Resource element) throws MappingException {        \r
47         try {\r
48                 \r
49                 for(Resource type : g.getTypes(element)) {\r
50 \r
51                         ILinkType<Resource,Object> linkType = domainLinkTypes.get(type);\r
52                         if(linkType != null) return linkType;\r
53                         \r
54                 }\r
55                 \r
56                 throw new MappingException("Didn't find a link type for " +\r
57                                 NameUtils.getSafeName(g, element) + ".");\r
58                                 \r
59         } catch (DatabaseException e) {\r
60             throw new MappingException(e);\r
61         }\r
62     }\r
63 \r
64     @Override\r
65     public ILinkType<Resource,Object> linkTypeOfRangeElement(Object element) throws MappingException {\r
66         ILinkType<Resource,Object> type = rangeLinkTypes.get(element.getClass());\r
67                 if(type == null)  {\r
68                         for (Class<?> clazz : element.getClass().getInterfaces()) {\r
69                                 type = rangeLinkTypes.get(clazz);\r
70                                 if (type != null)\r
71                                         return type;\r
72                                 \r
73                         }\r
74                         throw new MappingException("Didn't find a link type for " +     element + ".");\r
75                 }\r
76                 return type;\r
77     }\r
78 \r
79     \r
80     public ILinkType<Resource,Object> linkTypeOfDomainType(ReadGraph g, Resource type)  {        \r
81         return domainLinkTypes.get(type);\r
82     }\r
83     \r
84     public ILinkType<Resource,Object> linkTypeOfRangeType(Class<?> clazz) {\r
85         ILinkType<Resource,Object> type = rangeLinkTypes.get(clazz);\r
86         if(type == null)  {\r
87                 // FIXME: c is not referenced at all, should it be?\r
88                 // TODO: should this method take inheritance into account ?\r
89                         for (Class<?> c : clazz.getInterfaces()) {\r
90                                 type = rangeLinkTypes.get(clazz);\r
91                                 if (type != null)\r
92                                         return type;\r
93                                 \r
94                         }\r
95                 }\r
96         return null;\r
97     }\r
98 }\r