private void delete(ActionForm form, HttpServletRequest request) throws Exception {
   HttpSession session = request.getSession();
   Profile profile = SessionMethods.getProfile(session);
   ModifyBagForm mbf = (ModifyBagForm) form;
   for (int i = 0; i < mbf.getSelectedBags().length; i++) {
     String bagName = mbf.getSelectedBags()[i];
     InterMineBag bag = profile.getSavedBags().get(bagName);
     if (bag != null) {
       deleteQueriesThatMentionBag(profile, bag.getName());
       deleteBag(session, profile, bag);
     } else {
       bag = profile.getSharedBags().get(bagName);
       if (bag == null) {
         LOG.error("Asked to delete a bag this user does not have access to.");
       } else {
         unshareBag(session, profile, bag);
       }
     }
   }
 }
  /**
   * Forward to the correct method based on the button pressed
   *
   * @param mapping The ActionMapping used to select this instance
   * @param form The optional ActionForm bean for this request (if any)
   * @param request The HTTP request we are processing
   * @param response The HTTP response we are creating
   * @return an ActionForward object defining where control goes next
   * @exception Exception if the application business logic throws an exception
   */
  @Override
  public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    ModifyBagForm mbf = (ModifyBagForm) form;
    String[] selectedBagNames = mbf.getSelectedBags();

    // This should already be caught by Ajax code
    if (selectedBagNames.length == 0) {
      recordError(new ActionMessage("errors.bag.listnotselected"), request);
      return getReturn(mbf.getPageName(), mapping);
    }

    if (request.getParameter("union") != null
        || (mbf.getListsButton() != null && "union".equals(mbf.getListsButton()))) {
      combine(form, request, BagOperations.UNION);
    } else if (request.getParameter("intersect") != null
        || (mbf.getListsButton() != null && "intersect".equals(mbf.getListsButton()))) {
      combine(form, request, BagOperations.INTERSECT);
    } else if (request.getParameter("subtract") != null
        || (mbf.getListsButton() != null && "subtract".equals(mbf.getListsButton()))) {
      combine(form, request, BagOperations.SUBTRACT);
    } else if (request.getParameter("delete") != null
        || (mbf.getListsButton() != null && "delete".equals(mbf.getListsButton()))) {
      delete(form, request);
    } else if (request.getParameter("copy") != null
        || (mbf.getListsButton() != null && "copy".equals(mbf.getListsButton()))) {
      copy(form, request);
    }

    return getReturn(mbf.getPageName(), mapping);
  }
  private void combine(ActionForm form, HttpServletRequest request, String opText) {
    HttpSession session = request.getSession();
    final InterMineAPI im = SessionMethods.getInterMineAPI(session);
    Profile profile = SessionMethods.getProfile(session);
    ModifyBagForm mbf = (ModifyBagForm) form;

    BagManager bagManager = im.getBagManager();
    Map<String, InterMineBag> allBags = bagManager.getBags(profile);

    String[] selectedBagNames = mbf.getSelectedBags();

    Collection<InterMineBag> selectedBags = getSelectedBags(allBags, selectedBagNames);

    String newBagName = NameUtil.validateName(allBags.keySet(), mbf.getNewBagName());

    int newBagSize = 0;
    try {
      if (opText.equals(BagOperations.UNION)) {
        newBagSize = BagOperations.union(selectedBags, newBagName, profile, im.getClassKeys());
      } else if (opText.equals(BagOperations.INTERSECT)) {
        newBagSize = BagOperations.intersect(selectedBags, newBagName, profile, im.getClassKeys());
      } else if (opText.equals(BagOperations.SUBTRACT)) {
        newBagSize = BagOperations.subtract(selectedBags, newBagName, profile, im.getClassKeys());
      }
    } catch (IncompatibleTypesException e) {
      SessionMethods.recordError(
          "You can only perform operations on lists of the same type."
              + " Lists "
              + StringUtil.prettyList(Arrays.asList(selectedBagNames))
              + " do not match.",
          session);
      return;
    } catch (ObjectStoreException e) {
      LOG.error(e);
      ActionMessage actionMessage = new ActionMessage("An error occurred while saving the list");
      recordError(actionMessage, request);
      return;
    }

    if (newBagSize > 0) {
      SessionMethods.recordMessage(
          "Created list \""
              + newBagName
              + "\" as "
              + opText
              + " of  "
              + StringUtil.prettyList(Arrays.asList(selectedBagNames))
              + ".",
          session);
      // track the list creation
      im.getTrackerDelegate()
          .trackListCreation(
              BagOperations.getCommonBagType(selectedBags),
              newBagSize,
              ListBuildMode.OPERATION,
              profile,
              session.getId());
    } else {
      SessionMethods.recordError(
          opText
              + " operation on lists "
              + StringUtil.prettyList(Arrays.asList(selectedBagNames))
              + " produced no results.",
          session);
    }
  }
  private void copy(ActionForm form, HttpServletRequest request) throws ObjectStoreException {
    HttpSession session = request.getSession();
    final InterMineAPI im = SessionMethods.getInterMineAPI(session);
    Profile profile = SessionMethods.getProfile(session);
    ModifyBagForm frm = (ModifyBagForm) form;
    String[] selectedBagNames = frm.getSelectedBags();

    BagManager bagManager = im.getBagManager();
    Map<String, InterMineBag> allBags = bagManager.getBags(profile);

    String newNameTextBox = getNewNameTextBox(request, frm.getNewBagName());

    if (selectedBagNames.length == 1) {
      String selectedBagName = selectedBagNames[0];
      InterMineBag origBag = allBags.get(selectedBagName);

      if (origBag == null) {
        recordError(new ActionMessage("errors.bag.notfound"), request);
        return;
      }

      String newBagName = "";
      if (newNameTextBox != null) {
        newBagName = NameUtil.validateName(allBags.keySet(), newNameTextBox);
        if (newBagName.isEmpty()) {
          recordError(new ActionMessage("bag.createdlists.notvalidname", newNameTextBox), request);
          return;
        }
      }
      if (newNameTextBox == null) {
        newBagName = NameUtil.generateNewName(allBags.keySet(), selectedBagName);
      }

      if (createBag(origBag, newBagName, profile)) {
        recordMessage(new ActionMessage("bag.createdlists", newBagName), request);
        // track the list creation
        im.getTrackerDelegate()
            .trackListCreation(
                origBag.getType(),
                origBag.getSize(),
                ListBuildMode.OPERATION,
                profile,
                session.getId());
      }
    } else {
      if (newNameTextBox != null) {
        recordError(new ActionMessage("errors.bag.namecannotbespecified"), request);
        return;
      }
      String msg = "";
      for (int i = 0; i < selectedBagNames.length; i++) {

        String selectedBagName = selectedBagNames[i];
        InterMineBag origBag = allBags.get(selectedBagName);

        if (origBag == null) {
          recordError(new ActionMessage("errors.bag.notfound"), request);
          return;
        }

        String newBagName = NameUtil.generateNewName(allBags.keySet(), selectedBagName);
        if (createBag(origBag, newBagName, profile)) {
          msg += newBagName + ", ";
        }
      }
      if (msg.length() > 2) {
        msg = msg.substring(0, msg.length() - 2);
      }
      if (msg.length() > 0) {
        recordMessage(new ActionMessage("bag.createdlists", msg), request);
      }
    }
  }