1 package org
.argeo
.jcr
.ui
.explorer
.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
.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
;
23 public class ImportFileSystemWizard
extends Wizard
{
24 private final static Log log
= LogFactory
25 .getLog(ImportFileSystemWizard
.class);
27 private ImportToServerWizardPage importPage
;
28 private final Node folder
;
30 public ImportFileSystemWizard(Node folder
) {
32 setWindowTitle("Import from file system");
36 public void addPages() {
37 importPage
= new ImportToServerWizardPage();
39 setNeedsProgressMonitor(importPage
.getNeedsProgressMonitor());
43 * Called when the user click on 'Finish' in the wizard. The real upload to
44 * the JCR repository is done here.
47 public boolean performFinish() {
50 final String objectType
= importPage
.getObjectType();
51 final String objectPath
= importPage
.getObjectPath();
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();
58 Node fileNode
= folder
.addNode(importPage
.getObjectName(),
60 Node resNode
= fileNode
.addNode(Property
.JCR_CONTENT
,
61 NodeType
.NT_RESOURCE
);
64 binary
= folder
.getSession().getValueFactory()
65 .createBinary(importPage
.getFileInputStream());
66 resNode
.setProperty(Property
.JCR_DATA
, binary
);
70 IOUtils
.closeQuietly(importPage
.getFileInputStream());
72 folder
.getSession().save();
73 } catch (Exception e
) {
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");
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();
90 getContainer().run(true, true, new IRunnableWithProgress() {
91 public void run(IProgressMonitor monitor
) {
93 Integer sizeKB
= (int) (sizeB
/ FileUtils
.ONE_KB
);
94 monitor
.beginTask("", sizeKB
);
95 importDirectory(folder
, dir
, monitor
, stats
);
97 } catch (Exception e
) {
98 if (e
instanceof RuntimeException
)
99 throw (RuntimeException
) e
;
101 throw new ArgeoException("Cannot import "
106 } catch (Exception e
) {
107 Error
.show("Cannot import " + objectPath
, e
);
111 Long duration
= System
.currentTimeMillis() - begin
;
112 Long durationS
= duration
/ 1000l;
113 String durationStr
= (durationS
/ 60) + " min " + (durationS
% 60)
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
));
120 message
.append(" of planned ").append(
121 FileUtils
.byteCountToDisplaySize(sizeB
));
122 message
.append("\n");
123 message
.append("in ").append(durationStr
).append("\n");
125 MessageDialog
.openError(getShell(), "Import failed",
128 MessageDialog
.openInformation(getShell(), "Import successful",
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
);
145 size
= size
+ file
.length();
152 * Import recursively a directory and its content to the repository.
154 protected void importDirectory(Node folder
, File dir
,
155 IProgressMonitor monitor
, Stats stats
) {
157 File
[] files
= dir
.listFiles();
158 for (File file
: files
) {
159 if (file
.isDirectory()) {
160 Node childFolder
= folder
.addNode(file
.getName(),
162 importDirectory(childFolder
, file
, monitor
, stats
);
163 folder
.getSession().save();
166 Long fileSize
= file
.length();
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()
174 monitor
.subTask(file
.getName() + " ("
175 + FileUtils
.byteCountToDisplaySize(fileSize
)
176 + ") " + file
.getCanonicalPath());
178 Node fileNode
= folder
.addNode(file
.getName(),
180 Node resNode
= fileNode
.addNode(
181 Property
.JCR_CONTENT
, NodeType
.NT_RESOURCE
);
182 Binary binary
= null;
187 .createBinary(new FileInputStream(file
));
188 resNode
.setProperty(Property
.JCR_DATA
, binary
);
193 folder
.getSession().save();
195 stats
.sizeB
= stats
.sizeB
+ fileSize
;
196 } catch (Exception e
) {
197 log
.warn("Import of "
201 .byteCountToDisplaySize(fileSize
)
203 folder
.getSession().refresh(false);
205 monitor
.worked((int) (fileSize
/ FileUtils
.ONE_KB
));
209 } catch (Exception e
) {
210 throw new ArgeoException("Cannot import " + dir
+ " to " + folder
,
216 public Long fileCount
= 0l;
217 public Long dirCount
= 0l;
218 public Long sizeB
= 0l;