From ff19b31347f06841730f74083694f208230d3a3c Mon Sep 17 00:00:00 2001 From: Bruno Sinou Date: Wed, 8 Jul 2015 19:04:36 +0000 Subject: [PATCH] Add a maintenance command to enable configurable node dumps git-svn-id: https://svn.argeo.org/commons/trunk@8250 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- org.argeo.eclipse.ui.workbench/plugin.xml | 19 ++ .../commands/NodeConfigurableDump.java | 297 ++++++++++++++++++ 2 files changed, 316 insertions(+) create mode 100644 org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/commands/NodeConfigurableDump.java diff --git a/org.argeo.eclipse.ui.workbench/plugin.xml b/org.argeo.eclipse.ui.workbench/plugin.xml index 5e140f418..210046381 100644 --- a/org.argeo.eclipse.ui.workbench/plugin.xml +++ b/org.argeo.eclipse.ui.workbench/plugin.xml @@ -164,6 +164,11 @@ id="org.argeo.eclipse.ui.workbench.dumpNode" name="Dump Current Selected Node"> + + + + + + + + + + + + diff --git a/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/commands/NodeConfigurableDump.java b/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/commands/NodeConfigurableDump.java new file mode 100644 index 000000000..32e6e67b6 --- /dev/null +++ b/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/commands/NodeConfigurableDump.java @@ -0,0 +1,297 @@ +/* + * Copyright (C) 2007-2012 Argeo GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.argeo.eclipse.ui.workbench.commands; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.GregorianCalendar; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import javax.jcr.Node; +import javax.jcr.NodeIterator; +import javax.jcr.RepositoryException; + +import org.argeo.ArgeoException; +import org.argeo.eclipse.ui.EclipseUiUtils; +import org.argeo.eclipse.ui.specific.OpenFile; +import org.argeo.eclipse.ui.workbench.CommandUtils; +import org.argeo.eclipse.ui.workbench.WorkbenchUiPlugin; +import org.argeo.eclipse.ui.workbench.jcr.internal.model.SingleJcrNodeElem; +import org.argeo.jcr.JcrUtils; +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.window.Window; +import org.eclipse.jface.wizard.Wizard; +import org.eclipse.jface.wizard.WizardDialog; +import org.eclipse.jface.wizard.WizardPage; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; +import org.eclipse.ui.handlers.HandlerUtil; + +/** + * First draft of a wizard that enable configurable recursive dump of the + * current selected Node (Only one at a time). Enable among other to export + * children Nodes and to choose to export binaries or not. It is useful to + * retrieve business data from live systems to prepare migration or test locally + */ +public class NodeConfigurableDump extends AbstractHandler { + public final static String ID = WorkbenchUiPlugin.ID + + ".nodeConfigurableDump"; + + private final static DateFormat df = new SimpleDateFormat( + "yyyy-MM-dd_HH-mm"); + + public final static int EXPORT_NODE = 0; + public final static int EXPORT_CHILDREN = 1; + public final static int EXPORT_GRAND_CHILDREN = 2; + + public Object execute(ExecutionEvent event) throws ExecutionException { + ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event) + .getActivePage().getSelection(); + if (selection == null || !(selection instanceof IStructuredSelection)) + return null; + + Iterator lst = ((IStructuredSelection) selection).iterator(); + if (lst.hasNext()) { + Object element = lst.next(); + if (element instanceof SingleJcrNodeElem) { + SingleJcrNodeElem sjn = (SingleJcrNodeElem) element; + Node node = sjn.getNode(); + + ConfigureDumpWizard wizard = new ConfigureDumpWizard( + HandlerUtil.getActiveShell(event), + "Import Resource CSV"); + WizardDialog dialog = new WizardDialog( + HandlerUtil.getActiveShell(event), wizard); + int result = dialog.open(); + + if (result == Window.OK) { + + String dateVal = df.format(new GregorianCalendar() + .getTime()); + try { + + Path tmpDirPath = Files.createTempDirectory(dateVal + + "-NodeDump-"); + List toExport = retrieveToExportNodes(node, + wizard.currExportType); + + for (Node currNode : toExport) { + FileOutputStream fos; + String fileName = wizard.prefix + + JcrUtils.replaceInvalidChars(currNode + .getName()) +"_"+ dateVal + ".xml"; + File currFile = new File(tmpDirPath.toString() + + "/" + fileName); + currFile.createNewFile(); + fos = new FileOutputStream(currFile); + node.getSession().exportSystemView( + currNode.getPath(), fos, + !wizard.includeBinaries, false); + fos.flush(); + fos.close(); + } + } catch (RepositoryException e) { + throw new ArgeoException( + "Unable to perform SystemExport on " + node, e); + } catch (IOException e) { + throw new ArgeoException("Unable to SystemExport " + + node, e); + } + } + } + } + return null; + } + + private List retrieveToExportNodes(Node node, int currExportType) + throws RepositoryException { + List nodes = new ArrayList(); + switch (currExportType) { + case EXPORT_NODE: + nodes.add(node); + return nodes; + case EXPORT_CHILDREN: + return JcrUtils.nodeIteratorToList(node.getNodes()); + case EXPORT_GRAND_CHILDREN: + NodeIterator nit = node.getNodes(); + while (nit.hasNext()) + nodes.addAll(JcrUtils.nodeIteratorToList(nit.nextNode() + .getNodes())); + return nodes; + + default: + return nodes; + } + } + + private synchronized void openGeneratedFile(String path, String fileName) { + Map params = new HashMap(); + params.put(OpenFile.PARAM_FILE_NAME, fileName); + params.put(OpenFile.PARAM_FILE_URI, "file://" + path); + CommandUtils.callCommand("org.argeo.security.ui.specific.openFile", + params); + } + + private class ConfigureDumpWizard extends Wizard { + + // parameters + protected String prefix; + protected int currExportType = EXPORT_NODE; + protected boolean includeBinaries = false; + + // UI Objects + private BasicPage page; + private Text prefixTxt; + private Button includeBinaryBtn; + private Button b1, b2, b3; + + public ConfigureDumpWizard(Shell parentShell, String title) { + setWindowTitle(title); + } + + @Override + public void addPages() { + try { + page = new BasicPage("Main page"); + addPage(page); + } catch (Exception e) { + throw new ArgeoException("Cannot add page to wizard", e); + } + } + + @Override + public boolean performFinish() { + prefix = prefixTxt.getText(); + if (b1.getSelection()) + currExportType = EXPORT_NODE; + else if (b2.getSelection()) + currExportType = EXPORT_CHILDREN; + else if (b3.getSelection()) + currExportType = EXPORT_GRAND_CHILDREN; + includeBinaries = includeBinaryBtn.getSelection(); + return true; + } + + @Override + public boolean performCancel() { + return true; + } + + @Override + public boolean canFinish() { + String errorMsg = "No prefix defined."; + if ("".equals(prefixTxt.getText().trim())) { + page.setErrorMessage(errorMsg); + return false; + } else { + page.setErrorMessage(null); + return true; + } + } + + protected class BasicPage extends WizardPage { + private static final long serialVersionUID = 1L; + + public BasicPage(String pageName) { + super(pageName); + setTitle("Configure dump before launching"); + setMessage("Define the parameters of the dump to launch"); + } + + public void createControl(Composite parent) { + parent.setLayout(EclipseUiUtils.noSpaceGridLayout()); + + // Main Layout + Composite mainCmp = new Composite(parent, SWT.NONE); + mainCmp.setLayout(new GridLayout(2, false)); + mainCmp.setLayoutData(EclipseUiUtils.fillAll()); + + // The path + createBoldLabel(mainCmp, "Prefix"); + prefixTxt = new Text(mainCmp, SWT.SINGLE | SWT.BORDER); + prefixTxt.setLayoutData(EclipseUiUtils.fillAll()); + prefixTxt.addModifyListener(new ModifyListener() { + private static final long serialVersionUID = 1L; + + @Override + public void modifyText(ModifyEvent event) { + if (prefixTxt.getText() != null) + getWizard().getContainer().updateButtons(); + } + }); + + new Label(mainCmp, SWT.SEPARATOR | SWT.HORIZONTAL) + .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, + false, 2, 1)); + + // Which node to export + Label typeLbl = new Label(mainCmp, SWT.RIGHT); + typeLbl.setText(" Type"); + typeLbl.setFont(EclipseUiUtils.getBoldFont(mainCmp)); + typeLbl.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, + false, 1, 3)); + + b1 = new Button(mainCmp, SWT.RADIO); + b1.setText("Export this node"); + b1.setSelection(true); + b2 = new Button(mainCmp, SWT.RADIO); + b2.setText("Export children nodes"); + b3 = new Button(mainCmp, SWT.RADIO); + b3.setText("Export grand-children nodes"); + + new Label(mainCmp, SWT.SEPARATOR | SWT.HORIZONTAL) + .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, + false, 2, 1)); + + createBoldLabel(mainCmp, "Files and images"); + includeBinaryBtn = new Button(mainCmp, SWT.CHECK); + includeBinaryBtn.setText("Include binaries"); + + prefixTxt.setFocus(); + setControl(mainCmp); + } + } + } + + private Label createBoldLabel(Composite parent, String value) { + Label label = new Label(parent, SWT.RIGHT); + label.setText(" " + value); + label.setFont(EclipseUiUtils.getBoldFont(parent)); + label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); + return label; + } +} \ No newline at end of file -- 2.30.2