]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/schema/DefaultSchema.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / structural / 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.structural.schema;\r
13 \r
14 \r
15 \r
16 import java.util.Stack;\r
17 \r
18 import gnu.trove.map.hash.THashMap;\r
19 \r
20 import org.simantics.db.ReadGraph;\r
21 import org.simantics.db.Resource;\r
22 import org.simantics.db.common.utils.NameUtils;\r
23 import org.simantics.db.exception.DatabaseException;\r
24 import org.simantics.objmap.exceptions.MappingException;\r
25 import org.simantics.objmap.graph.schema.ILinkType;\r
26 import org.simantics.objmap.graph.schema.IMappingSchema;\r
27 import org.simantics.objmap.structural.IStructuralObject;\r
28 import org.simantics.objmap.structural.StructuralResource;\r
29 \r
30 \r
31 /**\r
32  * \r
33  */\r
34 public class DefaultSchema implements IMappingSchema<StructuralResource, IStructuralObject> {\r
35 \r
36     THashMap<Resource, ILinkType<StructuralResource, IStructuralObject>> domainLinkTypes = \r
37         new THashMap<Resource, ILinkType<StructuralResource, IStructuralObject>>();\r
38     THashMap<Class<?>, ILinkType<StructuralResource, IStructuralObject>> rangeLinkTypes = \r
39         new THashMap<Class<?>, ILinkType<StructuralResource, IStructuralObject>>();\r
40     \r
41     public void addLinkType(SimpleLinkType linkType) {\r
42         domainLinkTypes.put(linkType.domainType, linkType);\r
43         rangeLinkTypes.put(linkType.rangeType, linkType);\r
44     }\r
45     \r
46     public void addLinkType(AdaptedLinkType linkType) {\r
47         domainLinkTypes.put(linkType.domainType, linkType);\r
48         rangeLinkTypes.put(linkType.rangeType, linkType);\r
49     }\r
50     \r
51     @Override\r
52     public ILinkType<StructuralResource, IStructuralObject> linkTypeOfDomainElement(ReadGraph g, StructuralResource element) throws MappingException {        \r
53         try {\r
54                 \r
55                 for(Resource type : g.getTypes(element.getResource())) {\r
56 \r
57                         ILinkType<StructuralResource, IStructuralObject> linkType = domainLinkTypes.get(type);\r
58                         if(linkType != null) return linkType;\r
59                         \r
60                 }\r
61                 \r
62                 throw new MappingException("Didn't find a link type for " +\r
63                                 NameUtils.getSafeName(g, element.getResource()) + ".");\r
64                                 \r
65         } catch (DatabaseException e) {\r
66             throw new MappingException(e);\r
67         }\r
68     }\r
69 \r
70     @Override\r
71     public ILinkType<StructuralResource, IStructuralObject> linkTypeOfRangeElement(IStructuralObject element) throws MappingException {\r
72         ILinkType<StructuralResource, IStructuralObject> type = rangeLinkTypes.get(element.getClass());\r
73                 if(type == null)  {\r
74                         Stack<Class<?>> clazzes = new Stack<Class<?>>();\r
75                 for (Class<?> clazz : element.getClass().getInterfaces()) {\r
76                         clazzes.add(clazz);\r
77                 }\r
78                 clazzes.add(element.getClass().getSuperclass());\r
79                 \r
80                         while (!clazzes.isEmpty()) {\r
81                                 Class<?> clazz = clazzes.pop();\r
82                         \r
83                                 type = rangeLinkTypes.get(clazz);\r
84                                 if (type != null)\r
85                                         return type;\r
86                                 for (Class<?> c : clazz.getInterfaces())\r
87                                         clazzes.add(c);\r
88                                 \r
89                         }\r
90                         throw new MappingException("Didn't find a link type for " +     element + ".");\r
91                 }\r
92                 return type;\r
93     }\r
94 \r
95     \r
96     public ILinkType<StructuralResource, IStructuralObject> linkTypeOfDomainType(ReadGraph g, Resource type)  {        \r
97         return domainLinkTypes.get(type);\r
98     }\r
99     \r
100     public ILinkType<StructuralResource, IStructuralObject> linkTypeOfRangeType(Class<?> clazz) {\r
101         ILinkType<StructuralResource, IStructuralObject> type = rangeLinkTypes.get(clazz);\r
102         if(type == null)  {\r
103                 Stack<Class<?>> clazzes = new Stack<Class<?>>();\r
104                 for (Class<?> c : clazz.getInterfaces()) {\r
105                         clazzes.add(c);\r
106                 }\r
107                 clazzes.add(clazz.getSuperclass());\r
108                 \r
109                 while (!clazzes.isEmpty()) {\r
110                                 Class<?> c = clazzes.pop();\r
111                         \r
112                                 type = rangeLinkTypes.get(c);\r
113                                 if (type != null)\r
114                                         return type;\r
115                                 for (Class<?> c2 : c.getInterfaces())\r
116                                         clazzes.add(c2);\r
117                                 \r
118                         }\r
119                 \r
120                 }\r
121         return null;\r
122     }\r
123 }\r