]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/objmap/schema/AdaptedLinkType.java
Copyrights
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / objmap / schema / AdaptedLinkType.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
3  * 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.g3d.objmap.schema;\r
13 \r
14 //import org.apache.log4j.Logger;\r
15 import org.eclipse.core.runtime.IAdaptable;\r
16 import org.simantics.db.ReadGraph;\r
17 import org.simantics.db.Resource;\r
18 import org.simantics.db.WriteGraph;\r
19 import org.simantics.db.exception.DatabaseException;\r
20 import org.simantics.objmap.IFunction;\r
21 import org.simantics.objmap.ILinkType;\r
22 import org.simantics.objmap.MappingException;\r
23 \r
24 /**\r
25  * A link type that is associated with adaptable resource (ReadGraph.getAdapter(Resource,Class)). \r
26  * The adapted object must implement IAdaptable interface for returning the original Resource. \r
27  * \r
28  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
29  *\r
30  */\r
31 public class AdaptedLinkType implements ILinkType {\r
32 \r
33         \r
34         //static Logger LOGGER = Logger.getLogger("org.simantics.objmap");\r
35     \r
36     Resource domainType;\r
37     Class<?> rangeType;\r
38     \r
39     \r
40     public AdaptedLinkType(Resource domainType, Class<?> rangeType) {\r
41         this.domainType = domainType;\r
42         this.rangeType = rangeType;\r
43     }\r
44     \r
45     @Override\r
46     public Resource createDomainElement(WriteGraph g, Object rangeElement)\r
47                 throws MappingException {\r
48         try {\r
49                 IAdaptable adaptable = (IAdaptable)rangeElement;\r
50                 Resource res = (Resource)adaptable.getAdapter(Resource.class);\r
51                 if (res == null)\r
52                         throw new NullPointerException();\r
53                 return res;\r
54         } catch (Exception e) {\r
55                 throw new MappingException("Adapted object must implement IAdaptable interface to return the source Resource.", e);\r
56         }\r
57         \r
58     }\r
59     \r
60     @Override\r
61     public Object createRangeElement(ReadGraph g, Resource domainElement)\r
62                 throws MappingException {\r
63         try {\r
64                 return g.adapt(domainElement, rangeType);\r
65         } catch (DatabaseException e) {\r
66                 throw new MappingException(e);\r
67         }\r
68     }\r
69     \r
70     @Override\r
71     public boolean updateDomain(WriteGraph g, IFunction<Object, Resource> map,\r
72                 Resource domainElement, Object rangeElement)\r
73                 throws MappingException {\r
74         return false;\r
75     }\r
76     @Override\r
77     public boolean updateRange(ReadGraph g, IFunction<Resource, Object> map,\r
78                 Resource domainElement, Object rangeElement)\r
79                 throws MappingException {\r
80         return false;\r
81     }\r
82 }\r