src/core/net/sf/basedb/util/zip/AbstractFileUnpacker.java

Code
Comments
Other
Rev Date Author Line
2689 02 Oct 06 nicklas 1 /**
2689 02 Oct 06 nicklas 2   $Id$
2689 02 Oct 06 nicklas 3
3675 16 Aug 07 jari 4   Copyright (C) 2006, 2007 Nicklas Nordborg
2689 02 Oct 06 nicklas 5
2689 02 Oct 06 nicklas 6   This file is part of BASE - BioArray Software Environment.
2689 02 Oct 06 nicklas 7   Available at http://base.thep.lu.se/
2689 02 Oct 06 nicklas 8
2689 02 Oct 06 nicklas 9   BASE is free software; you can redistribute it and/or
2689 02 Oct 06 nicklas 10   modify it under the terms of the GNU General Public License
4479 05 Sep 08 jari 11   as published by the Free Software Foundation; either version 3
2689 02 Oct 06 nicklas 12   of the License, or (at your option) any later version.
2689 02 Oct 06 nicklas 13
2689 02 Oct 06 nicklas 14   BASE is distributed in the hope that it will be useful,
2689 02 Oct 06 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
2689 02 Oct 06 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2689 02 Oct 06 nicklas 17   GNU General Public License for more details.
2689 02 Oct 06 nicklas 18
2689 02 Oct 06 nicklas 19   You should have received a copy of the GNU General Public License
4515 11 Sep 08 jari 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
2689 02 Oct 06 nicklas 21 */
2689 02 Oct 06 nicklas 22 package net.sf.basedb.util.zip;
2689 02 Oct 06 nicklas 23
2689 02 Oct 06 nicklas 24 import java.io.InputStream;
2689 02 Oct 06 nicklas 25 import java.util.ArrayList;
2689 02 Oct 06 nicklas 26 import java.util.Arrays;
2722 11 Oct 06 nicklas 27 import java.util.Collection;
2689 02 Oct 06 nicklas 28 import java.util.Collections;
2722 11 Oct 06 nicklas 29 import java.util.EnumSet;
2722 11 Oct 06 nicklas 30 import java.util.HashSet;
2689 02 Oct 06 nicklas 31 import java.util.List;
2689 02 Oct 06 nicklas 32 import java.util.Set;
2689 02 Oct 06 nicklas 33
2689 02 Oct 06 nicklas 34 import net.sf.basedb.core.AbsoluteProgressReporter;
2689 02 Oct 06 nicklas 35 import net.sf.basedb.core.BaseException;
2689 02 Oct 06 nicklas 36 import net.sf.basedb.core.BooleanParameterType;
2689 02 Oct 06 nicklas 37 import net.sf.basedb.core.DbControl;
2689 02 Oct 06 nicklas 38 import net.sf.basedb.core.Directory;
2689 02 Oct 06 nicklas 39 import net.sf.basedb.core.File;
2689 02 Oct 06 nicklas 40 import net.sf.basedb.core.Item;
2689 02 Oct 06 nicklas 41 import net.sf.basedb.core.ItemParameterType;
2689 02 Oct 06 nicklas 42 import net.sf.basedb.core.Path;
2689 02 Oct 06 nicklas 43 import net.sf.basedb.core.PathParameterType;
2722 11 Oct 06 nicklas 44 import net.sf.basedb.core.Permission;
2689 02 Oct 06 nicklas 45 import net.sf.basedb.core.PluginParameter;
2689 02 Oct 06 nicklas 46 import net.sf.basedb.core.ProgressReporter;
2689 02 Oct 06 nicklas 47 import net.sf.basedb.core.RequestInformation;
2689 02 Oct 06 nicklas 48 import net.sf.basedb.core.SimpleAbsoluteProgressReporter;
2689 02 Oct 06 nicklas 49 import net.sf.basedb.core.plugin.AbstractPlugin;
2689 02 Oct 06 nicklas 50 import net.sf.basedb.core.plugin.GuiContext;
2689 02 Oct 06 nicklas 51 import net.sf.basedb.core.plugin.InteractivePlugin;
2722 11 Oct 06 nicklas 52 import net.sf.basedb.core.plugin.Permissions;
2689 02 Oct 06 nicklas 53 import net.sf.basedb.core.plugin.Plugin;
2689 02 Oct 06 nicklas 54 import net.sf.basedb.core.plugin.Request;
2689 02 Oct 06 nicklas 55 import net.sf.basedb.core.plugin.Response;
4118 01 Feb 08 nicklas 56 import net.sf.basedb.core.signal.SignalHandler;
4118 01 Feb 08 nicklas 57 import net.sf.basedb.core.signal.SignalTarget;
4118 01 Feb 08 nicklas 58 import net.sf.basedb.core.signal.ThreadSignalHandler;
2689 02 Oct 06 nicklas 59
2689 02 Oct 06 nicklas 60 /**
2689 02 Oct 06 nicklas 61    This is a base class for all plugins that wants to implement the {@link FileUnpacker}
2689 02 Oct 06 nicklas 62    interface. This class takes care of all the hard work that is required by a plugin. 
2689 02 Oct 06 nicklas 63    Ie. the creating and saving the parameters that are needed, context checking, etc.
2689 02 Oct 06 nicklas 64    The subclass only has to implement the remaingin methods in the {@link FileUnpacker}
2689 02 Oct 06 nicklas 65    interface which basically boils down to implementing the actual unpacking of the
2689 02 Oct 06 nicklas 66    compressed file.
2689 02 Oct 06 nicklas 67    <p>
2689 02 Oct 06 nicklas 68    The implementation in this class asks for three parameters:
2689 02 Oct 06 nicklas 69    <ul>
2689 02 Oct 06 nicklas 70    <li>The file to unpack
2689 02 Oct 06 nicklas 71    <li>The directory where the files should be placed
2689 02 Oct 06 nicklas 72    <li>If existing files should be overwritten or not
2689 02 Oct 06 nicklas 73    </ul>
2689 02 Oct 06 nicklas 74    A subclass may override any of the methods in this class if it has different
2689 02 Oct 06 nicklas 75    needs.
2689 02 Oct 06 nicklas 76
2689 02 Oct 06 nicklas 77   @author nicklas
2689 02 Oct 06 nicklas 78   @version 2.0
2689 02 Oct 06 nicklas 79   @base.modified $Date$
2689 02 Oct 06 nicklas 80 */
2689 02 Oct 06 nicklas 81 public abstract class AbstractFileUnpacker
2689 02 Oct 06 nicklas 82   extends AbstractPlugin
4118 01 Feb 08 nicklas 83   implements FileUnpacker, InteractivePlugin, SignalTarget
2689 02 Oct 06 nicklas 84 {
2689 02 Oct 06 nicklas 85
2689 02 Oct 06 nicklas 86   /**
2689 02 Oct 06 nicklas 87     The context is always [ FILE, ITEM].
2689 02 Oct 06 nicklas 88   */
2689 02 Oct 06 nicklas 89   private static final Set<GuiContext> guiContexts = 
2689 02 Oct 06 nicklas 90     Collections.singleton(new GuiContext(Item.FILE, GuiContext.Type.ITEM));
2689 02 Oct 06 nicklas 91
2722 11 Oct 06 nicklas 92   private static final Set<Permissions> permissions = new HashSet<Permissions>();
2722 11 Oct 06 nicklas 93
2689 02 Oct 06 nicklas 94   /**
2689 02 Oct 06 nicklas 95     The parameter type for file to unpack.
2689 02 Oct 06 nicklas 96   */
2689 02 Oct 06 nicklas 97   private static final ItemParameterType<File> zipFileType = 
2689 02 Oct 06 nicklas 98     new ItemParameterType<File>(File.class, null, true, 1, null);
2689 02 Oct 06 nicklas 99   
2689 02 Oct 06 nicklas 100   /**
2689 02 Oct 06 nicklas 101     The parameter type for unpack directory.
2689 02 Oct 06 nicklas 102   */
2689 02 Oct 06 nicklas 103   private static final PathParameterType unpackType =
2689 02 Oct 06 nicklas 104     new PathParameterType(Path.Type.DIRECTORY, null, true);
2689 02 Oct 06 nicklas 105   
2689 02 Oct 06 nicklas 106   private RequestInformation configureJob;
2689 02 Oct 06 nicklas 107   
4118 01 Feb 08 nicklas 108   private ThreadSignalHandler signalHandler;
4118 01 Feb 08 nicklas 109   
2689 02 Oct 06 nicklas 110   /*
2689 02 Oct 06 nicklas 111     From the Plugin interface
2689 02 Oct 06 nicklas 112     -------------------------------------------
2689 02 Oct 06 nicklas 113   */
6127 14 Sep 12 nicklas 114   @Override
2689 02 Oct 06 nicklas 115   public MainType getMainType()
2689 02 Oct 06 nicklas 116   {
2689 02 Oct 06 nicklas 117     return Plugin.MainType.OTHER;
2689 02 Oct 06 nicklas 118   }
2691 03 Oct 06 nicklas 119   /**
2691 03 Oct 06 nicklas 120     Returns FALSE.
2691 03 Oct 06 nicklas 121   */
6127 14 Sep 12 nicklas 122   @Override
2691 03 Oct 06 nicklas 123   public boolean supportsConfigurations()
2691 03 Oct 06 nicklas 124   {
2691 03 Oct 06 nicklas 125     return false;
2691 03 Oct 06 nicklas 126   }
2691 03 Oct 06 nicklas 127   /**
2691 03 Oct 06 nicklas 128     Returns FALSE.
2691 03 Oct 06 nicklas 129   */
6127 14 Sep 12 nicklas 130   @Override
2691 03 Oct 06 nicklas 131   public boolean requiresConfiguration()
2691 03 Oct 06 nicklas 132   {
2691 03 Oct 06 nicklas 133     return false;
2691 03 Oct 06 nicklas 134   }
2722 11 Oct 06 nicklas 135   /**
2722 11 Oct 06 nicklas 136      Request create and write access to File:s and Directory:s.
2722 11 Oct 06 nicklas 137   */
6127 14 Sep 12 nicklas 138   @Override
2722 11 Oct 06 nicklas 139   public Collection<Permissions> getPermissions()
2722 11 Oct 06 nicklas 140   {
2722 11 Oct 06 nicklas 141     if (permissions.size() == 0)
2722 11 Oct 06 nicklas 142     {
2722 11 Oct 06 nicklas 143       permissions.add(new Permissions(Item.FILE, null, EnumSet.of(Permission.CREATE, Permission.WRITE)));
2722 11 Oct 06 nicklas 144       permissions.add(new Permissions(Item.DIRECTORY, null, EnumSet.of(Permission.CREATE, Permission.WRITE)));
5758 23 Sep 11 nicklas 145       permissions.add(new Permissions(Item.ITEMSUBTYPE, EnumSet.of(Permission.USE), null));
2722 11 Oct 06 nicklas 146     }
2722 11 Oct 06 nicklas 147     return permissions;
2722 11 Oct 06 nicklas 148   }
2722 11 Oct 06 nicklas 149   
6127 14 Sep 12 nicklas 150   @Override
2689 02 Oct 06 nicklas 151   public void run(Request request, Response response, ProgressReporter progress)
2689 02 Oct 06 nicklas 152   {
2689 02 Oct 06 nicklas 153     DbControl dc = sc.newDbControl();
2689 02 Oct 06 nicklas 154     InputStream in = null;
2689 02 Oct 06 nicklas 155     try
2689 02 Oct 06 nicklas 156     {
2689 02 Oct 06 nicklas 157       // Get all parameters
2689 02 Oct 06 nicklas 158       File zipFile = getZipFile(dc);
2689 02 Oct 06 nicklas 159       Directory dir = getUnpackDirectory(dc);
2689 02 Oct 06 nicklas 160       boolean overwrite = getOverwrite();
2689 02 Oct 06 nicklas 161       
2689 02 Oct 06 nicklas 162       in = zipFile.getDownloadStream(0);
2689 02 Oct 06 nicklas 163       AbsoluteProgressReporter absoluteProgress = progress == null ? 
2689 02 Oct 06 nicklas 164         null : new SimpleAbsoluteProgressReporter(progress, zipFile.getSize());
2689 02 Oct 06 nicklas 165       
2689 02 Oct 06 nicklas 166       // Unpack the file
5758 23 Sep 11 nicklas 167       int numFiles = unpack(dc, dir, in, zipFile, overwrite, absoluteProgress);
2689 02 Oct 06 nicklas 168       dc.commit();
2689 02 Oct 06 nicklas 169       
2689 02 Oct 06 nicklas 170       response.setDone(numFiles + " file(s) unpacked successfully");
2689 02 Oct 06 nicklas 171     }
2689 02 Oct 06 nicklas 172     catch (Throwable t)
2689 02 Oct 06 nicklas 173     {
2689 02 Oct 06 nicklas 174       response.setError(t.getMessage(), Arrays.asList(t));
2689 02 Oct 06 nicklas 175     }
2689 02 Oct 06 nicklas 176     finally
2689 02 Oct 06 nicklas 177     {
2689 02 Oct 06 nicklas 178       if (dc != null) dc.close();
2689 02 Oct 06 nicklas 179     }
2689 02 Oct 06 nicklas 180   }
2689 02 Oct 06 nicklas 181   // -------------------------------------------
2689 02 Oct 06 nicklas 182
2689 02 Oct 06 nicklas 183   /*
2689 02 Oct 06 nicklas 184     From the InteractivePlugin interface
2689 02 Oct 06 nicklas 185     -------------------------------------------
2689 02 Oct 06 nicklas 186   */
6127 14 Sep 12 nicklas 187   @Override
2689 02 Oct 06 nicklas 188   public Set<GuiContext> getGuiContexts()
2689 02 Oct 06 nicklas 189   {
2689 02 Oct 06 nicklas 190     return guiContexts;
2689 02 Oct 06 nicklas 191   }
6127 14 Sep 12 nicklas 192   @Override
2689 02 Oct 06 nicklas 193   public String isInContext(GuiContext context, Object item)
2689 02 Oct 06 nicklas 194   {
2689 02 Oct 06 nicklas 195     String message = null;  
2689 02 Oct 06 nicklas 196     if (item == null)
2689 02 Oct 06 nicklas 197     {
2689 02 Oct 06 nicklas 198       message = "The object is null";
2689 02 Oct 06 nicklas 199     }
2689 02 Oct 06 nicklas 200     else if (!(item instanceof File))
2689 02 Oct 06 nicklas 201     {
2689 02 Oct 06 nicklas 202       message = "The object is not a File: " + item;
2689 02 Oct 06 nicklas 203     }
2689 02 Oct 06 nicklas 204     else
2689 02 Oct 06 nicklas 205     {
2689 02 Oct 06 nicklas 206       File f = (File)item;
2689 02 Oct 06 nicklas 207       String name = f.getName();
2689 02 Oct 06 nicklas 208       String mimeType = f.getMimeType();
2689 02 Oct 06 nicklas 209       if (!getMimeTypes().contains(mimeType))
2689 02 Oct 06 nicklas 210       {
3510 19 Jun 07 nicklas 211         for (String extension : getExtensions())
2689 02 Oct 06 nicklas 212         {
3510 19 Jun 07 nicklas 213           if (name.endsWith(extension)) return null;
2689 02 Oct 06 nicklas 214         }
3510 19 Jun 07 nicklas 215         message = "Unsupported file: " + f.getPath().toString();
2689 02 Oct 06 nicklas 216       }
2689 02 Oct 06 nicklas 217     }
2689 02 Oct 06 nicklas 218     return message;
2689 02 Oct 06 nicklas 219   }
2689 02 Oct 06 nicklas 220
6127 14 Sep 12 nicklas 221   @Override
2689 02 Oct 06 nicklas 222   public RequestInformation getRequestInformation(GuiContext context, String command) 
2689 02 Oct 06 nicklas 223     throws BaseException
2689 02 Oct 06 nicklas 224   {
2689 02 Oct 06 nicklas 225     RequestInformation requestInformation = null;
2689 02 Oct 06 nicklas 226     if (command.equals(Request.COMMAND_CONFIGURE_JOB))
2689 02 Oct 06 nicklas 227     {
2689 02 Oct 06 nicklas 228       requestInformation = getConfigureJobParameters(context);
2689 02 Oct 06 nicklas 229     }
2689 02 Oct 06 nicklas 230     return requestInformation;
2689 02 Oct 06 nicklas 231   }
6127 14 Sep 12 nicklas 232   @Override
2689 02 Oct 06 nicklas 233   public void configure(GuiContext context, Request request, Response response)
2689 02 Oct 06 nicklas 234   {
2689 02 Oct 06 nicklas 235     String command = request.getCommand();    
2689 02 Oct 06 nicklas 236     try
2689 02 Oct 06 nicklas 237     {
2689 02 Oct 06 nicklas 238       if (command.equals(Request.COMMAND_CONFIGURE_JOB))
2689 02 Oct 06 nicklas 239       {
2689 02 Oct 06 nicklas 240         RequestInformation ri = getConfigureJobParameters(context);
2689 02 Oct 06 nicklas 241         List<Throwable> errors = validateRequestParameters (ri.getParameters(), request);
2689 02 Oct 06 nicklas 242         if (errors != null)
2689 02 Oct 06 nicklas 243         {
2689 02 Oct 06 nicklas 244           response.setError(errors.size() + " invalid parameters were found in the request",errors);
2689 02 Oct 06 nicklas 245           return;
2689 02 Oct 06 nicklas 246         }
2689 02 Oct 06 nicklas 247         
2689 02 Oct 06 nicklas 248         storeValue(job, request, ri.getParameter(ZIP_FILE));
2689 02 Oct 06 nicklas 249         storeValue(job, request, ri.getParameter(UNPACK_DIRECTORY));
2689 02 Oct 06 nicklas 250         storeValue(job, request, ri.getParameter(OVERWRITE));
2689 02 Oct 06 nicklas 251         
5481 08 Nov 10 nicklas 252         File file = (File)job.getValue(ZIP_FILE);
5481 08 Nov 10 nicklas 253         response.setSuggestedJobName("Unpack file '" + file.getName() + 
5481 08 Nov 10 nicklas 254             "' to " + job.getValue(UNPACK_DIRECTORY));
5481 08 Nov 10 nicklas 255         
2689 02 Oct 06 nicklas 256         response.setDone("The job configuration is complete");
2689 02 Oct 06 nicklas 257       }
2689 02 Oct 06 nicklas 258     }
2689 02 Oct 06 nicklas 259     catch(Throwable ex)
2689 02 Oct 06 nicklas 260     {
2689 02 Oct 06 nicklas 261       response.setError(ex.getMessage(), Arrays.asList(ex));
2689 02 Oct 06 nicklas 262     }
2689 02 Oct 06 nicklas 263   }
2689 02 Oct 06 nicklas 264   // -------------------------------------------
4118 01 Feb 08 nicklas 265   /*
4118 01 Feb 08 nicklas 266     From the SignalTarget interface
4118 01 Feb 08 nicklas 267     -------------------------------------------
4118 01 Feb 08 nicklas 268   */
6127 14 Sep 12 nicklas 269   @Override
4118 01 Feb 08 nicklas 270   public SignalHandler getSignalHandler()
4118 01 Feb 08 nicklas 271   {
4118 01 Feb 08 nicklas 272     signalHandler = new ThreadSignalHandler();
4118 01 Feb 08 nicklas 273     return signalHandler;
4118 01 Feb 08 nicklas 274   }
4118 01 Feb 08 nicklas 275   // -------------------------------------------
2689 02 Oct 06 nicklas 276
2689 02 Oct 06 nicklas 277   /**
2689 02 Oct 06 nicklas 278     The name of the parameter that asks for the file that is going to
2689 02 Oct 06 nicklas 279     be unpacked.
2689 02 Oct 06 nicklas 280     @see #getZipFileParameter(String, String)
2689 02 Oct 06 nicklas 281   */
2689 02 Oct 06 nicklas 282   protected static final String ZIP_FILE = "zipFile";
2689 02 Oct 06 nicklas 283   
2689 02 Oct 06 nicklas 284   /**
2689 02 Oct 06 nicklas 285     Get a plugin parameter that asks for the file that is going to
2689 02 Oct 06 nicklas 286     be unpacked.
2689 02 Oct 06 nicklas 287
2689 02 Oct 06 nicklas 288     @param label The parameter label, or null to use the default (File)
2689 02 Oct 06 nicklas 289     @param description The parameter description, or null to use the default 
2689 02 Oct 06 nicklas 290       (The file to unpack.)
2689 02 Oct 06 nicklas 291   */
2689 02 Oct 06 nicklas 292   protected PluginParameter<File> getZipFileParameter(String label, String description)
2689 02 Oct 06 nicklas 293   {
2689 02 Oct 06 nicklas 294     if (label == null) label = "File";
2689 02 Oct 06 nicklas 295     if (description == null) description = "The file to unpack.";
2689 02 Oct 06 nicklas 296     return new PluginParameter<File>(ZIP_FILE, label, description, zipFileType);
2689 02 Oct 06 nicklas 297   }
2689 02 Oct 06 nicklas 298
2689 02 Oct 06 nicklas 299   /**
2689 02 Oct 06 nicklas 300     Get the file that is going to be unpacked by the plugin.
2689 02 Oct 06 nicklas 301     The file is only available in the execution phase if the plugin has saved 
2689 02 Oct 06 nicklas 302     it as a job parameter during the configuration of a job (this is the default)
2689 02 Oct 06 nicklas 303     
2689 02 Oct 06 nicklas 304     @return The packed file or null
2689 02 Oct 06 nicklas 305     @see #getZipFileParameter(String, String)
3510 19 Jun 07 nicklas 306     @see #getCurrentFile(DbControl)
2689 02 Oct 06 nicklas 307   */
2689 02 Oct 06 nicklas 308   protected File getZipFile(DbControl dc)
2689 02 Oct 06 nicklas 309   {
2689 02 Oct 06 nicklas 310     File zipFile = (File)job.getValue(ZIP_FILE);
2689 02 Oct 06 nicklas 311     if (zipFile != null) zipFile = File.getById(dc, zipFile.getId());
2689 02 Oct 06 nicklas 312     return zipFile;
2689 02 Oct 06 nicklas 313   }
2689 02 Oct 06 nicklas 314   
2689 02 Oct 06 nicklas 315   /**
2689 02 Oct 06 nicklas 316     The name of the parameter that asks for the directory where the
2689 02 Oct 06 nicklas 317     unpacked files are stored.
2981 30 Nov 06 nicklas 318     @see #getZipFileParameter(String, String)
2689 02 Oct 06 nicklas 319   */
2689 02 Oct 06 nicklas 320   protected static final String UNPACK_DIRECTORY = "unpackDirectory";
2689 02 Oct 06 nicklas 321
2689 02 Oct 06 nicklas 322   /**
2689 02 Oct 06 nicklas 323     Get a plugin parameter that asks for the directory where the
2689 02 Oct 06 nicklas 324     unpacked files are stored.
2689 02 Oct 06 nicklas 325   
2689 02 Oct 06 nicklas 326     @param label The parameter label, or null to use the default (Save in)
2689 02 Oct 06 nicklas 327     @param description The parameter description, or null to use the default 
2689 02 Oct 06 nicklas 328       (The directory where the unpacked files should be saved.)
2689 02 Oct 06 nicklas 329   */
2689 02 Oct 06 nicklas 330   protected PluginParameter<String> getDirectoryParameter(String label, String description)
2689 02 Oct 06 nicklas 331   {
2689 02 Oct 06 nicklas 332     if (label == null) label = "Save in";
2689 02 Oct 06 nicklas 333     if (description == null) description = "The directory where the unpacked files should be saved.";
3510 19 Jun 07 nicklas 334     DbControl dc = sc.newDbControl();
3510 19 Jun 07 nicklas 335     String defaultDir = null;
3510 19 Jun 07 nicklas 336     try
3510 19 Jun 07 nicklas 337     {
3510 19 Jun 07 nicklas 338       Directory dir = getCurrentDirectory(dc);
3510 19 Jun 07 nicklas 339       if (dir != null) defaultDir = dir.getPath().toString();
3510 19 Jun 07 nicklas 340     }
3510 19 Jun 07 nicklas 341     finally
3510 19 Jun 07 nicklas 342     {
3510 19 Jun 07 nicklas 343       if (dc != null) dc.close();
3510 19 Jun 07 nicklas 344     }
3510 19 Jun 07 nicklas 345     return new PluginParameter<String>(UNPACK_DIRECTORY, label, description , defaultDir, unpackType); 
2689 02 Oct 06 nicklas 346   }
2689 02 Oct 06 nicklas 347   
2689 02 Oct 06 nicklas 348   /**
3510 19 Jun 07 nicklas 349     Get the current directory or null there is no current or it can't be loaded.
3510 19 Jun 07 nicklas 350     This method will first try to load the current file and use the file's
3510 19 Jun 07 nicklas 351     directory as the current. If there is no current file, the current directory
3510 19 Jun 07 nicklas 352     is loaded from the session context.
3510 19 Jun 07 nicklas 353     The current directory is only available in the configuration phase.
3510 19 Jun 07 nicklas 354     
3510 19 Jun 07 nicklas 355     @param dc A DbControl to use for database access
3510 19 Jun 07 nicklas 356     @return A directory or null
3510 19 Jun 07 nicklas 357     @since 2.4
3510 19 Jun 07 nicklas 358     @see #getUnpackDirectory(DbControl)
3510 19 Jun 07 nicklas 359   */
3510 19 Jun 07 nicklas 360   protected Directory getCurrentDirectory(DbControl dc)
3510 19 Jun 07 nicklas 361   {
3510 19 Jun 07 nicklas 362     File currentFile = getCurrentFile(dc);
3510 19 Jun 07 nicklas 363     if (currentFile != null) return currentFile.getDirectory();
3510 19 Jun 07 nicklas 364     int directoryId = sc.getCurrentContext(Item.DIRECTORY).getId();
3510 19 Jun 07 nicklas 365     if (directoryId != 0)
3510 19 Jun 07 nicklas 366     {
3510 19 Jun 07 nicklas 367       try
3510 19 Jun 07 nicklas 368       {
3510 19 Jun 07 nicklas 369         return Directory.getById(dc, directoryId);
3510 19 Jun 07 nicklas 370       }
3510 19 Jun 07 nicklas 371       catch (Throwable t)
3510 19 Jun 07 nicklas 372       {}
3510 19 Jun 07 nicklas 373     }
3510 19 Jun 07 nicklas 374     return null;
3510 19 Jun 07 nicklas 375   }
3510 19 Jun 07 nicklas 376   
3510 19 Jun 07 nicklas 377   /**
3510 19 Jun 07 nicklas 378     Get the current file or null there is no current or it can't be loaded.
3510 19 Jun 07 nicklas 379     The current file is only available in the configuration phase.
3510 19 Jun 07 nicklas 380     @param dc A DbControl to use for database access
3510 19 Jun 07 nicklas 381     @return A file or null
3510 19 Jun 07 nicklas 382     @since 2.4
3510 19 Jun 07 nicklas 383     @see #getZipFile(DbControl)
3510 19 Jun 07 nicklas 384   */
3510 19 Jun 07 nicklas 385   protected File getCurrentFile(DbControl dc)
3510 19 Jun 07 nicklas 386   {
3510 19 Jun 07 nicklas 387     int fileId = sc.getCurrentContext(Item.FILE).getId();
3510 19 Jun 07 nicklas 388     if (fileId != 0)
3510 19 Jun 07 nicklas 389     {
3510 19 Jun 07 nicklas 390       try
3510 19 Jun 07 nicklas 391       {
3510 19 Jun 07 nicklas 392         return File.getById(dc, fileId);
3510 19 Jun 07 nicklas 393       }
3510 19 Jun 07 nicklas 394       catch (Throwable t)
3510 19 Jun 07 nicklas 395       {}
3510 19 Jun 07 nicklas 396     }
3510 19 Jun 07 nicklas 397     return null;
3510 19 Jun 07 nicklas 398   }  
3510 19 Jun 07 nicklas 399   
3510 19 Jun 07 nicklas 400   /**
2689 02 Oct 06 nicklas 401     Get the directory where the unpacked files are placed by the plugin.
2689 02 Oct 06 nicklas 402     The directory is only available in the execution phase if the plugin has saved 
2689 02 Oct 06 nicklas 403     it as a job parameter during the configuration of a job.
2689 02 Oct 06 nicklas 404     
2689 02 Oct 06 nicklas 405     @return The directory or null
2689 02 Oct 06 nicklas 406     @see #getDirectoryParameter(String, String)
3510 19 Jun 07 nicklas 407     @see #getCurrentDirectory(DbControl)
2689 02 Oct 06 nicklas 408   */
2689 02 Oct 06 nicklas 409   protected Directory getUnpackDirectory(DbControl dc)
2689 02 Oct 06 nicklas 410   {
2689 02 Oct 06 nicklas 411     String path = (String)job.getValue(UNPACK_DIRECTORY);
2689 02 Oct 06 nicklas 412     Directory unpackDirectory = null;
2689 02 Oct 06 nicklas 413     if (path != null) 
2689 02 Oct 06 nicklas 414     {
2689 02 Oct 06 nicklas 415       unpackDirectory = Directory.getNew(dc, new Path(path, Path.Type.DIRECTORY));
2689 02 Oct 06 nicklas 416       if (!unpackDirectory.isInDatabase()) dc.saveItem(unpackDirectory);
2689 02 Oct 06 nicklas 417     }
2689 02 Oct 06 nicklas 418     return unpackDirectory;
2689 02 Oct 06 nicklas 419   }
2689 02 Oct 06 nicklas 420
2689 02 Oct 06 nicklas 421   /**
2689 02 Oct 06 nicklas 422     The name of the parameter that asks if existing files should be 
2689 02 Oct 06 nicklas 423     overwritten or not.
2981 30 Nov 06 nicklas 424     @see #getOverwriteParameter(String, String)
2689 02 Oct 06 nicklas 425   */
2689 02 Oct 06 nicklas 426   protected static final String OVERWRITE = "overwrite";
2689 02 Oct 06 nicklas 427
2689 02 Oct 06 nicklas 428   /**
2689 02 Oct 06 nicklas 429     Get a plugin parameter that asks if existing files should be overwritten
2689 02 Oct 06 nicklas 430     or not.
2689 02 Oct 06 nicklas 431   
2689 02 Oct 06 nicklas 432     @param label The parameter label, or null to use the default (Overwrite)
2689 02 Oct 06 nicklas 433     @param description The parameter description, or null to use the default 
2689 02 Oct 06 nicklas 434       (Specify if existing files should be overwritten or not.)
2689 02 Oct 06 nicklas 435   */
2689 02 Oct 06 nicklas 436   protected PluginParameter<Boolean> getOverwriteParameter(String label, String description)
2689 02 Oct 06 nicklas 437   {
2689 02 Oct 06 nicklas 438     if (label == null) label = "Overwrite";
2689 02 Oct 06 nicklas 439     if (description == null) description = "Specify if existing files should be overwritten or not.";
2689 02 Oct 06 nicklas 440     return new PluginParameter<Boolean>(OVERWRITE, label, description, 
2689 02 Oct 06 nicklas 441         new BooleanParameterType(false, false));
2689 02 Oct 06 nicklas 442   }
2689 02 Oct 06 nicklas 443   
2689 02 Oct 06 nicklas 444   /**
2689 02 Oct 06 nicklas 445     If existing file should be overwritten or not.
2689 02 Oct 06 nicklas 446     
2689 02 Oct 06 nicklas 447     @return TRUE if files should be overwritten, FALSE otherwise
2689 02 Oct 06 nicklas 448     @see #getOverwriteParameter(String, String)
2689 02 Oct 06 nicklas 449   */
2689 02 Oct 06 nicklas 450   protected boolean getOverwrite()
2689 02 Oct 06 nicklas 451   {
2689 02 Oct 06 nicklas 452     Boolean overwrite = (Boolean)job.getValue(OVERWRITE);
2689 02 Oct 06 nicklas 453     if (overwrite == null) overwrite = Boolean.FALSE;
2689 02 Oct 06 nicklas 454     return overwrite;
2689 02 Oct 06 nicklas 455   }
2689 02 Oct 06 nicklas 456
2689 02 Oct 06 nicklas 457   private RequestInformation getConfigureJobParameters(GuiContext context)
2689 02 Oct 06 nicklas 458   {
5384 13 Aug 10 nicklas 459     if (configureJob == null)
2689 02 Oct 06 nicklas 460     {
5384 13 Aug 10 nicklas 461       // Parameters for CONFIGURE_JOB
5384 13 Aug 10 nicklas 462       List<PluginParameter<?>> parameters = new ArrayList<PluginParameter<?>>();
2689 02 Oct 06 nicklas 463
5384 13 Aug 10 nicklas 464       // Source and child bioassay set parameters
5384 13 Aug 10 nicklas 465       parameters.add(getZipFileParameter(null, null));
5384 13 Aug 10 nicklas 466       parameters.add(getDirectoryParameter(null, null));
5384 13 Aug 10 nicklas 467       parameters.add(getOverwriteParameter(null, null));
5384 13 Aug 10 nicklas 468       
5384 13 Aug 10 nicklas 469       configureJob = new RequestInformation
5384 13 Aug 10 nicklas 470       (
5384 13 Aug 10 nicklas 471         Request.COMMAND_CONFIGURE_JOB,
5384 13 Aug 10 nicklas 472         "Unpack file",
5384 13 Aug 10 nicklas 473         "Select a file to unpack and where to put the unpacked files",
5384 13 Aug 10 nicklas 474         parameters
5384 13 Aug 10 nicklas 475       );
2689 02 Oct 06 nicklas 476     }
2689 02 Oct 06 nicklas 477     return configureJob;
2689 02 Oct 06 nicklas 478   }
2689 02 Oct 06 nicklas 479   
2689 02 Oct 06 nicklas 480 }