private void addColumnsInternal(List<Path> columnPaths) { List<String> types = new ArrayList<String>(); int i = columns.size(); for (Path columnPath : columnPaths) { String type = TypeUtil.unqualifiedName(columnPath.getLastClassDescriptor().getName()); Class typeCls = columnPath.getLastClassDescriptor().getType(); String columnDescription = pathQuery.getGeneratedPathDescription(columnPath.toStringNoConstraints()); Column column; if (columnDescription.equals(columnPath.toStringNoConstraints())) { column = new Column(columnPath, i, typeCls); } else { column = new Column(columnPath, columnDescription, i, typeCls); } if (!types.contains(column.getColumnId())) { String fieldName = columnPath.getEndFieldDescriptor().getName(); boolean isKeyField = ClassKeyHelper.isKeyField(classKeys, type, fieldName); if (isKeyField) { column.setSelectable(true); types.add(column.getColumnId()); } } columns.add(column); i++; } }
/** * Create summary information of the paths currently in a query and their constraints for display * in the QueryBuilder summary section. The list of SummaryPath objects collect information for * simple display in the JSP. * * @param query the query to create summary information from * @return a list if summary information about paths on the query * @throws PathException if the PathQuery is invalid */ public static List<SummaryPath> getDisplaySummary(PathQuery query) throws PathException { Set<SummaryPath> summaryPaths = new TreeSet<SummaryPath>(); Set<String> constrainedPaths = new HashSet<String>(); for (PathConstraint con : query.getConstraints().keySet()) { if (con instanceof PathConstraintSubclass) { // Do nothing } else if (con instanceof PathConstraintLoop) { // Put both paths in constrainedPaths.add(con.getPath()); constrainedPaths.add(((PathConstraintLoop) con).getLoopPath()); } else { constrainedPaths.add(con.getPath()); } } List<String> lockedPaths = findLockedPaths(query); List<String> forcedInnerJoins = findForcedInnerJoins(query); Set<String> paths = new HashSet<String>(); paths.addAll(constrainedPaths); paths.addAll(query.getView()); paths.addAll(query.getOuterJoinGroups().keySet()); for (String stringPath : paths) { Path path = query.makePath(stringPath); boolean isLocked = lockedPaths.contains(path.toStringNoConstraints()); boolean isForcedInner = forcedInnerJoins.contains(path.toStringNoConstraints()); SummaryPath summaryPath = new SummaryPath(path, isLocked, isForcedInner); for (PathConstraint con : query.getConstraintsForPath(path.toStringNoConstraints())) { boolean editable = false; String description = null; String switchable = SwitchOffAbility.LOCKED.toString().toLowerCase(); if (query instanceof TemplateQuery) { TemplateQuery template = (TemplateQuery) query; editable = template.getEditableConstraints().contains(con); description = template.getConstraintDescriptions().get(con); SwitchOffAbility constraintSwitchOffAbility = template.getConstraintSwitchOffAbility().get(con); if (SwitchOffAbility.ON.equals(constraintSwitchOffAbility)) { switchable = SwitchOffAbility.ON.toString().toLowerCase(); } else if (SwitchOffAbility.OFF.equals(constraintSwitchOffAbility)) { switchable = SwitchOffAbility.OFF.toString().toLowerCase(); } } // subclass constraints aren't display if (!(con instanceof PathConstraintSubclass)) { String code = query.getConstraints().get(con); summaryPath.addSummaryConstraint( new SummaryConstraint(con, code, editable, description, switchable)); } else { summaryPath.setSubclass(((PathConstraintSubclass) con).getType()); } } summaryPaths.add(summaryPath); } return new ArrayList<SummaryPath>(summaryPaths); }
// replace final dot in path with '+' if path represents and attribute, used for path ordering private String replaceAttributeDots(Path p) { String strPath = p.toStringNoConstraints(); if (p.endIsAttribute()) { int lastIndex = strPath.lastIndexOf('.'); strPath = strPath.substring(0, lastIndex) + "+" + strPath.substring(lastIndex + 1); } return strPath; }
private static List<String> getSubview(WebConfig webConfig, Model m, Path path) throws PathException { List<String> subview = new ArrayList<String>(); String basePath = path.toStringNoConstraints() + "."; List<FieldConfig> subconfs = getClassFieldConfigs(webConfig, path.getEndClassDescriptor()); for (FieldConfig fc : subconfs) { String pathString = basePath + fc.getFieldExpr(); Path pathToAdd = new Path(m, pathString); if (pathToAdd.endIsAttribute() && (fc.getDisplayer() == null && fc.getShowInSummary())) { subview.add(pathToAdd.getNoConstraintsString()); } } return subview; }
/** * Return a list of string paths that are defined as WebConfig to be shown in results. This will * include attributes of the given class and follow references. * * @param type the class name to create a view for * @param model the model * @param webConfig we configuration * @return the configured view paths for the class */ public static List<String> getDefaultViewForClass(String type, Model model, WebConfig webConfig) { List<String> view = new ArrayList<String>(); ClassDescriptor cld = model.getClassDescriptorByName(type); List<FieldConfig> fieldConfigs = FieldConfigHelper.getClassFieldConfigs(webConfig, cld); for (FieldConfig fieldConfig : fieldConfigs) { String relPath = fieldConfig.getFieldExpr(); // only add attributes, don't follow references, following references can be problematic // when subclasses get involved. if (fieldConfig.getShowInResults()) { try { Path path = new Path(model, type + "." + relPath); // add references if (path.isRootPath() || path.endIsReference()) { for (FieldConfig fc : FieldConfigHelper.getClassFieldConfigs(webConfig, path.getEndClassDescriptor())) { Path pathToAdd = new Path(model, path.toStringNoConstraints() + "." + fc.getFieldExpr()); if (pathToAdd.endIsAttribute() && (!view.contains(pathToAdd.getNoConstraintsString())) && (fc.getDisplayer() == null && fc.getShowInSummary())) { view.add(pathToAdd.getNoConstraintsString()); } } // add collections } else if (!path.endIsCollection()) { view.add(path.getNoConstraintsString()); } } catch (PathException e) { LOG.error("Invalid path configured in webconfig for class: " + type); } } } if (view.size() == 0) { for (AttributeDescriptor att : cld.getAllAttributeDescriptors()) { if (!"id".equals(att.getName())) { view.add(type + "." + att.getName()); } } } return view; }
/** {@inheritDoc} */ @Override public ActionForward execute( ComponentContext context, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { boolean canExportAsBED = false; HttpSession session = request.getSession(); final InterMineAPI im = SessionMethods.getInterMineAPI(session); Model model = im.getModel(); PathQuery query = new PathQuery(model); // org and dbkey for galaxy use Set<String> orgSet = new HashSet<String>(); Set<String> genomeBuildSet = new HashSet<String>(); // Build GenomicRegion pathquery, the request is from GenomicRegionSearch "export to Galaxy" if (request.getParameter("featureIds") != null) { String featureIds = request.getParameter("featureIds").trim(); String orgName = request.getParameter("orgName"); if (orgName != null && !"".equals(orgName)) { orgSet.add(orgName); } // Refer to GenomicRegionSearchService.getExportFeaturesQuery method String path = "SequenceFeature"; query.addView(path + ".primaryIdentifier"); query.addView(path + ".chromosomeLocation.locatedOn.primaryIdentifier"); query.addView(path + ".chromosomeLocation.start"); query.addView(path + ".chromosomeLocation.end"); query.addView(path + ".organism.name"); // use ids or pids String[] idsInStr = featureIds.split(","); Set<Integer> ids = new HashSet<Integer>(); boolean isIds = true; for (String id : idsInStr) { id = id.trim(); if (!Pattern.matches("^\\d+$", id)) { isIds = false; break; } ids.add(Integer.valueOf(id)); } if (isIds) { query.addConstraint(Constraints.inIds(path, ids)); } else { if (featureIds.contains("'")) { featureIds = featureIds.replaceAll("'", "\\\\'"); } query.addConstraint(Constraints.lookup(path, featureIds, null)); } canExportAsBED = true; } else { // request from normal result table String tableName = request.getParameter("table"); request.setAttribute("table", tableName); PagedTable pt = SessionMethods.getResultsTable(session, tableName); // Null check to page table, maybe session timeout? if (pt == null) { LOG.error("Page table is NULL..."); return null; } // Check if can export as BED TableHttpExporter tableExporter = new BEDHttpExporter(); try { canExportAsBED = tableExporter.canExport(pt); } catch (Exception e) { canExportAsBED = false; LOG.error("Caught an error running canExport() for: BEDHttpExporter. " + e); e.printStackTrace(); } LinkedHashMap<Path, Integer> exportClassPathsMap = getExportClassPaths(pt); List<Path> exportClassPaths = new ArrayList<Path>(exportClassPathsMap.keySet()); Map<String, String> pathMap = new LinkedHashMap<String, String>(); for (Path path : exportClassPaths) { String pathString = path.toStringNoConstraints(); String displayPath = pathString.replace(".", " > "); pathMap.put(pathString, displayPath); } Map<String, Integer> pathIndexMap = new LinkedHashMap<String, Integer>(); for (int index = 0; index < exportClassPaths.size(); index++) { String pathString = exportClassPaths.get(index).toStringNoConstraints(); Integer idx = exportClassPathsMap.get(exportClassPaths.get(index)); pathIndexMap.put(pathString, idx); } request.setAttribute("exportClassPaths", pathMap); request.setAttribute("pathIndexMap", pathIndexMap); // Support export public and private lists to Galaxy query = pt.getWebTable().getPathQuery(); ObjectStore os = im.getObjectStore(); Map<PathConstraint, String> constrains = query.getConstraints(); for (PathConstraint constraint : constrains.keySet()) { if (constraint instanceof PathConstraintBag) { String bagName = ((PathConstraintBag) constraint).getBag(); InterMineBag imBag = im.getBagManager().getBag(SessionMethods.getProfile(session), bagName); // find the classKeys Set<String> classKeySet = new LinkedHashSet<String>(); for (Integer id : imBag.getContentsAsIds()) { String classKey = pt.findClassKeyValue(im.getClassKeys(), os.getObjectById(id)); classKeySet.add(classKey); } String path = ((PathConstraintBag) constraint).getPath(); // replace constraint in the pathquery PathConstraintLookup newConstraint = new PathConstraintLookup( path, classKeySet.toString().substring(1, classKeySet.toString().length() - 1), null); query.replaceConstraint(constraint, newConstraint); } } orgSet = SequenceFeatureExportUtil.getOrganisms(query, session); } if (query instanceof TemplateQuery) { TemplateQuery templateQuery = (TemplateQuery) query; Map<PathConstraint, SwitchOffAbility> constraintSwitchOffAbilityMap = templateQuery.getConstraintSwitchOffAbility(); for (Map.Entry<PathConstraint, SwitchOffAbility> entry : constraintSwitchOffAbilityMap.entrySet()) { if (entry.getValue().compareTo(SwitchOffAbility.OFF) == 0) { templateQuery.removeConstraint(entry.getKey()); } } } String queryXML = PathQueryBinding.marshal(query, "", model.getName(), PathQuery.USERPROFILE_VERSION); // String encodedQueryXML = URLEncoder.encode(queryXML, "UTF-8"); String tableViewURL = new URLGenerator(request).getPermanentBaseURL() + "/service/query/results"; request.setAttribute("tableURL", tableViewURL); request.setAttribute("queryXML", queryXML); request.setAttribute("size", 1000000); // If can export as BED request.setAttribute("canExportAsBED", canExportAsBED); if (canExportAsBED) { String bedURL = new URLGenerator(request).getPermanentBaseURL() + "/service/query/results/bed"; request.setAttribute("bedURL", bedURL); genomeBuildSet = (Set<String>) OrganismGenomeBuildLookup.getGenomeBuildByOrgansimCollection(orgSet); String org = (orgSet.size() < 1) ? "Organism information not available" : StringUtil.join(orgSet, ","); // possible scenario: [null, ce3, null], should remove all null element and then join genomeBuildSet.removeAll(Collections.singleton(null)); String dbkey = (genomeBuildSet.size() < 1) ? "Genome Build information not available" : StringUtil.join(genomeBuildSet, ","); request.setAttribute("org", org); request.setAttribute("dbkey", dbkey); } // PathMap Map<String, String> pathsMap = new LinkedHashMap<String, String>(); List<String> views = query.getView(); for (String view : views) { String title = query.getGeneratedPathDescription(view); title = WebUtil.formatColumnName(title); pathsMap.put(view, title); } request.setAttribute("pathsMap", pathsMap); return null; }
// 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); } }