]>
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
2414f042b95ae830e3391cce3ef868919de65b91
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
;
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
;
28 import javax
.jcr
.Node
;
29 import javax
.jcr
.RepositoryException
;
30 import javax
.jcr
.Session
;
32 import org
.argeo
.cms
.ui
.workbench
.WorkbenchUiPlugin
;
33 import org
.argeo
.cms
.ui
.workbench
.commands
.OpenFile
;
34 import org
.argeo
.cms
.ui
.workbench
.internal
.jcr
.model
.SingleJcrNodeElem
;
35 import org
.argeo
.cms
.ui
.workbench
.util
.CommandUtils
;
36 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
37 import org
.argeo
.jcr
.JcrUtils
;
38 import org
.eclipse
.core
.commands
.AbstractHandler
;
39 import org
.eclipse
.core
.commands
.ExecutionEvent
;
40 import org
.eclipse
.core
.commands
.ExecutionException
;
41 import org
.eclipse
.jface
.viewers
.ISelection
;
42 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
43 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
46 * Canonically call JCR {@link Session#exportSystemView()} on the first element
47 * returned by {@link HandlerUtil#getActiveWorkbenchWindow()}
48 * (...getActivePage().getSelection()), if it is a {@link SingleJcrNodeElem},
49 * with both skipBinary & noRecurse boolean flags set to false.
51 * Resulting stream is saved in a tmp file and opened via the {@link OpenFile}
52 * single-sourced command.
54 public class DumpNode
extends AbstractHandler
{
55 public final static String ID
= WorkbenchUiPlugin
.PLUGIN_ID
+ ".dumpNode";
57 private final static DateFormat df
= new SimpleDateFormat(
60 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
61 ISelection selection
= HandlerUtil
.getActiveWorkbenchWindow(event
)
62 .getActivePage().getSelection();
63 if (selection
== null || !(selection
instanceof IStructuredSelection
))
66 Iterator
<?
> lst
= ((IStructuredSelection
) selection
).iterator();
68 Object element
= lst
.next();
69 if (element
instanceof SingleJcrNodeElem
) {
70 SingleJcrNodeElem sjn
= (SingleJcrNodeElem
) element
;
71 Node node
= sjn
.getNode();
73 // TODO add a dialog to configure the export and ask for
75 // Boolean ok = MessageDialog.openConfirm(
76 // HandlerUtil.getActiveShell(event), "Confirm deletion",
77 // "Do you want to delete " + buf + "?");
82 tmpFile
= File
.createTempFile("JcrExport", ".xml");
83 tmpFile
.deleteOnExit();
84 fos
= new FileOutputStream(tmpFile
);
85 String dateVal
= df
.format(new GregorianCalendar()
87 node
.getSession().exportSystemView(node
.getPath(), fos
,
89 openGeneratedFile(tmpFile
.getAbsolutePath(), "Dump-"
90 + JcrUtils
.replaceInvalidChars(node
.getName())
91 + "-" + dateVal
+ ".xml");
92 } catch (RepositoryException e
) {
93 throw new EclipseUiException(
94 "Unable to perform SystemExport on " + node
, e
);
95 } catch (IOException e
) {
96 throw new EclipseUiException("Unable to SystemExport "
104 private synchronized void openGeneratedFile(String path
, String fileName
) {
105 Map
<String
, String
> params
= new HashMap
<String
, String
>();
106 params
.put(OpenFile
.PARAM_FILE_NAME
, fileName
);
107 params
.put(OpenFile
.PARAM_FILE_URI
, "file://" + path
);
108 CommandUtils
.callCommand(OpenFile
.ID
, params
);