Search in sources :

Example 1 with SolrFacetProperties

use of org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties in project alfresco-remote-api by Alfresco.

the class SolrFacetConfigAdminPost method unprotectedExecuteImpl.

@Override
protected Map<String, Object> unprotectedExecuteImpl(WebScriptRequest req, Status status, Cache cache) {
    try {
        SolrFacetProperties fp = parseRequestForFacetProperties(req);
        facetService.createFacetNode(fp);
        if (logger.isDebugEnabled()) {
            logger.debug("Created facet node: " + fp);
        }
    } catch (Throwable t) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not save the facet configuration.", t);
    }
    // Needs to be mutable.
    return new HashMap<>();
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) SolrFacetProperties(org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties)

Example 2 with SolrFacetProperties

use of org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties in project alfresco-remote-api by Alfresco.

the class SolrFacetConfigAdminPut method parseRequestForFacetProperties.

private SolrFacetProperties parseRequestForFacetProperties(WebScriptRequest req) {
    JSONObject json = null;
    try {
        json = new JSONObject(new JSONTokener(req.getContent().getContent()));
        // Must exist
        final String filterID = json.getString(PARAM_FILTER_ID);
        final String facetQNameStr = getValue(String.class, json.opt(PARAM_FACET_QNAME), null);
        // Note that in resolving the QName string here, we expect there to be some facet QNames which are not
        // really QNames. These are SOLR/SearchService 'specials', examples being "SITE" or "TAG".
        // These will be resolved here to a QName with no namespace.
        final QName facetQName = (facetQNameStr == null) ? null : FacetQNameUtils.createQName(facetQNameStr, namespaceService);
        final String displayName = getValue(String.class, json.opt(PARAM_DISPLAY_NAME), null);
        final String displayControl = getValue(String.class, json.opt(PARAM_DISPLAY_CONTROL), null);
        final int maxFilters = getValue(Integer.class, json.opt(PARAM_MAX_FILTERS), -1);
        final int hitThreshold = getValue(Integer.class, json.opt(PARAM_HIT_THRESHOLD), -1);
        final int minFilterValueLength = getValue(Integer.class, json.opt(PARAM_MIN_FILTER_VALUE_LENGTH), -1);
        final String sortBy = getValue(String.class, json.opt(PARAM_SORT_BY), null);
        final String scope = getValue(String.class, json.opt(PARAM_SCOPE), null);
        final Boolean isEnabled = getValue(Boolean.class, json.opt(PARAM_IS_ENABLED), null);
        JSONArray scopedSitesJsonArray = getValue(JSONArray.class, json.opt(PARAM_SCOPED_SITES), null);
        final Set<String> scopedSites = getScopedSites(scopedSitesJsonArray);
        final JSONObject customPropJsonObj = getValue(JSONObject.class, json.opt(PARAM_CUSTOM_PROPERTIES), null);
        final Set<CustomProperties> customProps = getCustomProperties(customPropJsonObj);
        SolrFacetProperties fp = new SolrFacetProperties.Builder().filterID(filterID).facetQName(facetQName).displayName(displayName).displayControl(displayControl).maxFilters(maxFilters).hitThreshold(hitThreshold).minFilterValueLength(minFilterValueLength).sortBy(sortBy).scope(scope).isEnabled(isEnabled).scopedSites(scopedSites).customProperties(customProps).build();
        return fp;
    } catch (IOException e) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from req.", e);
    } catch (JSONException e) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from req.", e);
    }
}
Also used : QName(org.alfresco.service.namespace.QName) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) SolrFacetProperties(org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) CustomProperties(org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties.CustomProperties)

Example 3 with SolrFacetProperties

use of org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties in project alfresco-remote-api by Alfresco.

the class SolrFacetConfigAdminGet method unprotectedExecuteImpl.

@Override
protected Map<String, Object> unprotectedExecuteImpl(WebScriptRequest req, Status status, Cache cache) {
    // get the filterID parameter.
    Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
    String filterID = templateVars.get("filterID");
    Map<String, Object> model = new HashMap<String, Object>(1);
    if (filterID == null) {
        model.put("filters", facetService.getFacets());
    } else {
        SolrFacetProperties fp = facetService.getFacet(filterID);
        if (fp == null) {
            throw new WebScriptException(Status.STATUS_NOT_FOUND, "Filter not found");
        }
        model.put("filter", fp);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Retrieved all available facets: " + model.values());
    }
    return model;
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) SolrFacetProperties(org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties)

Example 4 with SolrFacetProperties

use of org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties in project alfresco-remote-api by Alfresco.

the class SolrFacetConfigAdminPost method parseRequestForFacetProperties.

