]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.team.ui/src/org/simantics/team/ui/handlers/SynchroniseAllHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.team.ui / src / org / simantics / team / ui / handlers / SynchroniseAllHandler.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in 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.team.ui.handlers;\r
13 \r
14 import java.io.File;\r
15 import java.io.IOException;\r
16 import java.nio.charset.Charset;\r
17 \r
18 import org.eclipse.core.commands.ExecutionEvent;\r
19 import org.eclipse.core.commands.ExecutionException;\r
20 import org.eclipse.core.runtime.IProgressMonitor;\r
21 import org.eclipse.core.runtime.NullProgressMonitor;\r
22 import org.eclipse.core.runtime.Platform;\r
23 import org.eclipse.jface.dialogs.MessageDialog;\r
24 import org.eclipse.ui.PlatformUI;\r
25 import org.simantics.db.Resource;\r
26 import org.simantics.db.ServerReference;\r
27 import org.simantics.db.Session;\r
28 import org.simantics.db.exception.DatabaseException;\r
29 import org.simantics.db.service.LifecycleSupport;\r
30 import org.simantics.db.service.TeamSupport;\r
31 import org.simantics.team.Activator;\r
32 import org.simantics.team.Utils;\r
33 import org.simantics.team.internal.StagingLauncher;\r
34 import org.simantics.team.internal.StagingLauncher.Config;\r
35 import org.simantics.team.internal.StagingLauncher.StagingResult;\r
36 import org.simantics.team.ui.StageInitWizard;\r
37 import org.simantics.ui.SimanticsUI;\r
38 import org.simantics.ui.workbench.handler.AbstractPreferenceHandler;\r
39 import org.simantics.utils.FileUtils;\r
40 import org.simantics.utils.ui.ErrorLogger;\r
41 import org.simantics.utils.ui.dialogs.ShowError;\r
42 \r
43 public class SynchroniseAllHandler extends AbstractPreferenceHandler {\r
44 \r
45     public SynchroniseAllHandler() {\r
46         super(Activator.getDefault());\r
47     }\r
48     @Override\r
49     public Object execute(ExecutionEvent event) throws ExecutionException {\r
50         System.out.println("DEBUG: SynchronizeAllHandler");\r
51 //        ISelection sel = HandlerUtil.getCurrentSelection(event);\r
52 //        IStructuredSelection ss = StructuredSelection.EMPTY;\r
53 //        if (sel instanceof IStructuredSelection)\r
54 //            ss = (IStructuredSelection) sel;\r
55         try {\r
56             Session session = SimanticsUI.getSession();\r
57             File wsFolder = new File(Platform.getInstanceLocation().getURL().getFile());\r
58             File stFolder = new File(wsFolder, "staging");\r
59             File teamFolder = pullAll(session, Utils.getTeamFolder(), stFolder);\r
60             if (null == teamFolder)\r
61                 return null; // User cancelled operation.\r
62             show(session, teamFolder, stFolder);\r
63         } catch (DatabaseException e) {\r
64             ShowError.showError("Staging In.", "Staging failed", e);\r
65         }\r
66         return null;\r
67     }\r
68     static File pullAll(Session session, File teamFolder, File stFolder)\r
69     throws DatabaseException {\r
70         File dbFolder = new File(stFolder, "db");\r
71         try {\r
72             FileUtils.deleteAll(dbFolder);\r
73             File tempFolder = new File(stFolder, "temp");\r
74             FileUtils.deleteAll(tempFolder);\r
75             File indexingFolder = new File(stFolder, ".metadata/.plugins/org.simantics.db.indexing");\r
76             FileUtils.deleteAll(indexingFolder);\r
77             File procoreFolder = new File(stFolder, "metadata/.plugins/org.simantics.db.procore");\r
78             FileUtils.deleteAll(procoreFolder);\r
79         } catch (IOException e) {\r
80             throw new DatabaseException("Pull failed.", e);\r
81         }\r
82         if (null == teamFolder || !teamFolder.isDirectory()) {\r
83             final StageInitWizard.Data data = new StageInitWizard.Data(stFolder, null);\r
84             data.requireExisting = true;\r
85             if (!StageInitWizard.openInitWizard(data)) {\r
86                 Utils.setTeamFolder(null);\r
87                 return null; // User cancelled the operation.\r
88             }\r
89             teamFolder = data.teamFolder;\r
90             assert(null != teamFolder);\r
91         }\r
92         TeamSupport ts = session.getService(TeamSupport.class);\r
93         ts.pull(teamFolder, dbFolder);\r
94         return teamFolder;\r
95     }\r
96     static void show(Session session, File teamFolder, File wsFolder) {\r
97         final IProgressMonitor monitor = new NullProgressMonitor();\r
98         try {\r
99             monitor.beginTask("begin", 1300);\r
100             LifecycleSupport lfs = session.getService(LifecycleSupport.class);\r
101             ServerReference sref = lfs.getSessionReference().getServerReference();\r
102 //            SerialisationSupport ss = session.getService(SerialisationSupport.class);\r
103             String randomAccessId = "target"; // String.valueOf(ss.getRandomAccessId(target));\r
104             Resource targetLibrary = null;\r
105             final Config config = new Config(session, targetLibrary, wsFolder, teamFolder);\r
106             config.titlePrefix = wsFolder.getName();\r
107             StagingResult result = StagingLauncher.launch(config, sref.toString(), randomAccessId);\r
108             if (result.getExitValue() != 0) {\r
109                 String msg = "";\r
110                 if (result.getLogFile().exists()) {\r
111                     byte[] data = FileUtils.readFile(result.getLogFile());\r
112                     msg = new String(data, Charset.defaultCharset());\r
113                 }\r
114                 if (msg.isEmpty() || msg.matches(" *"))\r
115                     msg = "Staging crashed and burned. Contact application support.";\r
116                 ShowError.showError("Staging failed, errorcode="+result.getExitValue(), msg, new Exception());\r
117             }\r
118             if (result.getMessageLog() != null) {\r
119                 MessageDialog.openError(PlatformUI.getWorkbench().getDisplay().getActiveShell(),\r
120                         "Staging failed",\r
121                         result.getMessageLog());\r
122             }\r
123             } catch (Exception e) {\r
124                 ErrorLogger.defaultLogError(e);\r
125             } finally {\r
126                 monitor.done();\r
127             }\r
128     }\r
129 }\r