]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui.workbench/src/org/argeo/cms/ui/workbench/internal/jcr/commands/DumpNode.java
Fix char array comparison
[lgpl/argeo-commons.git] / org.argeo.cms.ui.workbench / src / org / argeo / cms / ui / workbench / internal / jcr / 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.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.workbench.WorkbenchUiPlugin;
35 import org.argeo.cms.ui.workbench.internal.jcr.model.SingleJcrNodeElem;
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 * Canonically call JCR Session#exportSystemView() on the first element returned
49 * by HandlerUtil#getActiveWorkbenchWindow()
50 * (...getActivePage().getSelection()), if it is a {@link SingleJcrNodeElem},
51 * with both skipBinary and noRecurse boolean flags set to false.
52 *
53 * Resulting stream is saved in a tmp file and opened via the {@link OpenFile}
54 * single-sourced command.
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 // TODO add a dialog to configure the export and ask for
74 // confirmation
75 // Boolean ok = MessageDialog.openConfirm(
76 // HandlerUtil.getActiveShell(event), "Confirm dump",
77 // "Do you want to dump " + 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().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 }