private SolrFacetProperties parseRequestForFacetProperties(WebScriptRequest req) {
    JSONObject json = null;
    try {
        json = new JSONObject(new JSONTokener(req.getContent().getContent()));
        final String filterID = json.getString(PARAM_FILTER_ID);
        validateFilterID(filterID);
        final String facetQNameStr = json.getString(PARAM_FACET_QNAME);
        // Note: we're using this util class here because we need to be able to deal with
        // qnames without a URI e.g. "{}SITE" and *not* have them default to the cm: namespace
        // which happens with the 'normal' Alfresco QName code.
        final QName facetQName = FacetQNameUtils.createQName(facetQNameStr, namespaceService);
        final String displayName = json.getString(PARAM_DISPLAY_NAME);
        final String displayControl = json.getString(PARAM_DISPLAY_CONTROL);
        final int maxFilters = json.getInt(PARAM_MAX_FILTERS);
        final int hitThreshold = json.getInt(PARAM_HIT_THRESHOLD);
        final int minFilterValueLength = json.getInt(PARAM_MIN_FILTER_VALUE_LENGTH);
        final String sortBy = json.getString(PARAM_SORT_BY);
        // Optional params
        final String scope = getValue(String.class, json.opt(PARAM_SCOPE), "ALL");
        final boolean isEnabled = getValue(Boolean.class, json.opt(PARAM_IS_ENABLED), false);
        JSONArray scopedSitesJsonArray = getValue(JSONArray.class, json.opt(PARAM_SCOPED_SITES), null);
        final Set<String> scopedSites = getScopedSites(scopedSitesJsonArray);
        final JSONObject customPropJsonObj = getValue(JSONObject.class, json.opt(PARAM_CUSTOM_PROPERTIES), null);
        final Set<CustomProperties> customProps = getCustomProperties(customPropJsonObj);
        SolrFacetProperties fp = new SolrFacetProperties.Builder().filterID(filterID).facetQName(facetQName).displayName(displayName).displayControl(displayControl).maxFilters(maxFilters).hitThreshold(hitThreshold).minFilterValueLength(minFilterValueLength).sortBy(sortBy).scope(scope).isEnabled(isEnabled).scopedSites(scopedSites).customProperties(customProps).build();
        return fp;
    } catch (IOException e) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from req.", e);
    } catch (JSONException e) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from req.", e);
    }
}
Also used : QName(org.alfresco.service.namespace.QName) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) SolrFacetProperties(org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) CustomProperties(org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties.CustomProperties)

Example 5 with SolrFacetProperties

use of org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties in project alfresco-remote-api by Alfresco.

the class SolrFacetConfigAdminPut method unprotectedExecuteImpl.

@Override
protected Map<String, Object> unprotectedExecuteImpl(WebScriptRequest req, Status status, Cache cache) {
    final String relativePosString = req.getParameter(PARAM_RELATIVE_POS);
    try {
        if (relativePosString != null) {
            // This is a request to 'move' (reposition) the specified facet.
            // We need the relative position that the facet will move.
            final int relativePos;
            try {
                relativePos = Integer.parseInt(relativePosString);
            } catch (NumberFormatException nfe) {
                throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Cannot move facet as could not parse relative position: '" + relativePosString + "'");
            }
            // And we need the filterID for the facet we're moving.
            final Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
            String filterId = templateVars.get(URL_PARAM_FILTER_ID);
            if (filterId == null) {
                throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Illegal null filterId");
            }
            // So let's move the filter...
            try {
                // Get the current sequence of filter IDs.
                List<SolrFacetProperties> facets = facetService.getFacets();
                List<String> facetIDs = CollectionUtils.transform(facets, new Function<SolrFacetProperties, String>() {

                    @Override
                    public String apply(SolrFacetProperties value) {
                        return value.getFilterID();
                    }
                });
                List<String> reorderedIDs = CollectionUtils.moveRight(relativePos, filterId, facetIDs);
                this.facetService.reorderFacets(reorderedIDs);
                if (logger.isDebugEnabled()) {
                    logger.debug("Moved facet " + filterId + " to relative position: " + relativePos);
                }
            } catch (UnrecognisedFacetId ufi) {
                throw new WebScriptException(Status.STATUS_NOT_FOUND, "Unrecognised filter ID: " + ufi.getFacetId());
            }
        } else // TODO Allow for simultaneous move and update of facet.
        {
            SolrFacetProperties fp = parseRequestForFacetProperties(req);
            facetService.updateFacet(fp);
            if (logger.isDebugEnabled()) {
                logger.debug("Updated facet node: " + fp);
            }
        }
    } catch (Throwable t) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not update the facet configuration.", t);
    }
    Map<String, Object> model = new HashMap<String, Object>(1);
    return model;
}
Also used : HashMap(java.util.HashMap) UnrecognisedFacetId(org.alfresco.repo.search.impl.solr.facet.Exceptions.UnrecognisedFacetId) SolrFacetProperties(org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.JSONObject)

Aggregations

SolrFacetProperties (org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties)5 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)5 HashMap (java.util.HashMap)3 JSONObject (org.json.JSONObject)3 IOException (java.io.IOException)2 CustomProperties (org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties.CustomProperties)2 QName (org.alfresco.service.namespace.QName)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 JSONTokener (org.json.JSONTokener)2 UnrecognisedFacetId (org.alfresco.repo.search.impl.solr.facet.Exceptions.UnrecognisedFacetId)1