]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ImportFileSystemWizard.java
b10b066d42de531982af5ec8ce14110a281d1a80
[lgpl/argeo-commons.git] / ImportFileSystemWizard.java
1 package org.argeo.eclipse.ui.jcr.wizards;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5
6 import javax.jcr.Binary;
7 import javax.jcr.Node;
8 import javax.jcr.Property;
9 import javax.jcr.nodetype.NodeType;
10
11 import org.apache.commons.io.FileUtils;
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.argeo.ArgeoException;
15 import org.argeo.eclipse.ui.dialogs.Error;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.operation.IRunnableWithProgress;
19 import org.eclipse.jface.preference.DirectoryFieldEditor;
20 import org.eclipse.jface.wizard.Wizard;
21 import org.eclipse.jface.wizard.WizardPage;
22 import org.eclipse.swt.widgets.Composite;
23
24 public class ImportFileSystemWizard extends Wizard {
25 private final static Log log = LogFactory
26 .getLog(ImportFileSystemWizard.class);
27
28 private ImportFileSystemWizardPage page1;
29 private final Node folder;
30
31 public ImportFileSystemWizard(Node folder) {
32 this.folder = folder;
33 setNeedsProgressMonitor(true);
34 setWindowTitle("Import from file system");
35 }
36
37 @Override
38 public void addPages() {
39 page1 = new ImportFileSystemWizardPage();
40 addPage(page1);
41 }
42
43 @Override
44 public boolean performFinish() {
45 final String directory = page1.getDirectory();
46 if (directory == null || !new File(directory).exists()) {
47 Error.show("Directory " + directory + " does not exist");
48 return false;
49 }
50
51 Boolean failed = false;
52 final File dir = new File(directory).getAbsoluteFile();
53 final Long sizeB = directorySize(dir, 0l);
54 final Stats stats = new Stats();
55 Long begin = System.currentTimeMillis();
56 try {
57 getContainer().run(true, true, new IRunnableWithProgress() {
58 public void run(IProgressMonitor monitor) {
59 try {
60 Integer sizeKB = (int) (sizeB / FileUtils.ONE_KB);
61 monitor.beginTask("", sizeKB);
62 importDirectory(folder, dir, monitor, stats);
63 monitor.done();
64 } catch (Exception e) {
65 if (e instanceof RuntimeException)
66 throw (RuntimeException) e;
67 else
68 throw new ArgeoException("Cannot import "
69 + directory, e);
70 }
71 }
72 });
73 } catch (Exception e) {
74 Error.show("Cannot import " + directory, e);
75 failed = true;
76 }
77
78 Long duration = System.currentTimeMillis() - begin;
79 Long durationS = duration / 1000l;
80 String durationStr = (durationS / 60) + " min " + (durationS % 60)
81 + " s";
82 StringBuffer message = new StringBuffer("Imported\n");
83 message.append(stats.fileCount).append(" files\n");
84 message.append(stats.dirCount).append(" directories\n");
85 message.append(FileUtils.byteCountToDisplaySize(stats.sizeB));
86 if (failed)
87 message.append(" of planned ").append(
88 FileUtils.byteCountToDisplaySize(sizeB));
89 message.append("\n");
90 message.append("in ").append(durationStr).append("\n");
91 if (failed)
92 MessageDialog.openError(getShell(), "Import failed",
93 message.toString());
94 else
95 MessageDialog.openInformation(getShell(), "Import successful",
96 message.toString());
97
98 return true;
99 }
100
101 /** Recursively computes the size of the directory in bytes. */
102 protected Long directorySize(File dir, Long currentSize) {
103 Long size = currentSize;
104 File[] files = dir.listFiles();
105 for (File file : files) {
106 if (file.isDirectory()) {
107 size = directorySize(file, size);
108 } else {
109 size = size + file.length();
110 }
111 }
112 return size;
113 }
114
115 /** Recursively computes the size of the directory in bytes. */
116 protected void importDirectory(Node folder, File dir,
117 IProgressMonitor monitor, Stats stats) {
118 try {
119 File[] files = dir.listFiles();
120 for (File file : files) {
121 if (file.isDirectory()) {
122 Node childFolder = folder.addNode(file.getName(),
123 NodeType.NT_FOLDER);
124 importDirectory(childFolder, file, monitor, stats);
125 folder.getSession().save();
126 stats.dirCount++;
127 } else {
128 Long fileSize = file.length();
129 monitor.subTask(file.getName() + " ("
130 + FileUtils.byteCountToDisplaySize(fileSize) + ") "
131 + file.getCanonicalPath());
132 try {
133 Node fileNode = folder.addNode(file.getName(),
134 NodeType.NT_FILE);
135 Node resNode = fileNode.addNode(Property.JCR_CONTENT,
136 NodeType.NT_RESOURCE);
137 Binary binary = null;
138 try {
139 binary = folder.getSession().getValueFactory()
140 .createBinary(new FileInputStream(file));
141 resNode.setProperty(Property.JCR_DATA, binary);
142 } finally {
143 if (binary != null)
144 binary.dispose();
145 }
146 folder.getSession().save();
147 stats.fileCount++;
148 stats.sizeB = stats.sizeB + fileSize;
149 } catch (Exception e) {
150 log.warn("Import of " + file + " ("
151 + FileUtils.byteCountToDisplaySize(fileSize)
152 + ") failed: " + e);
153 folder.getSession().refresh(false);
154 }
155 monitor.worked((int) (fileSize / FileUtils.ONE_KB));
156 }
157 }
158 } catch (Exception e) {
159 throw new ArgeoException("Cannot import " + dir + " to " + folder,
160 e);
161 }
162 }
163
164 protected class ImportFileSystemWizardPage extends WizardPage {
165 private DirectoryFieldEditor dfe;
166
167 public ImportFileSystemWizardPage() {
168 super("Import from file system");
169 setDescription("Import files from the local file system into the JCR repository");
170 }
171
172 public void createControl(Composite parent) {
173 dfe = new DirectoryFieldEditor("directory", "From",
174 parent);
175 setControl(dfe.getTextControl(parent));
176 }
177
178 public String getDirectory() {
179 return dfe.getStringValue();
180 }
181
182 }
183
184 static class Stats {
185 public Long fileCount = 0l;
186 public Long dirCount = 0l;
187 public Long sizeB = 0l;
188 }
189 }