src/core/net/sf/basedb/util/bfs/GenericExtraFileImporter.java

Code
Comments
Other
Rev Date Author Line
5238 09 Feb 10 nicklas 1 /**
5238 09 Feb 10 nicklas 2   $Id$
5238 09 Feb 10 nicklas 3
5238 09 Feb 10 nicklas 4   Copyright (C) 2010 Nicklas Nordborg
5238 09 Feb 10 nicklas 5
5238 09 Feb 10 nicklas 6   This file is part of BASE - BioArray Software Environment.
5238 09 Feb 10 nicklas 7   Available at http://base.thep.lu.se/
5238 09 Feb 10 nicklas 8
5238 09 Feb 10 nicklas 9   BASE is free software; you can redistribute it and/or
5238 09 Feb 10 nicklas 10   modify it under the terms of the GNU General Public License
5238 09 Feb 10 nicklas 11   as published by the Free Software Foundation; either version 3
5238 09 Feb 10 nicklas 12   of the License, or (at your option) any later version.
5238 09 Feb 10 nicklas 13
5238 09 Feb 10 nicklas 14   BASE is distributed in the hope that it will be useful,
5238 09 Feb 10 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5238 09 Feb 10 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5238 09 Feb 10 nicklas 17   GNU General Public License for more details.
5238 09 Feb 10 nicklas 18
5238 09 Feb 10 nicklas 19   You should have received a copy of the GNU General Public License
5238 09 Feb 10 nicklas 20   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 34   Extra file importer implementation that imports file to a specified
5238 09 Feb 10 nicklas 35   directory in the BASE file system. Files that already exists can
5238 09 Feb 10 nicklas 36   be overwritten or ignored. Files that already exists and have their
5238 09 Feb 10 nicklas 37   removed flag set are always overwritten.
5238 09 Feb 10 nicklas 38
5238 09 Feb 10 nicklas 39   @author Nicklas
5238 09 Feb 10 nicklas 40   @since 2.15
5238 09 Feb 10 nicklas 41   @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 52     Create a new extra file importer.
5238 09 Feb 10 nicklas 53     @param dc A DbControl to use for database access
5238 09 Feb 10 nicklas 54     @param dir The directory in the BASE file system where the imported files
5238 09 Feb 10 nicklas 55       should be placed
5238 09 Feb 10 nicklas 56     @param overwrite TRUE to force overwriting existing files, FALSE to only
5238 09 Feb 10 nicklas 57       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 }