All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.cq.social.srp.internal.FacetResults Maven / Gradle / Ivy

/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2012 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/
package com.adobe.cq.social.srp.internal;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

/**
 * The parsed facet results.
 */
public class FacetResults {
    private Map facetFieldsFromResults;
    private final List> documents;
    private Map facetAggregations;
    private final List facetFields;
    private Map> pivotAggregations;
    private final List facetPivotFields;

    /**
     * Constructor.
     * @param docs the list of documents
     * @param facetFields the input list of facets
     * @param pivotFields the input list of facet pivot fields
     * @param facetFieldsFromResults the facet field results fetched from the data service
     * @param facetPivotFromResults the pivot field results fetched from the data service
     */
    public FacetResults(final List> docs, final List facetFields,
        final List pivotFields, final Map facetFieldsFromResults,
        final Map facetPivotFromResults) {
        this.documents = docs;
        this.facetFields = facetFields;
        this.facetPivotFields = pivotFields;
        if (facetFields != null && !facetFields.isEmpty()) {
            this.facetFieldsFromResults = facetFieldsFromResults;

        } else {
            this.facetFieldsFromResults = Collections.emptyMap(); // create empty object
        }
        if (pivotFields != null && !pivotFields.isEmpty()) {
            pivotAggregations = getForEachFacetPivotField(pivotFields, facetPivotFromResults);

        }
    }

    /**
     * @param pivotFields the list of pivot.field used in the query
     * @param facetPivotNode the "facet_pivot" node in the json reponse
     * @return a Map where the key is the pivot.field
     */
    private Map> getForEachFacetPivotField(final List pivotFields,
        final Map facetPivotNode) {
        final Map> pivotAggregate =
            new HashMap>();

        for (final String facetPivotField : pivotFields) {
            final Object[] facetPivotResults = (Object[]) facetPivotNode.get(facetPivotField);
            pivotAggregate.put(facetPivotField, getPivot(facetPivotField, facetPivotResults));
        }
        return pivotAggregate;
    }

    /**
     * @param pivotField one pivot.field
     * @param pivotArray the json array for the pivot.field
     * @return a List of FacetPivotResults
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    private Map getPivot(final String pivotField, final Object[] pivotArray) {
        final Map pivotAggregate = new HashMap();

        final int numOfResults = pivotArray == null ? 0 : pivotArray.length;
        for (int i = 0; i < numOfResults; i++) {
            final Map result = (Map) pivotArray[i];
            final String value = (String) result.get("value");
            final Object c = result.get("count");
            Integer count = 0;
            if (c instanceof Long) {
                count = ((Long) c).intValue();
            } else if (c instanceof Integer) {
                count = (Integer) c;
            }

            final Map[] pivot = (Map[]) result.get("pivot");
            if (pivot == null) {
                // no more pivot node, so must be the last field in the pivot.field.
                pivotAggregate.put(value, new FacetPivotResults(value, count, null));
            } else {
                // call itself again for the next set of pivot. e.g. where pivot.field has more the 2 fields
                pivotAggregate
                    .put(
                        value,
                        new FacetPivotResults(value, count, getPivot(StringUtils.substringAfter(pivotField, ","),
                            pivot)));
            }
        }
        return pivotAggregate;
    }

    /**
     * A method to return the first aggregation of the first field for the first pivot.field.
     * @return a Map where the key is the value of the first field in the first pivot.field
     */
    public Map getPivotAggregation() {
        return getPivotAggregation(facetPivotFields.get(0));
    }

    /**
     * @param fpf the facet.pivot field
     * @return a Map where the key is the value of the first field in the first pivot.field
     */
    public Map getPivotAggregation(final String fpf) {
        final Map pivotList = pivotAggregations.get(fpf);
        final Map pivotAggregate = new HashMap();
        if (pivotList != null) {
            for (final Map.Entry entry : pivotList.entrySet()) {
                pivotAggregate.put(entry.getKey(), pivotToMap(entry.getValue().getPivot()));
            }
        }
        return pivotAggregate;
    }

    /**
     * Return the aggregate with field matching. e.g. getPivotAggregation(":parent,reponse,:key",
     * ["/some/path/to/a/particular/parent","-1"])
     * @param fpf the facet.pivot field used in the search
     * @param fieldsFilter a list of
     * @return a Map of the aggregate or empty Map if nothing is found matchig filters
     */
    public Map getPivotAggregation(final String fpf, final List fieldsFilter) {
        final String[] fields = StringUtils.split(fpf, ",");
        final Map pivotList = pivotAggregations.get(fpf);
        if ((pivotList != null) && (fieldsFilter != null) && (fieldsFilter.size() < fields.length)) {
            Map pivot = pivotList;
            boolean found = true;
            for (final String filter : fieldsFilter) {
                final FacetPivotResults result = pivot.get(filter);
                if (!result.getValue().equalsIgnoreCase(filter)) {
                    found = false;
                    break;
                } else {
                    pivot = result.getPivot();
                }
            }
            if (found) {
                return pivotToMap(pivot);
            }
        }
        return Collections.emptyMap();
    }

    /**
     * Return an aggregate (in a Map), given a list of FacetPivotResults.
     * @param listOfResults
     * @return
     */
    private Map pivotToMap(final Map listOfResults) {
        final Map aggregate = new HashMap();
        for (final Map.Entry entry : listOfResults.entrySet()) {
            aggregate.put(entry.getKey(), entry.getValue().getCount());
        }
        return aggregate;
    }

    /**
     * Get the facetAggregations.
     * @return a Map from the values to their counts
     */
    public Map getAggregation() {
        if (facetAggregations == null) {
            facetAggregations = new HashMap();

            for (final String field : facetFields) {
                final Object[] fieldValues = (Object[]) facetFieldsFromResults.get(field);
                if (fieldValues != null) {
                    for (int i = 0; i < fieldValues.length; i += 2) {
                        facetAggregations.put((String) fieldValues[i], fieldValues[i + 1]);
                    }
                }
            }
        }
        return facetAggregations;
    }

    /**
     * Get the documents.
     * @return the list of documents
     */
    public List> getDocs() {
        return this.documents;
    }

    /**
     * A method for getting an empty facet result.
     * @return An empty result
     */
    public static FacetResults emptyResults() {
        return new FacetResults(Collections.>emptyList(), Collections.emptyList(),
            Collections.emptyList(), Collections.emptyMap(),
            Collections.emptyMap());
    }

    /**
     * Data structure to store a single pivot result.
     */
    private static class FacetPivotResults {
        private final String value;
        private final Integer count;
        private final Map pivot;

        FacetPivotResults(final String value, final Integer count, final Map pivot) {
            this.value = value;
            this.count = count;
            this.pivot = pivot;
        }

        public String getValue() {
            return value;
        }

        public Integer getCount() {
            return count;
        }

        public Map getPivot() {
            return pivot;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy