*/
private void addDiscoveryLocation(Options options, Request request, java.util.List fqs, DSpaceObject dso) throws NumberFormatException, SQLException, WingException {
// UPDATE: listing all locations returned by Solr is not what we want.
// We just want the selected comm/coll, its children/sibling collections, parent community, children community.
// FIXME: probably encapsulate in a new method and allow for a depth parameter like community-list.
List browseCommColl = options.addList("discovery-location");
browseCommColl.setHead(message("xmlui.ArtifactBrowser.AdvancedSearch.filter_by.head"));
if (this.queryResults != null) {
FacetField fieldLocation = this.queryResults.getFacetField("location");
if (fieldLocation != null) {
java.util.List<FacetField.Count> values = fieldLocation.getValues();
if (values != null) {
String queryString = request.getQueryString();
queryString = queryString != null ? queryString.replaceAll("&fq=location[:[%3A]+][ml][0-9]+", "") : queryString;
// Find selected community/collection.
String curCommCollSelectedName = "m1"; // Default
for (FacetField.Count v : values) {
if (fqs.contains(v.getAsFilterQuery())) {
curCommCollSelectedName = v.getName();
break;
}
int type = v.getName().startsWith("m") ? Constants.COMMUNITY : Constants.COLLECTION;
DSpaceObject commColl = DSpaceObject.find(context, type, Integer.parseInt(v.getName().substring(1)));
if (dso != null && commColl != null && dso.getHandle().equals(commColl.getHandle())) {
curCommCollSelectedName = v.getName();
break;
}
}
// UPDATE: see UPDATE above.
// Build list of locations (communities/collections) ordered by handle part after prefix.
Map<String, FacetField.Count> sortedSolrNameLocationWithValues = new TreeMap<String, FacetField.Count>();
for (FacetField.Count v : values) {
sortedSolrNameLocationWithValues.put(v.getName(), v);
}
// Default selection is "m1" (community with ID 1).
int curCommCollSelectedType = Constants.COMMUNITY;
int curCommCollSelectedID = 1;
if (curCommCollSelectedName != null) {
curCommCollSelectedType = curCommCollSelectedName.startsWith("m") ? Constants.COMMUNITY : Constants.COLLECTION;
curCommCollSelectedID = Integer.parseInt(curCommCollSelectedName.substring(1));
}
Map<Integer, String> sortedHandleWithSolrName = new TreeMap<Integer, String>();
Map<String, DSpaceObject> sortedSolrNameWithDSpaceObject = new TreeMap<String, DSpaceObject>();
int topCommID = 1;
String topCommName = "m1";
boolean curSelectedIsTop = curCommCollSelectedName.equals(topCommName) ? true : false;
Community mtop = Community.find(context, topCommID);
sortedHandleWithSolrName.put(Integer.parseInt(mtop.getHandle().split("/")[1]), topCommName);
sortedSolrNameWithDSpaceObject.put(topCommName, mtop);
Collection[] collections = mtop.getCollections();
for (Collection l : collections) {
String solrName = "l" + l.getID();
if (solrName.equals(curCommCollSelectedName))
curSelectedIsTop = true;
sortedHandleWithSolrName.put(Integer.parseInt(l.getHandle().split("/")[1]), solrName);
sortedSolrNameWithDSpaceObject.put(solrName, l);
}
Community[] communities = mtop.getSubcommunities();
for (Community mm : communities) {
String solrName = "m" + mm.getID();
if (solrName.equals(curCommCollSelectedName))
curSelectedIsTop = true;
sortedHandleWithSolrName.put(Integer.parseInt(mm.getHandle().split("/")[1]), solrName);
sortedSolrNameWithDSpaceObject.put(solrName, mm);
}
if (curSelectedIsTop == false) {
if (curCommCollSelectedType == Constants.COMMUNITY) {
Community msel = Community.find(context, curCommCollSelectedID);
Community[] parents = msel.getAllParents();
if (parents.length < 2) {
log.error("Something is wrong: this should not happen.");
}
Community topParent = parents[parents.length - 2];
curCommCollSelectedName = "m" + topParent.getID();
} else {
// Supposing Constants.COLLECTION
Collection lsel = Collection.find(context, curCommCollSelectedID);
Community firstParent = lsel.getCommunities()[0];
Community[] parents = firstParent.getAllParents();
if (parents.length == 0) {
//log.error("Something is wrong: this should not happen.");
//firstParent is a top community.
curCommCollSelectedName = "m" + firstParent.getID();
} else if (parents.length == 1) {
//firstParent is a second level community.
curCommCollSelectedName = "m" + firstParent.getID();
} else {
// get second level parent
Community topParent = parents[parents.length - 2];
curCommCollSelectedName = "m" + topParent.getID();
}
}
}
// Iterator<FacetField.Count> iter = sortedValues.values().iterator();
Iterator<String> iter = sortedHandleWithSolrName.values().iterator();
List facet = browseCommColl.addList(fieldLocation.getName());
// facet.setHead(message("xmlui.ArtifactBrowser.AdvancedSearch.type_" + fieldLocation.getName()));
facet.setHead(queryString);
for (int i = 0; i < this.queryArgs.getFacetLimit(); i++) {
if (!iter.hasNext()) {
break;
} // String displayedValue = value.getName();
String solrName = iter.next();
DSpaceObject commColl = sortedSolrNameWithDSpaceObject.get(solrName);
FacetField.Count value = sortedSolrNameLocationWithValues.get(solrName);
if (value == null) {
// This is probably due to an empty collection, which happens to not be indexed by Solr.
continue;
}
String displayedValue = commColl.getName();
String itemName = commColl.getHandle();
String itemRend = null;
//log.error("iter: solrName: " + solrName + "; " + value + "; handle: " + itemName + "; display: " + displayedValue);
//We have a community/collection, resolve it to a dspaceObject
// displayedValue = SolrServiceImpl.locationToName(context, field.getName(), displayedValue);
// int type = fieldLocation.getName().equals("location.comm") ? Constants.COMMUNITY : Constants.COLLECTION;
if (curCommCollSelectedName != null && curCommCollSelectedName.equals(solrName)) {
//facet.addItem(itemName, "selected").addContent(displayedValue + " (" + value.getCount() + ")");
itemRend = "selected";
} else if (curCommCollSelectedName == null && value.getName().equals("m1")) {
//facet.addItem(itemName, "selected").addContent(displayedValue + " (" + value.getCount() + ")");
itemRend = "selected";
}
if (value.getCount() > 0) {
String URI = null;
try {
URI = request.getSitemapURI().split("/")[request.getSitemapURI().split("/").length - 1];
if (URI.matches("[0-9]+")) {
URI = null;
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
facet.addItem(itemName, itemRend).addXref(contextPath + (commColl == null ? "" : "/handle/" + commColl.getHandle()) + "/" + (URI != null ? URI : "") + (queryString != null ? "?" + queryString : ""), displayedValue + " (" + "" + value.getCount() + ")");
} else {
facet.addItem(itemName, "disabled").addContent(displayedValue + " (" + value.getCount() + ")");
}
}
}
}
}