Java Code Examples for org.alfresco.service.cmr.search.IntervalSet

The following examples show how to use org.alfresco.service.cmr.search.IntervalSet. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
protected void validateSets(Set<IntervalSet> intervalSets, String prefix)
{
    if (intervalSets != null && !intervalSets.isEmpty())
    {
        for (IntervalSet aSet:intervalSets)
        {
            ParameterCheck.mandatory(prefix+" sets start", aSet.getStart());
            ParameterCheck.mandatory(prefix+" sets end", aSet.getEnd());

            if (aSet.getLabel() == null)
            {
                aSet.setLabel(aSet.toRange());
            }
        }
    }
}
 
Example 2
public static IntervalSet parseDateInterval(IntervalSet theSet, boolean isDate)
{
    if (isDate)
    {
        Pair<Date, Integer> dateAndResolution1 = parseDateString(theSet.getStart());
        Pair<Date, Integer> dateAndResolution2 = parseDateString(theSet.getEnd());
        String start = dateAndResolution1 == null ? theSet.getStart()
                    : (theSet.isStartInclusive() ? getDateStart(dateAndResolution1) : getDateEnd(dateAndResolution1));
        String end = dateAndResolution2 == null ?  theSet.getEnd()
                    : (theSet.isEndInclusive() ? getDateEnd(dateAndResolution2) : getDateStart(dateAndResolution2));
        return new IntervalSet(start, end, theSet.getLabel(), theSet.isStartInclusive(), theSet.isEndInclusive());

    }
    return theSet;
}
 
Example 3
protected void buildRangeParameters(SearchParameters searchParameters, URLCodec encoder, StringBuilder url) throws UnsupportedEncodingException
{
    if (searchParameters.getRanges() != null && !searchParameters.getRanges().isEmpty())
    {
        List<RangeParameters> ranges = searchParameters.getRanges();
        url.append("&facet=").append(encoder.encode("true", "UTF-8"));
        
        for(RangeParameters facetRange : ranges)
        {
            String fieldName = facetRange.getField();
            boolean isDate = false;
            PropertyDefinition propertyDef = QueryParserUtils.matchPropertyDefinition(searchParameters.getNamespace(),
                    namespaceDAO, dictionaryService, fieldName);
            if (propertyDef != null && (propertyDef.getDataType().getName().equals(DataTypeDefinition.DATETIME)
                    || propertyDef.getDataType().getName().equals(DataTypeDefinition.DATE)))
            {
                isDate = true;
            }
            
            
            IntervalSet rangeSet =
                    parseDateInterval(
                            new IntervalSet(facetRange.getStart(), 
                                            facetRange.getEnd(), 
                                            facetRange.getGap(), 
                                            null, 
                                            null), isDate);
            url.append("&facet.range=");

            if(facetRange.getLabel()!= null && !facetRange.getLabel().isEmpty())
            {
                url.append(encoder.encode("{!", "UTF-8"));
                url.append(encoder.encode(String.format("tag=%s ",facetRange.getLabel()), "UTF-8"));
                url.append(encoder.encode("}", "UTF-8"));
            }

            url.append(encoder.encode(facetRange.getField(), "UTF-8"));

            //Check if date and if inclusive or not
            url.append(String.format("&f.%s.facet.range.start=",fieldName)).append(encoder.encode(""+ rangeSet.getStart(), "UTF-8"));
            url.append(String.format("&f.%s.facet.range.end=",fieldName)).append(encoder.encode(""+ rangeSet.getEnd(), "UTF-8"));
            url.append(String.format("&f.%s.facet.range.gap=",fieldName)).append(encoder.encode(""+ rangeSet.getLabel(), "UTF-8"));
            url.append(String.format("&f.%s.facet.range.hardend=",fieldName)).append(encoder.encode("" + facetRange.isHardend(), "UTF-8"));
            if(facetRange.getInclude() != null && !facetRange.getInclude().isEmpty())
            {
                for(String include : facetRange.getInclude())
                {
                    url.append(String.format("&f.%s.facet.range.include=",fieldName)).append(encoder.encode("" + include, "UTF-8"));
                }
            }
            if(facetRange.getOther() != null && !facetRange.getOther().isEmpty())
            {
                for(String other : facetRange.getOther())
                {
                    url.append(String.format("&f.%s.facet.range.other=",fieldName)).append(encoder.encode("" + other, "UTF-8"));
                }
            }
            if(!facetRange.getExcludeFilters().isEmpty())
            {
                url.append("&facet.range=");
                if (facetRange.getExcludeFilters() != null && !facetRange.getExcludeFilters().isEmpty())
                {
                    StringBuilder prefix = new StringBuilder("{!ex=");
                    Iterator<String> itr = facetRange.getExcludeFilters().iterator();
                    while(itr.hasNext())
                    {
                        String val = itr.next();
                        prefix.append(val);
                        if(itr.hasNext())
                        {
                            prefix.append(",");
                        }
                    }
                    prefix.append("}");
                    url.append(prefix);
                    url.append(fieldName);
                }
                
            }
        }
    }
}
 
