001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of 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,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.uilib.form.control;
016:
017: import java.sql.Timestamp;
018: import java.text.SimpleDateFormat;
019: import org.apache.commons.lang.StringUtils;
020: import org.araneaframework.uilib.form.FilteredInputControl;
021: import org.araneaframework.uilib.form.control.inputfilter.InputFilter;
022: import org.araneaframework.uilib.support.UiLibMessages;
023: import org.araneaframework.uilib.util.MessageUtil;
024: import org.araneaframework.uilib.util.ValidationUtil;
025: import org.araneaframework.uilib.util.ValidationUtil.ParsedDate;
026:
027: /**
028: * This class represents a generalization of controls that have a value
029: * of type <code>Timestamp</code>.
030: *
031: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
032: */
033: public abstract class TimestampControl extends
034: EmptyStringNullableControl implements FilteredInputControl {
035:
036: //*********************************************************************
037: // FIELDS
038: //*********************************************************************
039:
040: /**
041: * The date-time format used by the element for parsing.
042: */
043: protected String dateTimeInputPattern;
044: protected String dateTimeOutputPattern;
045:
046: protected boolean confOverridden = false;
047:
048: private InputFilter inputFilter;
049:
050: //*********************************************************************
051: // CONSTRUCTORS
052: //*********************************************************************
053:
054: /**
055: * Creates the control setting it's date format pattern to the one provided.
056: *
057: * @param dateTimeFormat date format pattern for {@link SimpleDateFormat}.
058: */
059: public TimestampControl(String dateTimeFormat,
060: String defaultOutputFormat) {
061: this .dateTimeInputPattern = dateTimeFormat;
062: this .dateTimeOutputPattern = defaultOutputFormat;
063: }
064:
065: /** @since 1.0.11 */
066: public InputFilter getInputFilter() {
067: return inputFilter;
068: }
069:
070: /** @since 1.0.11 */
071: public void setInputFilter(InputFilter inputFilter) {
072: this .inputFilter = inputFilter;
073: }
074:
075: //*********************************************************************
076: //* INTERNAL METHODS
077: //*********************************************************************
078:
079: /**
080: * Parses the parameter using the <code>dateTimeFormat</code>,
081: * trying to get <code>Timestamp</code>. Adds a
082: * <code>error.form.date.wrongformat</code> error message if
083: * it fails.
084: */
085: protected Object fromRequest(String parameterValue) {
086: ValidationUtil.ParsedDate result = parseDate(parameterValue);
087:
088: if (result != null) {
089: dateTimeOutputPattern = result.getOutputPattern();
090: return new Timestamp(result.getDate().getTime());
091: }
092:
093: addWrongTimeFormatError();
094:
095: if (parameterValue != null
096: && getInputFilter() != null
097: && !StringUtils.containsOnly(parameterValue,
098: getInputFilter().getCharacterFilter())) {
099: addError(MessageUtil.localizeAndFormat(getInputFilter()
100: .getInvalidInputMessage(), MessageUtil.localize(
101: getLabel(), getEnvironment()), getInputFilter()
102: .getCharacterFilter(), getEnvironment()));
103: }
104:
105: return null;
106: }
107:
108: /** @since 1.1 */
109: protected void addWrongTimeFormatError() {
110: addError(MessageUtil.localizeAndFormat(
111: UiLibMessages.WRONG_DATE_FORMAT, MessageUtil.localize(
112: getLabel(), getEnvironment()),
113: dateTimeInputPattern, getEnvironment()));
114: }
115:
116: /**
117: * Used by {@link TimestampControl#fromRequest(String)} to convert value
118: * read from request to a <code>Date</code> in default <code>TimeZone</code>
119: * and <code>Locale</code>.
120: *
121: * @since 1.0.3
122: */
123: protected ParsedDate parseDate(String parameterValue) {
124: return ValidationUtil.parseDate(parameterValue,
125: dateTimeInputPattern);
126: }
127:
128: /**
129: * Formats the value using <code>dateTimeFormat</code>.
130: */
131: protected String toResponse(Object controlValue) {
132: return new SimpleDateFormat(dateTimeOutputPattern)
133: .format((Timestamp) controlValue);
134: }
135:
136: //*********************************************************************
137: //* PUBLIC METHODS
138: //*********************************************************************
139:
140: /**
141: * Returns {@link ViewModel}.
142: * @return {@link ViewModel}.
143: */
144: public Object getViewModel() {
145: return new ViewModel();
146: }
147:
148: //*********************************************************************
149: //* VIEWMODEL
150: //*********************************************************************
151:
152: public class ViewModel extends EmptyStringNullableControl.ViewModel {
153: private String dateTimeOutputPattern;
154: private InputFilter inputFilter;
155:
156: /**
157: * Takes an outer class snapshot.
158: */
159: public ViewModel() {
160: this .dateTimeOutputPattern = TimestampControl.this .dateTimeOutputPattern;
161: this .inputFilter = TimestampControl.this .getInputFilter();
162: }
163:
164: public SimpleDateFormat getCurrentSimpleDateTimeFormat() {
165: return new SimpleDateFormat(dateTimeOutputPattern);
166: }
167:
168: public InputFilter getInputFilter() {
169: return this.inputFilter;
170: }
171: }
172: }
|