01: /*
02: * Copyright 2004 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package org.outerj.daisy.repository.serverimpl.query.facets;
17:
18: import java.util.HashMap;
19: import java.util.Locale;
20: import java.util.Map;
21: import java.util.SortedMap;
22: import java.util.TreeMap;
23:
24: import org.outerj.daisy.query.model.ValueExpr;
25: import org.outerj.daisy.repository.HierarchyPath;
26: import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
27: import org.outerj.daisy.repository.query.FacetConf;
28: import org.outerj.daisy.repository.serverimpl.LocalRepositoryManager;
29: import org.outerj.daisy.repository.serverimpl.query.Counter;
30: import org.outerj.daisy.repository.serverimpl.query.FacetValueFormatter;
31: import org.outerj.daisy.repository.serverimpl.query.Formatter;
32: import org.outerj.daisy.repository.serverimpl.query.QueryFormatter;
33: import org.outerx.daisy.x10.FacetedQueryResultDocument;
34:
35: public class FacetValues {
36: private SortedMap<Object, Counter> values;
37:
38: private FacetSampler facetSampler;
39:
40: private Locale locale;
41:
42: private Map<String, Formatter> formatters = new HashMap<String, Formatter>();
43:
44: public FacetValues(FacetSampler facetSampler,
45: AuthenticatedUser user,
46: LocalRepositoryManager.Context context) {
47: this .facetSampler = facetSampler;
48: this .values = new TreeMap<Object, Counter>(this .facetSampler
49: .getComparator());
50: this .locale = facetSampler.getLocale();
51: formatters.put("value", new FacetValueFormatter(locale,
52: context, user));
53: formatters.put("query", new QueryFormatter(locale));
54: }
55:
56: public void addValue(Object value) {
57: Counter counter = values.get(value);
58: if (counter != null) {
59: counter.increment();
60: } else {
61: counter = new Counter();
62: counter.increment();
63: values.put(value, counter);
64: }
65: }
66:
67: public void addXml(
68: FacetedQueryResultDocument.FacetedQueryResult.Facets facetsXml,
69: FacetConf facetConf, ValueExpr valueExpr) {
70: FacetedQueryResultDocument.FacetedQueryResult.Facets.Facet facetXml = facetsXml
71: .addNewFacet();
72:
73: facetXml.setLabel(valueExpr.getTitle(locale));
74: facetXml.setExpression(valueExpr.getExpression());
75: facetXml.setMultiValue(valueExpr.isMultiValue());
76: boolean isHierarchical = false;
77: if (this .values != null && this .values.size() > 0)
78: isHierarchical = this .values.firstKey() instanceof HierarchyPath;
79: facetXml.setHierarchical(isHierarchical);
80:
81: facetSampler.valuesToXml(this , facetXml, valueExpr, formatters);
82: }
83:
84: public SortedMap<Object, Counter> getValues() {
85: return values;
86: }
87: }
|