]> gerrit.simantics Code Review - simantics/platform.git/blob
a2be5fa42bf978083642deb38b68807e46dc3329
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2007, 2013 Association for Decentralized Information Management
3  * in 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.annotations.factories;
13
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Method;
16
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.objmap.backward.IBackwardMapping;
23 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
24 import org.simantics.objmap.exceptions.MappingException;
25 import org.simantics.objmap.forward.IForwardMapping;
26 import org.simantics.objmap.graph.rules.factory.IMethodRuleFactory;
27
28 public class UpdateMethodFactory<Domain, Range> implements IMethodRuleFactory<Domain, Range> {
29
30     static Logger LOGGER = LoggerFactory.getLogger(UpdateMethodFactory.class);
31     
32     @Override
33     public IBidirectionalMappingRule<Domain, Range> create(ReadGraph g, 
34             Annotation annotation, 
35             final Method method)
36             throws DatabaseException {
37         method.setAccessible(true);
38         return new IBidirectionalMappingRule<Domain,Range>() {
39             
40             @Override
41             public boolean updateRange(ReadGraph g, IForwardMapping<Domain, Range> map,
42                     Domain domainElement, Range rangeElement)
43                     throws MappingException {
44                 LOGGER.trace("    UpdateMethodFactory.updateRange");      
45                 try {
46                     return (Boolean)method.invoke(rangeElement, g, domainElement);
47                 } catch (Exception e) {
48                     // TODO Auto-generated catch block
49                     e.printStackTrace();
50                 }
51                 return false;
52             }
53             
54             @Override
55             public boolean updateDomain(WriteGraph g, IBackwardMapping<Domain,Range> map,
56                     Domain domainElement, Range rangeElement)
57                     throws MappingException {
58                 return false;
59             }
60             
61             public void createDomain(WriteGraph g, IBackwardMapping<Domain,Range> map, Domain domainElement, Range rangeElement) throws MappingException {
62                 updateDomain(g, map, domainElement, rangeElement);
63             };
64             
65             public void createRange(ReadGraph g, IForwardMapping<Domain,Range> map, Domain domainElement, Range rangeElement) throws MappingException {
66                 updateRange(g, map, domainElement, rangeElement);
67             };
68         };
69     }
70
71 }