]>
git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/argeo-commons/org.argeo.cms.ui.workbench/src/org/argeo/cms/ui/workbench/internal/jcr/commands/DumpNode.java
ae23f1d542c5dd1caa9f8cb31ac3c4a6d4a6a66a
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
;
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
;
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.
53 * Resulting stream is saved in a tmp file and opened via the {@link OpenFile}
54 * single-sourced command.
56 public class DumpNode
extends AbstractHandler
{
57 public final static String ID
= WorkbenchUiPlugin
.PLUGIN_ID
+ ".dumpNode";
59 private final static DateFormat df
= new SimpleDateFormat("yyyy-MM-dd_HH-mm");
61 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
62 ISelection selection
= HandlerUtil
.getActiveWorkbenchWindow(event
).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 dump",
77 // "Do you want to dump " + buf + "?");
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
);
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
);