2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.jcr
.ui
.explorer
.wizards
;
19 import java
.io
.FileInputStream
;
21 import javax
.jcr
.Binary
;
22 import javax
.jcr
.Node
;
23 import javax
.jcr
.Property
;
24 import javax
.jcr
.nodetype
.NodeType
;
26 import org
.apache
.commons
.io
.FileUtils
;
27 import org
.apache
.commons
.io
.IOUtils
;
28 import org
.apache
.commons
.logging
.Log
;
29 import org
.apache
.commons
.logging
.LogFactory
;
30 import org
.argeo
.ArgeoException
;
31 import org
.argeo
.eclipse
.ui
.ErrorFeedback
;
32 import org
.argeo
.eclipse
.ui
.specific
.ImportToServerWizardPage
;
33 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
34 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
35 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
36 import org
.eclipse
.jface
.wizard
.Wizard
;
38 public class ImportFileSystemWizard
extends Wizard
{
39 private final static Log log
= LogFactory
40 .getLog(ImportFileSystemWizard
.class);
42 private ImportToServerWizardPage importPage
;
43 private final Node folder
;
45 public ImportFileSystemWizard(Node folder
) {
47 setWindowTitle("Import from file system");
51 public void addPages() {
52 importPage
= new ImportToServerWizardPage();
54 setNeedsProgressMonitor(importPage
.getNeedsProgressMonitor());
58 * Called when the user click on 'Finish' in the wizard. The real upload to
59 * the JCR repository is done here.
62 public boolean performFinish() {
65 final String objectType
= importPage
.getObjectType();
66 final String objectPath
= importPage
.getObjectPath();
68 // We do not display a progress bar for one file only
69 if (ImportToServerWizardPage
.FILE_ITEM_TYPE
.equals(objectType
)) {
70 // In Rap we must force the "real" upload of the file
71 importPage
.performFinish();
73 Node fileNode
= folder
.addNode(importPage
.getObjectName(),
75 Node resNode
= fileNode
.addNode(Property
.JCR_CONTENT
,
76 NodeType
.NT_RESOURCE
);
79 binary
= folder
.getSession().getValueFactory()
80 .createBinary(importPage
.getFileInputStream());
81 resNode
.setProperty(Property
.JCR_DATA
, binary
);
85 IOUtils
.closeQuietly(importPage
.getFileInputStream());
87 folder
.getSession().save();
88 } catch (Exception e
) {
93 } else if (ImportToServerWizardPage
.FOLDER_ITEM_TYPE
.equals(objectType
)) {
94 if (objectPath
== null || !new File(objectPath
).exists()) {
95 ErrorFeedback
.show("Directory " + objectPath
+ " does not exist");
99 Boolean failed
= false;
100 final File dir
= new File(objectPath
).getAbsoluteFile();
101 final Long sizeB
= directorySize(dir
, 0l);
102 final Stats stats
= new Stats();
103 Long begin
= System
.currentTimeMillis();
105 getContainer().run(true, true, new IRunnableWithProgress() {
106 public void run(IProgressMonitor monitor
) {
108 Integer sizeKB
= (int) (sizeB
/ FileUtils
.ONE_KB
);
109 monitor
.beginTask("", sizeKB
);
110 importDirectory(folder
, dir
, monitor
, stats
);
112 } catch (Exception e
) {
113 if (e
instanceof RuntimeException
)
114 throw (RuntimeException
) e
;
116 throw new ArgeoException("Cannot import "
121 } catch (Exception e
) {
122 ErrorFeedback
.show("Cannot import " + objectPath
, e
);
126 Long duration
= System
.currentTimeMillis() - begin
;
127 Long durationS
= duration
/ 1000l;
128 String durationStr
= (durationS
/ 60) + " min " + (durationS
% 60)
130 StringBuffer message
= new StringBuffer("Imported\n");
131 message
.append(stats
.fileCount
).append(" files\n");
132 message
.append(stats
.dirCount
).append(" directories\n");
133 message
.append(FileUtils
.byteCountToDisplaySize(stats
.sizeB
));
135 message
.append(" of planned ").append(
136 FileUtils
.byteCountToDisplaySize(sizeB
));
137 message
.append("\n");
138 message
.append("in ").append(durationStr
).append("\n");
140 MessageDialog
.openError(getShell(), "Import failed",
143 MessageDialog
.openInformation(getShell(), "Import successful",
152 /** Recursively computes the size of the directory in bytes. */
153 protected Long
directorySize(File dir
, Long currentSize
) {
154 Long size
= currentSize
;
155 File
[] files
= dir
.listFiles();
156 for (File file
: files
) {
157 if (file
.isDirectory()) {
158 size
= directorySize(file
, size
);
160 size
= size
+ file
.length();
167 * Import recursively a directory and its content to the repository.
169 protected void importDirectory(Node folder
, File dir
,
170 IProgressMonitor monitor
, Stats stats
) {
172 File
[] files
= dir
.listFiles();
173 for (File file
: files
) {
174 if (file
.isDirectory()) {
175 Node childFolder
= folder
.addNode(file
.getName(),
177 importDirectory(childFolder
, file
, monitor
, stats
);
178 folder
.getSession().save();
181 Long fileSize
= file
.length();
183 // we skip tempory files that are created by apps when a
184 // file is being edited.
185 // TODO : make this configurable.
186 if (file
.getName().lastIndexOf('~') != file
.getName()
189 monitor
.subTask(file
.getName() + " ("
190 + FileUtils
.byteCountToDisplaySize(fileSize
)
191 + ") " + file
.getCanonicalPath());
193 Node fileNode
= folder
.addNode(file
.getName(),
195 Node resNode
= fileNode
.addNode(
196 Property
.JCR_CONTENT
, NodeType
.NT_RESOURCE
);
197 Binary binary
= null;
202 .createBinary(new FileInputStream(file
));
203 resNode
.setProperty(Property
.JCR_DATA
, binary
);
208 folder
.getSession().save();
210 stats
.sizeB
= stats
.sizeB
+ fileSize
;
211 } catch (Exception e
) {
212 log
.warn("Import of "
216 .byteCountToDisplaySize(fileSize
)
218 folder
.getSession().refresh(false);
220 monitor
.worked((int) (fileSize
/ FileUtils
.ONE_KB
));
224 } catch (Exception e
) {
225 throw new ArgeoException("Cannot import " + dir
+ " to " + folder
,
231 public Long fileCount
= 0l;
232 public Long dirCount
= 0l;
233 public Long sizeB
= 0l;