Search in sources :

Example 1 with RecordsManagementSearchParameters

use of org.alfresco.module.org_alfresco_module_rm.search.RecordsManagementSearchParameters in project records-management by Alfresco.

the class RMSavedSearchesPost method executeImpl.

/**
 * @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.Status, org.alfresco.web.scripts.Cache)
 */
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    // Get the site id and confirm it is valid
    Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
    String siteId = templateVars.get("site");
    if (siteId == null || siteId.length() == 0) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Site id not provided.");
    }
    if (siteService.getSite(siteId) == null) {
        throw new WebScriptException(Status.STATUS_NOT_FOUND, "Site not found.");
    }
    try {
        // Parse the JSON passed in the request
        JSONObject json = new JSONObject(new JSONTokener(req.getContent().getContent()));
        // Get the details of the saved search
        if (!json.has("name")) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Mandatory 'name' parameter was not provided in request body");
        }
        String name = json.getString("name");
        String description = null;
        if (json.has("description")) {
            description = json.getString("description");
        }
        boolean isPublic = true;
        if (json.has("public")) {
            isPublic = json.getBoolean("public");
        }
        // NOTE: we do not need to worry about the query
        if (!json.has("params")) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Mandatory 'params' parameter was not provided in request body");
        }
        String params = json.getString("params");
        String sort = null;
        if (json.has("sort")) {
            sort = json.getString("sort");
        }
        // Use the compatibility class to create a saved search details and save
        String search = SavedSearchDetailsCompatibility.getSearchFromParams(params);
        if (search == null) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Mandatory 'terms' was not provided in 'params' parameter found in the request body");
        }
        RecordsManagementSearchParameters searchParameters = SavedSearchDetailsCompatibility.createSearchParameters(params, sort, namespaceService);
        recordsManagementSearchService.saveSearch(siteId, name, description, search, searchParameters, isPublic);
    } catch (IOException iox) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from req.", iox);
    } catch (JSONException je) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from req.", je);
    }
    // Indicate success in the model
    Map<String, Object> model = new HashMap<String, Object>(1);
    model.put("success", true);
    return model;
}
Also used : JSONTokener(org.json.JSONTokener) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) RecordsManagementSearchParameters(org.alfresco.module.org_alfresco_module_rm.search.RecordsManagementSearchParameters) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) IOException(java.io.IOException)

Example 2 with RecordsManagementSearchParameters

use of org.alfresco.module.org_alfresco_module_rm.search.RecordsManagementSearchParameters in project records-management by Alfresco.

the class RMSearchGet method executeImpl.

/*
     * @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.Status, org.alfresco.web.scripts.Cache)
     */
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    Map<String, Object> model = new HashMap<String, Object>(1);
    try {
        // Get the site id and confirm it is valid
        Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
        String siteId = templateVars.get("site");
        if (siteId == null || siteId.length() == 0) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Site id not provided.");
        }
        if (siteService.getSite(siteId) == null) {
            throw new WebScriptException(Status.STATUS_NOT_FOUND, "Site not found.");
        }
        // Get the query parameter
        String query = req.getParameter(PARAM_QUERY);
        // TODO check that this is there
        String sortby = req.getParameter(PARAM_SORTBY);
        // TODO this is optional
        String filters = req.getParameter(PARAM_FILTERS);
        // TODO this is optional
        // Convert into a rm search parameter object
        RecordsManagementSearchParameters searchParameters = SavedSearchDetailsCompatibility.createSearchParameters(filters, new String[] { ",", "/" }, sortby, namespaceService);
        // Set the max results
        String maxItems = req.getParameter(PARAM_MAX_ITEMS);
        if (maxItems != null && maxItems.length() != 0) {
            searchParameters.setMaxItems(Integer.parseInt(maxItems));
        }
        // Execute search
        List<Pair<NodeRef, NodeRef>> results = recordsManagementSearchService.search(siteId, query, searchParameters);
        // Reset person data cache
        personDataCache = new HashMap<String, String>(57);
        // Process the result items
        List<Item> items = new ArrayList<Item>(results.size());
        for (Pair<NodeRef, NodeRef> pair : results) {
            // TC 3-3  Create User Groups
            try {
                Item item = new Item(pair.getFirst(), pair.getSecond());
                items.add(item);
            } catch (Exception e) {
                LOGGER.debug("Ignoring failed attempt to add item to search results.", e);
            }
        }
        // Return model
        model.put("items", items);
    } catch (Exception ex) {
        model.put("errorMessage", ex.toString());
    }
    return model;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) RecordsManagementSearchParameters(org.alfresco.module.org_alfresco_module_rm.search.RecordsManagementSearchParameters) Pair(org.alfresco.util.Pair)

Aggregations

HashMap (java.util.HashMap)2 RecordsManagementSearchParameters (org.alfresco.module.org_alfresco_module_rm.search.RecordsManagementSearchParameters)2 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 Pair (org.alfresco.util.Pair)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 JSONTokener (org.json.JSONTokener)1