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.graph.schema;
14 import java.lang.reflect.InvocationTargetException;
15 import java.lang.reflect.Method;
16 import java.util.ArrayList;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.utils.NameUtils;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
25 import org.simantics.objmap.exceptions.MappingException;
26 import org.simantics.objmap.graph.annotations.GetType;
27 import org.simantics.objmap.graph.annotations.SetType;
29 public class DynamicSimpleLinkType<Range> extends SimpleLinkType<Range>{
31 protected Method typeGetter;
32 protected Method typeSetter;
34 public DynamicSimpleLinkType(Resource domainType, Class<?> rangeType, ArrayList<IBidirectionalMappingRule<Resource, Range>> rules) {
35 super(domainType, rangeType, rules);
36 findTypeGetter(rangeType);
39 public DynamicSimpleLinkType(Resource domainType, Class<?> rangeType) {
40 super(domainType, rangeType);
41 findTypeGetter(rangeType);
44 private void findTypeGetter(Class<?> clazz) {
45 for (Method m : clazz.getDeclaredMethods()) {
46 m.setAccessible(true);
47 GetType t = m.getAnnotation(GetType.class);
51 SetType t2 = m.getAnnotation(SetType.class);
56 if (typeGetter == null || typeSetter == null) {
57 Class<?> superClazz = clazz.getSuperclass();
58 if (superClazz != Object.class)
59 findTypeGetter(superClazz);
60 if (typeGetter == null || typeSetter == null) {
61 throw new RuntimeException("Cannot find dynamic type methods for class " + clazz.getSimpleName());
69 public Resource createDomainElement(WriteGraph g, Range rangeElement)
70 throws MappingException {
72 String typeUri = (String)typeGetter.invoke(rangeElement, (Object[]) null);
73 if(LOGGER.isInfoEnabled())
74 LOGGER.info("SimpleLinkType.createDomainElement " +
75 rangeElement.toString()
77 Resource actualDomainType = g.getResource(typeUri);
78 Resource result = g.newResource();
79 //g.claim(result, Layer0.getInstance(g).InstanceOf, null, domainType);
80 g.claim(result, Layer0.getInstance(g).InstanceOf, null, actualDomainType);
82 } catch(DatabaseException e) {
83 throw new MappingException(e);
84 } catch (IllegalArgumentException e) {
85 throw new MappingException(e);
86 } catch (IllegalAccessException e) {
87 throw new MappingException(e);
88 } catch (InvocationTargetException e) {
89 throw new MappingException(e.getCause());
93 @SuppressWarnings("unchecked")
95 public Range createRangeElement(ReadGraph g, Resource domainElement)
96 throws MappingException {
98 if(LOGGER.isInfoEnabled())
100 LOGGER.info("SimpleLinkType.createRangeElement " +
101 NameUtils.getSafeName(g, domainElement)
103 } catch(DatabaseException e) {
104 throw new MappingException(e);
106 Range r = (Range)rangeType.newInstance();
107 Resource type = g.getSingleType(domainElement, domainType);
108 String uri = g.getURI(type);
109 typeSetter.invoke(r, uri);
111 } catch (InstantiationException e) {
112 throw new MappingException(e);
113 } catch (IllegalAccessException e) {
114 throw new MappingException(e);
115 } catch (DatabaseException e) {
116 throw new MappingException(e);
117 } catch (IllegalArgumentException e) {
118 throw new MappingException(e);
119 } catch (InvocationTargetException e) {
120 throw new MappingException(e.getCause());