1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.objmap.structural.schema;
14 //import org.apache.log4j.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;
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.
31 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
34 public class AdaptedLinkType implements ILinkType<StructuralResource,IStructuralObject> {
37 //static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
43 public AdaptedLinkType(Resource domainType, Class<?> rangeType) {
44 this.domainType = domainType;
45 this.rangeType = rangeType;
49 public StructuralResource createDomainElement(WriteGraph g, IStructuralObject rangeElement)
50 throws MappingException {
52 IAdaptable adaptable = (IAdaptable)rangeElement;
53 Resource res = (Resource)adaptable.getAdapter(Resource.class);
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);
65 public IStructuralObject createRangeElement(ReadGraph g, StructuralResource domainElement)
66 throws MappingException {
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);
75 public void createDomain(WriteGraph graph, IBackwardMapping<StructuralResource, IStructuralObject> mapping, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
79 public void createRange(ReadGraph graph, org.simantics.objmap.forward.IForwardMapping<StructuralResource, IStructuralObject> mapping, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
83 public boolean updateDomain(WriteGraph g, IBackwardMapping<StructuralResource, IStructuralObject> map, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
87 public boolean updateRange(ReadGraph g, IForwardMapping<StructuralResource, IStructuralObject> map, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {