use of org.alfresco.repo.solr.MetaDataResultsFilter in project alfresco-remote-api by Alfresco.
the class NodesMetaDataGet method executeImpl.
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status) {
try {
Content content = req.getContent();
if (content == null) {
throw new WebScriptException("Failed to convert request to String");
}
JSONObject o = new JSONObject(content.getContent());
List<Long> nodeIds = null;
if (o.has("nodeIds")) {
JSONArray jsonNodeIds = o.getJSONArray("nodeIds");
nodeIds = new ArrayList<Long>(jsonNodeIds.length());
for (int i = 0; i < jsonNodeIds.length(); i++) {
Long nodeId = jsonNodeIds.getLong(i);
nodeIds.add(nodeId);
}
}
Long fromNodeId = o.has("fromNodeId") ? o.getLong("fromNodeId") : null;
Long toNodeId = o.has("toNodeId") ? o.getLong("toNodeId") : null;
// 0 or Integer.MAX_VALUE => ignore
int maxResults = o.has("maxResults") ? o.getInt("maxResults") : 0;
int size = 0;
if (maxResults != 0 && maxResults != Integer.MAX_VALUE) {
size = maxResults;
} else if (nodeIds != null) {
size = nodeIds.size();
} else if (fromNodeId != null && toNodeId != null) {
if ((toNodeId.longValue() - fromNodeId.longValue()) > Integer.MAX_VALUE) {
throw new WebScriptException("Too many nodes expected, try changing the criteria");
}
size = (int) (toNodeId - fromNodeId);
}
final boolean noSizeCalculated = (size == 0);
// filters, defaults are 'true'
MetaDataResultsFilter filter = new MetaDataResultsFilter();
if (o.has("includeAclId")) {
filter.setIncludeAclId(o.getBoolean("includeAclId"));
}
if (o.has("includeAspects")) {
filter.setIncludeAspects(o.getBoolean("includeAspects"));
}
if (o.has("includeNodeRef")) {
filter.setIncludeNodeRef(o.getBoolean("includeNodeRef"));
}
if (o.has("includeOwner")) {
filter.setIncludeOwner(o.getBoolean("includeOwner"));
}
if (o.has("includeProperties")) {
filter.setIncludeProperties(o.getBoolean("includeProperties"));
}
if (o.has("includePaths")) {
filter.setIncludePaths(o.getBoolean("includePaths"));
}
if (o.has("includeType")) {
filter.setIncludeType(o.getBoolean("includeType"));
}
if (o.has("includeParentAssociations")) {
filter.setIncludeParentAssociations(o.getBoolean("includeParentAssociations"));
}
if (o.has("includeChildIds")) {
filter.setIncludeChildIds(o.getBoolean("includeChildIds"));
}
if (o.has("includeTxnId")) {
filter.setIncludeTxnId(o.getBoolean("includeTxnId"));
}
final ArrayList<FreemarkerNodeMetaData> nodesMetaData = new ArrayList<FreemarkerNodeMetaData>(size > 0 ? size : INITIAL_DEFAULT_SIZE);
NodeMetaDataParameters params = new NodeMetaDataParameters();
params.setNodeIds(nodeIds);
params.setFromNodeId(fromNodeId);
params.setToNodeId(toNodeId);
params.setMaxResults(maxResults);
solrTrackingComponent.getNodesMetadata(params, filter, new NodeMetaDataQueryCallback() {
private int counter = BATCH_SIZE;
private int numBatches = 0;
@Override
public boolean handleNodeMetaData(NodeMetaData nodeMetaData) {
// e.g. Serializable -> String, QName -> String (because map keys must be string, number)
try {
FreemarkerNodeMetaData fNodeMetaData = new FreemarkerNodeMetaData(solrSerializer, nodeMetaData);
nodesMetaData.add(fNodeMetaData);
} catch (Exception e) {
throw new AlfrescoRuntimeException("Problem converting to Freemarker using node " + nodeMetaData.getNodeRef().toString(), e);
}
if (noSizeCalculated && --counter == 0) {
counter = BATCH_SIZE;
nodesMetaData.ensureCapacity(++numBatches * BATCH_SIZE);
}
return true;
}
});
Map<String, Object> model = new HashMap<String, Object>(1, 1.0f);
model.put("nodes", nodesMetaData);
model.put("filter", filter);
if (logger.isDebugEnabled()) {
logger.debug("Result: \n\tRequest: " + req + "\n\tModel: " + model);
}
return model;
} catch (IOException e) {
throw new WebScriptException("IO exception parsing request", e);
} catch (JSONException e) {
throw new WebScriptException("Invalid JSON", e);
}
}
Aggregations