Changeset 7591


Ignore:
Timestamp:
Feb 19, 2019, 11:17:42 AM (5 years ago)
Author:
Nicklas Nordborg
Message:

References #2149: Batch item importers should be able to use an annotation for item identification

The annotation importer plug-in has been updated with support for using annotations to identify items. An extra complication was that it was not using the IdMethod implementations for finding items. This has been changed which may not be fully backwards compatible if the plug-in is used programmatically.

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

Legend:

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

    r7478 r7591  
    7575import net.sf.basedb.core.plugin.Request;
    7676import net.sf.basedb.core.plugin.Response;
    77 import net.sf.basedb.core.query.Expressions;
    7877import net.sf.basedb.core.query.Hql;
    7978import net.sf.basedb.core.query.Orders;
    80 import net.sf.basedb.core.query.Restrictions;
    8179import net.sf.basedb.core.signal.ThreadSignalHandler;
     80import net.sf.basedb.plugins.batchimport.AnnotationIdMethod;
     81import net.sf.basedb.plugins.batchimport.IdMethod;
     82import net.sf.basedb.plugins.batchimport.InternalIdMethod;
     83import net.sf.basedb.plugins.batchimport.PropertyIdMethod;
    8284import net.sf.basedb.plugins.util.Parameters;
    8385import net.sf.basedb.util.Enumeration;
     
    159161      optionalColumnMapping
    160162      );
     163 
     164  /**
     165    Column mapping parameter for the annotation column used for item identification.
     166    @since 3.15
     167  */
     168  private static final PluginParameter<String> idAnnotationColumnMapping = new PluginParameter<String>(
     169      "idAnnotationColumnMapping",
     170      "Annotation ID",
     171      "Mapping that picks the annotation value to use for item identification from the data columns. This " +
     172      "parameter is required if the selected identification method is based on an annotation, " +
     173      "otherwise it is ignored. Example: \\Sample-ID\\",
     174      optionalColumnMapping
     175      );
    161176
    162177  /**
     
    174189  );
    175190 
    176   private static final PluginParameter<String> findItemsSection = new PluginParameter<String>(
    177     "findItemsSection",
    178     "Search for items",
    179     "Options that affect which items are searched when matching the name/external ID from the file",
    180     null
    181   );
    182  
     191  private static final PluginParameter<String> identificationSection = new PluginParameter<String>(
     192      "idSection",
     193      "Item identification",
     194      "Options related to how find existing items.",
     195      null
     196    );
     197
    183198  private static final PluginParameter<String> addReplaceSection = new PluginParameter<String>(
    184199    "addReplaceSection",
     
    418433  {
    419434    String command = request.getCommand();
     435    DbControl dc = null;
    420436    try
    421437    {
     
    457473        }
    458474
    459         // Check that a mapping for name OR external ID is given
    460         boolean hasExternalId = context == null || hasExternalId(context.getItem());
    461         String nameMapping = (String)request.getParameterValue("nameColumnMapping");
    462         String externalIdMapping = (String)request.getParameterValue("externalIdColumnMapping");
    463         String internalIdMapping = (String)request.getParameterValue("internalIdColumnMapping");
    464         int nullCount = 0;
    465         if (nameMapping == null) nullCount++;
    466         if (internalIdMapping == null) nullCount++;
    467         if (hasExternalId)
    468         {
    469           if (externalIdMapping == null) nullCount++;
    470           if (nullCount == 3)
    471           {
    472             response.setError("A mapping for either 'Name', 'External Id' or " +
    473               "'Internal ID' must be given.", null);
    474             return;
    475           }
    476           if (nullCount != 2)
    477           {
    478             response.setError("It is not possible to use a mapping for more than one of " +
    479               "'Name', 'External Id' and 'Internal ID'.", null);
    480             return;           
    481           }
    482         }
    483         else
    484         {
    485           if (nullCount == 2)
    486           {
    487             response.setError("A mapping for either 'Name' or " +
    488               "'Internal ID' must be given.", null);
    489             return;
    490           }
    491           if (nullCount != 1)
    492           {
    493             response.setError("It is not possible to use a mapping for both " +
    494               "'Name' and 'Internal ID'.", null);
    495             return;           
    496           }
     475        dc = sc.newDbControl();
     476        // Check that a mapping for name, internal/external ID or annotation is given
     477        IdMethod idMethod = getIdMethod(dc, context == null ? null : context.getItem(), (String)request.getParameterValue("idMethod"));
     478        PluginParameter<?> idMappingParameter = ri.getParameter(idMethod.getColumnMappingParameterName());
     479        String idMapping = (String)request.getParameterValue(idMappingParameter.getName());
     480        if (idMapping == null)
     481        {
     482          response.setError("No colum mapping provided for item identification: " +
     483              idMappingParameter.getLabel(), null);
     484          return;
    497485        }
    498486       
    499487        // Check the mapping expressions
    500488        boolean allowComplex = "allow".equals(request.getParameterValue(complexMappings.getName()));
    501         checkColumnMapping(parser, nameMapping, allowComplex, nameColumnMapping.getLabel());
    502         checkColumnMapping(parser, externalIdMapping, allowComplex, externalIdColumnMapping.getLabel());
    503         checkColumnMapping(parser, internalIdMapping, allowComplex, internalIdColumnMapping.getLabel());
     489        checkColumnMapping(parser, idMapping, allowComplex, idMappingParameter.getLabel());
    504490
    505491        // Everything is ok, save values
    506492        if (forJob) storeValue(wrapper, request, fileParameter);
     493        storeValue(wrapper, request, ri.getParameter("idMethod"));
     494        // Include options
     495        if (forJob)
     496        {
     497          storeValue(job, request, ri.getParameter("includeMine"));
     498          storeValue(job, request, ri.getParameter("includeShared"));
     499          storeValue(job, request, ri.getParameter("includeInProject"));
     500          storeValue(job, request, ri.getParameter("includeOthers"));
     501        }
     502
    507503        storeValue(wrapper, request, dataHeaderRegexpParameter);
    508504        storeValue(wrapper, request, dataSplitterRegexpParameter);
     
    520516        storeValue(wrapper, request, complexMappings);
    521517        storeValue(wrapper, request, nameColumnMapping);
    522         if (hasExternalId)
    523         {
    524           storeValue(wrapper, request, externalIdColumnMapping);
    525         }
     518        storeValue(wrapper, request, externalIdColumnMapping);
    526519        storeValue(wrapper, request, internalIdColumnMapping);
     520        storeValue(wrapper, request, idAnnotationColumnMapping);
    527521       
    528522        if (forJob)
     
    563557          return;
    564558        }
    565      
    566         // Include options
    567         storeValue(job, request, ri.getParameter("includeMine"));
    568         storeValue(job, request, ri.getParameter("includeShared"));
    569         storeValue(job, request, ri.getParameter("includeInProject"));
    570         storeValue(job, request, ri.getParameter("includeOthers"));
    571559       
    572560        // Add/Replace options
     
    599587      response.setError(ex.getMessage(), Arrays.asList(ex));
    600588    }
     589    finally
     590    {
     591      if (dc != null) dc.close();
     592    }
    601593  }
    602594  // -------------------------------------------
     
    616608  private DateFormatter timestampFormatter;
    617609  private Item itemType;
    618   private String searchProperty;
    619   private String searchMapping;
    620   private Type searchParameterType;
     610  private IdMethod idMethod;
     611  private String idMapping;
    621612  private Mapper itemMapper;
    622613  private Map<Mapper, AnnotationType> mappers;
     
    679670  protected void begin(FlatFileParser ffp)
    680671  {
     672    this.dc = sc.newDbControl();
    681673    this.ffp = ffp;
    682674    // Formatting options
     
    690682   
    691683    this.itemType = Item.valueOf((String)job.getValue("itemType"));
     684    this.idMethod = getIdMethod(dc, itemType, (String)job.getValue("idMethod"));
     685    this.idMapping = (String)job.getValue(idMethod.getColumnMappingParameterName());
     686
     687    /*
    692688    if (job.getValue("externalIdColumnMapping") != null)
    693689    {
     
    708704      this.searchMapping = (String)job.getValue("nameColumnMapping");
    709705    }
     706    */
     707   
    710708    this.addToUnlimited = Boolean.TRUE.equals(job.getValue("addToUnlimited"));
    711709    this.replaceExisting = Boolean.TRUE.equals(job.getValue("replaceExisting"));
     
    739737  protected void beginData()
    740738  {
    741     this.dc = sc.newDbControl();
    742739    this.unitCache = new UnitCache(dc);
    743740   
    744741    // Mapper to get name or external ID
    745     this.itemMapper = getMapper(ffp, searchMapping, null, null);
     742    this.itemMapper = getMapper(ffp, idMapping, null, null);
    746743
    747744    // Annotation type mappers
     
    780777    if (Boolean.TRUE.equals((Boolean)job.getValue("includeOthers"))) includes.add(Include.OTHERS);
    781778   
    782     itemQuery = createQuery(itemType, searchProperty, includes);
     779    itemQuery = idMethod.prepareQuery(dc, createQuery(itemType));
     780    itemQuery.setIncludes(includes);
    783781  }
    784782 
     
    805803      if (nameOrId != null)
    806804      {
    807         // The 'name' parameter can also query against the external ID
    808         itemQuery.setParameter("nameOrId", searchParameterType.parseString(nameOrId), searchParameterType);
    809         List<?> result = itemQuery.list(dc);
     805        List<?> result = idMethod.find(dc, itemQuery, nameOrId);
    810806        if (result.isEmpty())
    811807        {
     
    814810          {
    815811            throw new ItemNotFoundException(itemType +
    816               "[" + searchProperty + "=" + nameOrId + "]");
     812              "[" + idMethod + "=" + nameOrId + "]");
    817813          }
    818814          else
     
    824820        {
    825821          throw new BaseException("Found " + result.size() + " " + itemType.toString().toLowerCase() +
    826             " with " + searchProperty + "=" + nameOrId);
     822            " with " + idMethod + "=" + nameOrId);
    827823        }
    828824        for (Object item : result)
     
    835831      else if (!ignoreNotFoundItems)
    836832      {
    837         throw new ItemNotFoundException(itemType + "[" + searchProperty + " has no value in file]");
     833        throw new ItemNotFoundException(itemType + "[" + idMethod + " has no value in file]");
    838834      }
    839835      else
     
    10471043    if (configureParser == null)
    10481044    {
    1049       List<PluginParameter<?>> parameters = new ArrayList<PluginParameter<?>>();
    1050 
    1051       if (forJob)
    1052       {
    1053         // The file to import from
    1054         parameters.add(fileParameter);
    1055       }
    1056       else
    1057       {
    1058         Enumeration<String, String> items = new Enumeration<String, String>();
    1059         for (GuiContext ctx : getGuiContexts())
    1060         {
    1061           Item i = ctx.getItem();
    1062           ctx.getItem().name();
    1063           items.add(i.name(), i.toString());
    1064         }
    1065         items.sortValues();
     1045      DbControl dc = null;
     1046      try
     1047      {
     1048        dc = sc.newDbControl();
     1049        List<PluginParameter<?>> parameters = new ArrayList<PluginParameter<?>>();
     1050 
     1051        if (forJob)
     1052        {
     1053          // The file to import from
     1054          parameters.add(fileParameter);
     1055          parameters.add(identificationSection);
     1056        }
     1057        else
     1058        {
     1059          Enumeration<String, String> items = new Enumeration<String, String>();
     1060          for (GuiContext ctx : getGuiContexts())
     1061          {
     1062            Item i = ctx.getItem();
     1063            ctx.getItem().name();
     1064            items.add(i.name(), i.toString());
     1065          }
     1066          items.sortValues();
     1067         
     1068          PluginParameter<String> itemTypesParameter =
     1069            new PluginParameter<String>("itemTypes", "Item types",
     1070              "Specify which types of items this file format is used for. If " +
     1071              "not specified it will be used for all types.",
     1072            new StringParameterType(255, null, false, 0, 0, 0, items));
     1073         
     1074          parameters.add(itemTypesParameter);
     1075        }
    10661076       
    1067         PluginParameter<String> itemTypesParameter =
    1068           new PluginParameter<String>("itemTypes", "Item types",
    1069             "Specify which types of items this file format is used for. If " +
    1070             "not specified it will be used for all types.",
    1071           new StringParameterType(255, null, false, 0, 0, 0, items));
     1077        Enumeration<String, String> idMethods = new Enumeration<String, String>();
     1078        boolean hasAnnotationIdMethod = false;
     1079        for (IdMethod idm : getIdMethods(dc, context == null ? null : context.getItem()))
     1080        {
     1081          if (idm instanceof AnnotationIdMethod) hasAnnotationIdMethod = true;
     1082          idMethods.add(idm.getMethod(), idm.getTitle());
     1083        }
     1084        PluginParameter<String> idMethodParameter = new PluginParameter<String>(
     1085          "idMethod",
     1086          "Identification method",
     1087          "The method to use for finding existing items. You can use either the " +
     1088          "internal ID, the name, external ID or an annotation. If not specified, a " +
     1089          "method is automatically selected based on the available column mappings. " +
     1090          "If a method is selected, you need to makes sure that the corresponding " +
     1091          "column mapping is defined.",
     1092          new StringParameterType(null, idMethods.getKey(0), true, 1, 0, 0, idMethods)
     1093        );
     1094        parameters.add(idMethodParameter);
     1095
     1096        if (forJob)
     1097        {
     1098          ItemContext cc = sc.getCurrentContext(context.getItem());
     1099          Set<Include> include = cc.getInclude();
     1100          parameters.add(new PluginParameter<Boolean>(
     1101              "includeMine",
     1102              "Owned by me",
     1103              "Search items that are owned by yourself.",
     1104              include.contains(Include.MINE) ? Boolean.TRUE : null,
     1105              optionalBooleanType)
     1106            );
     1107          parameters.add(new PluginParameter<Boolean>(
     1108              "includeShared",
     1109              "Shared to me",
     1110              "Search items that have been shared to you by other users.",
     1111              include.contains(Include.SHARED) ? Boolean.TRUE : null,
     1112              optionalBooleanType)
     1113            );
     1114           
     1115          if (sc.getActiveProjectId() != 0)
     1116          {
     1117            parameters.add(new PluginParameter<Boolean>(
     1118                "includeInProject",
     1119                "In current project",
     1120                "Search items that are shared to the current project.",
     1121                include.contains(Include.IN_PROJECT) ? Boolean.TRUE : null,
     1122                optionalBooleanType)
     1123              );
     1124          }
     1125          if (sc.hasPermission(Permission.READ, context.getItem()))
     1126          {
     1127            parameters.add(new PluginParameter<Boolean>(
     1128                "includeOthers",
     1129                "Owned by others",
     1130                "Search items that are owned by other users.",
     1131                include.contains(Include.OTHERS) ? Boolean.TRUE : null,
     1132                optionalBooleanType)
     1133              );
     1134          }
     1135        }
    10721136       
    1073         parameters.add(itemTypesParameter);
    1074       }
    1075 
    1076       // Parser regular expressions
    1077       parameters.add(parserSection);
    1078       parameters.add(dataHeaderRegexpParameter);
    1079       parameters.add(dataSplitterRegexpParameter);
    1080       parameters.add(trimQuotesParameter);
    1081       parameters.add(ignoreRegexpParameter);
    1082       parameters.add(dataFooterRegexpParameter);
    1083       parameters.add(minDataColumnsParameter);
    1084       parameters.add(maxDataColumnsParameter);
    1085       parameters.add(Parameters.charsetParameter(null, null, getCharset()));
    1086       parameters.add(Parameters.decimalSeparatorParameter(null, null, getDecimalSeparator()));
    1087       parameters.add(Parameters.dateFormatParameter(null, null, sc.getUserClientSetting("formatter.date.formatstring")));
    1088       parameters.add(Parameters.timestampFormatParameter(null, null, sc.getUserClientSetting("formatter.datetime.formatstring")));
    1089      
    1090       // Mappings for Name and External ID
    1091       parameters.add(mappingSection);
    1092       parameters.add(complexMappings);
    1093       parameters.add(nameColumnMapping);
    1094       if (context == null || hasExternalId(context.getItem()))
    1095       {
    1096         parameters.add(externalIdColumnMapping);
    1097       }
    1098       parameters.add(internalIdColumnMapping);
    1099      
    1100       if (forJob)
    1101       {
    1102         configureParser = new RequestInformation
    1103         (
    1104           Request.COMMAND_CONFIGURE_JOB,
    1105           "Select a file to import annotations from",
    1106           "Here you select which file to import annotations from, and the " +
    1107           "regular expressions and other settings used to parse it. Step 1 of 3",
    1108           parameters
    1109         );
    1110       }
    1111       else
    1112       {
    1113         configureParser = new RequestInformation
    1114         (
    1115           Request.COMMAND_CONFIGURE_PLUGIN,
    1116           "Specify parser regular expressions and settings",
    1117           "This plug-in can be configured with the regular expressions and " +
    1118           "settings used to parse files. Mapping file columns to annotation types must " +
    1119           "be done for each import.",
    1120           parameters
    1121         );       
     1137        // Parser regular expressions
     1138        parameters.add(parserSection);
     1139        parameters.add(dataHeaderRegexpParameter);
     1140        parameters.add(dataSplitterRegexpParameter);
     1141        parameters.add(trimQuotesParameter);
     1142        parameters.add(ignoreRegexpParameter);
     1143        parameters.add(dataFooterRegexpParameter);
     1144        parameters.add(minDataColumnsParameter);
     1145        parameters.add(maxDataColumnsParameter);
     1146        parameters.add(Parameters.charsetParameter(null, null, getCharset()));
     1147        parameters.add(Parameters.decimalSeparatorParameter(null, null, getDecimalSeparator()));
     1148        parameters.add(Parameters.dateFormatParameter(null, null, sc.getUserClientSetting("formatter.date.formatstring")));
     1149        parameters.add(Parameters.timestampFormatParameter(null, null, sc.getUserClientSetting("formatter.datetime.formatstring")));
     1150       
     1151        // Mappings for Name and External ID
     1152        parameters.add(mappingSection);
     1153        parameters.add(complexMappings);
     1154        parameters.add(nameColumnMapping);
     1155        parameters.add(internalIdColumnMapping);
     1156        if (context == null || hasExternalId(context.getItem()))
     1157        {
     1158          parameters.add(externalIdColumnMapping);
     1159        }
     1160        if (hasAnnotationIdMethod)
     1161        {
     1162          parameters.add(idAnnotationColumnMapping);
     1163        }
     1164       
     1165        if (forJob)
     1166        {
     1167          configureParser = new RequestInformation
     1168          (
     1169            Request.COMMAND_CONFIGURE_JOB,
     1170            "Select a file to import annotations from",
     1171            "Here you select which file to import annotations from, and the " +
     1172            "regular expressions and other settings used to parse it. Step 1 of 3",
     1173            parameters
     1174          );
     1175        }
     1176        else
     1177        {
     1178          configureParser = new RequestInformation
     1179          (
     1180            Request.COMMAND_CONFIGURE_PLUGIN,
     1181            "Specify parser regular expressions and settings",
     1182            "This plug-in can be configured with the regular expressions and " +
     1183            "settings used to parse files. Mapping file columns to annotation types must " +
     1184            "be done for each import.",
     1185            parameters
     1186          );       
     1187        }
     1188      }
     1189      finally
     1190      {
     1191        if (dc != null) dc.close();
    11221192      }
    11231193    }
     
    11931263      List<PluginParameter<?>> parameters = new ArrayList<PluginParameter<?>>();
    11941264 
    1195       ItemContext cc = sc.getCurrentContext(context.getItem());
    1196       Set<Include> include = cc.getInclude();
    1197      
    1198       // Should we search in MINE, SHARED, IN_PROJECT, etc.
    1199       parameters.add(findItemsSection);
    1200       parameters.add(new PluginParameter<Boolean>(
    1201           "includeMine",
    1202           "Owned by me",
    1203           "Search items that are owned by yourself.",
    1204           include.contains(Include.MINE) ? Boolean.TRUE : null,
    1205           optionalBooleanType)
    1206         );
    1207       parameters.add(new PluginParameter<Boolean>(
    1208           "includeShared",
    1209           "Shared to me",
    1210           "Search items that have been shared to you by other users.",
    1211           include.contains(Include.SHARED) ? Boolean.TRUE : null,
    1212           optionalBooleanType)
    1213         );
    1214        
    1215       if (sc.getActiveProjectId() != 0)
    1216       {
    1217         parameters.add(new PluginParameter<Boolean>(
    1218             "includeInProject",
    1219             "In current project",
    1220             "Search items that are shared to the current project.",
    1221             include.contains(Include.IN_PROJECT) ? Boolean.TRUE : null,
    1222             optionalBooleanType)
    1223           );
    1224       }
    1225       if (sc.hasPermission(Permission.READ, context.getItem()))
    1226       {
    1227         parameters.add(new PluginParameter<Boolean>(
    1228             "includeOthers",
    1229             "Owned by others",
    1230             "Search items that are owned by other users.",
    1231             include.contains(Include.OTHERS) ? Boolean.TRUE : null,
    1232             optionalBooleanType)
    1233           );
    1234       }
    1235 
    12361265      // Update/add/replace options
    12371266      parameters.add(addReplaceSection);
     
    12811310  }
    12821311 
     1312  private List<IdMethod> getIdMethods(DbControl dc, Item item)
     1313  {
     1314    List<IdMethod> methods = new ArrayList<>();
     1315    methods.add(PropertyIdMethod.NAME);
     1316    methods.add(InternalIdMethod.INTERNAL_ID);
     1317    if (item == null || hasExternalId(item)) methods.add(PropertyIdMethod.EXTERNAL_ID);
     1318    methods.addAll(AnnotationIdMethod.getIdMethods(dc, item));
     1319    return methods;
     1320  }
     1321 
     1322  /**
     1323
     1324  */
     1325  protected IdMethod getIdMethod(DbControl dc, Item item, String method)
     1326  {
     1327    for (IdMethod im : getIdMethods(dc, item))
     1328    {
     1329      if (im.getMethod().equals(method))
     1330      {
     1331        return im;
     1332      }
     1333    }
     1334    return PropertyIdMethod.NAME;
     1335  }
     1336
    12831337  /**
    12841338    Create a query that return items of the specified type. We use reflection
    12851339    to call the static method 'getQuery' on the item's item class, for
    1286     example {@link net.sf.basedb.core.Sample#getQuery()}. We add a restriction
    1287     to either the 'name' or the 'externalId' property but the parameter is
    1288     always called 'name' to make it easier to implement the code that is
    1289     using the query.
     1340    example {@link net.sf.basedb.core.Sample#getQuery()}.
    12901341   
    12911342    @param itemType The type of items to search
    1292     @param searchProperty If we should search by name, external ID or internal ID
    1293     @param includes Include options passed to {@link ItemQuery#include(java.util.Collection)}
    12941343    @return A query that searches items of the specified type.
    12951344  */
    1296   private ItemQuery<?> createQuery(Item itemType, String searchProperty, Set<Include> includes)
    1297   {
    1298     // Try to create an ItemQuery through reflection
    1299     // We assume that the item class has a static method: getQuery()
    1300     Class<?> itemClass = itemType.getItemClass();
    1301     Method queryMethod = null;
    1302     ItemQuery<?> query = null;
    1303     try
    1304     {
    1305       queryMethod = itemClass.getMethod("getQuery");
    1306     }
    1307     catch (NoSuchMethodException ex)
    1308     {
    1309       throw new BaseException("Can't find method: " + itemType + ".getQuery()", ex);
    1310     }
    1311     try
    1312     {
    1313       query = (ItemQuery<?>)queryMethod.invoke(null);
    1314     }
    1315     catch (Exception ex)
    1316     {
    1317       throw new BaseException("Can't invoke method: " + itemType + ".getQuery()", ex);
    1318     }
    1319 
    1320     // Search on name, external ID or internal ID
    1321     // To make it easier set values, the parameter is always called 'nameOrId'
    1322     query.restrictPermanent(
    1323       Restrictions.eq(
    1324         Hql.property(searchProperty),
    1325         Expressions.parameter("nameOrId")
    1326       )
    1327     );
    1328    
    1329     // Include options
    1330     query.include(includes);
    1331     return query;
     1345  private ItemQuery<?> createQuery(Item itemType)
     1346  {
     1347    return itemType.getQuery();
    13321348  }
    13331349 
  • trunk/src/plugins/core/net/sf/basedb/plugins/batchimport/AbstractItemImporter.java

    r7590 r7591  
    3434import java.util.regex.Pattern;
    3535
    36 import net.sf.basedb.core.AnnotationType;
    3736import net.sf.basedb.core.AnnotationTypeCategory;
    3837import net.sf.basedb.core.ArrayBatch;
     
    106105import net.sf.basedb.core.query.Expressions;
    107106import net.sf.basedb.core.query.Hql;
    108 import net.sf.basedb.core.query.Orders;
    109107import net.sf.basedb.core.query.Restrictions;
    110108import net.sf.basedb.plugins.AbstractFlatFileImporter;
     
    925923    if (itemType != null)
    926924    {
    927       ItemQuery<AnnotationType> query = AnnotationType.getQuery(itemType);
    928       query.include(Include.MINE, Include.IN_PROJECT, Include.SHARED);
    929       // The annotation type must be STRING, INTEGER or LONG
    930       query.restrict(Restrictions.in(Hql.property("valueType"), Expressions.parameter("valueTypes", Type.INT)));
    931       query.setParameter("valueTypes", Arrays.asList(Type.STRING.getValue(), Type.INT.getValue(), Type.LONG.getValue()), Type.INT);
    932       // Enumerations can't be used
    933       query.restrict(Restrictions.eq(Hql.property("enumeration"), Expressions.bool(false)));
    934      
    935       query.order(Orders.asc(Hql.property("name")));
    936       // Get a list of possible annotation types
    937       List<AnnotationType> annotationTypes = new ArrayList<AnnotationType>(query.list(dc));
    938    
    939       for (AnnotationType at : annotationTypes)
    940       {
    941         idMethods.add(new AnnotationIdMethod(at));
    942       }
     925      idMethods = AnnotationIdMethod.getIdMethods(dc, itemType);
    943926    }
    944927    return idMethods;
  • trunk/src/plugins/core/net/sf/basedb/plugins/batchimport/AnnotationIdMethod.java

    r7590 r7591  
    2222package net.sf.basedb.plugins.batchimport;
    2323
     24import java.util.ArrayList;
     25import java.util.Arrays;
    2426import java.util.List;
    2527
     
    2931import net.sf.basedb.core.BasicItem;
    3032import net.sf.basedb.core.DbControl;
     33import net.sf.basedb.core.Include;
     34import net.sf.basedb.core.Item;
    3135import net.sf.basedb.core.ItemQuery;
    3236import net.sf.basedb.core.Type;
     
    3438import net.sf.basedb.core.query.Expressions;
    3539import net.sf.basedb.core.query.Hql;
     40import net.sf.basedb.core.query.Orders;
    3641import net.sf.basedb.core.query.Restrictions;
    3742
     
    4752{
    4853
     54  /**
     55    Get identification methods that are based on annotations. The default implementation
     56    first calls the {@link #getItemForAnnotationTypes()}. If this returns a non-null value it will
     57    make a query for all annotation types that are suitable as identifiers:
     58   
     59     * The value type must be one of STRING, INT or LONG
     60     * It must not be an enumeration
     61    @param itemType Either a specified item or null to return all possible annotation id methods
     62  */
     63  public static List<IdMethod> getIdMethods(DbControl dc, Item itemType)
     64  {
     65    ItemQuery<AnnotationType> query = AnnotationType.getQuery(itemType);
     66    query.include(Include.MINE, Include.IN_PROJECT, Include.SHARED);
     67    // The annotation type must be STRING, INTEGER or LONG
     68    query.restrict(Restrictions.in(Hql.property("valueType"), Expressions.parameter("valueTypes", Type.INT)));
     69    query.setParameter("valueTypes", Arrays.asList(Type.STRING.getValue(), Type.INT.getValue(), Type.LONG.getValue()), Type.INT);
     70    // Enumerations can't be used
     71    query.restrict(Restrictions.eq(Hql.property("enumeration"), Expressions.bool(false)));
     72   
     73    query.order(Orders.asc(Hql.property("name")));
     74    // Get a list of possible annotation types
     75    List<AnnotationType> annotationTypes = new ArrayList<AnnotationType>(query.list(dc));
     76 
     77    List<IdMethod> idMethods = new ArrayList<>();
     78    for (AnnotationType at : annotationTypes)
     79    {
     80      idMethods.add(new AnnotationIdMethod(at));
     81    }
     82    return idMethods;
     83  }
     84
     85 
    4986  private final AnnotationType annotationType;
    5087  /**
Note: See TracChangeset for help on using the changeset viewer.