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