]>
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
2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.cms
.ui
.workbench
.internal
.jcr
.commands
;
18 import static org
.argeo
.eclipse
.ui
.utils
.SingleSourcingConstants
.FILE_SCHEME
;
19 import static org
.argeo
.eclipse
.ui
.utils
.SingleSourcingConstants
.SCHEME_HOST_SEPARATOR
;
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
;
31 import javax
.jcr
.Node
;
32 import javax
.jcr
.RepositoryException
;
33 import javax
.jcr
.Session
;
35 import org
.argeo
.cms
.ui
.workbench
.WorkbenchUiPlugin
;
36 import org
.argeo
.cms
.ui
.workbench
.internal
.jcr
.model
.SingleJcrNodeElem
;
37 import org
.argeo
.cms
.ui
.workbench
.util
.CommandUtils
;
38 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
39 import org
.argeo
.eclipse
.ui
.specific
.OpenFile
;
40 import org
.argeo
.jcr
.JcrUtils
;
41 import org
.eclipse
.core
.commands
.AbstractHandler
;
42 import org
.eclipse
.core
.commands
.ExecutionEvent
;
43 import org
.eclipse
.core
.commands
.ExecutionException
;
44 import org
.eclipse
.jface
.viewers
.ISelection
;
45 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
46 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
49 * Canonically call JCR {@link Session#exportSystemView()} on the first element
50 * returned by {@link HandlerUtil#getActiveWorkbenchWindow()}
51 * (...getActivePage().getSelection()), if it is a {@link SingleJcrNodeElem},
52 * with both skipBinary & noRecurse boolean flags set to false.
54 * Resulting stream is saved in a tmp file and opened via the {@link OpenFile}
55 * single-sourced command.
57 public class DumpNode
extends AbstractHandler
{
58 public final static String ID
= WorkbenchUiPlugin
.PLUGIN_ID
+ ".dumpNode";
60 private final static DateFormat df
= new SimpleDateFormat("yyyy-MM-dd_HH-mm");
62 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
63 ISelection selection
= HandlerUtil
.getActiveWorkbenchWindow(event
).getActivePage().getSelection();
64 if (selection
== null || !(selection
instanceof IStructuredSelection
))
67 Iterator
<?
> lst
= ((IStructuredSelection
) selection
).iterator();
69 Object element
= lst
.next();
70 if (element
instanceof SingleJcrNodeElem
) {
71 SingleJcrNodeElem sjn
= (SingleJcrNodeElem
) element
;
72 Node node
= sjn
.getNode();
74 // TODO add a dialog to configure the export and ask for
76 // Boolean ok = MessageDialog.openConfirm(
77 // HandlerUtil.getActiveShell(event), "Confirm dump",
78 // "Do you want to dump " + buf + "?");
83 tmpFile
= File
.createTempFile("JcrExport", ".xml");
84 tmpFile
.deleteOnExit();
85 fos
= new FileOutputStream(tmpFile
);
86 String dateVal
= df
.format(new GregorianCalendar().getTime());
87 node
.getSession().exportSystemView(node
.getPath(), fos
, true, false);
88 openGeneratedFile(tmpFile
.getAbsolutePath(),
89 "Dump-" + JcrUtils
.replaceInvalidChars(node
.getName()) + "-" + dateVal
+ ".xml");
90 } catch (RepositoryException e
) {
91 throw new EclipseUiException("Unable to perform SystemExport on " + node
, e
);
92 } catch (IOException e
) {
93 throw new EclipseUiException("Unable to SystemExport " + node
, e
);
100 private synchronized void openGeneratedFile(String path
, String fileName
) {
101 Map
<String
, String
> params
= new HashMap
<String
, String
>();
102 params
.put(OpenFile
.PARAM_FILE_NAME
, fileName
);
103 params
.put(OpenFile
.PARAM_FILE_URI
, FILE_SCHEME
+ SCHEME_HOST_SEPARATOR
+ path
);
104 CommandUtils
.callCommand(OpenFile
.ID
, params
);