]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetSessionManager.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / SpreadsheetSessionManager.java
1 package org.simantics.spreadsheet.graph;
2
3 import java.io.BufferedInputStream;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.ObjectInputStream;
9 import java.io.ObjectStreamClass;
10 import java.util.Collection;
11 import java.util.HashSet;
12 import java.util.Set;
13
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.layer0.StandardRealm;
18 import org.simantics.db.layer0.StandardSessionManager;
19 import org.simantics.db.layer0.variable.ProxyVariables;
20 import org.simantics.db.layer0.variable.Variable;
21 import org.simantics.db.layer0.variable.Variables;
22 import org.simantics.spreadsheet.graph.synchronization.SpreadsheetSynchronizationEventHandler;
23 import org.simantics.spreadsheet.resource.SpreadsheetResource;
24 import org.simantics.structural.synchronization.Synchronizer;
25
26 public class SpreadsheetSessionManager extends StandardSessionManager<SheetNode, SpreadsheetBook> {
27
28         private static SpreadsheetSessionManager INSTANCE;
29         
30         public static SpreadsheetSessionManager getInstance() {
31                 if(INSTANCE == null) {
32                         INSTANCE = new SpreadsheetSessionManager();
33                 }
34                 return INSTANCE;
35         }
36         
37         public class ClassLoaderObjectInputStream extends ObjectInputStream{
38
39              private ClassLoader classLoader;
40
41              public ClassLoaderObjectInputStream(ClassLoader classLoader, InputStream in) throws IOException {
42                   super(in);
43                   this.classLoader = classLoader;
44              }
45              
46              @Override
47              protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException{
48              
49                   try{
50                        String name = desc.getName();
51                        return Class.forName(name, false, classLoader);
52                   }
53                   catch(ClassNotFoundException e){
54                        return super.resolveClass(desc);
55                   }
56              }
57         }
58         
59     private SpreadsheetBook getPossibleInitialCondition(ReadGraph graph, Resource context, Resource book) throws DatabaseException {
60         SpreadsheetResource SR = SpreadsheetResource.getInstance(graph);
61         Collection<Resource> ics = graph.getObjects(context, SR.HasInitialCondition);
62         
63         Resource found = null;
64         Set<Resource> founds = new HashSet<>();
65         Set<Resource> foundDefaults = new HashSet<>();
66
67         for (Resource initialCondition : ics) {
68             if (graph.hasStatement(book, SR.Book_HasDefaultInitialCondition, initialCondition)) {
69                 foundDefaults.add(initialCondition);
70             }
71             if (graph.hasStatement(initialCondition, SR.InitialCondition_ConditionOf, book)) {
72                 founds.add(initialCondition);
73             }
74         }
75         if (foundDefaults.size() == 1) {
76             found = foundDefaults.iterator().next();
77         } else if (foundDefaults.size() == 0 && founds.size() == 1) {
78             found = founds.iterator().next();
79         } else {
80             System.err.println("Could not find IC for SpreadsheetBook " + graph.getPossibleURI(book));
81             System.err.println("foundDefaults : " + foundDefaults.size());
82             if (!foundDefaults.isEmpty()) {
83                 for (Resource foundDefault : foundDefaults) {
84                     System.err.println(graph.getPossibleURI(foundDefault));
85                 }
86             }
87             System.err.println("founds : " + founds.size());
88             if (!founds.isEmpty()) {
89                 for (Resource foun : founds) {
90                     System.err.println(graph.getPossibleURI(foun));
91                 }
92             }
93         }
94
95         if (found != null) {
96             try {
97                 File tmp = SpreadsheetGraphUtils.extractInitialCondition(graph, found);
98                 System.err.println("Extracting IC from " + tmp.getAbsolutePath());
99                 InputStream fileIn = new BufferedInputStream(new FileInputStream(tmp));
100                 ObjectInputStream in = new ClassLoaderObjectInputStream(getClass().getClassLoader(), fileIn);
101                 SpreadsheetBook srBook = (SpreadsheetBook) in.readObject();
102                 in.close();
103                 return srBook;
104             } catch (IOException e) {
105                 throw new DatabaseException(e);
106             } catch (ClassNotFoundException e) {
107                 throw new DatabaseException(e);
108             }
109         }
110         return null;
111     }
112         
113     @Override
114     protected SpreadsheetBook createEngine(ReadGraph graph, String id) throws DatabaseException {
115
116         Variable run = Variables.getVariable(graph, id);
117         Variable context = ProxyVariables.proxyVariableInput(graph, run);
118         if (context != null) {
119             Variable base = ProxyVariables.proxyVariableBase(graph, run);
120             Resource bookResource = base.getRepresents(graph);
121             Resource contextResource = context.getRepresents(graph);
122             if (contextResource != null) {
123                 SpreadsheetBook ic = getPossibleInitialCondition(graph, contextResource, bookResource);
124                 if (ic != null)
125                     return ic;
126             }
127
128             SpreadsheetBook ic = getPossibleInitialCondition(graph, bookResource, bookResource);
129             if (ic != null)
130                 return ic;
131         }
132
133         SpreadsheetBook book = new SpreadsheetBook();
134         
135         Variable base = ProxyVariables.proxyVariableBase(graph, run);
136         Resource bookResource = base.getRepresents(graph);
137         Variable configuration = Variables.getVariable(graph, bookResource);
138         
139         SpreadsheetSynchronizationEventHandler handler = new SpreadsheetSynchronizationEventHandler(graph, book);
140         Synchronizer synchronizer = new Synchronizer(graph);
141         synchronizer.fullSynchronization(configuration, handler);
142         
143         return book;
144
145     }
146
147         @Override
148         protected StandardRealm<SheetNode, SpreadsheetBook> createRealm(SpreadsheetBook engine, String id) {
149                 return new SpreadsheetRealm(engine, id);
150         }
151         
152 //    static ConcurrentHashMap<String, SpreadsheetRealm> REALMS =
153 //            new ConcurrentHashMap<String, SpreadsheetRealm>(); 
154 //    
155 //    static ConcurrentHashMap<String, NodeSupport<String>> SUPPORTS =
156 //            new ConcurrentHashMap<String, NodeSupport<String>>(); 
157 //    
158 //    public static SpreadsheetRealm sclRealmById(String id) {
159 //        // CONNECTIONS is ConcurrentHashMap so no synchronization is needed here
160 //        return REALMS.get(id);
161 //    }
162 //    
163 //    public static NodeSupport<String> getOrCreateNodeSupport(String id) {
164 //        synchronized(SUPPORTS) {
165 //              NodeSupport<String> result = SUPPORTS.get(id);
166 //              if(result == null) {
167 //                      SpreadsheetRealm realm = getOrCreateSCLRealm(id);
168 //                      result = new NodeSupport<String>(realm.getNodeManager());
169 //                      SUPPORTS.put(id, result);
170 //              }
171 //              return result;
172 //        }
173 //    }
174 //    
175 //    public static SpreadsheetRealm createRealm() {
176 //        synchronized(REALMS) {
177 //            String id = UUID.randomUUID().toString();
178 //            return createRealm(id);
179 //        }
180 //    }
181 //    
182 //    public static SpreadsheetRealm getOrCreateSCLRealm(String id) {
183 //        synchronized(REALMS) {
184 //              SpreadsheetRealm session = sclRealmById(id);
185 //            if(session == null)
186 //                return createRealm(id);
187 //            else
188 //                return session;
189 //        }
190 //    }
191 //    
192 //    private static SpreadsheetRealm createRealm(String id) {
193 //        SpreadsheetBook book = new SpreadsheetBook();
194 //        SpreadsheetRealm realm = new SpreadsheetRealm(book, id);
195 //        REALMS.put(id, realm);
196 //        return realm;
197 //    }
198 //    
199 //    public static void removeRealm(String id) {
200 //              REALMS.remove(id);
201 //    }
202         
203 }