1 /*******************************************************************************
2 * Copyright (c) 2012 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.db.layer0.migration;
14 import java.io.DataInputStream;
16 import java.io.IOException;
17 import java.net.MalformedURLException;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashSet;
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;
69 public class MigrationUtils {
71 public static final boolean DEBUG = false;
73 public static MigrationState newState() {
74 return new MigrationStateImpl();
77 public static MigrationStep getStep(Session session, String uri) throws DatabaseException {
78 return session.sync(new UnaryRead<String, MigrationStep>(uri) {
81 public MigrationStep perform(ReadGraph graph) throws DatabaseException {
82 Resource r = graph.getResource(parameter);
83 return graph.adapt(r, MigrationStep.class);
89 // public static TransferableGraph1 getTG(Session session, MigrationState state) {
90 // return getTG(session, state, true, false);
93 public static void clearTempResource(Session session, final Resource resource) {
94 session.asyncRequest(new WriteRequest() {
97 public void perform(WriteGraph graph) throws DatabaseException {
98 graph.deny(resource, Layer0.getInstance(graph).PartOf);
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() {
110 public void perform(WriteGraph graph) throws DatabaseException {
112 Layer0 L0 = Layer0.getInstance(graph);
114 for(Resource root : graph.getObjects(resource, L0.ConsistsOf)) {
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);
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);
131 graph.deny(root, L0.PartOf);
132 graph.claim(root, L0.PartOf, parent);
134 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
135 graph.addMetadata(cm.add("Imported " + graph.getURI(root) + ", resource " + root));
141 graph.deny(resource, L0.PartOf);
146 TransferableGraph1 tg = getTG(session, state);
148 DefaultPasteHandler.defaultExecute(tg, parent, new IImportAdvisor() {
151 public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {
152 Resource r = advisor.createRoot(graph, root);
158 public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {
159 return advisor.analyzeRoot(graph, root);
168 public static Collection<MigrationStep> getMigrationSteps(DataContainer header) throws DatabaseException {
170 return SimanticsInternal.sync(new BinaryRead<String,Integer,Collection<MigrationStep>>(header.format, header.version) {
173 public Collection<MigrationStep> perform(ReadGraph graph) throws DatabaseException {
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)));
182 for(Resource migration : migrations) {
184 System.err.println("getMigrationSteps: " + graph.getURI(migration));
185 String format = graph.getRelatedValue(migration, L0.Migration_format);
187 System.err.println("-format=" + format);
188 if(parameter.equals(format)) {
189 Integer from = graph.getRelatedValue(migration, L0.Migration_from);
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)));
197 System.err.println("=> ACCEPT");
200 System.err.println("=> REJECT");
205 Resource base = graph.getResource(baseURI);
207 System.err.println("getMigrationSteps format=" + parameter + ", version=" + parameter2);
208 for(Resource migration : graph.sync(new ObjectsWithType(base, L0.ConsistsOf, L0.Migration))) {
210 return CollectionUtils.sortByFirst(steps);
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();
225 public static Collection<Resource> importMigratedMany(IProgressMonitor monitor, Session session, File modelFile, MigrationState state, IImportAdvisor advisor, Resource target) throws Exception {
227 //assert(target != null);
228 assert(advisor != null);
230 if(monitor == null) monitor = new NullProgressMonitor();
233 System.err.println("importMigrated: file=" + (modelFile != null ? modelFile.getAbsolutePath() : null));
235 //String baseURI = state.getProperty(MigrationStateKeys.BASE_URI);
237 // System.err.println("importMigrated: baseURI=" + baseURI);
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);
244 DataContainer dc = state.getProperty(MigrationStateKeys.CURRENT_DATA_CONTAINER);
245 Collection<MigrationStep> migration = getMigrationSteps(dc);
247 // TransferableGraph1 tg = state.getProperty(MigrationStateKeys.CURRENT_TG);
248 // state.setProperty(MigrationStateKeys.TG_EXTENSIONS, tg.extensions);
250 for(MigrationStep step : migration) {
251 step.applyTo(monitor, session, state);
252 if (monitor.isCanceled())
256 if (monitor.isCanceled()) {
257 // Move possibly created material into TrashBin and quit.
258 final Resource root = state.probeProperty(MigrationStateKeys.CURRENT_RESOURCE);
260 session.syncRequest(new WriteRequest() {
262 public void perform(WriteGraph graph) throws DatabaseException {
263 new TrashBinRemover(root).remove(graph);
267 return Collections.emptyList();
270 // Absolute path imports end up here
272 Collection<Resource> roots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
276 // Finally import model into final destination
277 return importTo(monitor, session, state, target, advisor);
281 public static TransferableGraph1 getTG(Session session, MigrationState state) throws DatabaseException {
282 return state.getProperty(MigrationStateKeys.CURRENT_TG);
285 public static Resource getResource(IProgressMonitor monitor, Session session, MigrationState state) throws DatabaseException {
286 return state.getProperty(MigrationStateKeys.CURRENT_RESOURCE);
289 public static Collection<Resource> getRootResources(IProgressMonitor monitor, Session session, MigrationState state) throws DatabaseException {
290 return state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
294 * Get a property from the specified MigrationState and return specified
295 * default value if the property value is <code>null</code>.
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
302 * @return the property value
303 * @throws DatabaseException
304 * if fetching the property fails for some reason
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;
311 public static MigratedImportResult importSharedOntology(Session session, TransferableGraph1 tg, boolean published) throws DatabaseException {
312 return importSharedOntology(null, session, tg, published);
315 public static MigratedImportResult importSharedOntology(IProgressMonitor monitor, Session session, TransferableGraph1 tg, boolean published) throws DatabaseException {
317 if(monitor == null) monitor = new NullProgressMonitor();
319 Variant edbVariant = tg.extensions.get(ExternalDownloadBean.EXTENSION_KEY);
320 if(edbVariant != null) {
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);
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);
344 Collection<Identity> roots = TransferableGraphUtils.getRoots(tg);
345 if(roots.size() == 1) {
347 TGTransferableGraphSource tgSource = new TGTransferableGraphSource(tg);
348 SharedOntologyImportAdvisor advisor = new SharedOntologyImportAdvisor(published);
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)));
357 MigrationUtils.importMigrated(monitor, session, null, state, advisor, null);
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) {
366 } catch (DatabaseException e) {
368 } catch (Exception e) {
369 throw new DatabaseException(e);
371 session.getService(XSupport.class).setServiceMode(false, false);