]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/DumpNode.java
5bc261ea7b9b99726ae82a0f0f2b1dc3d2cee162
[lgpl/argeo-commons.git] / server / plugins / org.argeo.jcr.ui.explorer / src / main / java / org / argeo / jcr / ui / explorer / commands / DumpNode.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.jcr.ui.explorer.commands;
17
18 import java.io.File;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.text.DateFormat;
22 import java.text.SimpleDateFormat;
23 import java.util.GregorianCalendar;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27
28 import javax.jcr.Node;
29 import javax.jcr.RepositoryException;
30
31 import org.argeo.ArgeoException;
32 import org.argeo.eclipse.ui.specific.OpenFile;
33 import org.argeo.eclipse.ui.utils.CommandUtils;
34 import org.argeo.jcr.ui.explorer.JcrExplorerPlugin;
35 import org.argeo.jcr.ui.explorer.model.SingleJcrNodeElem;
36 import org.eclipse.core.commands.AbstractHandler;
37 import org.eclipse.core.commands.ExecutionEvent;
38 import org.eclipse.core.commands.ExecutionException;
39 import org.eclipse.jface.viewers.ISelection;
40 import org.eclipse.jface.viewers.IStructuredSelection;
41 import org.eclipse.ui.handlers.HandlerUtil;
42
43 /**
44 * If the method
45 * <code> HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection() </code>
46 * exits and has a SingleJcrNodeElem as first element, it canonically calls the
47 * JCR Session.exportSystemView() method on the underlying node with both
48 * skipBinary & noRecurse boolean flags set to false.
49 *
50 * Resulting stream is saved in a tmp file and opened via the "open file"
51 * single-sourced command.
52 */
53 public class DumpNode extends AbstractHandler {
54 public final static String ID = JcrExplorerPlugin.ID + ".dumpNode";
55
56 private final static DateFormat df = new SimpleDateFormat(
57 "yyyy-MM-dd_HH-mm");
58
59 public Object execute(ExecutionEvent event) throws ExecutionException {
60 ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
61 .getActivePage().getSelection();
62 if (selection == null || !(selection instanceof IStructuredSelection))
63 return null;
64
65 Iterator<?> lst = ((IStructuredSelection) selection).iterator();
66 if (lst.hasNext()) {
67 Object element = lst.next();
68 if (element instanceof SingleJcrNodeElem) {
69 SingleJcrNodeElem sjn = (SingleJcrNodeElem) element;
70 Node node = sjn.getNode();
71
72 // TODO add a dialog to configure the export and ask for
73 // confirmation
74 // Boolean ok = MessageDialog.openConfirm(
75 // HandlerUtil.getActiveShell(event), "Confirm deletion",
76 // "Do you want to delete " + buf + "?");
77
78 File tmpFile;
79 FileOutputStream fos;
80 try {
81 tmpFile = File.createTempFile("JcrExport", ".xml");
82 tmpFile.deleteOnExit();
83 fos = new FileOutputStream(tmpFile);
84 String dateVal = df.format(new GregorianCalendar()
85 .getTime());
86 node.getSession().exportSystemView(node.getPath(), fos,
87 true, false);
88 openGeneratedFile(tmpFile.getAbsolutePath(),
89 "Dump-" + node.getName() + dateVal + ".xml");
90 } catch (RepositoryException e) {
91 throw new ArgeoException(
92 "Unable to perform SystemExport on " + node, e);
93 } catch (IOException e) {
94 throw new ArgeoException("Unable to SystemExport " + node,
95 e);
96 }
97 }
98 }
99 return null;
100 }
101
102 private synchronized void openGeneratedFile(String path, String fileName) {
103 Map<String, String> params = new HashMap<String, String>();
104 params.put(OpenFile.PARAM_FILE_NAME, fileName);
105 params.put(OpenFile.PARAM_FILE_URI, "file://" + path);
106 CommandUtils.callCommand("org.argeo.security.ui.specific.openFile",
107 params);
108 }
109 }