/** {@inheritDoc} */
  @Override
  public ActionForward execute(
      ComponentContext context,
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    HttpSession session = request.getSession();
    final InterMineAPI im = SessionMethods.getInterMineAPI(session);

    InterMineBag imBag = (InterMineBag) request.getAttribute("bag");
    WebConfig webConfig = SessionMethods.getWebConfig(request);
    Model model = im.getModel();
    TemplateManager templateManager = im.getTemplateManager();

    Map<Class, ApiTemplate> conversionTypesMap =
        TypeConverter.getConversionTemplates(
            templateManager.getConversionTemplates(),
            TypeUtil.instantiate(model.getPackageName() + "." + imBag.getType()));
    ArrayList<String> conversionTypes = new ArrayList<String>();
    Map<Type, Boolean> fastaMap = new HashMap<Type, Boolean>();
    for (Class<?> clazz : conversionTypesMap.keySet()) {
      conversionTypes.add(TypeUtil.unqualifiedName(clazz.getName()));
      Type type = webConfig.getTypes().get(clazz.getName());
      FieldConfig fieldConfig = type.getFieldConfigMap().get("length");
      if (fieldConfig != null && fieldConfig.getDisplayer() != null) {
        fastaMap.put(type, Boolean.TRUE);
      } else {
        fastaMap.put(type, Boolean.FALSE);
      }
    }
    // Use custom converters
    BagQueryConfig bagQueryConfig = im.getBagQueryConfig();
    String bagType = imBag.getType();
    Set<AdditionalConverter> additionalConverters = bagQueryConfig.getAdditionalConverters(bagType);
    request.setAttribute("customConverters", additionalConverters);
    request.setAttribute("conversionTypes", conversionTypes);
    request.setAttribute("fastaMap", fastaMap);
    return null;
  }
  /**
   * 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 {
    HttpSession session = request.getSession();
    final InterMineAPI im = SessionMethods.getInterMineAPI(session);
    Profile profile = SessionMethods.getProfile(session);

    Model model = im.getModel();
    ModifyBagDetailsForm mbdf = (ModifyBagDetailsForm) form;
    BagManager bagManager = im.getBagManager();

    InterMineBag imBag = bagManager.getBag(profile, mbdf.getBagName());
    String bagIdentifier = "bag." + imBag.getName();

    if (request.getParameter("removeFromBag") != null) {
      PagedTable pc = SessionMethods.getResultsTable(session, bagIdentifier);
      String msg = "";

      if (pc.isAllRowsSelected()) {
        // TODO these messages need to be moved to properties file
        msg = "You can't remove all items from your list.  Try deleting your list instead.";
      } else {
        int removed = pc.removeSelectedFromBag(imBag, session);
        msg = "You have removed " + removed + " items from your list.";
      }
      SessionMethods.recordMessage(msg, session);
      // return new ForwardParameters(mapping.findForward("bagDetails"))
      // .addParameter("bagName", mbdf.getBagName()).forward();

      // pass an extra parameter telling the JSP to open up the results table
      return new ForwardParameters(mapping.findForward("bagDetails"))
          .addParameter("bagName", mbdf.getBagName())
          .addParameter("table", "open")
          .forward();

    } else if (request.getParameter("addToBag") != null) {
      InterMineBag newBag = bagManager.getBag(profile, mbdf.getExistingBagName());
      String msg = "";
      if (newBag.getType().equals(imBag.getType())) {
        PagedTable pc = SessionMethods.getResultsTable(session, bagIdentifier);
        int oldSize = newBag.size();
        pc.addSelectedToBag(newBag);
        int newSize = newBag.size();
        int added = newSize - oldSize;
        msg =
            "You have added "
                + added
                + " items from list <strong>"
                + imBag.getName()
                + "</strong> to list <strong>"
                + newBag.getName()
                + "</strong>";
      } else {
        msg = "You can only add objects to other lists of the same type";
      }
      SessionMethods.recordMessage(msg, session);
      // orthologues form
    } else if (request.getParameter("convertToThing") != null) {
      BagQueryConfig bagQueryConfig = im.getBagQueryConfig();
      Set<AdditionalConverter> additionalConverters =
          bagQueryConfig.getAdditionalConverters(imBag.getType());
      if (additionalConverters != null && !additionalConverters.isEmpty()) {
        for (AdditionalConverter additionalConverter : additionalConverters) {
          BagConverter bagConverter =
              PortalHelper.getBagConverter(
                  im, SessionMethods.getWebConfig(request), additionalConverter.getClassName());
          List<Integer> converted =
              bagConverter.getConvertedObjectIds(
                  profile, imBag.getType(), imBag.getContentsAsIds(), mbdf.getExtraFieldValue());

          if (converted.size() == 1) {
            return goToReport(mapping, converted.get(0).toString());
          }

          String bagName =
              NameUtil.generateNewName(
                  profile.getSavedBags().keySet(),
                  mbdf.getExtraFieldValue() + " orthologues of " + imBag.getName());

          InterMineBag newBag = profile.createBag(bagName, imBag.getType(), "", im.getClassKeys());
          return createBagAndGoToBagDetails(mapping, newBag, converted);
        }
      }

      // "use in bag" link
    } else if (request.getParameter("useBag") != null) {
      PagedTable pc = SessionMethods.getResultsTable(session, bagIdentifier);
      PathQuery pathQuery = pc.getWebTable().getPathQuery().clone();
      SessionMethods.setQuery(session, pathQuery);
      session.setAttribute("path", imBag.getType());
      session.setAttribute("prefix", imBag.getType());
      String msg = "You can now create a query using your list " + imBag.getName();
      SessionMethods.recordMessage(msg, session);
      return mapping.findForward("query");

      // convert links
    } else if (request.getParameter("convert") != null && request.getParameter("bagName") != null) {
      String type2 = request.getParameter("convert");
      TemplateManager templateManager = im.getTemplateManager();
      PathQuery q =
          BagConversionHelper.getConvertedObjects(
              session,
              templateManager.getConversionTemplates(),
              TypeUtil.instantiate(model.getPackageName() + "." + imBag.getType()),
              TypeUtil.instantiate(model.getPackageName() + "." + type2),
              imBag);
      q.setTitle(type2 + "s from list '" + imBag.getName() + "'");
      SessionMethods.loadQuery(q, session, response);
      String qid = SessionMethods.startQueryWithTimeout(request, false, q);
      Thread.sleep(200); // slight pause in the hope of avoiding holding page
      final String trail = "|bag." + imBag.getName();
      return new ForwardParameters(mapping.findForward("waiting"))
          .addParameter("trail", trail)
          .addParameter("qid", qid)
          .forward();
    }
    return new ForwardParameters(mapping.findForward("bagDetails"))
        .addParameter("bagName", mbdf.getBagName())
        .forward();
  }
  /** {@inheritDoc} */
  @SuppressWarnings("unused")
  @Override
  public ActionForward execute(
      @SuppressWarnings("unused") ActionMapping mapping,
      @SuppressWarnings("unused") ActionForm form,
      HttpServletRequest request,
      @SuppressWarnings("unused") HttpServletResponse response)
      throws Exception {

    long startTime = System.currentTimeMillis();

    HttpSession session = request.getSession();
    InterMineAPI im = SessionMethods.getInterMineAPI(session);

    if (im.getBagManager().isAnyBagToUpgrade(SessionMethods.getProfile(session))) {
      recordError(new ActionMessage("login.upgradeListManually"), request);
    }
    // fetch & set requested object
    InterMineObject requestedObject = getRequestedObject(im, request);

    if (requestedObject != null) {
      ReportObjectFactory reportObjectFactory = SessionMethods.getReportObjects(session);
      ReportObject reportObject = reportObjectFactory.get(requestedObject);

      request.setAttribute("object", reportObject);
      request.setAttribute("reportObject", reportObject);

      request.setAttribute("requestedObject", requestedObject);

      // hell starts here
      TagManager tagManager = im.getTagManager();
      ServletContext servletContext = session.getServletContext();
      ObjectStore os = im.getObjectStore();
      String superuser = im.getProfileManager().getSuperuser();
      if (superuser.equals(SessionMethods.getProfile(session).getUsername())) {
        request.setAttribute("SHOW_TAGS", true);
      }
      // place InlineLists based on TagManager, reportObject is cached while Controller is not
      Map<String, List<InlineList>> placedInlineLists = new TreeMap<String, List<InlineList>>();
      // traverse all unplaced (non-header) InlineLists
      for (InlineList list : reportObject.getNormalInlineLists()) {
        FieldDescriptor fd = list.getDescriptor();
        String taggedType = getTaggedType(fd);

        // assign lists to any aspects they are tagged to or put in unplaced lists
        String fieldPath = fd.getClassDescriptor().getUnqualifiedName() + "." + fd.getName();
        List<Tag> tags = tagManager.getTags(null, fieldPath, taggedType, superuser);
        for (Tag tag : tags) {
          String tagName = tag.getTagName();
          if (AspectTagUtil.isAspectTag(tagName)) {
            List<InlineList> listsForAspect = placedInlineLists.get(tagName);
            if (listsForAspect == null) {
              listsForAspect = new ArrayList<InlineList>();
              placedInlineLists.put(tagName, listsForAspect);
            }
            listsForAspect.add(list);
          } else if (tagName.equals(TagNames.IM_SUMMARY)) {
            List<InlineList> summaryLists = placedInlineLists.get(tagName);
            if (summaryLists == null) {
              summaryLists = new ArrayList<InlineList>();
              placedInlineLists.put(tagName, summaryLists);
            }
            summaryLists.add(list);
          }
        }
      }

      // any lists that aren't tagged will be 'unplaced'
      List<InlineList> unplacedInlineLists =
          new ArrayList<InlineList>(reportObject.getNormalInlineLists());
      unplacedInlineLists.removeAll(placedInlineLists.values());

      long now = System.currentTimeMillis();
      LOG.info("TIME placed inline lists: " + (now - startTime) + "ms");
      long stepTime = now;

      request.setAttribute("mapOfInlineLists", placedInlineLists);
      request.setAttribute("listOfUnplacedInlineLists", unplacedInlineLists);

      Map<String, Map<String, DisplayField>> placementRefsAndCollections =
          new TreeMap<String, Map<String, DisplayField>>();
      Set<String> aspects = new LinkedHashSet<String>(SessionMethods.getCategories(servletContext));

      Set<ClassDescriptor> cds =
          os.getModel().getClassDescriptorsForClass(requestedObject.getClass());

      for (String aspect : aspects) {
        placementRefsAndCollections.put(
            TagNames.IM_ASPECT_PREFIX + aspect,
            new TreeMap<String, DisplayField>(String.CASE_INSENSITIVE_ORDER));
      }

      Map<String, DisplayField> miscRefs =
          new TreeMap<String, DisplayField>(reportObject.getRefsAndCollections());
      placementRefsAndCollections.put(TagNames.IM_ASPECT_MISC, miscRefs);

      // summary refs and colls
      Map<String, DisplayField> summaryRefsCols = new TreeMap<String, DisplayField>();
      placementRefsAndCollections.put(TagNames.IM_SUMMARY, summaryRefsCols);

      for (Iterator<Entry<String, DisplayField>> iter =
              reportObject.getRefsAndCollections().entrySet().iterator();
          iter.hasNext(); ) {
        Map.Entry<String, DisplayField> entry = iter.next();
        DisplayField df = entry.getValue();
        if (df instanceof DisplayReference) {
          categoriseBasedOnTags(
              ((DisplayReference) df).getDescriptor(),
              "reference",
              df,
              miscRefs,
              tagManager,
              superuser,
              placementRefsAndCollections,
              SessionMethods.isSuperUser(session));
        } else if (df instanceof DisplayCollection) {
          categoriseBasedOnTags(
              ((DisplayCollection) df).getDescriptor(),
              "collection",
              df,
              miscRefs,
              tagManager,
              superuser,
              placementRefsAndCollections,
              SessionMethods.isSuperUser(session));
        }
      }

      // remove any fields overridden by displayers
      removeFieldsReplacedByReportDisplayers(reportObject, placementRefsAndCollections);
      request.setAttribute("placementRefsAndCollections", placementRefsAndCollections);

      String type = reportObject.getType();
      request.setAttribute("objectType", type);

      String stableLink = PortalHelper.generatePortalLink(reportObject.getObject(), im, request);
      if (stableLink != null) {
        request.setAttribute("stableLink", stableLink);
      }

      stepTime = System.currentTimeMillis();
      startTime = stepTime;

      // attach only non empty categories
      Set<String> allClasses = new HashSet<String>();
      for (ClassDescriptor cld : cds) {
        allClasses.add(cld.getUnqualifiedName());
      }
      TemplateManager templateManager = im.getTemplateManager();
      Map<String, List<ReportDisplayer>> displayerMap = reportObject.getReportDisplayers();

      stepTime = System.currentTimeMillis();
      startTime = stepTime;

      List<String> categories = new LinkedList<String>();
      for (String aspect : aspects) {
        // 1) Displayers
        // 2) Inline Lists
        if ((displayerMap != null && displayerMap.containsKey(aspect))
            || placedInlineLists.containsKey(aspect)) {
          categories.add(aspect);
        } else {
          // 3) Templates
          if (!templateManager.getReportPageTemplatesForAspect(aspect, allClasses).isEmpty()) {
            categories.add(aspect);
          } else {
            // 4) References & Collections
            if (placementRefsAndCollections.containsKey("im:aspect:" + aspect)
                && placementRefsAndCollections.get("im:aspect:" + aspect) != null) {
              for (DisplayField df :
                  placementRefsAndCollections.get("im:aspect:" + aspect).values()) {
                categories.add(aspect);
                break;
              }
            }
          }
        }
      }
      if (!categories.isEmpty()) {
        request.setAttribute("categories", categories);
      }
      now = System.currentTimeMillis();
      LOG.info("TIME made list of categories: " + (now - stepTime) + "ms");
    }

    return null;
  }