]> 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
fix a typo in the resulting file.
[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.JcrUtils;
35 import org.argeo.jcr.ui.explorer.JcrExplorerPlugin;
36 import org.argeo.jcr.ui.explorer.model.SingleJcrNodeElem;
37 import org.eclipse.core.commands.AbstractHandler;
38 import org.eclipse.core.commands.ExecutionEvent;
39 import org.eclipse.core.commands.ExecutionException;
40 import org.eclipse.jface.viewers.ISelection;
41 import org.eclipse.jface.viewers.IStructuredSelection;
42 import org.eclipse.ui.handlers.HandlerUtil;
43
44 /**
45 * If the method
46 * <code> HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection() </code>
47 * exits and has a SingleJcrNodeElem as first element, it canonically calls the
48 * JCR Session.exportSystemView() method on the underlying node with both
49 * skipBinary & noRecurse boolean flags set to false.
50 *
51 * Resulting stream is saved in a tmp file and opened via the "open file"
52 * single-sourced command.
53 */
54 public class DumpNode extends AbstractHandler {
55 public final static String ID = JcrExplorerPlugin.ID + ".dumpNode";
56
57 private final static DateFormat df = new SimpleDateFormat(
58 "yyyy-MM-dd_HH-mm");
59
60 public Object execute(ExecutionEvent event) throws ExecutionException {
61 ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
62 .getActivePage().getSelection();
63 if (selection == null || !(selection instanceof IStructuredSelection))
64 return null;
65
66 Iterator<?> lst = ((IStructuredSelection) selection).iterator();
67 if (lst.hasNext()) {
68 Object element = lst.next();
69 if (element instanceof SingleJcrNodeElem) {
70 SingleJcrNodeElem sjn = (SingleJcrNodeElem) element;
71 Node node = sjn.getNode();
72
73 // TODO add a dialog to configure the export and ask for
74 // confirmation
75 // Boolean ok = MessageDialog.openConfirm(
76 // HandlerUtil.getActiveShell(event), "Confirm deletion",
77 // "Do you want to delete " + buf + "?");
78
79 File tmpFile;
80 FileOutputStream fos;
81 try {
82 tmpFile = File.createTempFile("JcrExport", ".xml");
83 tmpFile.deleteOnExit();
84 fos = new FileOutputStream(tmpFile);
85 String dateVal = df.format(new GregorianCalendar()
86 .getTime());
87 node.getSession().exportSystemView(node.getPath(), fos,
88 true, false);
89 openGeneratedFile(tmpFile.getAbsolutePath(),
90 "Dump-" + JcrUtils.replaceInvalidChars(node.getName())+ "-" + dateVal + ".xml");
91 } catch (RepositoryException e) {
92 throw new ArgeoException(
93 "Unable to perform SystemExport on " + node, e);
94 } catch (IOException e) {
95 throw new ArgeoException("Unable to SystemExport " + node,
96 e);
97 }
98 }
99 }
100 return null;
101 }
102
103 private synchronized void openGeneratedFile(String path, String fileName) {
104 Map<String, String> params = new HashMap<String, String>();
105 params.put(OpenFile.PARAM_FILE_NAME, fileName);
106 params.put(OpenFile.PARAM_FILE_URI, "file://" + path);
107 CommandUtils.callCommand("org.argeo.security.ui.specific.openFile",
108 params);
109 }
110 }