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;
017:
018: import java.text.DateFormat;
019: import java.text.NumberFormat;
020: import java.util.Date;
021: import java.util.Locale;
022:
023: import org.outerj.daisy.query.model.QValueType;
024: import org.outerj.daisy.query.model.ValueExpr;
025: import org.outerj.daisy.repository.Document;
026: import org.outerj.daisy.repository.HierarchyPath;
027: import org.outerj.daisy.repository.InvalidDocumentIdException;
028: import org.outerj.daisy.repository.RepositoryException;
029: import org.outerj.daisy.repository.VariantKey;
030: import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
031: import org.outerj.daisy.repository.serverimpl.LocalRepositoryManager;
032:
033: public class ValueFormatter implements Formatter {
034: private final DateFormat dateFormat;
035:
036: private final DateFormat dateTimeFormat;
037:
038: private final NumberFormat decimalFormat;
039:
040: private final Locale locale;
041:
042: private LocalRepositoryManager.Context context;
043:
044: private AuthenticatedUser user;
045:
046: public ValueFormatter(Locale locale,
047: LocalRepositoryManager.Context context,
048: AuthenticatedUser user) {
049: this .locale = locale;
050: dateFormat = DateFormat.getDateInstance(DateFormat.SHORT,
051: locale);
052: dateTimeFormat = DateFormat.getDateTimeInstance(
053: DateFormat.SHORT, DateFormat.MEDIUM, locale);
054: decimalFormat = NumberFormat.getNumberInstance(locale);
055: this .context = context;
056: this .user = user;
057: }
058:
059: public String format(ValueExpr valueExpr, Object value) {
060: String formattedValue;
061: QValueType outputValueType = valueExpr.getOutputValueType();
062:
063: if (value == null)
064: return "";
065:
066: if (value instanceof HierarchyPath) {
067: HierarchyPath path = (HierarchyPath) value;
068: StringBuilder result = new StringBuilder();
069: for (int i = 0; i < path.getElements().length; i++) {
070: result.append("/");
071: result.append(format(valueExpr, path.getElements()[i]));
072: }
073: return result.toString();
074: }
075:
076: switch (outputValueType) {
077: case DATE:
078: formattedValue = dateFormat.format((Date) value);
079: break;
080: case DATETIME:
081: formattedValue = dateTimeFormat.format((Date) value);
082: break;
083: case DECIMAL:
084: formattedValue = decimalFormat.format(value);
085: break;
086: case DOUBLE:
087: formattedValue = decimalFormat.format(value);
088: break;
089: case VERSION_STATE:
090: formattedValue = LocalQueryManager.getLocalizedString(value
091: .toString(), locale);
092: break;
093: case LINK:
094: VariantKey variantKey = (VariantKey) value;
095: String label = null;
096: try {
097: Document linkedDoc = context
098: .getCommonRepository()
099: .getDocument(variantKey.getDocumentId(),
100: variantKey.getBranchId(),
101: variantKey.getLanguageId(), false, user);
102: if (linkedDoc.getLiveVersion() != null)
103: label = linkedDoc.getLiveVersion()
104: .getDocumentName();
105: else
106: label = linkedDoc.getName();
107: } catch (InvalidDocumentIdException e) {
108: // ignore exception
109: } catch (RepositoryException e) {
110: // ignore exception (non existing doc variant)
111: }
112: if (label == null)
113: label = "daisy:" + variantKey.getDocumentId() + "@"
114: + variantKey.getBranchId() + ":"
115: + variantKey.getLanguageId();
116: formattedValue = label;
117: break;
118: default:
119: formattedValue = value.toString();
120: }
121:
122: return formattedValue;
123: }
124:
125: public Locale getLocale() {
126: return locale;
127: }
128: }
|