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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g3d.objmap.schema;
\r
14 import gnu.trove.THashMap;
\r
16 import org.simantics.db.ReadGraph;
\r
17 import org.simantics.db.Resource;
\r
18 import org.simantics.db.common.utils.NameUtils;
\r
19 import org.simantics.db.exception.DatabaseException;
\r
20 import org.simantics.objmap.ILinkType;
\r
21 import org.simantics.objmap.IMappingSchema;
\r
22 import org.simantics.objmap.MappingException;
\r
23 import org.simantics.objmap.schema.SimpleLinkType;
\r
28 public class DefaultSchema implements IMappingSchema {
\r
30 THashMap<Resource, ILinkType> domainLinkTypes =
\r
31 new THashMap<Resource, ILinkType>();
\r
32 THashMap<Class<?>, ILinkType> rangeLinkTypes =
\r
33 new THashMap<Class<?>, ILinkType>();
\r
35 public void addLinkType(SimpleLinkType linkType) {
\r
36 domainLinkTypes.put(linkType.domainType, linkType);
\r
37 rangeLinkTypes.put(linkType.rangeType, linkType);
\r
40 public void addLinkType(AdaptedLinkType linkType) {
\r
41 domainLinkTypes.put(linkType.domainType, linkType);
\r
42 rangeLinkTypes.put(linkType.rangeType, linkType);
\r
46 public ILinkType linkTypeOfDomainElement(ReadGraph g, Resource element) throws MappingException {
\r
49 for(Resource type : g.getTypes(element)) {
\r
51 ILinkType linkType = domainLinkTypes.get(type);
\r
52 if(linkType != null) return linkType;
\r
56 throw new MappingException("Didn't find a link type for " +
\r
57 NameUtils.getSafeName(g, element) + ".");
\r
59 } catch (DatabaseException e) {
\r
60 throw new MappingException(e);
\r
65 public ILinkType linkTypeOfRangeElement(Object element) throws MappingException {
\r
66 ILinkType type = rangeLinkTypes.get(element.getClass());
\r
68 for (Class<?> clazz : element.getClass().getInterfaces()) {
\r
69 type = rangeLinkTypes.get(clazz);
\r
74 throw new MappingException("Didn't find a link type for " + element + ".");
\r
80 public ILinkType linkTypeOfDomainType(ReadGraph g, Resource type) {
\r
81 return domainLinkTypes.get(type);
\r
84 public ILinkType linkTypeOfRangeType(Class<?> clazz) {
\r
85 ILinkType type = rangeLinkTypes.get(clazz);
\r
87 for (Class<?> c : clazz.getInterfaces()) {
\r
88 type = rangeLinkTypes.get(clazz);
\r