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
.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
;
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 public ImportFileSystemWizard(Node folder
) {
33 setNeedsProgressMonitor(true);
34 setWindowTitle("Import from file system");
38 public void addPages() {
39 page1
= new ImportFileSystemWizardPage();
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");
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();
57 getContainer().run(true, true, new IRunnableWithProgress() {
58 public void run(IProgressMonitor monitor
) {
60 Integer sizeKB
= (int) (sizeB
/ FileUtils
.ONE_KB
);
61 monitor
.beginTask("", sizeKB
);
62 importDirectory(folder
, dir
, monitor
, stats
);
64 } catch (Exception e
) {
65 if (e
instanceof RuntimeException
)
66 throw (RuntimeException
) e
;
68 throw new ArgeoException("Cannot import "
73 } catch (Exception e
) {
74 Error
.show("Cannot import " + directory
, e
);
78 Long duration
= System
.currentTimeMillis() - begin
;
79 Long durationS
= duration
/ 1000l;
80 String durationStr
= (durationS
/ 60) + " min " + (durationS
% 60)
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
));
87 message
.append(" of planned ").append(
88 FileUtils
.byteCountToDisplaySize(sizeB
));
90 message
.append("in ").append(durationStr
).append("\n");
92 MessageDialog
.openError(getShell(), "Import failed",
95 MessageDialog
.openInformation(getShell(), "Import successful",
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
);
109 size
= size
+ file
.length();
115 /** Recursively computes the size of the directory in bytes. */
116 protected void importDirectory(Node folder
, File dir
,
117 IProgressMonitor monitor
, Stats stats
) {
119 File
[] files
= dir
.listFiles();
120 for (File file
: files
) {
121 if (file
.isDirectory()) {
122 Node childFolder
= folder
.addNode(file
.getName(),
124 importDirectory(childFolder
, file
, monitor
, stats
);
125 folder
.getSession().save();
128 Long fileSize
= file
.length();
129 monitor
.subTask(file
.getName() + " ("
130 + FileUtils
.byteCountToDisplaySize(fileSize
) + ") "
131 + file
.getCanonicalPath());
133 Node fileNode
= folder
.addNode(file
.getName(),
135 Node resNode
= fileNode
.addNode(Property
.JCR_CONTENT
,
136 NodeType
.NT_RESOURCE
);
137 Binary binary
= null;
139 binary
= folder
.getSession().getValueFactory()
140 .createBinary(new FileInputStream(file
));
141 resNode
.setProperty(Property
.JCR_DATA
, binary
);
146 folder
.getSession().save();
148 stats
.sizeB
= stats
.sizeB
+ fileSize
;
149 } catch (Exception e
) {
150 log
.warn("Import of " + file
+ " ("
151 + FileUtils
.byteCountToDisplaySize(fileSize
)
153 folder
.getSession().refresh(false);
155 monitor
.worked((int) (fileSize
/ FileUtils
.ONE_KB
));
158 } catch (Exception e
) {
159 throw new ArgeoException("Cannot import " + dir
+ " to " + folder
,
164 protected class ImportFileSystemWizardPage
extends WizardPage
{
165 private DirectoryFieldEditor dfe
;
167 public ImportFileSystemWizardPage() {
168 super("Import from file system");
169 setDescription("Import files from the local file system into the JCR repository");
172 public void createControl(Composite parent
) {
173 dfe
= new DirectoryFieldEditor("directory", "From",
175 setControl(dfe
.getTextControl(parent
));
178 public String
getDirectory() {
179 return dfe
.getStringValue();
185 public Long fileCount
= 0l;
186 public Long dirCount
= 0l;
187 public Long sizeB
= 0l;