/** {@inheritDoc} */
  @Override
  public ActionForward execute(
      @SuppressWarnings("unused") ComponentContext context,
      @SuppressWarnings("unused") ActionMapping mapping,
      @SuppressWarnings("unused") ActionForm form,
      HttpServletRequest request,
      @SuppressWarnings("unused") HttpServletResponse response)
      throws Exception {
    HttpSession session = request.getSession();

    Profile profile = SessionMethods.getProfile(session);
    PathQuery query = SessionMethods.getQuery(session);
    DisplayConstraintFactory factory = getFactory(session);

    if (session.getAttribute("newConstraintPath") != null) {
      // ADDING A NEW CONSTRAINT
      DisplayPath displayPath = (DisplayPath) session.getAttribute("newConstraintPath");
      DisplayConstraint displayConstraint = factory.get(displayPath.getPath(), profile, query);
      request.setAttribute("dec", displayConstraint);
      session.removeAttribute("newConstraintPath");
      saveToken(request);
    } else if (session.getAttribute("editingConstraint") != null) {
      // EDITING AN EXISTING CONSTRAINT
      PathConstraint con = (PathConstraint) session.getAttribute("editingConstraint");
      DisplayConstraint displayConstraint;
      if (query instanceof TemplateQuery) {
        TemplateQuery template = (TemplateQuery) query;
        displayConstraint = factory.get(con, profile, template);
      } else {
        displayConstraint = factory.get(con, profile, query);
      }

      request.setAttribute("dec", displayConstraint);
      session.removeAttribute("editingConstraint");
      if (session.getAttribute("editingTemplateConstraint") != null) {
        SessionMethods.moveToRequest("editingTemplateConstraint", request);
      }
      saveToken(request);
    } else if (session.getAttribute("joinStylePath") != null) {
      // ONLY EDITING JOIN STYLE
      String joinStylePathStr = (String) session.getAttribute("joinStylePath");
      Path joinStylePath = query.makePath(joinStylePathStr);
      DisplayConstraint displayConstraint = factory.get(joinStylePath, profile, query);
      session.removeAttribute("joinStylePath");

      if (query.getOuterMap().containsKey(joinStylePathStr)) {
        request.setAttribute("joinType", "outer");
      } else {
        request.setAttribute("joinType", "inner");
      }
      request.setAttribute("dec", displayConstraint);
      request.setAttribute("joinStyleOnly", "true");
      saveToken(request);
    }

    return null;
  }
  /**
   * Either display the query builder or redirect to project.sitePrefix.
   *
   * @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();
    PathQuery query = SessionMethods.getQuery(session);
    boolean showTemplate = (request.getParameter("showTemplate") != null);

    if (query == null) {
      return new ForwardParameters(
              getWebProperties(request).getProperty("project.sitePrefix"), true)
          .forward();
    }

    if (query instanceof TemplateQuery && showTemplate) {
      TemplateQuery template = (TemplateQuery) query;
      Profile profile = SessionMethods.getProfile(session);
      String temporaryName = null;
      for (SavedQuery sq : profile.getHistory().values()) {
        if (sq.getPathQuery() instanceof TemplateQuery
            && ((TemplateQuery) sq.getPathQuery()).getName().equals(template.getName())) {
          temporaryName = sq.getName();
        }
      }
      if (temporaryName != null) {
        return new ForwardParameters(mapping.findForward("template"))
            .addParameter("loadModifiedTemplate", "true")
            .addParameter("name", template.getName())
            .addParameter("savedQueryName", temporaryName)
            .forward();
      }
      return new ForwardParameters(mapping.findForward("template"))
          .addParameter("name", template.getName())
          .forward();
    }
    if (!(query instanceof TemplateQuery)) {
      session.removeAttribute(Constants.EDITING_TEMPLATE);
      session.removeAttribute(Constants.NEW_TEMPLATE);
    }
    return mapping.findForward("query");
  }
  /**
   * Method called to export a saved Query.
   *
   * @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);

    String type = request.getParameter("type");
    String name = request.getParameter("name");

    PathQuery query = null;

    if (StringUtils.isEmpty(type) || StringUtils.isEmpty(name)) {
      query = SessionMethods.getQuery(session);
    } else if ("history".equals(type)) {
      SavedQuery sq = profile.getHistory().get(name);

      if (sq == null) {
        recordError(new ActionMessage("errors.query.missing", name), request);
        return mapping.findForward("mymine");
      }

      query = sq.getPathQuery();
    } else if ("saved".equals(type)) {
      SavedQuery sq = profile.getSavedQueries().get(name);

      if (sq == null) {
        recordError(new ActionMessage("errors.query.missing", name), request);
        return mapping.findForward("mymine");
      }

      query = sq.getPathQuery();
    } else {
      LOG.error("Bad type parameter: " + type);
      return null;
    }

    if (query == null) {
      LOG.error("Failed to find query " + name + " of type " + type);
      return null;
    }

    if (query.getView().size() == 0) {
      response.getWriter().write("Invalid query. No fields selected for output.");
      return null;
    }

    response.setContentType("text/plain; charset=utf-8");
    WebResultsExecutor webResultsExecutor = im.getWebResultsExecutor(profile);

    String format;
    if (!StringUtils.isEmpty(request.getParameter("as"))) {
      format = request.getParameter("as").toLowerCase();
    } else {
      format = "xml";
    }
    if ("xml".equals(format)) {
      String xml = getQueryXML(name, query);
      xml = XmlUtil.indentXmlSimple(xml);
      response.getWriter().write(xml);
    } else if ("iql".equals(format)) {
      Query osQuery = webResultsExecutor.makeQuery(query);
      response.getWriter().println(osQuery.toString());
    } else if ("sql".equals(format)) {
      response.getWriter().println(webResultsExecutor.makeSql(query));
    } else if ("link".equals(format)) {
      String serviceFormat;
      if (request.getParameter("serviceFormat") != null) {
        serviceFormat = request.getParameter("serviceFormat");
      } else {
        serviceFormat = "tab";
      }
      String xml = getQueryXML(name, query);
      String link =
          new QueryResultLinkGenerator()
              .getLink(new URLGenerator(request).getPermanentBaseURL(), xml, serviceFormat);
      response.getWriter().write(link);
    } else {
      response.getWriter().println("Unknown export type: " + request.getParameter("as"));
    }

    return null;
  }