Changeset 5646


Ignore:
Timestamp:
May 27, 2011, 1:50:50 PM (10 years ago)
Author:
Nicklas Nordborg
Message:

References #1597: Subtypes of items

Added subtype functionality to the biosource importer.

  • The "Identification method" section has a new parameter were it is possible to select one or more subtypes to use when searching for existing items. If exactly one subtype is selected in this list new items will automatically be assigned to that subtype. If no subtype or multiple subtypes are selected, the subtypes must be assigned using a column in the imported file.
  • Added a 'subtype' column mapping that can be used to assign subtypes to items. If a mapping is given, it always override the subtype used in the identification section.
Location:
trunk/src/plugins/core/net/sf/basedb/plugins/batchimport
Files:
4 edited

Legend:

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

    r5642 r5646  
    4848import net.sf.basedb.core.File;
    4949import net.sf.basedb.core.Hardware;
     50import net.sf.basedb.core.ItemParameterType;
    5051import net.sf.basedb.core.PhysicalBioAssay;
    5152import net.sf.basedb.core.Image;
     
    8384import net.sf.basedb.core.StringTooLongException;
    8485import net.sf.basedb.core.StringUtil;
     86import net.sf.basedb.core.Subtypable;
    8587import net.sf.basedb.core.SystemItems;
    8688import net.sf.basedb.core.Tag;
     
    177179      );
    178180 
     181  protected static final PluginParameter<String> subtypeColumnMapping = new PluginParameter<String>(
     182      "subtypeColumnMapping",
     183      "Item subtype",
     184      "Mapping that picks the subtype of the item from the data columns. " +
     185      "Example: \\Subtype\\",
     186      optionalColumnMapping
     187      );
     188 
    179189  protected static final PluginParameter<String> descriptionColumnMapping = new PluginParameter<String>(
    180190      "descriptionColumnMapping",
     
    287297        Arrays.asList( new String[] { "ignore", "fail"} ))
    288298    );
     299 
    289300  public AbstractItemImporter()
    290301  {}
     
    462473  private Mapper idMapper;
    463474  private Mapper permissionTemplateMapper;
     475  private Mapper subtypeMapper;
     476  private ItemSubtype selectedSubtype;
    464477  private ItemQuery<I> itemQuery;
    465478
     
    555568    Setup column mapping. Creates DbControl and query to find items.
    556569  */
     570  @SuppressWarnings("unchecked")
    557571  @Override
    558572  protected void beginData()
     
    563577    this.idMapper = getMapper(ffp, idMapping, null, null);
    564578    this.permissionTemplateMapper = getMapper(ffp, (String)job.getValue("permissionTemplateColumnMapping"), null, null);
     579    this.subtypeMapper = getMapper(ffp, (String)job.getValue("subtypeColumnMapping"), null, null);
    565580    createColumnMappers(ffp, cropStrings);
    566581   
     
    573588    itemQuery = idMethod.prepareQuery(dc, createItemQuery());
    574589    itemQuery.include(includes);
     590   
     591    List<ItemSubtype> subtypes = (List<ItemSubtype>)job.getValues("itemSubtypes");
     592    if (subtypes != null && subtypes.size() > 0)
     593    {
     594      Expression subtypeParameter = Hql.entityParameter("subtypes", Item.ITEMSUBTYPE);
     595      if (subtypes.size() == 1)
     596      {
     597        selectedSubtype = subtypes.get(0);
     598        itemQuery.restrict(Restrictions.eq(Hql.property("itemSubtype"), subtypeParameter));
     599        itemQuery.setEntityParameter("subtypes", selectedSubtype);
     600      }
     601      else
     602      {
     603        itemQuery.restrict(Restrictions.in(Hql.property("itemSubtype"), subtypeParameter));
     604        itemQuery.setParameter("subtypes", subtypes, null);
     605      }
     606    }
    575607  }
    576608 
     
    863895
    864896  /**
     897    Does the importer want to use item subtypes when importing?
     898    If this method returns a non-null, this class will add a parameter
     899    in the "Item identification" section that makes it possible to
     900    select which subtypes the importer should use to limit the
     901    search for existing items. The default implementation returns null.
     902    @return An Item or null
     903  */
     904  protected Item getItemForSubtypes()
     905  {
     906    return null;
     907  }
     908 
     909  /**
    865910    Create mappers that can be used to extract data values from the file
    866911    that is currently being parsed by the <code>ffp</code> {@link FlatFileParser}.
     
    9781023    }
    9791024  }
     1025 
     1026  /**
     1027    If a mapping for item subtype has been specified or if a single subtype was selected
     1028    for item identification in this job, this method is will update the subtype on the item.
     1029    @param dc A DbControl used to lookup the permission template
     1030    @param item The item to update
     1031    @param data Data from the file
     1032    @since 3.0
     1033  */
     1034  protected void updateItemSubtype(DbControl dc, Subtypable item, FlatFileParser.Data data)
     1035  {
     1036    if (subtypeMapper != null)
     1037    {
     1038      String nameOrId = subtypeMapper.getValue(data);
     1039      ItemSubtype subtype = findItemSubtype(dc, FallbackIdMethod.NAME_OR_SYSTEMID_OR_ID, item.getType(), nameOrId);
     1040      if (nameOrId == null || subtype != null) item.setItemSubtype(subtype);
     1041    }
     1042    else if (selectedSubtype != null)
     1043    {
     1044      item.setItemSubtype(selectedSubtype);
     1045    }
     1046  }
    9801047  // ---------------------------------------
    9811048 
     
    10071074  {
    10081075    return dateFormatter.parseString(date);
     1076  }
     1077 
     1078  private ItemQuery<ItemSubtype> subtypeQuery;
     1079 
     1080  /**
     1081    Find an item subtype with a given identifier. This is a utility method that
     1082    subclasses can use when creating or updating items.
     1083    <p>
     1084    NOTE! The first time this method is called a query object is initialised
     1085    using the {@link IdMethod#prepareQuery(DbControl, ItemQuery)} method.
     1086    Subsequent calls uses the same query. Thus, this method should always be
     1087    called with the same id method object, otherwise the result is undefined.
     1088   
     1089    @param dc The DbControl to use for database access
     1090    @param identifier The identifier for the subtype
     1091    @param mainType The main item type for the subtype
     1092    @return A subtype, or null if no item could be found
     1093    @since 3.0
     1094  */
     1095  protected ItemSubtype findItemSubtype(DbControl dc, IdMethod idMethod, Item mainType, String identifier)
     1096  {
     1097    if (identifier == null) return null;
     1098    if (subtypeQuery == null)
     1099    {
     1100      subtypeQuery = initReferenceQuery(dc, idMethod, ItemSubtype.getQuery(null));
     1101      subtypeQuery.restrictPermanent(
     1102        Restrictions.eq(
     1103          Hql.property("itemType"),
     1104          Expressions.parameter("itemType", Type.INT)
     1105        )
     1106      );     
     1107    }
     1108    subtypeQuery.setParameter("itemType", mainType.getValue(), Type.INT);
     1109    return findReferencedItem(dc, idMethod, subtypeQuery, identifier, true);
    10091110  }
    10101111 
     
    20192120      );
    20202121      parameters.add(idMethodParameter);
     2122     
     2123      if (getItemForSubtypes() != null)
     2124      {
     2125        DbControl dc = sc.newDbControl();
     2126        try
     2127        {
     2128          // Get a list of possible subtypes
     2129          List<ItemSubtype> subtypes = new ArrayList<ItemSubtype>(ItemSubtype.getQuery(getItemForSubtypes()).list(dc));
     2130       
     2131          if (subtypes != null && subtypes.size() > 0)
     2132          {
     2133            // Find a default subtype from the current context
     2134            ItemContext cc = sc.getCurrentContext(getItemForSubtypes());
     2135            int selectedSubtypeId = Values.getInt(cc.getPropertyValue("itemSubtype"));
     2136            ItemSubtype defaultSubtype = selectedSubtypeId == 0 ? null : ItemSubtype.getById(dc, selectedSubtypeId);
     2137           
     2138            // Create parameter
     2139            PluginParameter<ItemSubtype> subtypeParameter = new PluginParameter<ItemSubtype>(
     2140              "itemSubtypes",
     2141              "Item subtypes",
     2142              "Only look for items of selected subtypes. If none is selected, all subtypes " +
     2143              "will be searched. If a single subtype is selected, new items will automatically " +
     2144              "be assiged that subtype.",
     2145              new ItemParameterType<ItemSubtype>(ItemSubtype.class, defaultSubtype, false, 0, subtypes)
     2146            );
     2147            parameters.add(subtypeParameter);
     2148          }
     2149        }
     2150        finally
     2151        {
     2152          if (dc != null) dc.close();
     2153        }
     2154
     2155      }
    20212156     
    20222157      if (forJob)
  • trunk/src/plugins/core/net/sf/basedb/plugins/batchimport/BioSourceImporter.java

    r5612 r5646  
    8989  }
    9090 
     91  @Override
     92  protected Item getItemForSubtypes()
     93  {
     94    return Item.BIOSOURCE;
     95  }
     96 
    9197  /**
    9298    Adds column mappings for name, externalId and description.
     
    97103    parameters.add(internalIdColumnMapping);
    98104    parameters.add(nameColumnMapping);
     105    parameters.add(subtypeColumnMapping);
    99106    parameters.add(externalIdColumnMapping);
    100107    parameters.add(descriptionColumnMapping);
     
    132139    if (descriptionMapper != null) biosource.setDescription(descriptionMapper.getValue(data));
    133140    if (externalIdMapper != null) biosource.setExternalId(externalIdMapper.getValue(data));
     141    updateItemSubtype(dc, biosource, data);
    134142  }
    135143  // ------------------------------------
  • trunk/src/plugins/core/net/sf/basedb/plugins/batchimport/FallbackIdMethod.java

    r4513 r5646  
    5858    new FallbackIdMethod("nameOrBarcodeOrId", "Name, barcode or internal ID",
    5959        MultiPropertyIdMethod.NAME_OR_BARCODE);
     60 
     61  /**
     62    Use the name or system ID for finding items. If no item is found and
     63    the identifier is numerical, try loading it by ID.
     64    @since 3.0
     65  */
     66  public static final FallbackIdMethod NAME_OR_SYSTEMID_OR_ID =
     67    new FallbackIdMethod("nameOrSystemIdOrId", "Name, system ID or internal ID",
     68        MultiPropertyIdMethod.NAME_OR_SYSTEMID);
    6069 
    6170  private final String method;
  • trunk/src/plugins/core/net/sf/basedb/plugins/batchimport/MultiPropertyIdMethod.java

    r4513 r5646  
    6363        new String[] { "name", "barcode" }, false, Type.STRING);
    6464
     65  /**
     66    Use the name or system Id for finding items.
     67    @since 3.0
     68  */
     69  public static final MultiPropertyIdMethod NAME_OR_SYSTEMID =
     70    new MultiPropertyIdMethod("nameOrSystemId", "Name or system ID",
     71        new String[] { "name", "systemId" }, false, Type.STRING);
    6572   
    6673  private final String method;
Note: See TracChangeset for help on using the changeset viewer.