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