Code cleaning
[lgpl/argeo-commons.git] / org.argeo.cms.ui.workbench / src / org / argeo / cms / ui / workbench / internal / jcr / parts / ImportFileSystemWizard.java
index 21857075e99e907ef0c03e331c0d7d3f8c18b1f7..b9acf6146b0f2ed680c290a7ffb2cd7d64de4e4a 100644 (file)
@@ -17,7 +17,6 @@ package org.argeo.cms.ui.workbench.internal.jcr.parts;
 
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.InputStream;
 
 import javax.jcr.Binary;
 import javax.jcr.Node;
@@ -25,20 +24,16 @@ import javax.jcr.Property;
 import javax.jcr.nodetype.NodeType;
 
 import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.argeo.cms.CmsException;
 import org.argeo.eclipse.ui.EclipseUiException;
-import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
 import org.argeo.eclipse.ui.specific.UploadFileWizardPage;
 import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.jface.dialogs.MessageDialog;
-import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.eclipse.jface.wizard.Wizard;
 
 public class ImportFileSystemWizard extends Wizard {
-       private final static Log log = LogFactory
-                       .getLog(ImportFileSystemWizard.class);
+       private final static Log log = LogFactory.getLog(ImportFileSystemWizard.class);
 
        private UploadFileWizardPage importPage;
        private final Node folder;
@@ -46,13 +41,14 @@ public class ImportFileSystemWizard extends Wizard {
        public ImportFileSystemWizard(Node folder) {
                this.folder = folder;
                setWindowTitle("Import from file system");
+               throw new CmsException("Deprecated wizard, cannot be used");
        }
 
        @Override
        public void addPages() {
-               importPage = new UploadFileWizardPage();
-               addPage(importPage);
-               setNeedsProgressMonitor(importPage.getNeedsProgressMonitor());
+               // importPage = new UploadFileWizardPage();
+               // addPage(importPage);
+               // setNeedsProgressMonitor(importPage.getNeedsProgressMonitor());
        }
 
        /**
@@ -62,93 +58,94 @@ public class ImportFileSystemWizard extends Wizard {
        @Override
        public boolean performFinish() {
 
-               // Initialization
-               final String objectType = importPage.getObjectType();
-               final String objectPath = importPage.getObjectPath();
-
-               // We do not display a progress bar for one file only
-               if (UploadFileWizardPage.FILE_ITEM_TYPE.equals(objectType)) {
-                       // In Rap we must force the "real" upload of the file
-                       // importPage.performFinish();
-                       try {
-                               Node fileNode = folder.addNode(importPage.getObjectName(),
-                                               NodeType.NT_FILE);
-                               Node resNode = fileNode.addNode(Property.JCR_CONTENT,
-                                               NodeType.NT_RESOURCE);
-                               Binary binary = null;
-                               InputStream is = null;
-                               try {
-                                       is = importPage.getFileInputStream();
-                                       binary = folder.getSession().getValueFactory()
-                                                       .createBinary(is);
-                                       resNode.setProperty(Property.JCR_DATA, binary);
-                               } finally {
-                                       if (binary != null)
-                                               binary.dispose();
-                                       IOUtils.closeQuietly(is);
-                               }
-                               folder.getSession().save();
-                       } catch (Exception e) {
-                               e.printStackTrace();
-                               return false;
-                       }
-                       return true;
-               } else if (UploadFileWizardPage.FOLDER_ITEM_TYPE.equals(objectType)) {
-                       if (objectPath == null || !new File(objectPath).exists()) {
-                               ErrorFeedback.show("Directory " + objectPath
-                                               + " does not exist");
-                               return false;
-                       }
-
-                       Boolean failed = false;
-                       final File dir = new File(objectPath).getAbsoluteFile();
-                       final Long sizeB = directorySize(dir, 0l);
-                       final Stats stats = new Stats();
-                       Long begin = System.currentTimeMillis();
-                       try {
-                               getContainer().run(true, true, new IRunnableWithProgress() {
-                                       public void run(IProgressMonitor monitor) {
-                                               try {
-                                                       Integer sizeKB = (int) (sizeB / FileUtils.ONE_KB);
-                                                       monitor.beginTask("", sizeKB);
-                                                       importDirectory(folder, dir, monitor, stats);
-                                                       monitor.done();
-                                               } catch (Exception e) {
-                                                       if (e instanceof RuntimeException)
-                                                               throw (RuntimeException) e;
-                                                       else
-                                                               throw new EclipseUiException("Cannot import "
-                                                                               + objectPath, e);
-                                               }
-                                       }
-                               });
-                       } catch (Exception e) {
-                               ErrorFeedback.show("Cannot import " + objectPath, e);
-                               failed = true;
-                       }
-
-                       Long duration = System.currentTimeMillis() - begin;
-                       Long durationS = duration / 1000l;
-                       String durationStr = (durationS / 60) + " min " + (durationS % 60)
-                                       + " s";
-                       StringBuffer message = new StringBuffer("Imported\n");
-                       message.append(stats.fileCount).append(" files\n");
-                       message.append(stats.dirCount).append(" directories\n");
-                       message.append(FileUtils.byteCountToDisplaySize(stats.sizeB));
-                       if (failed)
-                               message.append(" of planned ").append(
-                                               FileUtils.byteCountToDisplaySize(sizeB));
-                       message.append("\n");
-                       message.append("in ").append(durationStr).append("\n");
-                       if (failed)
-                               MessageDialog.openError(getShell(), "Import failed",
-                                               message.toString());
-                       else
-                               MessageDialog.openInformation(getShell(), "Import successful",
-                                               message.toString());
-
-                       return true;
-               }
+               // // Initialization
+               // final String objectType = importPage.getObjectType();
+               // final String objectPath = importPage.getObjectPath();
+               //
+               // // We do not display a progress bar for one file only
+               // if (UploadFileWizardPage.FILE_ITEM_TYPE.equals(objectType)) {
+               // // In Rap we must force the "real" upload of the file
+               // // importPage.performFinish();
+               // try {
+               // Node fileNode = folder.addNode(importPage.getObjectName(),
+               // NodeType.NT_FILE);
+               // Node resNode = fileNode.addNode(Property.JCR_CONTENT,
+               // NodeType.NT_RESOURCE);
+               // Binary binary = null;
+               // InputStream is = null;
+               // try {
+               // is = importPage.getFileInputStream();
+               // binary = folder.getSession().getValueFactory()
+               // .createBinary(is);
+               // resNode.setProperty(Property.JCR_DATA, binary);
+               // } finally {
+               // if (binary != null)
+               // binary.dispose();
+               // IOUtils.closeQuietly(is);
+               // }
+               // folder.getSession().save();
+               // } catch (Exception e) {
+               // e.printStackTrace();
+               // return false;
+               // }
+               // return true;
+               // } else if (UploadFileWizardPage.FOLDER_ITEM_TYPE.equals(objectType))
+               // {
+               // if (objectPath == null || !new File(objectPath).exists()) {
+               // ErrorFeedback.show("Directory " + objectPath
+               // + " does not exist");
+               // return false;
+               // }
+               //
+               // Boolean failed = false;
+               // final File dir = new File(objectPath).getAbsoluteFile();
+               // final Long sizeB = directorySize(dir, 0l);
+               // final Stats stats = new Stats();
+               // Long begin = System.currentTimeMillis();
+               // try {
+               // getContainer().run(true, true, new IRunnableWithProgress() {
+               // public void run(IProgressMonitor monitor) {
+               // try {
+               // Integer sizeKB = (int) (sizeB / FileUtils.ONE_KB);
+               // monitor.beginTask("", sizeKB);
+               // importDirectory(folder, dir, monitor, stats);
+               // monitor.done();
+               // } catch (Exception e) {
+               // if (e instanceof RuntimeException)
+               // throw (RuntimeException) e;
+               // else
+               // throw new EclipseUiException("Cannot import "
+               // + objectPath, e);
+               // }
+               // }
+               // });
+               // } catch (Exception e) {
+               // ErrorFeedback.show("Cannot import " + objectPath, e);
+               // failed = true;
+               // }
+               //
+               // Long duration = System.currentTimeMillis() - begin;
+               // Long durationS = duration / 1000l;
+               // String durationStr = (durationS / 60) + " min " + (durationS % 60)
+               // + " s";
+               // StringBuffer message = new StringBuffer("Imported\n");
+               // message.append(stats.fileCount).append(" files\n");
+               // message.append(stats.dirCount).append(" directories\n");
+               // message.append(FileUtils.byteCountToDisplaySize(stats.sizeB));
+               // if (failed)
+               // message.append(" of planned ").append(
+               // FileUtils.byteCountToDisplaySize(sizeB));
+               // message.append("\n");
+               // message.append("in ").append(durationStr).append("\n");
+               // if (failed)
+               // MessageDialog.openError(getShell(), "Import failed",
+               // message.toString());
+               // else
+               // MessageDialog.openInformation(getShell(), "Import successful",
+               // message.toString());
+               //
+               // return true;
+               // }
                return false;
 
        }
@@ -170,14 +167,12 @@ public class ImportFileSystemWizard extends Wizard {
        /**
         * Import recursively a directory and its content to the repository.
         */
-       protected void importDirectory(Node folder, File dir,
-                       IProgressMonitor monitor, Stats stats) {
+       protected void importDirectory(Node folder, File dir, IProgressMonitor monitor, Stats stats) {
                try {
                        File[] files = dir.listFiles();
                        for (File file : files) {
                                if (file.isDirectory()) {
-                                       Node childFolder = folder.addNode(file.getName(),
-                                                       NodeType.NT_FOLDER);
+                                       Node childFolder = folder.addNode(file.getName(), NodeType.NT_FOLDER);
                                        importDirectory(childFolder, file, monitor, stats);
                                        folder.getSession().save();
                                        stats.dirCount++;
@@ -186,23 +181,16 @@ public class ImportFileSystemWizard extends Wizard {
                                        // we skip temporary files that are created by apps when a
                                        // file is being edited.
                                        // TODO : make this configurable.
-                                       if (file.getName().lastIndexOf('~') != file.getName()
-                                                       .length() - 1) {
+                                       if (file.getName().lastIndexOf('~') != file.getName().length() - 1) {
 
-                                               monitor.subTask(file.getName() + " ("
-                                                               + FileUtils.byteCountToDisplaySize(fileSize)
-                                                               + ") " + file.getCanonicalPath());
+                                               monitor.subTask(file.getName() + " (" + FileUtils.byteCountToDisplaySize(fileSize) + ") "
+                                                               + file.getCanonicalPath());
                                                try {
-                                                       Node fileNode = folder.addNode(file.getName(),
-                                                                       NodeType.NT_FILE);
-                                                       Node resNode = fileNode.addNode(
-                                                                       Property.JCR_CONTENT, NodeType.NT_RESOURCE);
+                                                       Node fileNode = folder.addNode(file.getName(), NodeType.NT_FILE);
+                                                       Node resNode = fileNode.addNode(Property.JCR_CONTENT, NodeType.NT_RESOURCE);
                                                        Binary binary = null;
                                                        try {
-                                                               binary = folder
-                                                                               .getSession()
-                                                                               .getValueFactory()
-                                                                               .createBinary(new FileInputStream(file));
+                                                               binary = folder.getSession().getValueFactory().createBinary(new FileInputStream(file));
                                                                resNode.setProperty(Property.JCR_DATA, binary);
                                                        } finally {
                                                                if (binary != null)
@@ -212,11 +200,7 @@ public class ImportFileSystemWizard extends Wizard {
                                                        stats.fileCount++;
                                                        stats.sizeB = stats.sizeB + fileSize;
                                                } catch (Exception e) {
-                                                       log.warn("Import of "
-                                                                       + file
-                                                                       + " ("
-                                                                       + FileUtils
-                                                                                       .byteCountToDisplaySize(fileSize)
+                                                       log.warn("Import of " + file + " (" + FileUtils.byteCountToDisplaySize(fileSize)
                                                                        + ") failed: " + e);
                                                        folder.getSession().refresh(false);
                                                }
@@ -225,8 +209,7 @@ public class ImportFileSystemWizard extends Wizard {
                                }
                        }
                } catch (Exception e) {
-                       throw new EclipseUiException("Cannot import " + dir + " to " + folder,
-                                       e);
+                       throw new EclipseUiException("Cannot import " + dir + " to " + folder, e);
                }
        }