Changeset 4366


Ignore:
Timestamp:
Jul 2, 2008, 1:23:58 PM (15 years ago)
Author:
Nicklas Nordborg
Message:

References #1028: Plug-in for batch upload/modification of items

Prototype for biosurce and sample import added.

Location:
trunk/src
Files:
10 added
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/Install.java

    r4355 r4366  
    639639      createPluginDefinition("net.sf.basedb.plugins.IlluminaRawDataImporter", null, keyEveryoneUse, true, null, false);
    640640      createPluginDefinition("net.sf.basedb.plugins.GalExporter", null, keyEveryoneUse, true, null, true);
     641
     642      createPluginDefinition("net.sf.basedb.plugins.batchimport.BioSourceImporter", null, keyEveryoneUse, true, null, false);
     643      createPluginDefinition("net.sf.basedb.plugins.batchimport.SampleImporter", null, keyEveryoneUse, true, null, false);
     644     
    641645     
    642646      // Plugin configurations
  • trunk/src/core/net/sf/basedb/core/plugin/AbstractPlugin.java

    r4118 r4366  
    2929import net.sf.basedb.core.BooleanParameterType;
    3030import net.sf.basedb.core.DbControl;
     31import net.sf.basedb.core.File;
    3132import net.sf.basedb.core.Job;
     33import net.sf.basedb.core.Path;
    3234import net.sf.basedb.core.PluginConfiguration;
    3335import net.sf.basedb.core.SessionControl;
     
    3638import net.sf.basedb.core.signal.ThreadSignalHandler;
    3739
     40import java.io.PrintWriter;
    3841import java.util.Collection;
    3942import java.util.List;
     
    5861  protected ParameterValues job = null;
    5962 
     63  private DbControl logDc;
     64  private File logfile;
     65  private PrintWriter logger;
     66 
    6067  /**
    6168    Section definition for grouping annotation options.
     
    99106  public void done()
    100107  {
     108    closeLogFile();
    101109    configuration = null;
    102110    job = null;
     
    324332  }
    325333 
     334  /**
     335    Create a log file in the BASE file system for logging
     336    detailed information from a plug-in. The log file will
     337    be created if it doesn't exists. The log file is handled
     338    in it's own transaction. Call {@link #closeLogFile()} to
     339    close the log file and commit changes to the database.
     340    If this method is called when a log file is open, it
     341    does nothing.
     342   
     343    @param path The path to log to, if null this method does nothing
     344    @since 2.8
     345  */
     346  protected void createLogFile(String path)
     347  {
     348    if (logfile != null || path == null) return;
     349   
     350    logDc = sc.newDbControl();
     351    try
     352    {
     353      Path p = new Path(path, Path.Type.FILE);
     354      File f = File.getByPath(logDc, p, true);
     355      if (!f.isInDatabase()) logDc.saveItem(f);
     356      logger = new PrintWriter(f.getUploadStream(false));
     357      logfile = f;
     358    }
     359    finally
     360    {
     361      // If we failed creating the logfile we must clean up the DbControl
     362      if (logfile == null && logDc != null)
     363      {
     364        logDc.close();
     365        logDc = null;
     366        logger = null;
     367      }
     368    }
     369  }
     370 
     371 
     372  /**
     373    Close the log file created by {@link #createLogFile(String)}.
     374    @since 2.8
     375  */
     376  protected void closeLogFile()
     377  {
     378    try
     379    {
     380      if (logger != null) logger.close();
     381      if (logDc != null) logDc.commit();
     382    }
     383    catch (Throwable t)
     384    {}
     385    finally
     386    {
     387      logger = null;
     388      logfile = null;
     389      if (logDc != null) logDc.close();
     390      logDc = null;
     391    }
     392  }
     393 
     394  /**
     395    Log a message to the log file created by {@link #createLogFile(String)}.
     396    If no log file has been created, this method does nothing.
     397   
     398    @param message The message to log
     399    @since 2.8
     400  */
     401  protected void log(String message)
     402  {
     403    if (logger == null) return;
     404    logger.println(message);
     405    logger.flush();
     406  }
     407 
     408  /**
     409    Log a message and stack trace to the log file created by
     410    {@link #createLogFile(String)}. If not log file exists,
     411    this method does nothing.
     412   
     413    @param message The message to log
     414    @param t A stack trace
     415    @since 2.8
     416  */
     417  protected void log(String message, Throwable t)
     418  {
     419    if (logger == null) return;
     420    logger.println(message);
     421    if (t != null)
     422    {
     423      logger.println(t.getMessage());
     424      t.printStackTrace(logger);
     425    }
     426    logger.flush();
     427  }
    326428}
  • trunk/src/plugins/core/net/sf/basedb/plugins/AbstractFlatFileImporter.java

    r4118 r4366  
    426426    if (command.equals(Request.COMMAND_EXECUTE))
    427427    {
     428      createLogFile((String)job.getValue(Parameters.LOGFILE_PARAMETER));
    428429      InputStream in = null;
    429430      try
    430431      {
    431432        File f = (File)job.getValue("file");
     433        log("Parsing file: " + f.getName());
    432434        in = f.getDownloadStream(0);
    433435        boolean importable = isImportable(in);
     
    441443        doImport(in, progress);
    442444        in.close();
    443         response.setDone(getSuccessMessage(skippedLines));
     445        String message = getSuccessMessage(skippedLines);
     446        if (message != null) log(message);
     447        response.setDone(message);
    444448      }
    445449      catch (Throwable ex)
    446450      {
     451        log("Failed", ex);
    447452        if (in != null)
    448453        {
     
    455460        }
    456461        response.setError(ex.getMessage(), Arrays.asList(ex));
     462      }
     463      finally
     464      {
     465        closeLogFile();
    457466      }
    458467    }
     
    533542                errorHandler.handleError(t);
    534543              }
     544              log("Skipped: " + t.getMessage(), section);
    535545              skippedLines++;
    536546            }
     
    571581                  errorHandler.handleError(t);
    572582                }
     583                log("Skipped: " + t.getMessage(), line);
    573584                skippedLines++;
    574585              }
     
    607618                  errorHandler.handleError(t);
    608619                }
     620                log("Skipped: " + t.getMessage(), dataline);
    609621                skippedLines++;
    610622              }
     
    11001112  }
    11011113 
     1114  /**
     1115    Log a message about a data line to the log file created by
     1116    {@link #createLogFile(String)}. If no log file has been created,
     1117    this method does nothing.
     1118   
     1119    @param message The message to log
     1120    @param data The data line the log message is related to
     1121    @since 2.8
     1122  */
     1123  protected void log(String message, FlatFileParser.Data data)
     1124  {
     1125    log("Line " + data.lineNo() + ": " + message);
     1126  }
     1127
     1128  /**
     1129    Log an error message about a data line to the log file created
     1130    by {@link #createLogFile(String)}. If no log file has been created,
     1131    this method does nothing.
     1132   
     1133    @param message The message to log
     1134    @param data The data line the log message is related to
     1135    @param t The error
     1136    @since 2.8
     1137  */
     1138  protected void log(String message, FlatFileParser.Data data, Throwable t)
     1139  {
     1140    log("Line " + data.lineNo() + ": " + message, t);
     1141  }
     1142 
     1143  /**
     1144    Log a message about a header line to the log file created by
     1145    {@link #createLogFile(String)}. If no log file has been created,
     1146    this method does nothing.
     1147   
     1148    @param message The message to log
     1149    @param line The header line line the log message is related to
     1150    @since 2.8
     1151  */
     1152  protected void log(String message, FlatFileParser.Line line)
     1153  {
     1154    log("Line " + line.lineNo() + ": " + message);
     1155  }
     1156
     1157  /**
     1158    Log an error message about a header line to the log file created by
     1159    {@link #createLogFile(String)}. If no log file has been created,
     1160    this method does nothing.
     1161   
     1162    @param message The message to log
     1163    @param line The header line line the log message is related to
     1164    @param t The error
     1165    @since 2.8
     1166  */
     1167  protected void log(String message, FlatFileParser.Line line, Throwable t)
     1168  {
     1169    log("Line " + line.lineNo() + ": " + message, t);
     1170  }
     1171
     1172 
    11021173}
  • trunk/src/plugins/core/net/sf/basedb/plugins/util/Parameters.java

    r4145 r4366  
    2525
    2626import java.nio.charset.Charset;
     27import java.text.SimpleDateFormat;
    2728import java.util.ArrayList;
    2829import java.util.Arrays;
    2930
     31import net.sf.basedb.core.BooleanParameterType;
    3032import net.sf.basedb.core.Config;
     33import net.sf.basedb.core.Path;
     34import net.sf.basedb.core.PathParameterType;
    3135import net.sf.basedb.core.PluginParameter;
    3236import net.sf.basedb.core.StringParameterType;
     
    327331  }
    328332
     333  /**
     334    The name of the parameter that is created by
     335    {@link #decimalSeparatorParameter(String, String, String)}
     336    @since 2.8
     337  */
     338  public static final String DATE_FORMAT_PARAMETER = "dateFormat";
     339 
     340  /**
     341    Parameter definition that asks for the format used by date values when parsed from
     342    strings. See {@link SimpleDateFormat} for more info about template values.
     343    @param title The title to use for the parameter or null to use the default title
     344      (Date format)
     345    @param description The description to use for the parameter or null to use the
     346      default description
     347    @param defaultValue The default value for the date format
     348    @since 2.8
     349  */
     350  public static final PluginParameter<String> dateFormatParameter(String title, String description,
     351    String defaultValue)
     352  {
     353    if (title == null) title = "Date format";
     354    if (description == null) description =
     355      "The format used for date values, if not specified, yyyy-MM-dd is assumed.";
     356   
     357    return new PluginParameter<String>(
     358        DATE_FORMAT_PARAMETER, title, description, defaultValue,
     359        new StringParameterType(
     360          255, "yyyy-MM-dd", false)
     361        );
     362  }
     363 
     364  /**
     365    The name of the parameter that is created by
     366    {@link #logFileParameter(String, String, String)}
     367    @since 2.8
     368  */
     369  public static final String LOGFILE_PARAMETER = "logFile";
     370 
     371  /**
     372    Parameter definition that asks for the path to a log file that
     373    a plug-in can use for logging detailed information about it's
     374    progress.
     375   
     376    @param title The title to use for the parameter or null to use the default title
     377      (Log file)
     378    @param description The description to use for the parameter or null to use the
     379      default description
     380    @param defaultValue The default path
     381    @since 2.8
     382  */
     383  public static PluginParameter<String> logFileParameter(String title, String description, String defaultValue)
     384  {
     385    if (title == null) title = "Log file";
     386    if (description == null)
     387    {
     388      description = "Specify a file path for logging detailed information about the plug-ins " +
     389        "work. Logging will be disabled if this parameter is empty.";
     390    }
     391    return new PluginParameter<String>(
     392        LOGFILE_PARAMETER, title, description, defaultValue,
     393        new PathParameterType(Path.Type.FILE, defaultValue, false)
     394    );
     395  }
     396
     397  /**
     398    The name of the parameter that is created by
     399    {@link #logFileParameter(String, String, String)}
     400    @since 2.8
     401  */
     402  public static final String DRY_RUN_PARAMETER = "dryRun";
     403
     404  /**
     405    Parameter definition that enable/disable dry-run of a plug-in. A dry run
     406    should do everything the plug-in normally does, except that the result should
     407    not be committed to the database. A dry-run should be combined with
     408    a log file (see {@link #logFileParameter(String, String, String)}.
     409   
     410    @param title The title to use for the parameter or null to use the default title
     411      (Dry run)
     412    @param description The description to use for the parameter or null to use the
     413      default description
     414    @param defaultValue The default value
     415    @since 2.8
     416  */
     417  public static PluginParameter<Boolean> dryRunParameter(String title, String description, Boolean defaultValue)
     418  {
     419    if (title == null) title = "Dry run";
     420    if (description == null)
     421    {
     422      description = "Enables dry-run of the plug-in. It will do it's work as normal, " +
     423          "but not commit the result to the database. Enabling this option is mostly " +
     424          "useful if a 'Log file' is also selected.";
     425    }
     426    return new PluginParameter<Boolean>(
     427        DRY_RUN_PARAMETER, title, description, defaultValue,
     428        new BooleanParameterType()
     429    );
     430  }
     431 
    329432}
  • trunk/src/test/TestBioSource.java

    r3679 r4366  
    2424*/
    2525import net.sf.basedb.core.*;
     26
    2627import java.util.Date;
    2728
     
    166167  }
    167168
     169  static void test_delete_all()
     170  {
     171    DbControl dc = null;
     172    try
     173    {
     174      dc = TestUtil.getDbControl();
     175      ItemResultList<BioSource> l = BioSource.getQuery().list(dc);
     176      Trashcan.delete(dc.getSessionControl(), l, true, null);
     177      dc.commit();
     178      write("--Delete all biosources OK ("+l.size()+")");
     179    }
     180    catch (Throwable ex)
     181    {
     182      write("--Delete all biosources FAILED");
     183      ex.printStackTrace();
     184      ok = false;
     185    }
     186    finally
     187    {
     188      if (dc != null) dc.close();
     189    }
     190  }
    168191  static void write_header()
    169192  {
  • trunk/src/test/TestIlluminaImporter.java

    r4256 r4366  
    4747    int hybId = TestHybridization.test_create(0, false, new int[] {});
    4848    int scanId = TestScan.test_create(hybId, 0, 0, false);
    49     int protocolId = TestProtocol.test_create(SystemItems.getId(ProtocolType.FEATURE_EXTRACTION), false);
     49    int protocolId = TestProtocol.test_create(SystemItems.getId(ProtocolType.FEATURE_EXTRACTION), null, false);
    5050    int softwareId = TestSoftware.test_create(SystemItems.getId(SoftwareType.FEATURE_EXTRACTION), false);
    5151    int experimentId = TestExperiment.test_create("illumina", false);
  • trunk/src/test/TestPlate.java

    r4239 r4366  
    6060    // Test events
    6161    int protocolTypeId = TestProtocolType.test_create(false);
    62     int protocolId = TestProtocol.test_create(protocolTypeId, false);
     62    int protocolId = TestProtocol.test_create(protocolTypeId, null, false);
    6363    int eventTypeId = TestPlateType.test_create_event_type(plateTypeId, protocolTypeId);
    6464    int eventId = test_create_event(id, eventTypeId, protocolId);
  • trunk/src/test/TestProtocol.java

    r3719 r4366  
    5151    write_header();
    5252    // Standard tests: create, load, list
    53     int id1 = test_create(SystemItems.getId(ProtocolType.SAMPLING), true);
    54     int id2 = test_create(SystemItems.getId(ProtocolType.LABELING), false);
     53    int id1 = test_create(SystemItems.getId(ProtocolType.SAMPLING), null, true);
     54    int id2 = test_create(SystemItems.getId(ProtocolType.LABELING), null, false);
    5555    test_load(id1);
    5656    test_list();
     
    7171  }
    7272 
    73   static int test_create(int protocoltypeId, boolean setAll)
     73  static int test_create(int protocoltypeId, String name, boolean setAll)
    7474  {
    7575    int id = 0;
     
    8585        p.setDescription("Added at "+new Date());
    8686      }
     87      if (name != null) p.setName(name);
    8788      dc.saveItem(p);
    8889      dc.commit();
  • trunk/src/test/TestRawBioAssay.java

    r4256 r4366  
    6060      "\"Block\"\\t\"Column\"\\t\"Row\"\\t\"Name\"\\t\"ID\".*", null, 0, 2, 1, 4);
    6161    int softwareId = TestSoftware.test_create(SystemItems.getId(SoftwareType.FEATURE_EXTRACTION), false);
    62     int protocolId = TestProtocol.test_create(SystemItems.getId(ProtocolType.FEATURE_EXTRACTION), false);
     62    int protocolId = TestProtocol.test_create(SystemItems.getId(ProtocolType.FEATURE_EXTRACTION), null, false);
    6363   
    6464    // Standard tests: create, load, list
  • trunk/src/test/TestSample.java

    r3679 r4366  
    2525*/
    2626import net.sf.basedb.core.*;
     27
    2728import java.util.Date;
    2829
     
    234235  }
    235236
     237  static void test_delete_all()
     238  {
     239    DbControl dc = null;
     240    try
     241    {
     242      dc = TestUtil.getDbControl();
     243      ItemResultList<Sample> l = Sample.getQuery().list(dc);
     244      Trashcan.delete(dc.getSessionControl(), l, true, null);
     245      dc.commit();
     246      write("--Delete all samples OK ("+l.size()+")");
     247    }
     248    catch (Throwable ex)
     249    {
     250      write("--Delete all samples FAILED");
     251      ex.printStackTrace();
     252      ok = false;
     253    }
     254    finally
     255    {
     256      if (dc != null) dc.close();
     257    }
     258  }
     259 
    236260  static void write_header()
    237261  {
  • trunk/src/test/TestScan.java

    r3679 r4366  
    4545    int hybridizationId = TestHybridization.test_create(0, false);
    4646    int scannerId = TestHardware.test_create(SystemItems.getId(HardwareType.SCANNER), false);
    47     int protocolId = TestProtocol.test_create(SystemItems.getId(ProtocolType.SCANNING), false);
     47    int protocolId = TestProtocol.test_create(SystemItems.getId(ProtocolType.SCANNING), null, false);
    4848   
    4949    // Standard tests: create, load, list
Note: See TracChangeset for help on using the changeset viewer.