Changeset 4922


Ignore:
Timestamp:
May 6, 2009, 12:45:08 PM (13 years ago)
Author:
Martin Svensson
Message:

Fixes #1303 Batch scan importer improvements

Location:
trunk/src/plugins/core/net/sf/basedb/plugins/batchimport
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/plugins/core/net/sf/basedb/plugins/batchimport/AbstractItemImporter.java

    r4921 r4922  
    14631463  }
    14641464 
    1465   private ItemQuery<Image> imageQuery;
    1466   /**
    1467    * 
    1468       @param dc
    1469       @param idMethod
    1470       @param scan
    1471       @param identifier
    1472       @return
     1465  /**
     1466    Find an image with given scan and file. This is a utility method that
     1467    subclasses can use when creating or updating items.
     1468    New image will be created if given scan hasn't been saved to the database or
     1469    if no image could be found for the given file.
     1470    Null-value is returned if more then one image were found and
     1471    {@link #failIfMultipleFoundReferences} is set to FALSE.
     1472   
     1473      @param dc The DbControl to use for database access
     1474      @param scan The Scan for which the image should belong to.
     1475      @param imageFile The File, which the image is connected to.
     1476      @return An image, or null.
    14731477      @since 2.12
    14741478   */
    1475   protected Image findImage(DbControl dc, IdMethod idMethod, Scan scan, String identifier)
    1476   {
    1477     if (identifier == null) return null;
    1478     if (imageQuery == null)
    1479     {
    1480       imageQuery = initReferenceQuery(dc, idMethod, scan.getImages());
    1481     }
    1482     return findReferencedItem(dc, idMethod, imageQuery, identifier, false);
     1479  protected Image findImage(DbControl dc, Scan scan, File imageFile)
     1480  {
     1481    if (imageFile == null) return null;   
     1482    Image image = null;
     1483    if (scan.isInDatabase())
     1484    {
     1485      ItemQuery<Image> imageQuery = scan.getImages();
     1486      imageQuery.include(Include.MINE, Include.IN_PROJECT, Include.SHARED, Include.OTHERS);
     1487      imageQuery.restrict(Restrictions.eq(Hql.property("file"), Hql.entity(imageFile)));
     1488      List<Image> images = imageQuery.list(dc);     
     1489      if (images.size() == 1)
     1490      {
     1491        image = images.get(0);
     1492      }
     1493      else if (images.size() > 1)
     1494      {
     1495        // Several images with given file were found
     1496        if (failIfMultipleFoundReferences)
     1497        {
     1498          throw new BaseException("Found " + images.size() + " " + Item.IMAGE + " [ File =" + imageFile.getPath() + "]");
     1499        }
     1500      }
     1501      else if (images.size() == 0)
     1502      {
     1503        // Create new image if no one could be found with the given file.     
     1504        image = scan.newImage();
     1505        dc.saveItemIf(scan, image, false);
     1506        image.setFile(imageFile);
     1507      }
     1508    }
     1509    else
     1510    {
     1511      // Create new image based on the given file if the scan hasn't been saved to the database.
     1512      image = scan.newImage();
     1513      dc.saveItemIf(scan, image, false);
     1514      image.setFile(imageFile);
     1515    }
     1516    return image;
    14831517  }
    14841518 
  • trunk/src/plugins/core/net/sf/basedb/plugins/batchimport/ScanImporter.java

    r4921 r4922  
    2222package net.sf.basedb.plugins.batchimport;
    2323
    24 import net.sf.basedb.core.BaseException;
    2524import net.sf.basedb.core.DbControl;
    2625import net.sf.basedb.core.File;
     
    3130import net.sf.basedb.core.ItemQuery;
    3231import net.sf.basedb.core.Path;
     32import net.sf.basedb.core.PathParameterType;
    3333import net.sf.basedb.core.PluginParameter;
    3434import net.sf.basedb.core.Protocol;
     
    4040import net.sf.basedb.core.plugin.AboutImpl;
    4141import net.sf.basedb.core.plugin.GuiContext;
    42 import net.sf.basedb.core.query.Hql;
    43 import net.sf.basedb.core.query.Restrictions;
    4442import net.sf.basedb.util.parser.FlatFileParser;
    4543import net.sf.basedb.util.parser.Mapper;
     
    9896 
    9997  protected static final PluginParameter<String> imageFileColumnMapping = new PluginParameter<String>(
    100       "fileColumnMapping",
     98      "imageFileColumnMapping",
    10199      "Image file",
    102100      "Mapping that picks the path of a image file from the columns. " +
     
    108106      );
    109107 
    110   protected static final PluginParameter<String> imageColumnMapping = new PluginParameter<String>(
     108  protected static final PluginParameter<String> imageNameColumnMapping = new PluginParameter<String>(
    111109      "imageColumnMapping",
    112       "Image",
     110      "Image name",
    113111      "Mapping that picks the name the image from the data columns. " +     
    114112      "Example: \\Image\\",
     
    121119      "Mapping that picks the format of the given image from the data columns. " +
    122120      "The plug-in tries to parse the given format into one of the supported in BASE. " +
    123       "The image will get format 'unknown' if this isn't succeded."+
     121      "The image will get format 'unknown' if this isn't specified or has null-value." +
     122      "The column can either be empty or has one of the values 'TIFF', 'JPEG' or 'Unknown'."+
    124123      "Example: \\Image format\\",
    125124      optionalColumnMapping
     
    179178  private Mapper hybridizationMapper;
    180179  private Mapper imageFileMapper;
    181   private Mapper imageMapper;
     180  private Mapper imageNameMapper;
    182181  private Mapper imageFormatMapper;
    183182  private Mapper imagePreviewMapper;
     
    215214    parameters.add(hardwareColumnMapping);
    216215    parameters.add(imageFileColumnMapping);
    217     parameters.add(imageColumnMapping);
     216    parameters.add(imageNameColumnMapping);
    218217    parameters.add(imageFormatColumnMapping);
    219218    parameters.add(imagePreviewColumnMapping);
    220219    parameters.add(imageDescriptionColumnMapping);
    221220  }
     221 
     222  /**
     223    Add parameter for "Image directory" directory where image files may be located.
     224  */
     225  @Override
     226  protected void addMoreJobParameters(List<PluginParameter<?>> parameters)
     227  {
     228    parameters.add(new PluginParameter<String>(
     229        "imagesDirectory",
     230        "Images directory",
     231        "A base directory were image files are located. Relative file paths are " +
     232        "resolved against this directory. If no directory is given, only absolute " +
     233        "paths (=paths starting with /) can be used.",
     234        new PathParameterType(Path.Type.DIRECTORY))
     235    );
     236  }
     237
    222238 
    223239  /**
     
    241257    hybridizationMapper = getMapper(ffp, (String)job.getValue("hybridizationColumnMapping"), null, null);
    242258    imageFileMapper = getMapper(ffp, (String)job.getValue("imageFileColumnMapping"), null, null);
    243     imageMapper = getMapper(ffp, (String)job.getValue("imageColumnMapping"), null, null);
     259    imageNameMapper = getMapper(ffp, (String)job.getValue("imageColumnMapping"), null, null);
    244260    imageFormatMapper = getMapper(ffp, (String)job.getValue("imageFormatColumnMapping"), null, null);
    245261    imagePreviewMapper = getMapper(ffp, (String)job.getValue("imagePreviewColumnMapping"), null, null);
     
    300316  protected void updateMultiLineItem(DbControl dc, Scan scan, FlatFileParser.Data data, int multiLineNum)
    301317  {
    302     ItemQuery<Image> imageQuery = scan.getImages();
    303318    Image image = null;
    304319    File imageFile = null;
    305    
     320    String imageName = null;
    306321    // Get the imagefile
    307     if (imageFileMapper != null)
     322    if (imageFileMapper != null && imageFileMapper.getValue(data) != null)
    308323    {
    309324      String filePath = imageFileMapper.getValue(data);
     
    312327        filePath = imagesDirectory + filePath;
    313328      }
    314       imageFile = File.getByPath(dc, new Path(filePath, Path.Type.FILE), false);     
     329      imageFile = File.getByPath(dc, new Path(filePath, Path.Type.FILE), false);
     330      image = findImage(dc, scan, imageFile);
     331    }
     332    if (imageNameMapper != null)
     333    {
     334      imageName = imageNameMapper.getValue(data);
     335    }
     336    else if (image != null)
     337    {
     338      imageName = image.getName();
    315339    }
    316340   
    317     // Get image by name or id and check image file.
    318     if (imageMapper != null)
    319     {
    320       String nameOrId = imageMapper.getValue(data);
    321       image = findImage(dc, FallbackIdMethod.NAME_OR_ID, scan, nameOrId);
    322 
    323       if (image != null && imageFile != null)
    324       {
    325         if (image.getFile() == null) image.setFile(imageFile);
    326         else image = null;
    327       }
    328     }   
    329    
    330     // Create new image if no one could be found with the given name (and file).
    331     if (image == null)
    332     {
    333       image = scan.newImage();
    334       image.setFile(imageFile);
    335       image.setName(scan.getName() + "." + (imageFile != null ? imageFile.getName() : "i" + scan.countImages()));
    336     }   
    337    
    338     // Update the image's properties   
    339     if (imageDescriptionMapper != null) image.setDescription(imageDescriptionMapper.getValue(data));
    340     if (imageFormatMapper != null) image.setFormat(Image.Format.valueOf(imageFormatMapper.getValue(data)));
    341     if (imagePreviewMapper != null) image.setPreview(Boolean.getBoolean(imagePreviewMapper.getValue(data)));     
    342    
     341    if (image != null)
     342    {
     343      image.setName(imageName != null ? imageName : scan.getName() + "." + imageFile.getName());
     344      if (imageDescriptionMapper != null) image.setDescription(imageDescriptionMapper.getValue(data));
     345      if (imageFormatMapper != null && imageFormatMapper.getValue(data) != null) image.setFormat(Image.Format.valueOf(imageFormatMapper.getValue(data)));
     346      if (imagePreviewMapper != null) image.setPreview(Boolean.getBoolean(imagePreviewMapper.getValue(data)));
     347    }
    343348  }
    344349  // ------------------------------------
Note: See TracChangeset for help on using the changeset viewer.