use of org.alfresco.util.Pair in project alfresco-remote-api by Alfresco.
the class ResultMapper method toCollectionWithPagingInfo.
/**
* Turns the results into a CollectionWithPagingInfo
* @param params
* @param searchQuery
*@param results @return CollectionWithPagingInfo<Node>
*/
public CollectionWithPagingInfo<Node> toCollectionWithPagingInfo(Params params, SearchRequestContext searchRequestContext, SearchQuery searchQuery, ResultSet results) {
SearchContext context = null;
Integer total = null;
List<Node> noderesults = new ArrayList<Node>();
Map<String, UserInfo> mapUserInfo = new HashMap<>(10);
Map<NodeRef, List<Pair<String, List<String>>>> hightLighting = results.getHighlighting();
int notFound = 0;
boolean isHistory = searchRequestContext.getStores().contains(StoreMapper.HISTORY);
for (ResultSetRow row : results) {
Node aNode = getNode(row, params, mapUserInfo, isHistory);
if (aNode != null) {
float f = row.getScore();
List<HighlightEntry> highlightEntries = null;
List<Pair<String, List<String>>> high = hightLighting.get(row.getNodeRef());
if (high != null && !high.isEmpty()) {
highlightEntries = new ArrayList<HighlightEntry>(high.size());
for (Pair<String, List<String>> highlight : high) {
highlightEntries.add(new HighlightEntry(highlight.getFirst(), highlight.getSecond()));
}
}
aNode.setSearch(new SearchEntry(f, highlightEntries));
noderesults.add(aNode);
} else {
logger.debug("Unknown noderef returned from search results " + row.getNodeRef());
notFound++;
}
}
SolrJSONResultSet solrResultSet = findSolrResultSet(results);
if (solrResultSet != null) {
// We used Solr for this query
context = toSearchContext(solrResultSet, searchRequestContext, searchQuery, notFound);
total = setTotal(solrResultSet);
} else {
// This probably wasn't solr
if (!results.hasMore()) {
// If there are no more results then we are confident that the number found is correct
// otherwise we are not confident enough that its accurate
total = setTotal(results);
}
}
return CollectionWithPagingInfo.asPaged(params.getPaging(), noderesults, results.hasMore(), total, null, context);
}
use of org.alfresco.util.Pair in project alfresco-remote-api by Alfresco.
the class ResultMapper method getGenericFacetsForIntervals.
/**
* Returns generic faceting responses for Intervals
* @param facetFields
* @param searchQuery
* @return GenericFacetResponse
*/
protected static List<GenericFacetResponse> getGenericFacetsForIntervals(Map<String, List<Pair<String, Integer>>> facetFields, SearchQuery searchQuery) {
if (facetFields != null && !facetFields.isEmpty()) {
List<GenericFacetResponse> ffcs = new ArrayList<>(facetFields.size());
for (Entry<String, List<Pair<String, Integer>>> facet : facetFields.entrySet()) {
if (facet.getValue() != null && !facet.getValue().isEmpty()) {
List<GenericBucket> buckets = new ArrayList<>(facet.getValue().size());
for (Pair<String, Integer> buck : facet.getValue()) {
String filterQuery = null;
Map<String, String> bucketInfo = new HashMap<>();
if (searchQuery != null && searchQuery.getFacetIntervals() != null && searchQuery.getFacetIntervals().getIntervals() != null && !searchQuery.getFacetIntervals().getIntervals().isEmpty()) {
Optional<Interval> found = searchQuery.getFacetIntervals().getIntervals().stream().filter(interval -> facet.getKey().equals(interval.getLabel() != null ? interval.getLabel() : interval.getField())).findFirst();
if (found.isPresent()) {
if (found.get().getSets() != null) {
Optional<IntervalSet> foundSet = found.get().getSets().stream().filter(aSet -> buck.getFirst().equals(aSet.getLabel())).findFirst();
if (foundSet.isPresent()) {
filterQuery = found.get().getField() + ":" + foundSet.get().toAFTSQuery();
bucketInfo.put(GenericFacetResponse.START, foundSet.get().getStart());
bucketInfo.put(GenericFacetResponse.END, foundSet.get().getEnd());
bucketInfo.put(GenericFacetResponse.START_INC, String.valueOf(foundSet.get().isStartInclusive()));
bucketInfo.put(GenericFacetResponse.END_INC, String.valueOf(foundSet.get().isEndInclusive()));
}
}
}
}
GenericBucket bucket = new GenericBucket(buck.getFirst(), filterQuery, null, new HashSet<Metric>(Arrays.asList(new SimpleMetric(METRIC_TYPE.count, String.valueOf(buck.getSecond())))), null, bucketInfo);
buckets.add(bucket);
}
ffcs.add(new GenericFacetResponse(FACET_TYPE.interval, facet.getKey(), buckets));
}
}
return ffcs;
}
return Collections.emptyList();
}
use of org.alfresco.util.Pair in project alfresco-remote-api by Alfresco.
the class ResultMapper method getFacetBucketsForFacetFields.
protected List<FacetFieldContext> getFacetBucketsForFacetFields(Map<String, List<Pair<String, Integer>>> facetFields, SearchQuery searchQuery) {
if (facetFields != null && !facetFields.isEmpty()) {
List<FacetFieldContext> ffcs = new ArrayList<>(facetFields.size());
for (Entry<String, List<Pair<String, Integer>>> facet : facetFields.entrySet()) {
if (facet.getValue() != null && !facet.getValue().isEmpty()) {
List<Bucket> buckets = new ArrayList<>(facet.getValue().size());
for (Pair<String, Integer> buck : facet.getValue()) {
Object display = null;
String filterQuery = null;
if (searchQuery != null && searchQuery.getFacetFields() != null && searchQuery.getFacetFields().getFacets() != null && !searchQuery.getFacetFields().getFacets().isEmpty()) {
Optional<FacetField> found = searchQuery.getFacetFields().getFacets().stream().filter(queryable -> facet.getKey().equals(queryable.getLabel() != null ? queryable.getLabel() : queryable.getField())).findFirst();
if (found.isPresent()) {
display = propertyLookup.lookup(found.get().getField(), buck.getFirst());
String fq = found.get().toFilterQuery(buck.getFirst());
if (fq != null) {
filterQuery = fq;
}
}
}
buckets.add(new Bucket(buck.getFirst(), filterQuery, buck.getSecond(), display));
}
ffcs.add(new FacetFieldContext(facet.getKey(), buckets));
}
}
return ffcs;
}
return Collections.emptyList();
}
use of org.alfresco.util.Pair in project alfresco-remote-api by Alfresco.
the class ResultMapper method toSearchContext.
/**
* Uses the results from Solr to set the Search Context
* @param SolrJSONResultSet
* @param searchQuery
* @return SearchContext
*/
public SearchContext toSearchContext(SolrJSONResultSet solrResultSet, SearchRequestContext searchRequestContext, SearchQuery searchQuery, int notFound) {
SearchContext context = null;
Map<String, Integer> facetQueries = solrResultSet.getFacetQueries();
List<GenericFacetResponse> facets = new ArrayList<>();
List<FacetQueryContext> facetResults = null;
SpellCheckContext spellCheckContext = null;
List<FacetFieldContext> ffcs = new ArrayList<FacetFieldContext>();
if (searchQuery == null) {
throw new IllegalArgumentException("searchQuery can't be null");
}
// Facet queries
if (facetQueries != null && !facetQueries.isEmpty()) {
// If group by field populated in query facet return bucketing into facet field.
List<GenericFacetResponse> facetQueryForFields = getFacetBucketsFromFacetQueries(facetQueries, searchQuery);
if (hasGroup(searchQuery) || FacetFormat.V2 == searchQuery.getFacetFormat()) {
facets.addAll(facetQueryForFields);
} else {
// Return the old way facet query with no bucketing.
facetResults = new ArrayList<>(facetQueries.size());
for (Entry<String, Integer> fq : facetQueries.entrySet()) {
String filterQuery = null;
if (searchQuery.getFacetQueries() != null) {
Optional<FacetQuery> found = searchQuery.getFacetQueries().stream().filter(facetQuery -> fq.getKey().equals(facetQuery.getLabel())).findFirst();
filterQuery = found.isPresent() ? found.get().getQuery() : fq.getKey();
}
facetResults.add(new FacetQueryContext(fq.getKey(), filterQuery, fq.getValue()));
}
}
}
// Field Facets
Map<String, List<Pair<String, Integer>>> facetFields = solrResultSet.getFieldFacets();
if (FacetFormat.V2 == searchQuery.getFacetFormat()) {
facets.addAll(getFacetBucketsForFacetFieldsAsFacets(facetFields, searchQuery));
} else {
ffcs.addAll(getFacetBucketsForFacetFields(facetFields, searchQuery));
}
Map<String, List<Pair<String, Integer>>> facetInterval = solrResultSet.getFacetIntervals();
facets.addAll(getGenericFacetsForIntervals(facetInterval, searchQuery));
Map<String, List<Map<String, String>>> facetRanges = solrResultSet.getFacetRanges();
facets.addAll(RangeResultMapper.getGenericFacetsForRanges(facetRanges, searchQuery.getFacetRanges()));
List<GenericFacetResponse> stats = getFieldStats(searchRequestContext, solrResultSet.getStats());
List<GenericFacetResponse> pimped = getPivots(searchRequestContext, solrResultSet.getPivotFacets(), stats);
facets.addAll(pimped);
facets.addAll(stats);
// Spelling
SpellCheckResult spell = solrResultSet.getSpellCheckResult();
if (spell != null && spell.getResultName() != null && !spell.getResults().isEmpty()) {
spellCheckContext = new SpellCheckContext(spell.getResultName(), spell.getResults());
}
// Put it all together
context = new SearchContext(solrResultSet.getLastIndexedTxId(), facets, facetResults, ffcs, spellCheckContext, searchRequestContext.includeRequest() ? searchQuery : null);
return isNullContext(context) ? null : context;
}
use of org.alfresco.util.Pair in project alfresco-remote-api by Alfresco.
the class DeclarativeSpreadsheetWebScript method generateSpreadsheet.
/**
* Generates the spreadsheet, based on the properties in the header
* and a callback for the body.
*/
public void generateSpreadsheet(Object resource, String format, WebScriptRequest req, Status status, Map<String, Object> model) throws IOException {
Pattern qnameMunger = Pattern.compile("([A-Z][a-z]+)([A-Z].*)");
String delimiterParam = req.getParameter(PARAM_REQ_DELIMITER);
CSVStrategy reqCSVstrategy = null;
if (delimiterParam != null && !delimiterParam.isEmpty()) {
reqCSVstrategy = new CSVStrategy(delimiterParam.charAt(0), '"', CSVStrategy.COMMENTS_DISABLED);
}
// Build up the details of the header
List<Pair<QName, Boolean>> propertyDetails = buildPropertiesForHeader(resource, format, req);
String[] headings = new String[propertyDetails.size()];
String[] descriptions = new String[propertyDetails.size()];
boolean[] required = new boolean[propertyDetails.size()];
for (int i = 0; i < headings.length; i++) {
Pair<QName, Boolean> property = propertyDetails.get(i);
if (property == null || property.getFirst() == null) {
headings[i] = "";
required[i] = false;
} else {
QName column = property.getFirst();
required[i] = property.getSecond();
// Ask the dictionary service nicely for the details
PropertyDefinition pd = dictionaryService.getProperty(column);
if (pd != null && pd.getTitle(dictionaryService) != null) {
// Use the friendly titles, which may even be localised!
headings[i] = pd.getTitle(dictionaryService);
descriptions[i] = pd.getDescription(dictionaryService);
} else {
// Nothing friendly found, try to munge the raw qname into
// something we can show to a user...
String raw = column.getLocalName();
raw = raw.substring(0, 1).toUpperCase() + raw.substring(1);
Matcher m = qnameMunger.matcher(raw);
if (m.matches()) {
headings[i] = m.group(1) + " " + m.group(2);
} else {
headings[i] = raw;
}
}
}
}
// Build a list of just the properties
List<QName> properties = new ArrayList<QName>(propertyDetails.size());
for (Pair<QName, Boolean> p : propertyDetails) {
QName qn = null;
if (p != null) {
qn = p.getFirst();
}
properties.add(qn);
}
// Output
if ("csv".equals(format)) {
StringWriter sw = new StringWriter();
CSVPrinter csv = new CSVPrinter(sw, reqCSVstrategy != null ? reqCSVstrategy : getCsvStrategy());
csv.println(headings);
populateBody(resource, csv, properties);
model.put(MODEL_CSV, sw.toString());
} else {
Workbook wb;
if ("xlsx".equals(format)) {
wb = new XSSFWorkbook();
// TODO Properties
} else {
wb = new HSSFWorkbook();
// TODO Properties
}
// Add our header row
Sheet sheet = wb.createSheet("Export");
Row hr = sheet.createRow(0);
sheet.createFreezePane(0, 1);
Font fb = wb.createFont();
fb.setBold(true);
Font fi = wb.createFont();
fi.setBold(true);
fi.setItalic(true);
CellStyle csReq = wb.createCellStyle();
csReq.setFont(fb);
CellStyle csOpt = wb.createCellStyle();
csOpt.setFont(fi);
// Populate the header
Drawing draw = null;
for (int i = 0; i < headings.length; i++) {
Cell c = hr.createCell(i);
c.setCellValue(headings[i]);
if (required[i]) {
c.setCellStyle(csReq);
} else {
c.setCellStyle(csOpt);
}
if (headings[i].length() == 0) {
sheet.setColumnWidth(i, 3 * 250);
} else {
sheet.setColumnWidth(i, 18 * 250);
}
if (descriptions[i] != null && descriptions[i].length() > 0) {
// Add a description for it too
if (draw == null) {
draw = sheet.createDrawingPatriarch();
}
ClientAnchor ca = wb.getCreationHelper().createClientAnchor();
ca.setCol1(c.getColumnIndex());
ca.setCol2(c.getColumnIndex() + 1);
ca.setRow1(hr.getRowNum());
ca.setRow2(hr.getRowNum() + 2);
Comment cmt = draw.createCellComment(ca);
cmt.setAuthor("");
cmt.setString(wb.getCreationHelper().createRichTextString(descriptions[i]));
cmt.setVisible(false);
c.setCellComment(cmt);
}
}
// Have the contents populated
populateBody(resource, wb, sheet, properties);
// Save it for the template
ByteArrayOutputStream baos = new ByteArrayOutputStream();
wb.write(baos);
model.put(MODEL_EXCEL, baos.toByteArray());
}
}
Aggregations