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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.team.ui.handlers;
\r
14 import java.io.File;
\r
15 import java.nio.charset.Charset;
\r
17 import org.eclipse.core.commands.ExecutionEvent;
\r
18 import org.eclipse.core.commands.ExecutionException;
\r
19 import org.eclipse.core.runtime.IProgressMonitor;
\r
20 import org.eclipse.core.runtime.NullProgressMonitor;
\r
21 import org.eclipse.core.runtime.Platform;
\r
22 import org.eclipse.jface.dialogs.MessageDialog;
\r
23 import org.eclipse.ui.PlatformUI;
\r
24 import org.simantics.db.Resource;
\r
25 import org.simantics.db.ServerReference;
\r
26 import org.simantics.db.Session;
\r
27 import org.simantics.db.exception.DatabaseException;
\r
28 import org.simantics.db.service.LifecycleSupport;
\r
29 import org.simantics.team.Activator;
\r
30 import org.simantics.team.internal.StagingLauncher;
\r
31 import org.simantics.team.internal.StagingLauncher.Config;
\r
32 import org.simantics.team.internal.StagingLauncher.StagingResult;
\r
33 import org.simantics.ui.SimanticsUI;
\r
34 import org.simantics.ui.workbench.handler.AbstractPreferenceHandler;
\r
35 import org.simantics.utils.FileUtils;
\r
36 import org.simantics.utils.ui.ErrorLogger;
\r
37 import org.simantics.utils.ui.dialogs.ShowError;
\r
39 public class StageEditHandler extends AbstractPreferenceHandler {
\r
41 public StageEditHandler() {
\r
42 super(Activator.getDefault());
\r
44 private void showError(String msg, Throwable t) {
\r
45 String title = "Staging failed!";
\r
46 ShowError.showError(title, msg, t);
\r
49 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
50 System.out.println("DEBUG: EditStageHandler");
\r
52 Session session = SimanticsUI.getSession();
\r
53 File wsFolder = new File(Platform.getInstanceLocation().getURL().getFile());
\r
54 File stFolder = new File(wsFolder, "staging");
\r
55 if (!stFolder.isDirectory())
\r
56 throw new DatabaseException("Folder must exist. path=" + stFolder.getAbsolutePath());
\r
57 show(session, stFolder);
\r
58 } catch (Throwable t) {
\r
59 showError("Staging failed:", t);
\r
64 private void show(Session session, File stFolder) {
\r
65 final IProgressMonitor monitor = new NullProgressMonitor();
\r
67 monitor.beginTask("begin", 1300);
\r
68 LifecycleSupport lfs = session.getService(LifecycleSupport.class);
\r
69 ServerReference sref = lfs.getSessionReference().getServerReference();
\r
70 // SerialisationSupport ss = session.getService(SerialisationSupport.class);
\r
71 String randomAccessId = "target"; // String.valueOf(ss.getRandomAccessId(target));
\r
72 Resource targetLibrary = null;
\r
73 final Config config = new Config(session, targetLibrary, stFolder, null);
\r
74 StagingResult result = StagingLauncher.launch(config, sref.toString(), randomAccessId);
\r
75 if (result.getExitValue() != 0) {
\r
77 if (result.getLogFile().exists()) {
\r
78 byte[] data = FileUtils.readFile(result.getLogFile());
\r
79 msg = new String(data, Charset.defaultCharset());
\r
81 if (msg.isEmpty() || msg.matches(" *"))
\r
82 msg = "Staging crashed. Contact application support.";
\r
83 ShowError.showError("Staging failed, errorcode="+result.getExitValue(), msg, new Exception());
\r
85 if (result.getMessageLog() != null) {
\r
86 MessageDialog.openError(PlatformUI.getWorkbench().getDisplay().getActiveShell(),
\r
88 result.getMessageLog());
\r
90 } catch (Exception e) {
\r
91 ErrorLogger.defaultLogError(e);
\r
96 // private String openInitDialog()
\r
97 // throws DatabaseException {
\r
98 // final Display display = PlatformUI.getWorkbench().getDisplay();
\r
99 // final StageInitDialog.Data data = new StageInitDialog.Data();
\r
100 // display.syncExec(new Runnable() {
\r
102 // public void run() {
\r
103 // data.comment = "Help!";
\r
104 // StageInitDialog d = new StageInitDialog(display.getActiveShell(), data);
\r
105 // if (Dialog.OK != d.open())
\r
106 // data.comment = null;
\r
108 // return data.comment;
\r