]> gerrit.simantics Code Review - simantics/platform.git/blob
bfef0a95ac823adbc455a495a9471461222fa485
[simantics/platform.git] /
1 package org.simantics.db.services.adaption.reflection;
2
3 import org.simantics.db.AsyncReadGraph;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.adaption.Adapter;
7 import org.simantics.db.common.request.ReadRequest;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.procedure.AsyncProcedure;
10
11 public abstract class AbstractReflectionAdapter<T> implements Adapter<T,Resource>  {
12     IDynamicAdapter2[] parameters;
13     
14     public AbstractReflectionAdapter(IDynamicAdapter2 ... parameters) throws SecurityException, NoSuchMethodException, DatabaseException {
15         this.parameters = parameters;
16     }
17     
18     public abstract void construct(AsyncReadGraph g, AsyncProcedure<T> procedure, Object ... args);
19     
20     @Override
21     public void adapt(AsyncReadGraph g, final Resource source, final Resource r, final AsyncProcedure<T> procedure) {
22
23         if(parameters.length == 0) {
24             
25 //            System.out.println("ReflectionAdapter2 " + ReflectionAdapter2.this);
26             construct(g, procedure);
27             
28         } else if( parameters.length == 1 && parameters[0] instanceof ThisResource2) {
29
30             construct(g, procedure, r);
31             
32         } else {
33         
34             g.asyncRequest(new ReadRequest() {
35     
36                 @Override
37                 public void run(ReadGraph graph) throws DatabaseException {
38                     
39 //                    System.out.println("ReflectionAdapter2 " + ReflectionAdapter2.this);
40                     
41                     Object[] args = new Object[parameters.length];
42                     for(int i=0;i<parameters.length;++i)
43                         args[i] = parameters[i].adapt(graph, r);
44                     construct(graph, procedure, args);
45                 }
46                 
47             });
48         
49         }
50         
51     }
52 }