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
.eclipse
.ui
.workbench
.commands
;
19 import java
.io
.FileOutputStream
;
20 import java
.io
.IOException
;
21 import java
.nio
.file
.Files
;
22 import java
.nio
.file
.Path
;
23 import java
.text
.DateFormat
;
24 import java
.text
.SimpleDateFormat
;
25 import java
.util
.ArrayList
;
26 import java
.util
.GregorianCalendar
;
27 import java
.util
.HashMap
;
28 import java
.util
.Iterator
;
29 import java
.util
.List
;
32 import javax
.jcr
.Node
;
33 import javax
.jcr
.NodeIterator
;
34 import javax
.jcr
.RepositoryException
;
36 import org
.argeo
.ArgeoException
;
37 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
38 import org
.argeo
.eclipse
.ui
.specific
.OpenFile
;
39 import org
.argeo
.eclipse
.ui
.workbench
.CommandUtils
;
40 import org
.argeo
.eclipse
.ui
.workbench
.WorkbenchUiPlugin
;
41 import org
.argeo
.eclipse
.ui
.workbench
.jcr
.internal
.model
.SingleJcrNodeElem
;
42 import org
.argeo
.jcr
.JcrUtils
;
43 import org
.eclipse
.core
.commands
.AbstractHandler
;
44 import org
.eclipse
.core
.commands
.ExecutionEvent
;
45 import org
.eclipse
.core
.commands
.ExecutionException
;
46 import org
.eclipse
.jface
.viewers
.ISelection
;
47 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
48 import org
.eclipse
.jface
.window
.Window
;
49 import org
.eclipse
.jface
.wizard
.Wizard
;
50 import org
.eclipse
.jface
.wizard
.WizardDialog
;
51 import org
.eclipse
.jface
.wizard
.WizardPage
;
52 import org
.eclipse
.swt
.SWT
;
53 import org
.eclipse
.swt
.events
.ModifyEvent
;
54 import org
.eclipse
.swt
.events
.ModifyListener
;
55 import org
.eclipse
.swt
.layout
.GridData
;
56 import org
.eclipse
.swt
.layout
.GridLayout
;
57 import org
.eclipse
.swt
.widgets
.Button
;
58 import org
.eclipse
.swt
.widgets
.Composite
;
59 import org
.eclipse
.swt
.widgets
.Label
;
60 import org
.eclipse
.swt
.widgets
.Shell
;
61 import org
.eclipse
.swt
.widgets
.Text
;
62 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
65 * First draft of a wizard that enable configurable recursive dump of the
66 * current selected Node (Only one at a time). Enable among other to export
67 * children Nodes and to choose to export binaries or not. It is useful to
68 * retrieve business data from live systems to prepare migration or test locally
70 public class NodeConfigurableDump
extends AbstractHandler
{
71 public final static String ID
= WorkbenchUiPlugin
.ID
72 + ".nodeConfigurableDump";
74 private final static DateFormat df
= new SimpleDateFormat(
77 public final static int EXPORT_NODE
= 0;
78 public final static int EXPORT_CHILDREN
= 1;
79 public final static int EXPORT_GRAND_CHILDREN
= 2;
81 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
82 ISelection selection
= HandlerUtil
.getActiveWorkbenchWindow(event
)
83 .getActivePage().getSelection();
84 if (selection
== null || !(selection
instanceof IStructuredSelection
))
87 Iterator
<?
> lst
= ((IStructuredSelection
) selection
).iterator();
89 Object element
= lst
.next();
90 if (element
instanceof SingleJcrNodeElem
) {
91 SingleJcrNodeElem sjn
= (SingleJcrNodeElem
) element
;
92 Node node
= sjn
.getNode();
94 ConfigureDumpWizard wizard
= new ConfigureDumpWizard(
95 HandlerUtil
.getActiveShell(event
),
96 "Import Resource CSV");
97 WizardDialog dialog
= new WizardDialog(
98 HandlerUtil
.getActiveShell(event
), wizard
);
99 int result
= dialog
.open();
101 if (result
== Window
.OK
) {
103 String dateVal
= df
.format(new GregorianCalendar()
107 Path tmpDirPath
= Files
.createTempDirectory(dateVal
109 List
<Node
> toExport
= retrieveToExportNodes(node
,
110 wizard
.currExportType
);
112 for (Node currNode
: toExport
) {
113 FileOutputStream fos
;
114 String fileName
= wizard
.prefix
115 + JcrUtils
.replaceInvalidChars(currNode
116 .getName()) +"_"+ dateVal
+ ".xml";
117 File currFile
= new File(tmpDirPath
.toString()
119 currFile
.createNewFile();
120 fos
= new FileOutputStream(currFile
);
121 node
.getSession().exportSystemView(
122 currNode
.getPath(), fos
,
123 !wizard
.includeBinaries
, false);
127 } catch (RepositoryException e
) {
128 throw new ArgeoException(
129 "Unable to perform SystemExport on " + node
, e
);
130 } catch (IOException e
) {
131 throw new ArgeoException("Unable to SystemExport "
140 private List
<Node
> retrieveToExportNodes(Node node
, int currExportType
)
141 throws RepositoryException
{
142 List
<Node
> nodes
= new ArrayList
<Node
>();
143 switch (currExportType
) {
147 case EXPORT_CHILDREN
:
148 return JcrUtils
.nodeIteratorToList(node
.getNodes());
149 case EXPORT_GRAND_CHILDREN
:
150 NodeIterator nit
= node
.getNodes();
151 while (nit
.hasNext())
152 nodes
.addAll(JcrUtils
.nodeIteratorToList(nit
.nextNode()
161 private synchronized void openGeneratedFile(String path
, String fileName
) {
162 Map
<String
, String
> params
= new HashMap
<String
, String
>();
163 params
.put(OpenFile
.PARAM_FILE_NAME
, fileName
);
164 params
.put(OpenFile
.PARAM_FILE_URI
, "file://" + path
);
165 CommandUtils
.callCommand("org.argeo.security.ui.specific.openFile",
169 private class ConfigureDumpWizard
extends Wizard
{
172 protected String prefix
;
173 protected int currExportType
= EXPORT_NODE
;
174 protected boolean includeBinaries
= false;
177 private BasicPage page
;
178 private Text prefixTxt
;
179 private Button includeBinaryBtn
;
180 private Button b1
, b2
, b3
;
182 public ConfigureDumpWizard(Shell parentShell
, String title
) {
183 setWindowTitle(title
);
187 public void addPages() {
189 page
= new BasicPage("Main page");
191 } catch (Exception e
) {
192 throw new ArgeoException("Cannot add page to wizard", e
);
197 public boolean performFinish() {
198 prefix
= prefixTxt
.getText();
199 if (b1
.getSelection())
200 currExportType
= EXPORT_NODE
;
201 else if (b2
.getSelection())
202 currExportType
= EXPORT_CHILDREN
;
203 else if (b3
.getSelection())
204 currExportType
= EXPORT_GRAND_CHILDREN
;
205 includeBinaries
= includeBinaryBtn
.getSelection();
210 public boolean performCancel() {
215 public boolean canFinish() {
216 String errorMsg
= "No prefix defined.";
217 if ("".equals(prefixTxt
.getText().trim())) {
218 page
.setErrorMessage(errorMsg
);
221 page
.setErrorMessage(null);
226 protected class BasicPage
extends WizardPage
{
227 private static final long serialVersionUID
= 1L;
229 public BasicPage(String pageName
) {
231 setTitle("Configure dump before launching");
232 setMessage("Define the parameters of the dump to launch");
235 public void createControl(Composite parent
) {
236 parent
.setLayout(EclipseUiUtils
.noSpaceGridLayout());
239 Composite mainCmp
= new Composite(parent
, SWT
.NONE
);
240 mainCmp
.setLayout(new GridLayout(2, false));
241 mainCmp
.setLayoutData(EclipseUiUtils
.fillAll());
244 createBoldLabel(mainCmp
, "Prefix");
245 prefixTxt
= new Text(mainCmp
, SWT
.SINGLE
| SWT
.BORDER
);
246 prefixTxt
.setLayoutData(EclipseUiUtils
.fillAll());
247 prefixTxt
.addModifyListener(new ModifyListener() {
248 private static final long serialVersionUID
= 1L;
251 public void modifyText(ModifyEvent event
) {
252 if (prefixTxt
.getText() != null)
253 getWizard().getContainer().updateButtons();
257 new Label(mainCmp
, SWT
.SEPARATOR
| SWT
.HORIZONTAL
)
258 .setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
261 // Which node to export
262 Label typeLbl
= new Label(mainCmp
, SWT
.RIGHT
);
263 typeLbl
.setText(" Type");
264 typeLbl
.setFont(EclipseUiUtils
.getBoldFont(mainCmp
));
265 typeLbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.TOP
, false,
268 b1
= new Button(mainCmp
, SWT
.RADIO
);
269 b1
.setText("Export this node");
270 b1
.setSelection(true);
271 b2
= new Button(mainCmp
, SWT
.RADIO
);
272 b2
.setText("Export children nodes");
273 b3
= new Button(mainCmp
, SWT
.RADIO
);
274 b3
.setText("Export grand-children nodes");
276 new Label(mainCmp
, SWT
.SEPARATOR
| SWT
.HORIZONTAL
)
277 .setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
280 createBoldLabel(mainCmp
, "Files and images");
281 includeBinaryBtn
= new Button(mainCmp
, SWT
.CHECK
);
282 includeBinaryBtn
.setText("Include binaries");
284 prefixTxt
.setFocus();
290 private Label
createBoldLabel(Composite parent
, String value
) {
291 Label label
= new Label(parent
, SWT
.RIGHT
);
292 label
.setText(" " + value
);
293 label
.setFont(EclipseUiUtils
.getBoldFont(parent
));
294 label
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));