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