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;
17:
18: import java.util.Locale;
19:
20: import org.outerj.daisy.query.model.Identifier;
21: import org.outerj.daisy.query.model.ValueExpr;
22: import org.outerj.daisy.query.model.Identifier.FieldIdentifier;
23: import org.outerj.daisy.repository.HierarchyPath;
24: import org.outerj.daisy.repository.RepositoryException;
25: import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
26: import org.outerj.daisy.repository.schema.FieldType;
27: import org.outerj.daisy.repository.schema.SelectionList;
28: import org.outerj.daisy.repository.schema.StaticSelectionList;
29: import org.outerj.daisy.repository.serverimpl.LocalRepositoryManager;
30:
31: public class FacetValueFormatter implements Formatter {
32:
33: private Formatter delegate;
34:
35: private final Locale locale;
36:
37: private LocalRepositoryManager.Context context;
38:
39: private AuthenticatedUser user;
40:
41: public FacetValueFormatter(Locale locale,
42: LocalRepositoryManager.Context context,
43: AuthenticatedUser user) {
44: this .locale = locale;
45: this .context = context;
46: this .user = user;
47:
48: this .delegate = new ValueFormatter(locale, context, user);
49: }
50:
51: public String format(ValueExpr valueExpr, Object value) {
52: if (value == null)
53: return "";
54:
55: if (value instanceof HierarchyPath) {
56: HierarchyPath path = (HierarchyPath) value;
57: StringBuilder result = new StringBuilder();
58: for (int i = 0; i < path.getElements().length; i++) {
59: result.append("/");
60: result.append(format(valueExpr, path.getElements()[i]));
61: }
62: return result.toString();
63: }
64:
65: String formattedValue = null;
66:
67: // find label for a static field list
68: if (valueExpr instanceof Identifier
69: && ((Identifier) valueExpr).getDelegate() instanceof FieldIdentifier) {
70: FieldIdentifier fieldIdentifier = (FieldIdentifier) ((Identifier) valueExpr)
71: .getDelegate();
72: try {
73: FieldType fieldType = context.getCommonRepository()
74: .getRepositorySchema().getFieldTypeById(
75: fieldIdentifier.getfieldTypeId(),
76: false, user);
77: SelectionList list = fieldType.getSelectionList();
78: if (list != null && list instanceof StaticSelectionList) {
79: String listValue = list.getItemLabel(value, locale);
80: if (listValue != null && listValue.length() > 0)
81: formattedValue = listValue;
82: }
83: } catch (RepositoryException e) {
84: // if the field cannot be found just use the stored value
85: }
86: }
87:
88: if (formattedValue == null) {
89: formattedValue = this .delegate.format(valueExpr, value);
90: }
91: return formattedValue;
92: }
93:
94: public Locale getLocale() {
95: return locale;
96: }
97: }
|