Search in sources :

Example 1 with NodeMetaDataQueryCallback

use of org.alfresco.repo.solr.SOLRTrackingComponent.NodeMetaDataQueryCallback 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);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MetaDataResultsFilter(org.alfresco.repo.solr.MetaDataResultsFilter) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) NodeMetaDataParameters(org.alfresco.repo.solr.NodeMetaDataParameters) NodeMetaData(org.alfresco.repo.solr.NodeMetaData) NodeMetaDataQueryCallback(org.alfresco.repo.solr.SOLRTrackingComponent.NodeMetaDataQueryCallback) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) JSONException(org.json.JSONException) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) IndexerException(org.alfresco.repo.search.IndexerException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.JSONObject) Content(org.springframework.extensions.surf.util.Content) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) JSONObject(org.json.JSONObject)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 IndexerException (org.alfresco.repo.search.IndexerException)1 MetaDataResultsFilter (org.alfresco.repo.solr.MetaDataResultsFilter)1 NodeMetaData (org.alfresco.repo.solr.NodeMetaData)1 NodeMetaDataParameters (org.alfresco.repo.solr.NodeMetaDataParameters)1 NodeMetaDataQueryCallback (org.alfresco.repo.solr.SOLRTrackingComponent.NodeMetaDataQueryCallback)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 Content (org.springframework.extensions.surf.util.Content)1 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)1