package org.simantics.g3d.objmap.schema; //import org.apache.log4j.Logger; import org.eclipse.core.runtime.IAdaptable; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.objmap.IFunction; import org.simantics.objmap.ILinkType; import org.simantics.objmap.MappingException; /** * A link type that is associated with adaptable resource (ReadGraph.getAdapter(Resource,Class)). * The adapted object must implement IAdaptable interface for returning the original Resource. * * @author Marko Luukkainen * */ public class AdaptedLinkType implements ILinkType { //static Logger LOGGER = Logger.getLogger("org.simantics.objmap"); Resource domainType; Class rangeType; public AdaptedLinkType(Resource domainType, Class rangeType) { this.domainType = domainType; this.rangeType = rangeType; } @Override public Resource createDomainElement(WriteGraph g, Object rangeElement) throws MappingException { try { IAdaptable adaptable = (IAdaptable)rangeElement; Resource res = (Resource)adaptable.getAdapter(Resource.class); if (res == null) throw new NullPointerException(); return res; } catch (Exception e) { throw new MappingException("Adapted object must implement IAdaptable interface to return the source Resource.", e); } } @Override public Object createRangeElement(ReadGraph g, Resource domainElement) throws MappingException { try { return g.adapt(domainElement, rangeType); } catch (DatabaseException e) { throw new MappingException(e); } } @Override public boolean updateDomain(WriteGraph g, IFunction map, Resource domainElement, Object rangeElement) throws MappingException { return false; } @Override public boolean updateRange(ReadGraph g, IFunction map, Resource domainElement, Object rangeElement) throws MappingException { return false; } }