Changeset 7594


Ignore:
Timestamp:
Feb 20, 2019, 3:41:55 PM (5 years ago)
Author:
Nicklas Nordborg
Message:

References #2150: Batch item importers in item list members view

Started with implementing support in batch item importers. The plug-ins will now detect the active item list and change the mode parameter options to add-members, remove-members and update. So far, only the update mode is working.

Location:
trunk
Files:
3 edited

Legend:

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

    r7593 r7594  
    10291029  private ItemList getItemListFromContext(DbControl dc, GuiContext context)
    10301030  {
     1031    if (context == null) return null;
    10311032    ItemList list = null;
    10321033    String subcontext = context.getSubContext();
  • trunk/src/plugins/core/net/sf/basedb/plugins/batchimport/AbstractItemImporter.java

    r7591 r7594  
    6565import net.sf.basedb.core.ItemContext;
    6666import net.sf.basedb.core.ItemKey;
     67import net.sf.basedb.core.ItemList;
    6768import net.sf.basedb.core.ItemNotFoundException;
    6869import net.sf.basedb.core.ItemQuery;
     
    530531  private ItemSubtype selectedSubtype;
    531532  private ItemQuery<I> itemQuery;
     533  private ItemList itemList;
    532534
    533535  private String lastIdentifier;
     
    588590    this.idMethod = getIdMethod(dc, (String)job.getValue("idMethod"));
    589591    this.idMapping = (String)job.getValue(idMethod.getColumnMappingParameterName());
     592    this.itemList = (ItemList)job.getValue("memberOfList");
    590593    this.itemCache = new HashMap<String, BasicItem>();
    591594    String mode = (String)job.getValue("mode");
    592595    boolean updateMode = mode == null || mode.contains("update");
    593     boolean createMode = mode == null || mode.contains("create");
     596    boolean createMode = (mode == null || mode.contains("create")) && itemList == null;
    594597   
    595598    // Error handling, we don't use the error handling in AbstractFlatFileImporter
    596599    this.dryRun = job.getJob().isDryRun();
    597     this.createNotFoundItems = createMode || "create".equals(getErrorOption("itemNotFoundError"));
     600    this.createNotFoundItems = createMode;
    598601    this.failIfNotFoundItems = !createMode && "fail".equals(getErrorOption("itemNotFoundError"));
    599     this.updateExistingItems = updateMode || "update".equals(getErrorOption("itemExistsError"));
     602    this.updateExistingItems = updateMode;
    600603    this.failIfItemExists = !updateMode && "fail".equals(getErrorOption("itemExistsError"));
    601604    this.failIfMultipleFoundItems = "fail".equals(getErrorOption("multipleItemsFoundError"));
     
    644647    if (Boolean.TRUE.equals((Boolean)job.getValue("includeInProject"))) includes.add(Include.IN_PROJECT);
    645648    if (Boolean.TRUE.equals((Boolean)job.getValue("includeOthers"))) includes.add(Include.OTHERS);
    646     itemQuery = idMethod.prepareQuery(dc, createItemQuery());
     649    itemQuery = idMethod.prepareQuery(dc, createItemQuery(itemList));
    647650    itemQuery.setIncludes(includes);
    648651   
     
    973976  protected void addMoreErrorParameters(List<PluginParameter<?>> parameters)
    974977  {}
     978 
     979  /**
     980    Create a new query that returns items that are members of the given list.
     981    The default implementation calls {@link #createItemQuery()} and then
     982    applies a restriction to it if the list is not null and have the
     983    same member type as the query.
     984    @since 3.15
     985  */
     986  protected ItemQuery<I> createItemQuery(ItemList list)
     987  {
     988    ItemQuery<I> query = createItemQuery();
     989    if (list != null && list.getMemberType() == query.getItemType())
     990    {
     991      query.restrictPermanent(
     992        Hql.restriction("$id=ANY(SELECT mmb FROM ItemListData lst INNER JOIN lst.members mmb WHERE lst.id=" + list.getId()+")", "$")
     993      );
     994    }
     995    return query;
     996  }
    975997 
    976998  /**
     
    23592381  }
    23602382 
     2383  /**
     2384    If the current context is the "Members" tab for an item list we load
     2385    the item list and will later require that all items to be annotated
     2386    are members of that list.
     2387  */
     2388  private ItemList getItemListFromContext(DbControl dc, GuiContext context)
     2389  {
     2390    if (context == null) return null;
     2391    ItemList list = null;
     2392    String subcontext = context.getSubContext();
     2393    if (subcontext != null && subcontext.startsWith("listmembers"))
     2394    {
     2395      // If subcontext is: "listmembers.nnnn" nnn is the id of the item list
     2396      int listId = Values.getInt(subcontext.split("\\.")[1]);
     2397      list = ItemList.getById(dc, listId);
     2398      // Reject lists with different type of member item (should not happen)
     2399      if (list.getMemberType() != context.getItem()) list = null;
     2400    }
     2401    return list;
     2402  }
     2403
    23612404  protected RequestInformation getConfigureParserParameters(GuiContext context, boolean forJob)
    23622405  {
     
    23682411        dc = sc.newDbControl();
    23692412        List<PluginParameter<?>> parameters = new ArrayList<PluginParameter<?>>();
    2370  
     2413        ItemList list = getItemListFromContext(dc, context);
     2414
    23712415        if (forJob)
    23722416        {
     
    23802424        List<String> allowedModes = new ArrayList<String>();
    23812425        String defaultMode = "update";
    2382         if (context == null || sc.hasPermission(Permission.CREATE, context.getItem()))
    2383         {
     2426        if (list != null)
     2427        {
     2428          allowedModes.add(null); // To force the user to manually select a mode (BASE will auto-select the first option)
     2429          allowedModes.add("add-members");
     2430          allowedModes.add("remove-members");
     2431          defaultMode = null;
     2432        }
     2433        else if (context == null || sc.hasPermission(Permission.CREATE, context.getItem()))
     2434        {
     2435          allowedModes.add(null); // To force the user to manually select a mode (BASE will auto-select the first option)
    23842436          allowedModes.add("create+update");
    23852437          allowedModes.add("create");
    2386           defaultMode = "create+update";
     2438          defaultMode = null;
    23872439        }
    23882440        allowedModes.add("update");
     
    23902442            "mode",
    23912443            "Mode",
    2392             "Select an import mode: \n" +
    2393             "create+update = create missing items, update existing items\n" +
    2394             "create = create missing items only\n" +
    2395             "update = update existing items only\n\n" +
    2396             "NOTE! Not all modes may be available due to lack of permissions.",
     2444            "Select an import mode: \n\n" +
     2445            (allowedModes.contains("add-members") ? "<b>add-members</b> = add members to the list (no updates)\n" : "") +
     2446            (allowedModes.contains("remove-members") ? "<b>remove-members</b> = remove members from the list (no updates)\n" : "") +
     2447            (allowedModes.contains("create+update") ? "<b>create+update</b> = create missing items, update existing items\n" : "") +
     2448            (allowedModes.contains("create") ? "<b>create</b> = create missing items only\n" : "") +
     2449            (allowedModes.contains("update") ? "<b>update</b> = update existing items only\n" : ""),
    23972450            new StringParameterType(255, defaultMode, forJob, 1, 0, 0, allowedModes)
    23982451            );
     
    24312484        );
    24322485        parameters.add(idMethodParameter);
     2486       
     2487        if (list != null)
     2488        {
     2489          parameters.add(new PluginParameter<ItemList>(
     2490            "memberOfList", "Member of list",
     2491            "In 'update' mode the plug-in will only update values for items if they are members of this list.",
     2492            new ItemParameterType<>(ItemList.class, list, true, 1, Arrays.asList(list))
     2493          ));
     2494        }
    24332495     
    24342496        if (getItemForSubtypes() != null)
     
    24432505            int selectedSubtypeId = Values.getInt(cc.getPropertyValue("itemSubtype"));
    24442506            ItemSubtype defaultSubtype = selectedSubtypeId == 0 ? null : ItemSubtype.getById(dc, selectedSubtypeId);
     2507            if (defaultSubtype == null && list != null) defaultSubtype = list.getItemSubtype();
    24452508           
    24462509            // Create parameter
     
    25582621
    25592622      String mode = (String)job.getValue("mode");
     2623      ItemList itemList = (ItemList)job.getValue("memberOfList");
    25602624      boolean updateMode = mode == null || mode.contains("update");
    2561       boolean createMode = mode == null || mode.contains("create");
     2625      boolean createMode = (mode == null || mode.contains("create")) && itemList == null;;
    25622626     
    25632627      // Error handling options
     
    25672631      parameters.add(defaultErrorParameter);
    25682632      if (!createMode) parameters.add(itemNotFoundErrorParameter);
    2569       if (!updateMode) parameters.add(itemExistsErrorParameter);
     2633      if (createMode) parameters.add(itemExistsErrorParameter);
    25702634      parameters.add(multipleItemsFoundErrorParameter);
    25712635      if (updateMode) parameters.add(noWritePermissionToItemErrorParameter);
  • trunk/www/common/plugin/configure.jsp

    r7494 r7594  
    537537                      {
    538538                        listValue = HTML.encodeTags(value == null ? "" : value.toString());
    539                         if (enumeration == null) listText = listValue;
     539                        if (enumeration == null) listText = value == null ? "- not specified -" : listValue;
    540540                      }
    541541                      %>
Note: See TracChangeset for help on using the changeset viewer.