]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/MigrationUtils.java
5f20036a54e1ea75638fba8699a1f7044e8cb26c
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / migration / MigrationUtils.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * 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.db.layer0.migration;
13
14 import java.io.DataInputStream;
15 import java.io.File;
16 import java.io.IOException;
17 import java.net.MalformedURLException;
18 import java.net.URL;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.Map;
24 import java.util.Set;
25
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.NullProgressMonitor;
28 import org.simantics.databoard.Bindings;
29 import org.simantics.databoard.adapter.AdaptException;
30 import org.simantics.databoard.binding.mutable.Variant;
31 import org.simantics.databoard.container.DataContainer;
32 import org.simantics.databoard.container.DataContainers;
33 import org.simantics.db.ReadGraph;
34 import org.simantics.db.Resource;
35 import org.simantics.db.Session;
36 import org.simantics.db.WriteGraph;
37 import org.simantics.db.WriteOnlyGraph;
38 import org.simantics.db.common.CommentMetadata;
39 import org.simantics.db.common.primitiverequest.PossibleResource;
40 import org.simantics.db.common.request.BinaryRead;
41 import org.simantics.db.common.request.FreshEscapedName;
42 import org.simantics.db.common.request.UnaryRead;
43 import org.simantics.db.common.request.WriteRequest;
44 import org.simantics.db.common.utils.VersionMap;
45 import org.simantics.db.common.utils.VersionMapRequest;
46 import org.simantics.db.common.utils.Versions;
47 import org.simantics.db.exception.DatabaseException;
48 import org.simantics.db.layer0.adapter.Instances;
49 import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;
50 import org.simantics.db.layer0.adapter.impl.SharedOntologyImportAdvisor;
51 import org.simantics.db.layer0.adapter.impl.TrashBinRemover;
52 import org.simantics.db.layer0.internal.SimanticsInternal;
53 import org.simantics.db.layer0.util.ExternalDownloadBean;
54 import org.simantics.db.layer0.util.Layer0Utils;
55 import org.simantics.db.layer0.util.TGTransferableGraphSource;
56 import org.simantics.db.service.XSupport;
57 import org.simantics.graph.db.IImportAdvisor;
58 import org.simantics.graph.db.ImportResult;
59 import org.simantics.graph.db.MissingDependencyException;
60 import org.simantics.graph.db.TransferableGraphException;
61 import org.simantics.graph.representation.Identity;
62 import org.simantics.graph.representation.Root;
63 import org.simantics.graph.representation.TransferableGraph1;
64 import org.simantics.graph.representation.TransferableGraphUtils;
65 import org.simantics.layer0.Layer0;
66 import org.simantics.utils.datastructures.Pair;
67 import org.simantics.utils.datastructures.collections.CollectionUtils;
68
69 public class MigrationUtils {
70         
71         public static final boolean DEBUG = false;
72
73     public static MigrationState newState() {
74         return new MigrationStateImpl();
75     }
76
77     public static MigrationStep getStep(Session session, String uri) throws DatabaseException {
78         return session.sync(new UnaryRead<String, MigrationStep>(uri) {
79
80             @Override
81             public MigrationStep perform(ReadGraph graph) throws DatabaseException {
82                 Resource r = graph.getResource(parameter);
83                 return graph.adapt(r, MigrationStep.class);
84             }
85
86         });
87     }
88
89 //    public static TransferableGraph1 getTG(Session session, MigrationState state) {
90 //        return getTG(session, state, true, false);
91 //    }
92     
93     public static void clearTempResource(Session session, final Resource resource) {
94         session.asyncRequest(new WriteRequest() {
95
96             @Override
97             public void perform(WriteGraph graph) throws DatabaseException {
98                 graph.deny(resource, Layer0.getInstance(graph).PartOf);
99             }
100         });
101     }
102
103     public static Collection<Resource> importTo(IProgressMonitor monitor, Session session, MigrationState state, final Resource parent, final IImportAdvisor advisor) throws DatabaseException, TransferableGraphException {
104         final Resource resource = getResource(monitor, session, state);
105         final ArrayList<Resource> result = new ArrayList<Resource>();
106         if(resource != null) {
107             session.syncRequest(new WriteRequest() {
108                 
109                 @Override
110                 public void perform(WriteGraph graph) throws DatabaseException {
111                     
112                         Layer0 L0 = Layer0.getInstance(graph);
113                         
114                         for(Resource root : graph.getObjects(resource, L0.ConsistsOf)) {
115                                 
116                                 String baseName = Versions.getBaseName(graph, root);
117                             String version = Versions.getVersion(graph, root);
118                             if(version != null) {
119                                         VersionMap map = graph.syncRequest(new VersionMapRequest(parent));
120                                         if(map.contains(baseName, version)) {
121                                     String newName = graph.syncRequest(new FreshEscapedName(parent, Layer0.getInstance(graph).ConsistsOf, baseName));
122                                         graph.claimLiteral(root, L0.HasName, newName + "@1", Bindings.STRING);
123                                         }
124                             } else {
125                             String newName = graph.syncRequest(new FreshEscapedName(parent, Layer0.getInstance(graph).ConsistsOf, baseName));
126                             if(!newName.equals(baseName)) {
127                                 graph.claimLiteral(root, L0.HasName, newName, Bindings.STRING);
128                             }
129                             }
130
131                             graph.deny(root, L0.PartOf);
132                             graph.claim(root, L0.PartOf, parent);
133
134                             CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
135                             graph.addMetadata(cm.add("Imported " + graph.getURI(root) + ", resource " + root));
136                             
137                             result.add(root);
138                     
139                         }
140                         
141                         graph.deny(resource, L0.PartOf);
142
143                 }
144             });
145         } else {
146             TransferableGraph1 tg = getTG(session, state);
147             if(tg != null) {
148                 DefaultPasteHandler.defaultExecute(tg, parent, new IImportAdvisor() {
149                     
150                     @Override
151                     public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {
152                         Resource r = advisor.createRoot(graph, root);
153                         result.add(r);
154                         return r;
155                     }
156                     
157                     @Override
158                     public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {
159                         return advisor.analyzeRoot(graph, root);
160                     }
161                 });
162             }
163         }
164         return result;
165     }
166     
167     
168         public static Collection<MigrationStep> getMigrationSteps(DataContainer header) throws DatabaseException {
169         
170         return SimanticsInternal.sync(new BinaryRead<String,Integer,Collection<MigrationStep>>(header.format, header.version) {
171
172                         @Override
173                         public Collection<MigrationStep> perform(ReadGraph graph) throws DatabaseException {
174                                 
175                                 Layer0 L0 = Layer0.getInstance(graph);
176                                 ArrayList<Pair<Double,MigrationStep>> steps = new ArrayList<Pair<Double,MigrationStep>>();
177                         Instances query = graph.adapt(L0.Migration, Instances.class);
178                         Set<Resource> migrations = new HashSet<Resource>();
179                                 for(Resource ontology : Layer0Utils.listOntologies(graph)) {
180                                         migrations.addAll(Layer0Utils.sortByCluster(graph, query.find(graph, ontology)));
181                                 }
182                                 for(Resource migration : migrations) {
183                                         if(DEBUG)
184                                                 System.err.println("getMigrationSteps: " + graph.getURI(migration));
185                                         String format = graph.getRelatedValue(migration, L0.Migration_format);
186                                         if(DEBUG)
187                                                 System.err.println("-format=" + format);
188                                         if(parameter.equals(format)) {
189                                                 Integer from = graph.getRelatedValue(migration, L0.Migration_from);
190                                                 if(DEBUG)
191                                                         System.err.println("-from=" + from);
192                                                 Resource step = graph.getSingleObject(migration, L0.Migration_step);
193                                                 if(parameter2.equals(from)) {
194                                                         Double priority = graph.getRelatedValue(migration, L0.Migration_priority);
195                                                         steps.add(Pair.make(-priority, graph.adapt(step, MigrationStep.class))); 
196                                                         if(DEBUG)
197                                                                 System.err.println("=> ACCEPT");
198                                                 } else {
199                                                         if(DEBUG)
200                                                                 System.err.println("=> REJECT");
201                                                 }
202                                         }
203                                 }
204                                 /*
205                                 Resource base = graph.getResource(baseURI);
206                         if(DEBUG)
207                                 System.err.println("getMigrationSteps format=" + parameter + ", version=" + parameter2);
208                                 for(Resource migration : graph.sync(new ObjectsWithType(base, L0.ConsistsOf, L0.Migration))) {
209                                 }*/
210                                 return CollectionUtils.sortByFirst(steps);
211                         }
212                 
213         });
214     }
215     
216     public static Resource importMigrated(IProgressMonitor monitor, Session session, File modelFile, MigrationState state, IImportAdvisor advisor, Resource target) throws Exception {
217         Collection<Resource> roots = importMigratedMany(monitor, session, modelFile, state, advisor, target);
218         if(roots.size() == 1) {
219             return roots.iterator().next();
220         } else {
221             return null;
222         }
223     }
224
225     public static Collection<Resource> importMigratedMany(IProgressMonitor monitor, Session session, File modelFile, MigrationState state, IImportAdvisor advisor, Resource target) throws Exception {
226
227         //assert(target != null);
228         assert(advisor != null);
229         
230         if(monitor == null) monitor = new NullProgressMonitor();
231         
232         if(DEBUG)
233                 System.err.println("importMigrated: file=" + (modelFile != null ? modelFile.getAbsolutePath() : null));
234         
235         //String baseURI = state.getProperty(MigrationStateKeys.BASE_URI);
236 //      if(DEBUG)
237 //              System.err.println("importMigrated: baseURI=" + baseURI);
238
239         state.setProperty(MigrationStateKeys.MODEL_FILE, modelFile);
240         state.setProperty(MigrationStateKeys.SESSION, session);
241         state.setProperty(MigrationStateKeys.PROGRESS_MONITOR, monitor);
242         state.setProperty(MigrationStateKeys.IMPORT_ADVISOR, advisor);
243
244         DataContainer dc = state.getProperty(MigrationStateKeys.CURRENT_DATA_CONTAINER);
245         Collection<MigrationStep> migration = getMigrationSteps(dc);
246
247 //        TransferableGraph1 tg = state.getProperty(MigrationStateKeys.CURRENT_TG);
248 //        state.setProperty(MigrationStateKeys.TG_EXTENSIONS, tg.extensions);
249         
250         for(MigrationStep step : migration) {
251             step.applyTo(monitor, session, state);
252             if (monitor.isCanceled())
253                 break;
254         }
255
256         if (monitor.isCanceled()) {
257             // Move possibly created material into TrashBin and quit.
258             final Resource root = state.probeProperty(MigrationStateKeys.CURRENT_RESOURCE);
259             if (root != null) {
260                 session.syncRequest(new WriteRequest() {
261                     @Override
262                     public void perform(WriteGraph graph) throws DatabaseException {
263                         new TrashBinRemover(root).remove(graph);
264                     }
265                 });
266             }
267             return Collections.emptyList();
268         }
269
270         // Absolute path imports end up here
271         if(target == null) {
272                 Collection<Resource> roots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
273                 return roots;
274         }
275                         
276         // Finally import model into final destination
277         return importTo(monitor, session, state, target, advisor);
278         
279     }
280     
281     public static TransferableGraph1 getTG(Session session, MigrationState state) throws DatabaseException {
282         return state.getProperty(MigrationStateKeys.CURRENT_TG);
283     }
284     
285     public static Resource getResource(IProgressMonitor monitor, Session session, MigrationState state) throws DatabaseException {
286         return state.getProperty(MigrationStateKeys.CURRENT_RESOURCE);
287     }
288
289     public static Collection<Resource> getRootResources(IProgressMonitor monitor, Session session, MigrationState state) throws DatabaseException {
290         return state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
291     }
292
293     /**
294      * Get a property from the specified MigrationState and return specified
295      * default value if the property value is <code>null</code>.
296      * 
297      * @param state the state to get the property from
298      * @param key the property to get
299      * @param defaultValue
300      *            the default value to return if the property value is
301      *            <code>null</code>
302      * @return the property value
303      * @throws DatabaseException
304      *             if fetching the property fails for some reason
305      */
306     public static <T> T getProperty(MigrationState state, String key, T defaultValue) throws DatabaseException {
307         T t = state.getProperty(key);
308         return t != null ? t : defaultValue;
309     }
310
311     public static MigratedImportResult importSharedOntology(Session session, TransferableGraph1 tg, boolean published) throws DatabaseException {
312         return importSharedOntology(null, session, tg, published);
313     }
314
315     public static MigratedImportResult importSharedOntology(IProgressMonitor monitor, Session session, TransferableGraph1 tg, boolean published) throws DatabaseException {
316         
317         if(monitor == null) monitor = new NullProgressMonitor();
318         
319         Variant edbVariant = tg.extensions.get(ExternalDownloadBean.EXTENSION_KEY);
320         if(edbVariant != null) {
321                 try {
322                                 ExternalDownloadBean edb = (ExternalDownloadBean)edbVariant.getValue(ExternalDownloadBean.BINDING);
323                                 for(Map.Entry<String, String> entry : edb.downloads.entrySet()) {
324                                         String uri = entry.getKey();
325                                         Resource existing = session.syncRequest(new PossibleResource(uri));
326                                         if(existing == null) {
327                                                 String download = entry.getValue();
328                                                  URL url = new URL(download);
329                                                  DataContainer container = DataContainers.readFile(new DataInputStream(url.openStream()));
330                                                  TransferableGraph1 dependencyTg = (TransferableGraph1) container.content.getValue(TransferableGraph1.BINDING);
331                                                  importSharedOntology(monitor, session, dependencyTg, true);
332                                         }
333                                 }
334                         } catch (AdaptException e) {
335                                 throw new DatabaseException(e);
336                         } catch (MalformedURLException e) {
337                                 throw new DatabaseException(e);
338                         } catch (IOException e) {
339                                 throw new DatabaseException(e);
340                         }
341                 
342         }
343         
344         Collection<Identity> roots = TransferableGraphUtils.getRoots(tg);
345         if(roots.size() == 1) {
346             try {
347                 TGTransferableGraphSource tgSource = new TGTransferableGraphSource(tg);
348                 SharedOntologyImportAdvisor advisor = new SharedOntologyImportAdvisor(published);
349
350                 MigrationState state = newState();
351                 state.setProperty(MigrationStateKeys.UPDATE_DEPENDENCIES, false);
352                 state.setProperty(MigrationStateKeys.CURRENT_TGS, tgSource);
353                 state.setProperty(MigrationStateKeys.SESSION, session);
354                 state.setProperty(MigrationStateKeys.PROGRESS_MONITOR, monitor);
355                 state.setProperty(MigrationStateKeys.CURRENT_DATA_CONTAINER, new DataContainer("sharedLibrary", 1, new Variant(TransferableGraph1.BINDING, tg)));
356
357                 MigrationUtils.importMigrated(monitor, session, null, state, advisor, null);
358
359                 Collection<Resource> resultRoots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
360                 ImportResult result = state.getProperty(MigrationStateKeys.IMPORT_RESULT);
361                 return new MigratedImportResult(resultRoots, result);
362                         } catch (TransferableGraphException e) {
363                                 throw new DatabaseException(e);
364                         } catch (MissingDependencyException e) {
365                                 throw e;
366                         } catch (DatabaseException e) {
367                                 throw e;
368                         } catch (Exception e) {
369                             throw new DatabaseException(e);
370             } finally {
371                 session.getService(XSupport.class).setServiceMode(false, false);
372             }
373             
374         }
375         
376         return null;
377         
378     }
379     
380 }