]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/wizards/ImportFileSystemWizard.java
Deprecate TreeObject (not used anywhere anymore)
[lgpl/argeo-commons.git] / server / plugins / org.argeo.jcr.ui.explorer / src / main / java / org / argeo / jcr / ui / explorer / wizards / ImportFileSystemWizard.java
1 package org.argeo.jcr.ui.explorer.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.io.IOUtils;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.argeo.ArgeoException;
16 import org.argeo.eclipse.ui.dialogs.Error;
17 import org.argeo.eclipse.ui.specific.ImportToServerWizardPage;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.operation.IRunnableWithProgress;
21 import org.eclipse.jface.wizard.Wizard;
22
23 public class ImportFileSystemWizard extends Wizard {
24 private final static Log log = LogFactory
25 .getLog(ImportFileSystemWizard.class);
26
27 private ImportToServerWizardPage importPage;
28 private final Node folder;
29
30 public ImportFileSystemWizard(Node folder) {
31 this.folder = folder;
32 setWindowTitle("Import from file system");
33 }
34
35 @Override
36 public void addPages() {
37 importPage = new ImportToServerWizardPage();
38 addPage(importPage);
39 setNeedsProgressMonitor(importPage.getNeedsProgressMonitor());
40 }
41
42 /**
43 * Called when the user click on 'Finish' in the wizard. The real upload to
44 * the JCR repository is done here.
45 */
46 @Override
47 public boolean performFinish() {
48
49 // Initialization
50 final String objectType = importPage.getObjectType();
51 final String objectPath = importPage.getObjectPath();
52
53 // We do not display a progress bar for one file only
54 if (importPage.FILE_ITEM_TYPE.equals(objectType)) {
55 // In Rap we must force the "real" upload of the file
56 importPage.performFinish();
57 try {
58 Node fileNode = folder.addNode(importPage.getObjectName(),
59 NodeType.NT_FILE);
60 Node resNode = fileNode.addNode(Property.JCR_CONTENT,
61 NodeType.NT_RESOURCE);
62 Binary binary = null;
63 try {
64 binary = folder.getSession().getValueFactory()
65 .createBinary(importPage.getFileInputStream());
66 resNode.setProperty(Property.JCR_DATA, binary);
67 } finally {
68 if (binary != null)
69 binary.dispose();
70 IOUtils.closeQuietly(importPage.getFileInputStream());
71 }
72 folder.getSession().save();
73 } catch (Exception e) {
74 e.printStackTrace();
75 return false;
76 }
77 return true;
78 } else if (importPage.FOLDER_ITEM_TYPE.equals(objectType)) {
79 if (objectPath == null || !new File(objectPath).exists()) {
80 Error.show("Directory " + objectPath + " does not exist");
81 return false;
82 }
83
84 Boolean failed = false;
85 final File dir = new File(objectPath).getAbsoluteFile();
86 final Long sizeB = directorySize(dir, 0l);
87 final Stats stats = new Stats();
88 Long begin = System.currentTimeMillis();
89 try {
90 getContainer().run(true, true, new IRunnableWithProgress() {
91 public void run(IProgressMonitor monitor) {
92 try {
93 Integer sizeKB = (int) (sizeB / FileUtils.ONE_KB);
94 monitor.beginTask("", sizeKB);
95 importDirectory(folder, dir, monitor, stats);
96 monitor.done();
97 } catch (Exception e) {
98 if (e instanceof RuntimeException)
99 throw (RuntimeException) e;
100 else
101 throw new ArgeoException("Cannot import "
102 + objectPath, e);
103 }
104 }
105 });
106 } catch (Exception e) {
107 Error.show("Cannot import " + objectPath, e);
108 failed = true;
109 }
110
111 Long duration = System.currentTimeMillis() - begin;
112 Long durationS = duration / 1000l;
113 String durationStr = (durationS / 60) + " min " + (durationS % 60)
114 + " s";
115 StringBuffer message = new StringBuffer("Imported\n");
116 message.append(stats.fileCount).append(" files\n");
117 message.append(stats.dirCount).append(" directories\n");
118 message.append(FileUtils.byteCountToDisplaySize(stats.sizeB));
119 if (failed)
120 message.append(" of planned ").append(
121 FileUtils.byteCountToDisplaySize(sizeB));
122 message.append("\n");
123 message.append("in ").append(durationStr).append("\n");
124 if (failed)
125 MessageDialog.openError(getShell(), "Import failed",
126 message.toString());
127 else
128 MessageDialog.openInformation(getShell(), "Import successful",
129 message.toString());
130
131 return true;
132 }
133 return false;
134
135 }
136
137 /** Recursively computes the size of the directory in bytes. */
138 protected Long directorySize(File dir, Long currentSize) {
139 Long size = currentSize;
140 File[] files = dir.listFiles();
141 for (File file : files) {
142 if (file.isDirectory()) {
143 size = directorySize(file, size);
144 } else {
145 size = size + file.length();
146 }
147 }
148 return size;
149 }
150
151 /**
152 * Import recursively a directory and its content to the repository.
153 */
154 protected void importDirectory(Node folder, File dir,
155 IProgressMonitor monitor, Stats stats) {
156 try {
157 File[] files = dir.listFiles();
158 for (File file : files) {
159 if (file.isDirectory()) {
160 Node childFolder = folder.addNode(file.getName(),
161 NodeType.NT_FOLDER);
162 importDirectory(childFolder, file, monitor, stats);
163 folder.getSession().save();
164 stats.dirCount++;
165 } else {
166 Long fileSize = file.length();
167
168 // we skip tempory files that are created by apps when a
169 // file is being edited.
170 // TODO : make this configurable.
171 if (file.getName().lastIndexOf('~') != file.getName()
172 .length() - 1) {
173
174 monitor.subTask(file.getName() + " ("
175 + FileUtils.byteCountToDisplaySize(fileSize)
176 + ") " + file.getCanonicalPath());
177 try {
178 Node fileNode = folder.addNode(file.getName(),
179 NodeType.NT_FILE);
180 Node resNode = fileNode.addNode(
181 Property.JCR_CONTENT, NodeType.NT_RESOURCE);
182 Binary binary = null;
183 try {
184 binary = folder
185 .getSession()
186 .getValueFactory()
187 .createBinary(new FileInputStream(file));
188 resNode.setProperty(Property.JCR_DATA, binary);
189 } finally {
190 if (binary != null)
191 binary.dispose();
192 }
193 folder.getSession().save();
194 stats.fileCount++;
195 stats.sizeB = stats.sizeB + fileSize;
196 } catch (Exception e) {
197 log.warn("Import of "
198 + file
199 + " ("
200 + FileUtils
201 .byteCountToDisplaySize(fileSize)
202 + ") failed: " + e);
203 folder.getSession().refresh(false);
204 }
205 monitor.worked((int) (fileSize / FileUtils.ONE_KB));
206 }
207 }
208 }
209 } catch (Exception e) {
210 throw new ArgeoException("Cannot import " + dir + " to " + folder,
211 e);
212 }
213 }
214
215 static class Stats {
216 public Long fileCount = 0l;
217 public Long dirCount = 0l;
218 public Long sizeB = 0l;
219 }
220 }