Ejemplo n.º 1
0
  public void setUp() throws Exception {
    super.setUp();

    userprofileOS.setModel(Model.getInstanceByName("userprofile"));
    Model testmodel = Model.getInstanceByName("testmodel");
    query = new PathQuery(testmodel);

    query.addView("Employee");
    query.addView("Employee.name");
    queryBag = new PathQuery(testmodel);

    queryBag.addView("Employee");
    queryBag.addView("Employee.name");
    queryBag.addConstraint(Constraints.in("Employee", "bag2"));
    sq = new SavedQuery("query1", date, query);
    sqBag = new SavedQuery("query3", date, queryBag);
    hist = new SavedQuery("query2", date, (PathQuery) query.clone());
    hist2 = new SavedQuery("query1", date, query.clone());
    template = new TemplateQuery("template", "ttitle", "tdesc", new PathQuery(testmodel));

    SessionMethods.initSession(this.getSession());
    Profile profile = (Profile) getSession().getAttribute(Constants.PROFILE);
    profile.saveQuery(sq.getName(), sq);
    profile.saveQuery(sqBag.getName(), sqBag);
    profile.saveHistory(hist);
    profile.saveHistory(hist2);
  }
Ejemplo n.º 2
0
  /**
   * 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");
  }
Ejemplo n.º 3
0
  private static void deleteQueriesThatMentionBag(Profile profile, String bagName) {
    // delete query from history
    Map<String, SavedQuery> savedQueries = profile.getHistory();
    Set<String> savedQueriesNames = new HashSet<String>(profile.getHistory().keySet());
    for (String queryName : savedQueriesNames) {
      SavedQuery query = (SavedQuery) savedQueries.get(queryName);
      if (query.getPathQuery().getBagNames().contains(bagName)) {
        profile.deleteHistory(queryName);
      }
    }

    // delete query from saved queries
    savedQueries = profile.getSavedQueries();
    savedQueriesNames = new HashSet<String>(profile.getSavedQueries().keySet());
    for (String queryName : savedQueriesNames) {
      SavedQuery query = (SavedQuery) savedQueries.get(queryName);
      if (query.getPathQuery().getBagNames().contains(bagName)) {
        profile.deleteQuery(queryName);
      }
    }
  }
Ejemplo n.º 4
0
  /**
   * Convert a SavedQuery to XML and write XML to given writer.
   *
   * @param query the SavedQuery
   * @param writer the XMLStreamWriter to write to
   * @param version the version number of the XML format, an attribute of the ProfileManager
   */
  public static void marshal(SavedQuery query, XMLStreamWriter writer, int version) {
    try {
      writer.writeCharacters("\n");
      writer.writeStartElement("saved-query");

      writer.writeAttribute("name", query.getName());
      if (query.getDateCreated() != null) {
        writer.writeAttribute("date-created", "" + query.getDateCreated().getTime());
      }
      PathQueryBinding.marshal(
          query.getPathQuery(),
          query.getName(),
          query.getPathQuery().getModel().getName(),
          writer,
          version);
      writer.writeEndElement();
    } catch (XMLStreamException e) {
      throw new RuntimeException(e);
    }
  }
Ejemplo n.º 5
0
  /**
   * 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;
  }