001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package org.outerj.daisy.repository.serverimpl.query.facets;
017:
018: import java.util.Date;
019: import java.util.Locale;
020: import java.util.Map;
021: import java.util.SortedMap;
022:
023: import org.apache.commons.logging.Log;
024: import org.outerj.daisy.query.model.ValueExpr;
025: import org.outerj.daisy.repository.query.FacetConf;
026: import org.outerj.daisy.repository.serverimpl.query.Formatter;
027:
028: class DateFacetSampler extends AbstractFacetSampler {
029: private double spreadFactor;
030:
031: public DateFacetSampler(FacetConf facetConf, Locale locale,
032: Log logger) {
033: super (facetConf, locale, logger);
034: Map<String, String> facetProperties = facetConf.getProperties();
035:
036: if (facetProperties.containsKey("threshold"))
037: threshold = Integer.parseInt(facetProperties
038: .get("threshold"));
039: else {
040: logger
041: .warn("Could not find facet property with name 'threshold'. Falling back to default value MAX_VALUE");
042: threshold = Integer.MAX_VALUE;
043: }
044: if (facetProperties.containsKey("spreadFactor"))
045: spreadFactor = Double.parseDouble(facetProperties
046: .get("spreadFactor"));
047: else {
048: logger
049: .warn("Could not find facet property with name 'spreadFactor'. Falling back to default value '1'");
050: spreadFactor = 1;
051: }
052: }
053:
054: public String createUserFormat(CountGroup group,
055: ValueExpr valueExpr, Formatter valueFormatter) {
056: String s1 = valueFormatter.format(valueExpr, group.getMin());
057: String s2 = valueFormatter.format(valueExpr, group.getMax());
058: return s1 + " - " + s2;
059: }
060:
061: public void regroup(SortedMap<Object, CountGroup> groups) {
062: Map.Entry<Object, CountGroup>[] countGroups = (Map.Entry<Object, CountGroup>[]) groups
063: .entrySet().toArray(new Map.Entry[groups.size()]);
064:
065: long min = ((Date) countGroups[0].getValue().getMin())
066: .getTime();
067: long max = ((Date) countGroups[countGroups.length - 1]
068: .getValue().getMax()).getTime();
069: int ascFactor = facetConf.getSortAscending() ? 1 : -1;
070: long diff = Math.abs(max - min);
071: double z = Math.pow(diff, 1.0 / spreadFactor) / threshold;
072: int groupCount = 1;
073: Date dateThreshold = new Date(min
074: + (long) Math.pow(groupCount * z, spreadFactor)
075: * ascFactor);
076:
077: CountGroup currentGroup = countGroups[0].getValue();
078: for (int i = 1; i < countGroups.length; i++) {
079: Object key = countGroups[i].getKey();
080: CountGroup group = countGroups[i].getValue();
081: boolean changedGroup = false;
082: while (comparator.compare(dateThreshold, group.getMin()) < 0
083: || comparator
084: .compare(dateThreshold, group.getMax()) < 0) {
085: groupCount++;
086: dateThreshold.setTime(min
087: + (long) Math.pow(groupCount * z, spreadFactor)
088: * ascFactor);
089: changedGroup = true;
090: }
091: if (changedGroup) {
092: currentGroup = group;
093: } else {
094: currentGroup.merge(group);
095: groups.remove(key);
096: }
097: }
098: }
099:
100: @Override
101: protected String createQueryFormat(CountGroup g,
102: ValueExpr valueExpr, Formatter formatter) {
103: if (g.getMin().equals(g.getMax())) {
104: return formatter.format(valueExpr, g.getMin());
105: } else {
106: Object realmin = g.getMin(), realmax = g.getMax();
107: if (!facetConf.getSortAscending()) {
108: realmin = g.getMax();
109: realmax = g.getMin();
110: }
111:
112: return formatter.format(valueExpr, realmin) + " AND "
113: + formatter.format(valueExpr, realmax);
114:
115: }
116: }
117:
118: }
|