]> git.argeo.org Git - gpl/argeo-slc.git/blob - commands/ConfigurableNodeDump.java
Prepare next development cycle
[gpl/argeo-slc.git] / commands / ConfigurableNodeDump.java
1 package org.argeo.cms.ui.workbench.internal.jcr.commands;
2
3 import java.io.File;
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;
14
15 import javax.jcr.Node;
16 import javax.jcr.NodeIterator;
17 import javax.jcr.RepositoryException;
18
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;
44
45 /**
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
50 */
51 public class ConfigurableNodeDump extends AbstractHandler {
52 public final static String ID = WorkbenchUiPlugin.PLUGIN_ID
53 + ".nodeConfigurableDump";
54
55 private final static DateFormat df = new SimpleDateFormat(
56 "yyyy-MM-dd_HH-mm");
57
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;
61
62 public Object execute(ExecutionEvent event) throws ExecutionException {
63 ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
64 .getActivePage().getSelection();
65 if (selection == null || !(selection instanceof IStructuredSelection))
66 return null;
67
68 Iterator<?> lst = ((IStructuredSelection) selection).iterator();
69 if (lst.hasNext()) {
70 Object element = lst.next();
71 if (element instanceof SingleJcrNodeElem) {
72 SingleJcrNodeElem sjn = (SingleJcrNodeElem) element;
73 Node node = sjn.getNode();
74
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();
81
82 if (result == Window.OK) {
83
84 String dateVal = df.format(new GregorianCalendar()
85 .getTime());
86 try {
87
88 Path tmpDirPath = Files.createTempDirectory(dateVal
89 + "-NodeDump-");
90 List<Node> toExport = retrieveToExportNodes(node,
91 wizard.currExportType);
92
93 for (Node currNode : toExport) {
94 FileOutputStream fos;
95 String fileName = wizard.prefix
96 + JcrUtils.replaceInvalidChars(currNode
97 .getName()) + "_" + dateVal
98 + ".xml";
99 File currFile = new File(tmpDirPath.toString()
100 + "/" + fileName);
101 currFile.createNewFile();
102 fos = new FileOutputStream(currFile);
103 node.getSession().exportSystemView(
104 currNode.getPath(), fos,
105 !wizard.includeBinaries, false);
106 fos.flush();
107 fos.close();
108 }
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 "
114 + node, e);
115 }
116 }
117 }
118 }
119 return null;
120 }
121
122 private List<Node> retrieveToExportNodes(Node node, int currExportType)
123 throws RepositoryException {
124 List<Node> nodes = new ArrayList<Node>();
125 switch (currExportType) {
126 case EXPORT_NODE:
127 nodes.add(node);
128 return nodes;
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()
135 .getNodes()));
136 return nodes;
137
138 default:
139 return nodes;
140 }
141 }
142
143 // private synchronized void openGeneratedFile(String path, String fileName)
144 // {
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",
149 // params);
150 // }
151
152 private class ConfigureDumpWizard extends Wizard {
153
154 // parameters
155 protected String prefix;
156 protected int currExportType = EXPORT_NODE;
157 protected boolean includeBinaries = false;
158
159 // UI Objects
160 private BasicPage page;
161 private Text prefixTxt;
162 private Button includeBinaryBtn;
163 private Button b1, b2, b3;
164
165 public ConfigureDumpWizard(Shell parentShell, String title) {
166 setWindowTitle(title);
167 }
168
169 @Override
170 public void addPages() {
171 try {
172 page = new BasicPage("Main page");
173 addPage(page);
174 } catch (Exception e) {
175 throw new EclipseUiException("Cannot add page to wizard", e);
176 }
177 }
178
179 @Override
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();
189 return true;
190 }
191
192 @Override
193 public boolean performCancel() {
194 return true;
195 }
196
197 @Override
198 public boolean canFinish() {
199 String errorMsg = "No prefix defined.";
200 if ("".equals(prefixTxt.getText().trim())) {
201 page.setErrorMessage(errorMsg);
202 return false;
203 } else {
204 page.setErrorMessage(null);
205 return true;
206 }
207 }
208
209 protected class BasicPage extends WizardPage {
210 private static final long serialVersionUID = 1L;
211
212 public BasicPage(String pageName) {
213 super(pageName);
214 setTitle("Configure dump before launching");
215 setMessage("Define the parameters of the dump to launch");
216 }
217
218 public void createControl(Composite parent) {
219 parent.setLayout(EclipseUiUtils.noSpaceGridLayout());
220
221 // Main Layout
222 Composite mainCmp = new Composite(parent, SWT.NONE);
223 mainCmp.setLayout(new GridLayout(2, false));
224 mainCmp.setLayoutData(EclipseUiUtils.fillAll());
225
226 // The path
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;
232
233 @Override
234 public void modifyText(ModifyEvent event) {
235 if (prefixTxt.getText() != null)
236 getWizard().getContainer().updateButtons();
237 }
238 });
239
240 new Label(mainCmp, SWT.SEPARATOR | SWT.HORIZONTAL)
241 .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
242 false, 2, 1));
243
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,
249 false, 1, 3));
250
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");
258
259 new Label(mainCmp, SWT.SEPARATOR | SWT.HORIZONTAL)
260 .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
261 false, 2, 1));
262
263 createBoldLabel(mainCmp, "Files and images");
264 includeBinaryBtn = new Button(mainCmp, SWT.CHECK);
265 includeBinaryBtn.setText("Include binaries");
266
267 prefixTxt.setFocus();
268 setControl(mainCmp);
269 }
270 }
271 }
272
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));
278 return label;
279 }
280 }