001: // Copyright 2006 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry;
016:
017: import java.io.Serializable;
018: import java.util.List;
019: import java.util.Map;
020:
021: import org.apache.tapestry.ioc.internal.util.CollectionFactory;
022: import org.apache.tapestry.ioc.internal.util.InternalUtils;
023:
024: /**
025: * Standard implmentation of {@link ValidationTracker}. Works pretty hard to ensure a minimum
026: * amount of data is stored in the HttpSession.
027: */
028: public final class ValidationTrackerImpl implements ValidationTracker,
029: Serializable {
030: private static final long serialVersionUID = -8029192726659275677L;
031:
032: private static class FieldTracker implements Serializable {
033: private static final long serialVersionUID = -3653306147088451811L;
034:
035: private final String _fieldName;
036:
037: private String _input;
038:
039: private String _errorMessage;
040:
041: FieldTracker(String fieldName) {
042: _fieldName = fieldName;
043: }
044:
045: public String getFieldName() {
046: return _fieldName;
047: }
048:
049: public void setErrorMessage(String errorMessage) {
050: _errorMessage = errorMessage;
051: }
052:
053: public String getErrorMessage() {
054: return _errorMessage;
055: }
056:
057: public String getInput() {
058: return _input;
059: }
060:
061: public void setInput(String input) {
062: _input = input;
063: }
064:
065: }
066:
067: private List<String> _extraErrors;
068:
069: private List<FieldTracker> _fieldTrackers;
070:
071: // Rebuilt on-demand
072:
073: private transient Map<String, FieldTracker> _fieldToTracker;
074:
075: private void refreshFieldToTracker() {
076: if (_fieldToTracker != null)
077: return;
078:
079: if (_fieldTrackers == null)
080: return;
081:
082: _fieldToTracker = CollectionFactory.newMap();
083:
084: for (FieldTracker ft : _fieldTrackers)
085: _fieldToTracker.put(ft.getFieldName(), ft);
086: }
087:
088: private FieldTracker get(Field field) {
089: String key = field.getElementName();
090:
091: refreshFieldToTracker();
092:
093: FieldTracker result = InternalUtils.get(_fieldToTracker, key);
094:
095: if (result == null)
096: result = new FieldTracker(key);
097:
098: return result;
099: }
100:
101: private void store(FieldTracker fieldTracker) {
102: if (_fieldTrackers == null)
103: _fieldTrackers = CollectionFactory.newList();
104:
105: refreshFieldToTracker();
106:
107: String key = fieldTracker.getFieldName();
108:
109: if (!_fieldToTracker.containsKey(key)) {
110: _fieldTrackers.add(fieldTracker);
111: _fieldToTracker.put(key, fieldTracker);
112: }
113: }
114:
115: public void clear() {
116: _extraErrors = null;
117: _fieldTrackers = null;
118: _fieldToTracker = null;
119: }
120:
121: public String getError(Field field) {
122: return get(field).getErrorMessage();
123: }
124:
125: public List<String> getErrors() {
126: List<String> result = CollectionFactory.newList();
127:
128: if (_extraErrors != null)
129: result.addAll(_extraErrors);
130:
131: if (_fieldTrackers != null) {
132: for (FieldTracker ft : _fieldTrackers) {
133: String errorMessage = ft.getErrorMessage();
134:
135: if (errorMessage != null)
136: result.add(errorMessage);
137: }
138: }
139:
140: return result;
141: }
142:
143: public boolean getHasErrors() {
144: return !getErrors().isEmpty();
145: }
146:
147: public String getInput(Field field) {
148: return get(field).getInput();
149: }
150:
151: public boolean inError(Field field) {
152: return InternalUtils.isNonBlank(get(field).getErrorMessage());
153: }
154:
155: public void recordError(Field field, String errorMessage) {
156: FieldTracker ft = get(field);
157:
158: ft.setErrorMessage(errorMessage);
159:
160: store(ft);
161: }
162:
163: public void recordError(String errorMessage) {
164: if (_extraErrors == null)
165: _extraErrors = CollectionFactory.newList();
166:
167: _extraErrors.add(errorMessage);
168: }
169:
170: public void recordInput(Field field, String input) {
171: FieldTracker ft = get(field);
172:
173: ft.setInput(input);
174:
175: store(ft);
176: }
177:
178: }
|