]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
260c058b164e60bb42a0f197cb383ce78f6b8516
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.utils.imports;\r
13 \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
31 \r
32 /**\r
33  * Handler for migrating a tg and importing it to database\r
34  * @author Teemu Lempinen\r
35  *\r
36  */\r
37 public class SysdynImportFormatHandler implements FormatHandler<Object> {\r
38     \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
43     \r
44     /**\r
45      * Works 2 (2 * 1) in the monitor\r
46      * @param parent\r
47      * @param migrationURI\r
48      * @param monitor\r
49      */\r
50     public SysdynImportFormatHandler(Resource parent, String migrationURI, IProgressMonitor monitor) {\r
51         this(parent, migrationURI, monitor, null);\r
52     }\r
53     \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
62     }\r
63 \r
64     private void subTask(String taskName) {\r
65         if(monitor != null)\r
66             monitor.subTask(taskName);\r
67     }\r
68     \r
69     private void worked(int work) {\r
70         if(monitor != null)\r
71             monitor.worked(work);\r
72     }\r
73     \r
74     @Override\r
75     public Binding getBinding() {\r
76         return TransferableGraph1.BINDING;\r
77     }\r
78 \r
79     @Override\r
80     public Object process(DataContainer container) throws DataFormatException {\r
81         DefaultPasteImportAdvisor ia = importAdvisor != null ? importAdvisor : new DefaultPasteImportAdvisor(parent);\r
82         try {\r
83             subTask("Migrate");\r
84             Session session = Simantics.getSession();\r
85             TransferableGraph1 tg = (TransferableGraph1)container.content.getValue();\r
86 \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
92 \r
93             worked(1);\r
94             subTask("Import");\r
95             \r
96 //            DefaultPasteHandler.defaultExecute(tg, parent, ia);\r
97             MigrationUtils.importTo(monitor, session, state, parent, ia);\r
98             worked(1);\r
99             \r
100             Resource result = state.getProperty(MigrationStateKeys.CURRENT_RESOURCE);\r
101             if(result == null)\r
102                 result = ia.getRoot();\r
103             \r
104             return result;\r
105 \r
106         } catch (Exception e) {\r
107             return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import module failed", e);\r
108         }\r
109     }\r
110 \r
111 }\r