1 /*******************************************************************************
\r
2 * Copyright (c) 2012 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.utils.imports;
\r
14 import org.eclipse.core.runtime.IProgressMonitor;
\r
15 import org.eclipse.core.runtime.IStatus;
\r
16 import org.eclipse.core.runtime.Status;
\r
17 import org.simantics.Simantics;
\r
18 import org.simantics.databoard.binding.Binding;
\r
19 import org.simantics.databoard.container.DataContainer;
\r
20 import org.simantics.databoard.container.DataFormatException;
\r
21 import org.simantics.databoard.container.FormatHandler;
\r
22 import org.simantics.db.Resource;
\r
23 import org.simantics.db.Session;
\r
24 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
\r
25 import org.simantics.db.layer0.migration.MigrationState;
\r
26 import org.simantics.db.layer0.migration.MigrationStateKeys;
\r
27 import org.simantics.db.layer0.migration.MigrationStep;
\r
28 import org.simantics.db.layer0.migration.MigrationUtils;
\r
29 import org.simantics.graph.representation.TransferableGraph1;
\r
30 import org.simantics.sysdyn.Activator;
\r
33 * Handler for migrating a tg and importing it to database
\r
34 * @author Teemu Lempinen
\r
37 public class SysdynImportFormatHandler implements FormatHandler<Object> {
\r
39 private final Resource parent;
\r
40 private final IProgressMonitor monitor;
\r
41 private final String migrationURI;
\r
42 private final DefaultPasteImportAdvisor importAdvisor;
\r
45 * Works 2 (2 * 1) in the monitor
\r
47 * @param migrationURI
\r
50 public SysdynImportFormatHandler(Resource parent, String migrationURI, IProgressMonitor monitor) {
\r
51 this(parent, migrationURI, monitor, null);
\r
54 public SysdynImportFormatHandler(Resource parent, String migrationURI, IProgressMonitor monitor,
\r
55 DefaultPasteImportAdvisor importAdvisor) {
\r
56 assert(parent != null);
\r
57 assert(migrationURI != null);
\r
58 this.parent = parent;
\r
59 this.monitor = monitor;
\r
60 this.migrationURI = migrationURI;
\r
61 this.importAdvisor = importAdvisor;
\r
64 private void subTask(String taskName) {
\r
66 monitor.subTask(taskName);
\r
69 private void worked(int work) {
\r
71 monitor.worked(work);
\r
75 public Binding getBinding() {
\r
76 return TransferableGraph1.BINDING;
\r
80 public Object process(DataContainer container) throws DataFormatException {
\r
81 DefaultPasteImportAdvisor ia = importAdvisor != null ? importAdvisor : new DefaultPasteImportAdvisor(parent);
\r
84 Session session = Simantics.getSession();
\r
85 TransferableGraph1 tg = (TransferableGraph1)container.content.getValue();
\r
87 MigrationStep step = MigrationUtils.getStep(session, migrationURI);
\r
88 MigrationState state = MigrationUtils.newState();
\r
89 state.setProperty(MigrationStateKeys.CURRENT_TG, tg);
\r
90 step.applyTo(monitor, session, state);
\r
91 // tg = state.getProperty(MigrationStateKeys.CURRENT_TG);
\r
96 // DefaultPasteHandler.defaultExecute(tg, parent, ia);
\r
97 MigrationUtils.importTo(monitor, session, state, parent, ia);
\r
100 Resource result = state.getProperty(MigrationStateKeys.CURRENT_RESOURCE);
\r
102 result = ia.getRoot();
\r
106 } catch (Exception e) {
\r
107 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import module failed", e);
\r