1 /*******************************************************************************
\r
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
\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.objmap.structural.schema;
\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.backward.IBackwardMapping;
\r
21 import org.simantics.objmap.exceptions.MappingException;
\r
22 import org.simantics.objmap.forward.IForwardMapping;
\r
23 import org.simantics.objmap.graph.schema.ILinkType;
\r
24 import org.simantics.objmap.structural.IStructuralObject;
\r
25 import org.simantics.objmap.structural.StructuralResource;
\r
28 * A link type that is associated with adaptable resource (ReadGraph.getAdapter(Resource,Class)).
\r
29 * The adapted object must implement IAdaptable interface for returning the original Resource.
\r
31 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
34 public class AdaptedLinkType implements ILinkType<StructuralResource,IStructuralObject> {
\r
37 //static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
\r
39 Resource domainType;
\r
43 public AdaptedLinkType(Resource domainType, Class<?> rangeType) {
\r
44 this.domainType = domainType;
\r
45 this.rangeType = rangeType;
\r
49 public StructuralResource createDomainElement(WriteGraph g, IStructuralObject rangeElement)
\r
50 throws MappingException {
\r
52 IAdaptable adaptable = (IAdaptable)rangeElement;
\r
53 Resource res = (Resource)adaptable.getAdapter(Resource.class);
\r
55 throw new NullPointerException();
\r
56 return new StructuralResource(g,res);
\r
57 } catch (Exception e) {
\r
58 throw new MappingException("Adapted object must implement IAdaptable interface to return the source Resource.", e);
\r
65 public IStructuralObject createRangeElement(ReadGraph g, StructuralResource domainElement)
\r
66 throws MappingException {
\r
68 // FIXME : this will not work, we cannot assume that adapted objects would implement IStructuralObject interface.
\r
69 return (IStructuralObject)g.adapt(domainElement.getResource(), rangeType);
\r
70 } catch (DatabaseException e) {
\r
71 throw new MappingException(e);
\r
75 public void createDomain(WriteGraph graph, IBackwardMapping<StructuralResource, IStructuralObject> mapping, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
\r
79 public void createRange(ReadGraph graph, org.simantics.objmap.forward.IForwardMapping<StructuralResource, IStructuralObject> mapping, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
\r
83 public boolean updateDomain(WriteGraph g, IBackwardMapping<StructuralResource, IStructuralObject> map, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
\r
87 public boolean updateRange(ReadGraph g, IForwardMapping<StructuralResource, IStructuralObject> map, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
\r