1 package org
.argeo
.cms
.ui
.workbench
.internal
.jcr
.commands
;
4 import java
.io
.FileOutputStream
;
5 import java
.io
.IOException
;
6 import java
.nio
.file
.Files
;
7 import java
.nio
.file
.Path
;
8 import java
.text
.DateFormat
;
9 import java
.text
.SimpleDateFormat
;
10 import java
.util
.ArrayList
;
11 import java
.util
.GregorianCalendar
;
12 import java
.util
.Iterator
;
13 import java
.util
.List
;
15 import javax
.jcr
.Node
;
16 import javax
.jcr
.NodeIterator
;
17 import javax
.jcr
.RepositoryException
;
19 import org
.argeo
.cms
.ui
.jcr
.model
.SingleJcrNodeElem
;
20 import org
.argeo
.cms
.ui
.workbench
.WorkbenchUiPlugin
;
21 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
22 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
23 import org
.argeo
.jcr
.JcrUtils
;
24 import org
.eclipse
.core
.commands
.AbstractHandler
;
25 import org
.eclipse
.core
.commands
.ExecutionEvent
;
26 import org
.eclipse
.core
.commands
.ExecutionException
;
27 import org
.eclipse
.jface
.viewers
.ISelection
;
28 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
29 import org
.eclipse
.jface
.window
.Window
;
30 import org
.eclipse
.jface
.wizard
.Wizard
;
31 import org
.eclipse
.jface
.wizard
.WizardDialog
;
32 import org
.eclipse
.jface
.wizard
.WizardPage
;
33 import org
.eclipse
.swt
.SWT
;
34 import org
.eclipse
.swt
.events
.ModifyEvent
;
35 import org
.eclipse
.swt
.events
.ModifyListener
;
36 import org
.eclipse
.swt
.layout
.GridData
;
37 import org
.eclipse
.swt
.layout
.GridLayout
;
38 import org
.eclipse
.swt
.widgets
.Button
;
39 import org
.eclipse
.swt
.widgets
.Composite
;
40 import org
.eclipse
.swt
.widgets
.Label
;
41 import org
.eclipse
.swt
.widgets
.Shell
;
42 import org
.eclipse
.swt
.widgets
.Text
;
43 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
46 * First draft of a wizard that enable configurable recursive dump of the
47 * current selected Node (Only one at a time). Enable among other to export
48 * children Nodes and to choose to export binaries or not. It is useful to
49 * retrieve business data from live systems to prepare migration or test locally
51 public class ConfigurableNodeDump
extends AbstractHandler
{
52 public final static String ID
= WorkbenchUiPlugin
.PLUGIN_ID
53 + ".nodeConfigurableDump";
55 private final static DateFormat df
= new SimpleDateFormat(
58 public final static int EXPORT_NODE
= 0;
59 public final static int EXPORT_CHILDREN
= 1;
60 public final static int EXPORT_GRAND_CHILDREN
= 2;
62 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
63 ISelection selection
= HandlerUtil
.getActiveWorkbenchWindow(event
)
64 .getActivePage().getSelection();
65 if (selection
== null || !(selection
instanceof IStructuredSelection
))
68 Iterator
<?
> lst
= ((IStructuredSelection
) selection
).iterator();
70 Object element
= lst
.next();
71 if (element
instanceof SingleJcrNodeElem
) {
72 SingleJcrNodeElem sjn
= (SingleJcrNodeElem
) element
;
73 Node node
= sjn
.getNode();
75 ConfigureDumpWizard wizard
= new ConfigureDumpWizard(
76 HandlerUtil
.getActiveShell(event
),
77 "Import Resource CSV");
78 WizardDialog dialog
= new WizardDialog(
79 HandlerUtil
.getActiveShell(event
), wizard
);
80 int result
= dialog
.open();
82 if (result
== Window
.OK
) {
84 String dateVal
= df
.format(new GregorianCalendar()
88 Path tmpDirPath
= Files
.createTempDirectory(dateVal
90 List
<Node
> toExport
= retrieveToExportNodes(node
,
91 wizard
.currExportType
);
93 for (Node currNode
: toExport
) {
95 String fileName
= wizard
.prefix
96 + JcrUtils
.replaceInvalidChars(currNode
97 .getName()) + "_" + dateVal
99 File currFile
= new File(tmpDirPath
.toString()
101 currFile
.createNewFile();
102 fos
= new FileOutputStream(currFile
);
103 node
.getSession().exportSystemView(
104 currNode
.getPath(), fos
,
105 !wizard
.includeBinaries
, false);
109 } catch (RepositoryException e
) {
110 throw new EclipseUiException(
111 "Unable to perform SystemExport on " + node
, e
);
112 } catch (IOException e
) {
113 throw new EclipseUiException("Unable to SystemExport "
122 private List
<Node
> retrieveToExportNodes(Node node
, int currExportType
)
123 throws RepositoryException
{
124 List
<Node
> nodes
= new ArrayList
<Node
>();
125 switch (currExportType
) {
129 case EXPORT_CHILDREN
:
130 return JcrUtils
.nodeIteratorToList(node
.getNodes());
131 case EXPORT_GRAND_CHILDREN
:
132 NodeIterator nit
= node
.getNodes();
133 while (nit
.hasNext())
134 nodes
.addAll(JcrUtils
.nodeIteratorToList(nit
.nextNode()
143 // private synchronized void openGeneratedFile(String path, String fileName)
145 // Map<String, String> params = new HashMap<String, String>();
146 // params.put(OpenFile.PARAM_FILE_NAME, fileName);
147 // params.put(OpenFile.PARAM_FILE_URI, "file://" + path);
148 // CommandUtils.callCommand("org.argeo.security.ui.specific.openFile",
152 private class ConfigureDumpWizard
extends Wizard
{
155 protected String prefix
;
156 protected int currExportType
= EXPORT_NODE
;
157 protected boolean includeBinaries
= false;
160 private BasicPage page
;
161 private Text prefixTxt
;
162 private Button includeBinaryBtn
;
163 private Button b1
, b2
, b3
;
165 public ConfigureDumpWizard(Shell parentShell
, String title
) {
166 setWindowTitle(title
);
170 public void addPages() {
172 page
= new BasicPage("Main page");
174 } catch (Exception e
) {
175 throw new EclipseUiException("Cannot add page to wizard", e
);
180 public boolean performFinish() {
181 prefix
= prefixTxt
.getText();
182 if (b1
.getSelection())
183 currExportType
= EXPORT_NODE
;
184 else if (b2
.getSelection())
185 currExportType
= EXPORT_CHILDREN
;
186 else if (b3
.getSelection())
187 currExportType
= EXPORT_GRAND_CHILDREN
;
188 includeBinaries
= includeBinaryBtn
.getSelection();
193 public boolean performCancel() {
198 public boolean canFinish() {
199 String errorMsg
= "No prefix defined.";
200 if ("".equals(prefixTxt
.getText().trim())) {
201 page
.setErrorMessage(errorMsg
);
204 page
.setErrorMessage(null);
209 protected class BasicPage
extends WizardPage
{
210 private static final long serialVersionUID
= 1L;
212 public BasicPage(String pageName
) {
214 setTitle("Configure dump before launching");
215 setMessage("Define the parameters of the dump to launch");
218 public void createControl(Composite parent
) {
219 parent
.setLayout(EclipseUiUtils
.noSpaceGridLayout());
222 Composite mainCmp
= new Composite(parent
, SWT
.NONE
);
223 mainCmp
.setLayout(new GridLayout(2, false));
224 mainCmp
.setLayoutData(EclipseUiUtils
.fillAll());
227 createBoldLabel(mainCmp
, "Prefix");
228 prefixTxt
= new Text(mainCmp
, SWT
.SINGLE
| SWT
.BORDER
);
229 prefixTxt
.setLayoutData(EclipseUiUtils
.fillAll());
230 prefixTxt
.addModifyListener(new ModifyListener() {
231 private static final long serialVersionUID
= 1L;
234 public void modifyText(ModifyEvent event
) {
235 if (prefixTxt
.getText() != null)
236 getWizard().getContainer().updateButtons();
240 new Label(mainCmp
, SWT
.SEPARATOR
| SWT
.HORIZONTAL
)
241 .setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
244 // Which node to export
245 Label typeLbl
= new Label(mainCmp
, SWT
.RIGHT
);
246 typeLbl
.setText(" Type");
247 typeLbl
.setFont(EclipseUiUtils
.getBoldFont(mainCmp
));
248 typeLbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.TOP
, false,
251 b1
= new Button(mainCmp
, SWT
.RADIO
);
252 b1
.setText("Export this node");
253 b1
.setSelection(true);
254 b2
= new Button(mainCmp
, SWT
.RADIO
);
255 b2
.setText("Export children nodes");
256 b3
= new Button(mainCmp
, SWT
.RADIO
);
257 b3
.setText("Export grand-children nodes");
259 new Label(mainCmp
, SWT
.SEPARATOR
| SWT
.HORIZONTAL
)
260 .setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
263 createBoldLabel(mainCmp
, "Files and images");
264 includeBinaryBtn
= new Button(mainCmp
, SWT
.CHECK
);
265 includeBinaryBtn
.setText("Include binaries");
267 prefixTxt
.setFocus();
273 private Label
createBoldLabel(Composite parent
, String value
) {
274 Label label
= new Label(parent
, SWT
.RIGHT
);
275 label
.setText(" " + value
);
276 label
.setFont(EclipseUiUtils
.getBoldFont(parent
));
277 label
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));