]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/DefaultSchema.java
Support for linked lists in objmap2.
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / schema / DefaultSchema.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
16 import gnu.trove.map.hash.THashMap;
17
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.utils.NameUtils;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.objmap.exceptions.MappingException;
23
24
25 /**
26  * 
27  */
28 public class DefaultSchema implements IMappingSchema<Resource,Object> {
29
30     THashMap<Resource, ILinkType<Resource,Object>> domainLinkTypes = 
31         new THashMap<Resource, ILinkType<Resource,Object>>();
32     THashMap<Class<?>, ILinkType<Resource,Object>> rangeLinkTypes = 
33         new THashMap<Class<?>, ILinkType<Resource,Object>>();
34     
35     public void addLinkType(SimpleLinkType<Object> linkType) {
36         domainLinkTypes.put(linkType.domainType, linkType);
37         rangeLinkTypes.put(linkType.rangeType, linkType);
38     }
39     
40     public void addLinkType(AdaptedLinkType<Object> linkType) {
41         domainLinkTypes.put(linkType.domainType, linkType);
42         rangeLinkTypes.put(linkType.rangeType, linkType);
43     }
44     
45     @Override
46     public ILinkType<Resource,Object> linkTypeOfDomainElement(ReadGraph g, Resource element) throws MappingException {        
47         try {
48                 
49                 for(Resource type : g.getTypes(element)) {
50
51                         ILinkType<Resource,Object> linkType = domainLinkTypes.get(type);
52                         if(linkType != null) return linkType;
53                         
54                 }
55                 
56                 throw new MappingException("Didn't find a link type for " +
57                                 NameUtils.getSafeName(g, element) + ".");
58                                 
59         } catch (DatabaseException e) {
60             throw new MappingException(e);
61         }
62     }
63
64     @Override
65     public ILinkType<Resource,Object> linkTypeOfRangeElement(Object element) throws MappingException {
66         ILinkType<Resource,Object> type = rangeLinkTypes.get(element.getClass());
67                 if(type == null)  {
68                         for (Class<?> clazz : element.getClass().getInterfaces()) {
69                                 type = rangeLinkTypes.get(clazz);
70                                 if (type != null)
71                                         return type;
72                                 
73                         }
74                         throw new MappingException("Didn't find a link type for " +     element + ".");
75                 }
76                 return type;
77     }
78
79     
80     public ILinkType<Resource,Object> linkTypeOfDomainType(ReadGraph g, Resource type)  {        
81         return domainLinkTypes.get(type);
82     }
83     
84     public ILinkType<Resource,Object> linkTypeOfRangeType(Class<?> clazz) {
85         ILinkType<Resource,Object> type = rangeLinkTypes.get(clazz);
86         if(type == null)  {
87                 // FIXME: c is not referenced at all, should it be?
88                 // TODO: should this method take inheritance into account ?
89                         for (Class<?> c : clazz.getInterfaces()) {
90                                 type = rangeLinkTypes.get(clazz);
91                                 if (type != null)
92                                         return type;
93                                 
94                         }
95                 }
96         return null;
97     }
98 }