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
.eclipse
.ui
.workbench
.jcr
.internal
.parts
;
19 import java
.io
.FileInputStream
;
20 import java
.io
.InputStream
;
22 import javax
.jcr
.Binary
;
23 import javax
.jcr
.Node
;
24 import javax
.jcr
.Property
;
25 import javax
.jcr
.nodetype
.NodeType
;
27 import org
.apache
.commons
.io
.FileUtils
;
28 import org
.apache
.commons
.io
.IOUtils
;
29 import org
.apache
.commons
.logging
.Log
;
30 import org
.apache
.commons
.logging
.LogFactory
;
31 import org
.argeo
.ArgeoException
;
32 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
33 import org
.argeo
.eclipse
.ui
.specific
.UploadFileWizardPage
;
34 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
35 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
36 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
37 import org
.eclipse
.jface
.wizard
.Wizard
;
39 public class ImportFileSystemWizard
extends Wizard
{
40 private final static Log log
= LogFactory
41 .getLog(ImportFileSystemWizard
.class);
43 private UploadFileWizardPage importPage
;
44 private final Node folder
;
46 public ImportFileSystemWizard(Node folder
) {
48 setWindowTitle("Import from file system");
52 public void addPages() {
53 importPage
= new UploadFileWizardPage();
55 setNeedsProgressMonitor(importPage
.getNeedsProgressMonitor());
59 * Called when the user click on 'Finish' in the wizard. The real upload to
60 * the JCR repository is done here.
63 public boolean performFinish() {
66 final String objectType
= importPage
.getObjectType();
67 final String objectPath
= importPage
.getObjectPath();
69 // We do not display a progress bar for one file only
70 if (UploadFileWizardPage
.FILE_ITEM_TYPE
.equals(objectType
)) {
71 // In Rap we must force the "real" upload of the file
72 // importPage.performFinish();
74 Node fileNode
= folder
.addNode(importPage
.getObjectName(),
76 Node resNode
= fileNode
.addNode(Property
.JCR_CONTENT
,
77 NodeType
.NT_RESOURCE
);
79 InputStream is
= null;
81 is
= importPage
.getFileInputStream();
82 binary
= folder
.getSession().getValueFactory()
84 resNode
.setProperty(Property
.JCR_DATA
, binary
);
88 IOUtils
.closeQuietly(is
);
90 folder
.getSession().save();
91 } catch (Exception e
) {
96 } else if (UploadFileWizardPage
.FOLDER_ITEM_TYPE
.equals(objectType
)) {
97 if (objectPath
== null || !new File(objectPath
).exists()) {
98 ErrorFeedback
.show("Directory " + objectPath
103 Boolean failed
= false;
104 final File dir
= new File(objectPath
).getAbsoluteFile();
105 final Long sizeB
= directorySize(dir
, 0l);
106 final Stats stats
= new Stats();
107 Long begin
= System
.currentTimeMillis();
109 getContainer().run(true, true, new IRunnableWithProgress() {
110 public void run(IProgressMonitor monitor
) {
112 Integer sizeKB
= (int) (sizeB
/ FileUtils
.ONE_KB
);
113 monitor
.beginTask("", sizeKB
);
114 importDirectory(folder
, dir
, monitor
, stats
);
116 } catch (Exception e
) {
117 if (e
instanceof RuntimeException
)
118 throw (RuntimeException
) e
;
120 throw new ArgeoException("Cannot import "
125 } catch (Exception e
) {
126 ErrorFeedback
.show("Cannot import " + objectPath
, e
);
130 Long duration
= System
.currentTimeMillis() - begin
;
131 Long durationS
= duration
/ 1000l;
132 String durationStr
= (durationS
/ 60) + " min " + (durationS
% 60)
134 StringBuffer message
= new StringBuffer("Imported\n");
135 message
.append(stats
.fileCount
).append(" files\n");
136 message
.append(stats
.dirCount
).append(" directories\n");
137 message
.append(FileUtils
.byteCountToDisplaySize(stats
.sizeB
));
139 message
.append(" of planned ").append(
140 FileUtils
.byteCountToDisplaySize(sizeB
));
141 message
.append("\n");
142 message
.append("in ").append(durationStr
).append("\n");
144 MessageDialog
.openError(getShell(), "Import failed",
147 MessageDialog
.openInformation(getShell(), "Import successful",
156 /** Recursively computes the size of the directory in bytes. */
157 protected Long
directorySize(File dir
, Long currentSize
) {
158 Long size
= currentSize
;
159 File
[] files
= dir
.listFiles();
160 for (File file
: files
) {
161 if (file
.isDirectory()) {
162 size
= directorySize(file
, size
);
164 size
= size
+ file
.length();
171 * Import recursively a directory and its content to the repository.
173 protected void importDirectory(Node folder
, File dir
,
174 IProgressMonitor monitor
, Stats stats
) {
176 File
[] files
= dir
.listFiles();
177 for (File file
: files
) {
178 if (file
.isDirectory()) {
179 Node childFolder
= folder
.addNode(file
.getName(),
181 importDirectory(childFolder
, file
, monitor
, stats
);
182 folder
.getSession().save();
185 Long fileSize
= file
.length();
186 // we skip temporary files that are created by apps when a
187 // file is being edited.
188 // TODO : make this configurable.
189 if (file
.getName().lastIndexOf('~') != file
.getName()
192 monitor
.subTask(file
.getName() + " ("
193 + FileUtils
.byteCountToDisplaySize(fileSize
)
194 + ") " + file
.getCanonicalPath());
196 Node fileNode
= folder
.addNode(file
.getName(),
198 Node resNode
= fileNode
.addNode(
199 Property
.JCR_CONTENT
, NodeType
.NT_RESOURCE
);
200 Binary binary
= null;
205 .createBinary(new FileInputStream(file
));
206 resNode
.setProperty(Property
.JCR_DATA
, binary
);
211 folder
.getSession().save();
213 stats
.sizeB
= stats
.sizeB
+ fileSize
;
214 } catch (Exception e
) {
215 log
.warn("Import of "
219 .byteCountToDisplaySize(fileSize
)
221 folder
.getSession().refresh(false);
223 monitor
.worked((int) (fileSize
/ FileUtils
.ONE_KB
));
227 } catch (Exception e
) {
228 throw new ArgeoException("Cannot import " + dir
+ " to " + folder
,
234 public Long fileCount
= 0l;
235 public Long dirCount
= 0l;
236 public Long sizeB
= 0l;