1 package org
.argeo
.eclipse
.ui
.jcr
.wizards
;
4 import java
.io
.FileInputStream
;
6 import javax
.jcr
.Binary
;
8 import javax
.jcr
.Property
;
9 import javax
.jcr
.nodetype
.NodeType
;
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
.ImportFileSystemHandler
;
18 import org
.argeo
.eclipse
.ui
.specific
.ImportFileSystemWizardPage
;
19 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
20 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
21 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
22 import org
.eclipse
.jface
.wizard
.Wizard
;
24 public class ImportFileSystemWizard
extends Wizard
{
25 private final static Log log
= LogFactory
26 .getLog(ImportFileSystemWizard
.class);
28 private ImportFileSystemWizardPage page1
;
29 private final Node folder
;
31 private ImportFileSystemHandler ifsh
= new ImportFileSystemHandler();
33 public ImportFileSystemWizard(Node folder
) {
35 setNeedsProgressMonitor(ifsh
.getNeedsProgressMonitor());
36 setWindowTitle("Import from file system");
40 public void addPages() {
42 page1
= new ImportFileSystemWizardPage();
44 } catch (Exception e
) {
50 * Called when the user click on 'Finish' in the wizard. The real upload to
51 * the JCR repository is done here.
54 public boolean performFinish() {
57 final String objectType
= page1
.getObjectType();
58 final String objectPath
= page1
.getObjectPath();
60 // We do not display a progress bar for one file only
61 if ("nt:file".equals(objectType
)) {
62 // In Rap we must force the "real" upload of the file
63 page1
.performFinish();
65 Node fileNode
= folder
.addNode(page1
.getObjectName(),
67 Node resNode
= fileNode
.addNode(Property
.JCR_CONTENT
,
68 NodeType
.NT_RESOURCE
);
71 binary
= folder
.getSession().getValueFactory()
72 .createBinary(page1
.getFileInputStream());
73 resNode
.setProperty(Property
.JCR_DATA
, binary
);
77 IOUtils
.closeQuietly(page1
.getFileInputStream());
79 folder
.getSession().save();
80 } catch (Exception e
) {
85 } else if ("nt:folder".equals(objectType
)) {
86 if (objectPath
== null || !new File(objectPath
).exists()) {
87 Error
.show("Directory " + objectPath
+ " does not exist");
91 Boolean failed
= false;
92 final File dir
= new File(objectPath
).getAbsoluteFile();
93 final Long sizeB
= directorySize(dir
, 0l);
94 final Stats stats
= new Stats();
95 Long begin
= System
.currentTimeMillis();
97 getContainer().run(true, true, new IRunnableWithProgress() {
98 public void run(IProgressMonitor monitor
) {
100 Integer sizeKB
= (int) (sizeB
/ FileUtils
.ONE_KB
);
101 monitor
.beginTask("", sizeKB
);
102 importDirectory(folder
, dir
, monitor
, stats
);
104 } catch (Exception e
) {
105 if (e
instanceof RuntimeException
)
106 throw (RuntimeException
) e
;
108 throw new ArgeoException("Cannot import "
113 } catch (Exception e
) {
114 Error
.show("Cannot import " + objectPath
, e
);
118 Long duration
= System
.currentTimeMillis() - begin
;
119 Long durationS
= duration
/ 1000l;
120 String durationStr
= (durationS
/ 60) + " min " + (durationS
% 60)
122 StringBuffer message
= new StringBuffer("Imported\n");
123 message
.append(stats
.fileCount
).append(" files\n");
124 message
.append(stats
.dirCount
).append(" directories\n");
125 message
.append(FileUtils
.byteCountToDisplaySize(stats
.sizeB
));
127 message
.append(" of planned ").append(
128 FileUtils
.byteCountToDisplaySize(sizeB
));
129 message
.append("\n");
130 message
.append("in ").append(durationStr
).append("\n");
132 MessageDialog
.openError(getShell(), "Import failed",
135 MessageDialog
.openInformation(getShell(), "Import successful",
144 /** Recursively computes the size of the directory in bytes. */
145 protected Long
directorySize(File dir
, Long currentSize
) {
146 Long size
= currentSize
;
147 File
[] files
= dir
.listFiles();
148 for (File file
: files
) {
149 if (file
.isDirectory()) {
150 size
= directorySize(file
, size
);
152 size
= size
+ file
.length();
159 * Import recursively a directory and its content to the repository.
161 protected void importDirectory(Node folder
, File dir
,
162 IProgressMonitor monitor
, Stats stats
) {
164 File
[] files
= dir
.listFiles();
165 for (File file
: files
) {
166 if (file
.isDirectory()) {
167 Node childFolder
= folder
.addNode(file
.getName(),
169 importDirectory(childFolder
, file
, monitor
, stats
);
170 folder
.getSession().save();
173 Long fileSize
= file
.length();
175 // we skip tempory files that are created by apps when a
176 // file is being edited.
177 // TODO : make this configurable.
178 if (file
.getName().lastIndexOf('~') != file
.getName()
181 monitor
.subTask(file
.getName() + " ("
182 + FileUtils
.byteCountToDisplaySize(fileSize
)
183 + ") " + file
.getCanonicalPath());
185 Node fileNode
= folder
.addNode(file
.getName(),
187 Node resNode
= fileNode
.addNode(
188 Property
.JCR_CONTENT
, NodeType
.NT_RESOURCE
);
189 Binary binary
= null;
194 .createBinary(new FileInputStream(file
));
195 resNode
.setProperty(Property
.JCR_DATA
, binary
);
200 folder
.getSession().save();
202 stats
.sizeB
= stats
.sizeB
+ fileSize
;
203 } catch (Exception e
) {
204 log
.warn("Import of "
208 .byteCountToDisplaySize(fileSize
)
210 folder
.getSession().refresh(false);
212 monitor
.worked((int) (fileSize
/ FileUtils
.ONE_KB
));
216 } catch (Exception e
) {
217 throw new ArgeoException("Cannot import " + dir
+ " to " + folder
,
223 public Long fileCount
= 0l;
224 public Long dirCount
= 0l;
225 public Long sizeB
= 0l;