Example 4
/**
 * Sets the Interval Parameters object on search parameters
 *
 * It does some valiation then takes any "SETS" at the top level and sets them at every field level.
 *
 * @param sp SearchParameters
 * @param facetIntervals IntervalParameters
 */
public void fromFacetIntervals(SearchParameters sp, IntervalParameters facetIntervals)
{
    if (facetIntervals != null)
    {
        ParameterCheck.mandatory("facetIntervals intervals", facetIntervals.getIntervals());

        Set<IntervalSet> globalSets = facetIntervals.getSets();
        validateSets(globalSets, "facetIntervals");

        if (facetIntervals.getIntervals() != null && !facetIntervals.getIntervals().isEmpty())
        {
            List<String> intervalLabels = new ArrayList<>(facetIntervals.getIntervals().size());
            for (Interval interval:facetIntervals.getIntervals())
            {
                ParameterCheck.mandatory("facetIntervals intervals field", interval.getField());
                validateSets(interval.getSets(), "facetIntervals intervals "+interval.getField());
                if (interval.getSets() != null && globalSets != null)
                {
                    interval.getSets().addAll(globalSets);
                }
                ParameterCheck.mandatoryCollection("facetIntervals intervals sets", interval.getSets());

                List<Map.Entry<String, Long>> duplicateSetLabels =
                            interval.getSets().stream().collect(groupingBy(IntervalSet::getLabel, Collectors.counting()))
                            .entrySet().stream().filter(e -> e.getValue().intValue() > 1).collect(toList());
                if (!duplicateSetLabels.isEmpty())
                {
                    throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_MESSAGE_ID,
                                new Object[] { ": duplicate set interval label "+duplicateSetLabels.toString() });

                }
                if (interval.getLabel() != null) intervalLabels.add(interval.getLabel());
            }

            List<Map.Entry<String, Long>> duplicateIntervalLabels =
                        intervalLabels.stream().collect(groupingBy(Function.identity(), Collectors.counting()))
                        .entrySet().stream().filter(e -> e.getValue().intValue() > 1).collect(toList());
            if (!duplicateIntervalLabels.isEmpty())
            {
                throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_MESSAGE_ID,
                            new Object[] { ": duplicate interval label "+duplicateIntervalLabels.toString() });
            }
        }

        if (facetIntervals.getSets() != null)
        {
            facetIntervals.getSets().clear();
        }
    }
    sp.setInterval(facetIntervals);
}
 
Example 5
/**
 * Returns generic faceting responses for Intervals
 * @param facetFields
 * @param searchQuery
 * @return GenericFacetResponse
 */
protected static List<GenericFacetResponse> getGenericFacetsForIntervals(Map<String, List<Pair<String, Integer>>> facetFields, SearchQuery searchQuery)
{
    if (facetFields != null && !facetFields.isEmpty())
    {
        List<GenericFacetResponse> ffcs = new ArrayList<>(facetFields.size());
        for (Entry<String, List<Pair<String, Integer>>> facet:facetFields.entrySet())
        {
            if (facet.getValue() != null && !facet.getValue().isEmpty())
            {
                List<GenericBucket> buckets = new ArrayList<>(facet.getValue().size());
                for (Pair<String, Integer> buck:facet.getValue())
                {
                    String filterQuery = null;
                    Map<String, String> bucketInfo = new HashMap<>();

                    if (searchQuery != null
                                && searchQuery.getFacetIntervals() != null
                                && searchQuery.getFacetIntervals().getIntervals() != null
                                && !searchQuery.getFacetIntervals().getIntervals().isEmpty())
                    {
                        Optional<Interval> found = searchQuery.getFacetIntervals().getIntervals().stream().filter(
                                    interval -> facet.getKey().equals(interval.getLabel()!=null?interval.getLabel():interval.getField())).findFirst();
                        if (found.isPresent())
                        {
                            if (found.get().getSets() != null)
                            {
                                Optional<IntervalSet> foundSet = found.get().getSets().stream().filter(aSet -> buck.getFirst().equals(aSet.getLabel())).findFirst();
                                if (foundSet.isPresent())
                                {
                                    filterQuery = found.get().getField() + ":" + foundSet.get().toAFTSQuery();
                                    bucketInfo.put(GenericFacetResponse.START, foundSet.get().getStart());
                                    bucketInfo.put(GenericFacetResponse.END, foundSet.get().getEnd());
                                    bucketInfo.put(GenericFacetResponse.START_INC, String.valueOf(foundSet.get().isStartInclusive()));
                                    bucketInfo.put(GenericFacetResponse.END_INC, String.valueOf(foundSet.get().isEndInclusive()));
                                }
                            }
                        }
                    }
                    GenericBucket bucket = new GenericBucket(buck.getFirst(), filterQuery, null , new HashSet<Metric>(Arrays.asList(new SimpleMetric(METRIC_TYPE.count,String.valueOf(buck.getSecond())))), null, bucketInfo);
                    buckets.add(bucket);
                }
                ffcs.add(new GenericFacetResponse(FACET_TYPE.interval, facet.getKey(), buckets));
            }
        }

        return ffcs;
    }
    return Collections.emptyList();
}