/**
   * Return the query specified in the request, shorn of all duplicate classes in the view. Note, it
   * is the users responsibility to ensure that there are only SequenceFeatures in the view.
   *
   * @return A query.
   */
  protected PathQuery getQuery() {
    String xml = getRequiredParameter(XML_PARAM);
    PathQueryBuilder builder = getQueryBuilder(xml);
    PathQuery pq = builder.getQuery();

    List<String> newView = new ArrayList<String>();
    Set<ClassDescriptor> seenTypes = new HashSet<ClassDescriptor>();

    for (String viewPath : pq.getView()) {
      Path p;
      try {
        p = new Path(pq.getModel(), viewPath);
      } catch (PathException e) {
        throw new BadRequestException("Query is invalid", e);
      }
      ClassDescriptor cd = p.getLastClassDescriptor();
      if (!seenTypes.contains(cd)) {
        newView.add(viewPath);
      }
      seenTypes.add(cd);
    }
    if (!newView.equals(pq.getView())) {
      pq.clearView();
      pq.addViews(newView);
    }

    return pq;
  }
 private String getQueryXML(String name, PathQuery query) {
   String modelName = query.getModel().getName();
   return PathQueryBinding.marshal(
       query, (name != null ? name : ""), modelName, PathQuery.USERPROFILE_VERSION);
 }
Beispiel #3
0
  // TODO javadoc to describe what this does
  private MultiRow<ResultsRow<MultiRowValue<ResultElement>>> translateRow(
      MultiRow<ResultsRow<MultiRowValue>> multiRow) {
    try {
      MultiRow<ResultsRow<MultiRowValue<ResultElement>>> retval =
          new MultiRow<ResultsRow<MultiRowValue<ResultElement>>>();
      for (ResultsRow<MultiRowValue> initialList : multiRow) {
        ResultsRow<MultiRowValue<ResultElement>> rowCells =
            new ResultsRow<MultiRowValue<ResultElement>>();
        for (Path columnPath : columnPaths) {
          String columnName = columnPath.toStringNoConstraints();
          Integer columnIndexInteger = pathToIndex.get(columnName);
          String parentColumnName = columnPath.getPrefix().toStringNoConstraints();
          if (columnIndexInteger == null) {
            columnIndexInteger = pathToIndex.get(parentColumnName);
          }

          if (columnIndexInteger == null) {
            throw new NullPointerException(
                "Path: \""
                    + columnName
                    + "\", pathToIndex: \""
                    + pathToIndex
                    + "\", prefix: \""
                    + parentColumnName
                    + "\", query: \""
                    + PathQueryBinding.marshal(
                        pathQuery,
                        "",
                        pathQuery.getModel().getName(),
                        PathQuery.USERPROFILE_VERSION)
                    + "\"");
          }
          int columnIndex = columnIndexInteger.intValue();
          MultiRowValue origO = initialList.get(columnIndex);
          FastPathObject o = (FastPathObject) (origO == null ? null : origO.getValue());
          int rowspan = -1;
          if (origO == null) {
            rowspan = 1;
          } else if (origO instanceof MultiRowFirstValue) {
            rowspan = ((MultiRowFirstValue) origO).getRowspan();
          }
          String lastCd;
          if (columnPath.endIsAttribute()) {
            lastCd = columnPath.getLastClassDescriptor().getName();
          } else {
            // special case for columns that contain objects eg. Gene.chromosomeLocation
            lastCd = columnPath.getLastClassDescriptor().getName();
          }
          String type = TypeUtil.unqualifiedName(lastCd);
          Path path;
          String fieldName = null;
          try {
            if (columnPath.endIsAttribute()) {
              fieldName = columnName.substring(columnName.lastIndexOf(".") + 1);
              path = new Path(model, type + '.' + fieldName);
            } else {
              // special case for columns that contain objects
              path = new Path(model, type);
            }
          } catch (PathException e) {
            // Should never happen if the field name is valid
            throw new Error("There must be a bug", e);
          }

          // Three cases:
          // 1) attribute has a value so create a result element
          // 2) we have an object but attribute is null -> create a ResultElement with
          // value null
          // 3) the object is null (outer join) so add null value rowCells
          //                    Object fieldValue;
          //                    try {
          //                        fieldValue = (o == null ? null : PathUtil.resolvePath(path, o));
          //                    } catch (PathException e) {
          //                        throw new IllegalArgumentException("Path: \"" + columnName
          //                                + "\", pathToIndex: \"" + pathToIndex + "\", prefix: \""
          //                                + parentColumnName + "\", query: \""
          //                                + PathQueryBinding.marshal(pathQuery, "",
          // pathQuery.getModel()
          //                                    .getName(), PathQuery.USERPROFILE_VERSION)
          //                                + "\", columnIndex: \"" + columnIndex + "\",
          // initialList: \""
          //                                + initialList + "\"", e);
          //                    }
          if (o != null) {
            String fieldCDName = path.getLastClassDescriptor().getName();
            String unqualifiedFieldCD = TypeUtil.unqualifiedName(fieldCDName);
            boolean isKeyField;
            if (fieldName == null) {
              // special case for columns that contain objects
              isKeyField = false;
            } else {
              isKeyField = ClassKeyHelper.isKeyField(classKeys, unqualifiedFieldCD, fieldName);
            }
            ResultElement resultElement = new ResultElement(o, path, isKeyField);
            // link to report page by default, unless it says otherwise in config

            if (redirector != null) {
              try {
                String linkRedirect = redirector.generateLink(im, (InterMineObject) o);
                if (linkRedirect != null) {
                  resultElement.setLinkRedirect(linkRedirect);
                }
              } catch (ClassCastException e) {
                // Simple objects cannot be consumed by redirectors.
              }
            }

            if (rowspan >= 0) {
              rowCells.add(new MultiRowFirstValue(resultElement, rowspan));
            } else {
              rowCells.add(null);
            }
          } else {
            if (rowspan >= 0) {
              rowCells.add(new MultiRowFirstValue(null, rowspan));
            } else {
              rowCells.add(null);
            }
          }
        }
        retval.add(rowCells);
      }
      return retval;
    } catch (IndexOutOfBoundsException e) {
      throw new RuntimeException(e);
    }
  }