]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/commands/NodeConfigurableDump.java
32e6e67b6a743df100499a0dbe116f69002d2238
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui.workbench / src / org / argeo / eclipse / ui / workbench / commands / NodeConfigurableDump.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.eclipse.ui.workbench.commands;
17
18 import java.io.File;
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;
30 import java.util.Map;
31
32 import javax.jcr.Node;
33 import javax.jcr.NodeIterator;
34 import javax.jcr.RepositoryException;
35
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;
63
64 /**
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
69 */
70 public class NodeConfigurableDump extends AbstractHandler {
71 public final static String ID = WorkbenchUiPlugin.ID
72 + ".nodeConfigurableDump";
73
74 private final static DateFormat df = new SimpleDateFormat(
75 "yyyy-MM-dd_HH-mm");
76
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;
80
81 public Object execute(ExecutionEvent event) throws ExecutionException {
82 ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
83 .getActivePage().getSelection();
84 if (selection == null || !(selection instanceof IStructuredSelection))
85 return null;
86
87 Iterator<?> lst = ((IStructuredSelection) selection).iterator();
88 if (lst.hasNext()) {
89 Object element = lst.next();
90 if (element instanceof SingleJcrNodeElem) {
91 SingleJcrNodeElem sjn = (SingleJcrNodeElem) element;
92 Node node = sjn.getNode();
93
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();
100
101 if (result == Window.OK) {
102
103 String dateVal = df.format(new GregorianCalendar()
104 .getTime());
105 try {
106
107 Path tmpDirPath = Files.createTempDirectory(dateVal
108 + "-NodeDump-");
109 List<Node> toExport = retrieveToExportNodes(node,
110 wizard.currExportType);
111
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()
118 + "/" + fileName);
119 currFile.createNewFile();
120 fos = new FileOutputStream(currFile);
121 node.getSession().exportSystemView(
122 currNode.getPath(), fos,
123 !wizard.includeBinaries, false);
124 fos.flush();
125 fos.close();
126 }
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 "
132 + node, e);
133 }
134 }
135 }
136 }
137 return null;
138 }
139
140 private List<Node> retrieveToExportNodes(Node node, int currExportType)
141 throws RepositoryException {
142 List<Node> nodes = new ArrayList<Node>();
143 switch (currExportType) {
144 case EXPORT_NODE:
145 nodes.add(node);
146 return nodes;
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()
153 .getNodes()));
154 return nodes;
155
156 default:
157 return nodes;
158 }
159 }
160
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",
166 params);
167 }
168
169 private class ConfigureDumpWizard extends Wizard {
170
171 // parameters
172 protected String prefix;
173 protected int currExportType = EXPORT_NODE;
174 protected boolean includeBinaries = false;
175
176 // UI Objects
177 private BasicPage page;
178 private Text prefixTxt;
179 private Button includeBinaryBtn;
180 private Button b1, b2, b3;
181
182 public ConfigureDumpWizard(Shell parentShell, String title) {
183 setWindowTitle(title);
184 }
185
186 @Override
187 public void addPages() {
188 try {
189 page = new BasicPage("Main page");
190 addPage(page);
191 } catch (Exception e) {
192 throw new ArgeoException("Cannot add page to wizard", e);
193 }
194 }
195
196 @Override
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();
206 return true;
207 }
208
209 @Override
210 public boolean performCancel() {
211 return true;
212 }
213
214 @Override
215 public boolean canFinish() {
216 String errorMsg = "No prefix defined.";
217 if ("".equals(prefixTxt.getText().trim())) {
218 page.setErrorMessage(errorMsg);
219 return false;
220 } else {
221 page.setErrorMessage(null);
222 return true;
223 }
224 }
225
226 protected class BasicPage extends WizardPage {
227 private static final long serialVersionUID = 1L;
228
229 public BasicPage(String pageName) {
230 super(pageName);
231 setTitle("Configure dump before launching");
232 setMessage("Define the parameters of the dump to launch");
233 }
234
235 public void createControl(Composite parent) {
236 parent.setLayout(EclipseUiUtils.noSpaceGridLayout());
237
238 // Main Layout
239 Composite mainCmp = new Composite(parent, SWT.NONE);
240 mainCmp.setLayout(new GridLayout(2, false));
241 mainCmp.setLayoutData(EclipseUiUtils.fillAll());
242
243 // The path
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;
249
250 @Override
251 public void modifyText(ModifyEvent event) {
252 if (prefixTxt.getText() != null)
253 getWizard().getContainer().updateButtons();
254 }
255 });
256
257 new Label(mainCmp, SWT.SEPARATOR | SWT.HORIZONTAL)
258 .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
259 false, 2, 1));
260
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,
266 false, 1, 3));
267
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");
275
276 new Label(mainCmp, SWT.SEPARATOR | SWT.HORIZONTAL)
277 .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
278 false, 2, 1));
279
280 createBoldLabel(mainCmp, "Files and images");
281 includeBinaryBtn = new Button(mainCmp, SWT.CHECK);
282 includeBinaryBtn.setText("Include binaries");
283
284 prefixTxt.setFocus();
285 setControl(mainCmp);
286 }
287 }
288 }
289
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));
295 return label;
296 }
297 }