5238 |
09 Feb 10 |
nicklas |
1 |
/** |
5238 |
09 Feb 10 |
nicklas |
$Id$ |
5238 |
09 Feb 10 |
nicklas |
3 |
|
5238 |
09 Feb 10 |
nicklas |
Copyright (C) 2010 Nicklas Nordborg |
5238 |
09 Feb 10 |
nicklas |
5 |
|
5238 |
09 Feb 10 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
5238 |
09 Feb 10 |
nicklas |
Available at http://base.thep.lu.se/ |
5238 |
09 Feb 10 |
nicklas |
8 |
|
5238 |
09 Feb 10 |
nicklas |
BASE is free software; you can redistribute it and/or |
5238 |
09 Feb 10 |
nicklas |
modify it under the terms of the GNU General Public License |
5238 |
09 Feb 10 |
nicklas |
as published by the Free Software Foundation; either version 3 |
5238 |
09 Feb 10 |
nicklas |
of the License, or (at your option) any later version. |
5238 |
09 Feb 10 |
nicklas |
13 |
|
5238 |
09 Feb 10 |
nicklas |
BASE is distributed in the hope that it will be useful, |
5238 |
09 Feb 10 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
5238 |
09 Feb 10 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5238 |
09 Feb 10 |
nicklas |
GNU General Public License for more details. |
5238 |
09 Feb 10 |
nicklas |
18 |
|
5238 |
09 Feb 10 |
nicklas |
You should have received a copy of the GNU General Public License |
5238 |
09 Feb 10 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
5238 |
09 Feb 10 |
nicklas |
21 |
*/ |
5238 |
09 Feb 10 |
nicklas |
22 |
package net.sf.basedb.util.bfs; |
5238 |
09 Feb 10 |
nicklas |
23 |
|
5238 |
09 Feb 10 |
nicklas |
24 |
import java.io.IOException; |
5238 |
09 Feb 10 |
nicklas |
25 |
import java.io.InputStream; |
5238 |
09 Feb 10 |
nicklas |
26 |
|
5238 |
09 Feb 10 |
nicklas |
27 |
import net.sf.basedb.core.DbControl; |
5238 |
09 Feb 10 |
nicklas |
28 |
import net.sf.basedb.core.Directory; |
5238 |
09 Feb 10 |
nicklas |
29 |
import net.sf.basedb.core.File; |
5238 |
09 Feb 10 |
nicklas |
30 |
import net.sf.basedb.core.ItemNotFoundException; |
5238 |
09 Feb 10 |
nicklas |
31 |
import net.sf.basedb.util.FileUtil; |
5238 |
09 Feb 10 |
nicklas |
32 |
|
5238 |
09 Feb 10 |
nicklas |
33 |
/** |
5238 |
09 Feb 10 |
nicklas |
Extra file importer implementation that imports file to a specified |
5238 |
09 Feb 10 |
nicklas |
directory in the BASE file system. Files that already exists can |
5238 |
09 Feb 10 |
nicklas |
be overwritten or ignored. Files that already exists and have their |
5238 |
09 Feb 10 |
nicklas |
removed flag set are always overwritten. |
5238 |
09 Feb 10 |
nicklas |
38 |
|
5238 |
09 Feb 10 |
nicklas |
@author Nicklas |
5238 |
09 Feb 10 |
nicklas |
@since 2.15 |
5238 |
09 Feb 10 |
nicklas |
@base.modified $Date$ |
5238 |
09 Feb 10 |
nicklas |
42 |
*/ |
5238 |
09 Feb 10 |
nicklas |
43 |
public class GenericExtraFileImporter |
5238 |
09 Feb 10 |
nicklas |
44 |
implements ExtraFileImporter |
5238 |
09 Feb 10 |
nicklas |
45 |
{ |
5238 |
09 Feb 10 |
nicklas |
46 |
|
5238 |
09 Feb 10 |
nicklas |
47 |
private final DbControl dc; |
5238 |
09 Feb 10 |
nicklas |
48 |
private final Directory dir; |
5238 |
09 Feb 10 |
nicklas |
49 |
private final boolean overwrite; |
5238 |
09 Feb 10 |
nicklas |
50 |
|
5238 |
09 Feb 10 |
nicklas |
51 |
/** |
5238 |
09 Feb 10 |
nicklas |
Create a new extra file importer. |
5238 |
09 Feb 10 |
nicklas |
@param dc A DbControl to use for database access |
5238 |
09 Feb 10 |
nicklas |
@param dir The directory in the BASE file system where the imported files |
5238 |
09 Feb 10 |
nicklas |
should be placed |
5238 |
09 Feb 10 |
nicklas |
@param overwrite TRUE to force overwriting existing files, FALSE to only |
5238 |
09 Feb 10 |
nicklas |
overwrite files that have been marked for removal |
5238 |
09 Feb 10 |
nicklas |
58 |
*/ |
5238 |
09 Feb 10 |
nicklas |
59 |
public GenericExtraFileImporter(DbControl dc, Directory dir, boolean overwrite) |
5238 |
09 Feb 10 |
nicklas |
60 |
{ |
5238 |
09 Feb 10 |
nicklas |
61 |
this.dc = dc; |
5238 |
09 Feb 10 |
nicklas |
62 |
this.dir = dir; |
5238 |
09 Feb 10 |
nicklas |
63 |
this.overwrite = overwrite; |
5238 |
09 Feb 10 |
nicklas |
64 |
} |
5238 |
09 Feb 10 |
nicklas |
65 |
|
5238 |
09 Feb 10 |
nicklas |
66 |
@Override |
5238 |
09 Feb 10 |
nicklas |
67 |
public File importExtraFile(String filename, InputStreamLocator locator) |
5238 |
09 Feb 10 |
nicklas |
68 |
throws IOException |
5238 |
09 Feb 10 |
nicklas |
69 |
{ |
5238 |
09 Feb 10 |
nicklas |
70 |
File f = null; |
5238 |
09 Feb 10 |
nicklas |
71 |
try |
5238 |
09 Feb 10 |
nicklas |
72 |
{ |
5238 |
09 Feb 10 |
nicklas |
73 |
f = File.getFile(dc, dir, filename, true); |
5238 |
09 Feb 10 |
nicklas |
74 |
if (!f.isInDatabase()) |
5238 |
09 Feb 10 |
nicklas |
75 |
{ |
5238 |
09 Feb 10 |
nicklas |
76 |
dc.saveItem(f); |
5238 |
09 Feb 10 |
nicklas |
77 |
} |
5238 |
09 Feb 10 |
nicklas |
78 |
if (overwrite || !f.isInDatabase() || f.isRemoved()) |
5238 |
09 Feb 10 |
nicklas |
79 |
{ |
5238 |
09 Feb 10 |
nicklas |
80 |
InputStream in = null; |
5238 |
09 Feb 10 |
nicklas |
81 |
try |
5238 |
09 Feb 10 |
nicklas |
82 |
{ |
5238 |
09 Feb 10 |
nicklas |
83 |
in = locator.getInputStream(filename); |
5238 |
09 Feb 10 |
nicklas |
84 |
FileUtil.copy(in, f.getUploadStream(false)); |
5238 |
09 Feb 10 |
nicklas |
85 |
f.setRemoved(false); |
5238 |
09 Feb 10 |
nicklas |
86 |
} |
5238 |
09 Feb 10 |
nicklas |
87 |
finally |
5238 |
09 Feb 10 |
nicklas |
88 |
{ |
5238 |
09 Feb 10 |
nicklas |
89 |
FileUtil.close(in); |
5238 |
09 Feb 10 |
nicklas |
90 |
} |
5238 |
09 Feb 10 |
nicklas |
91 |
} |
5238 |
09 Feb 10 |
nicklas |
92 |
} |
5238 |
09 Feb 10 |
nicklas |
93 |
catch (ItemNotFoundException ex) |
5238 |
09 Feb 10 |
nicklas |
94 |
{} |
5238 |
09 Feb 10 |
nicklas |
95 |
return f; |
5238 |
09 Feb 10 |
nicklas |
96 |
} |
5238 |
09 Feb 10 |
nicklas |
97 |
|
5238 |
09 Feb 10 |
nicklas |
98 |
} |