1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.cms.ui.workbench.internal.jcr.commands;
17
18 import static org.argeo.eclipse.ui.utils.SingleSourcingConstants.FILE_SCHEME;
19 import static org.argeo.eclipse.ui.utils.SingleSourcingConstants.SCHEME_HOST_SEPARATOR;
20
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.GregorianCalendar;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.Map;
30
31 import javax.jcr.Node;
32 import javax.jcr.RepositoryException;
33
34 import org.argeo.cms.ui.jcr.model.SingleJcrNodeElem;
35 import org.argeo.cms.ui.workbench.WorkbenchUiPlugin;
36 import org.argeo.cms.ui.workbench.util.CommandUtils;
37 import org.argeo.eclipse.ui.EclipseUiException;
38 import org.argeo.eclipse.ui.specific.OpenFile;
39 import org.argeo.jcr.JcrUtils;
40 import org.eclipse.core.commands.AbstractHandler;
41 import org.eclipse.core.commands.ExecutionEvent;
42 import org.eclipse.core.commands.ExecutionException;
43 import org.eclipse.jface.viewers.ISelection;
44 import org.eclipse.jface.viewers.IStructuredSelection;
45 import org.eclipse.ui.handlers.HandlerUtil;
46
47
48
49
50
51
52
53
54
55
56 public class DumpNode extends AbstractHandler {
57 public final static String ID = WorkbenchUiPlugin.PLUGIN_ID + ".dumpNode";
58
59 private final static DateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm");
60
61 public Object execute(ExecutionEvent event) throws ExecutionException {
62 ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).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
74
75
76
77
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().getTime());
86 node.getSession().exportSystemView(node.getPath(), fos, true, false);
87 openGeneratedFile(tmpFile.getAbsolutePath(),
88 "Dump-" + JcrUtils.replaceInvalidChars(node.getName()) + "-" + dateVal + ".xml");
89 } catch (RepositoryException e) {
90 throw new EclipseUiException("Unable to perform SystemExport on " + node, e);
91 } catch (IOException e) {
92 throw new EclipseUiException("Unable to SystemExport " + node, e);
93 }
94 }
95 }
96 return null;
97 }
98
99 private synchronized void openGeneratedFile(String path, String fileName) {
100 Map<String, String> params = new HashMap<String, String>();
101 params.put(OpenFile.PARAM_FILE_NAME, fileName);
102 params.put(OpenFile.PARAM_FILE_URI, FILE_SCHEME + SCHEME_HOST_SEPARATOR + path);
103 CommandUtils.callCommand(OpenFile.ID, params);
104 }
105 }