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