001: package org.apache.turbine.util;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.Hashtable;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Vector;
026:
027: /**
028: * Used for adding and accessing messages that relate to a specific
029: * form and field. Allows to query for messages by form name and
030: * field name. Used together with FormMessage class.
031: *
032: * @author <a href="mailto:neeme@one.lv">Neeme Praks</a>
033: * @version $Id: FormMessages.java 534527 2007-05-02 16:10:59Z tv $
034: */
035: public class FormMessages {
036: private Hashtable forms_messages;
037: private Hashtable fields_messages;
038: private Hashtable messages_fields;
039: private Hashtable forms_fields;
040:
041: /**
042: * Constructor.
043: */
044: public FormMessages() {
045: forms_messages = new Hashtable();
046: fields_messages = new Hashtable();
047: messages_fields = new Hashtable();
048: forms_fields = new Hashtable();
049: }
050:
051: /**
052: * Sets a message for a field of a form. The message is given as
053: * a long representing a return code.
054: *
055: * @param formName A String with the form name.
056: * @param fieldName A String with the field name.
057: * @param returnCode A long with the return code.
058: */
059: public void setMessage(String formName, String fieldName,
060: long returnCode) {
061: setMessage(formName, fieldName, String.valueOf(returnCode));
062: }
063:
064: /**
065: * Sets a message for a field of a form. The message is given as
066: * a String.
067: *
068: * @param formName A String with the form name.
069: * @param fieldName A String with the field name.
070: * @param messageName A String with the message.
071: */
072: public void setMessage(String formName, String fieldName,
073: String messageName) {
074: fieldName = formName + "-" + fieldName;
075: addValue(forms_messages, formName, messageName);
076: addValue(fields_messages, fieldName, messageName);
077: addValue(messages_fields, messageName, fieldName);
078: addValue(forms_fields, formName, fieldName);
079: }
080:
081: /**
082: * Adds a pair key/value to a table, making sure not to add
083: * duplicate keys.
084: *
085: * @param table A Hastable.
086: * @param key A String with the key.
087: * @param value A String with value.
088: */
089: private void addValue(Hashtable table, String key, String value) {
090: Vector values;
091:
092: if (!table.containsKey(key)) {
093: values = new Vector();
094: values.addElement(value);
095: table.put(key, values);
096: } else {
097: values = ((Vector) table.get(key));
098: if (!values.contains(value)) {
099: values.addElement(value);
100: }
101: }
102: }
103:
104: /**
105: * Gets a pair key/value from a table.
106: *
107: * @param table A Hastable.
108: * @param key A String with the key.
109: * @return A Vector with the pair key/value, or null.
110: */
111: private Vector getValues(Hashtable table, String key) {
112: return (Vector) table.get(key);
113: }
114:
115: /**
116: * Gets all form messages for a given form.
117: *
118: * @param formName A String with the form name.
119: * @return A FormMessage[].
120: */
121: public FormMessage[] getFormMessages(String formName) {
122: Vector messages, fields;
123: String messageName, fieldName;
124: messages = getValues(forms_messages, formName);
125: if (messages != null) {
126: FormMessage[] result = new FormMessage[messages.size()];
127: for (int i = 0; i < messages.size(); i++) {
128: result[i] = new FormMessage(formName);
129: messageName = (String) messages.elementAt(i);
130: result[i].setMessage(messageName);
131: fields = getValues(messages_fields, messageName);
132: for (int j = 0; j < fields.size(); j++) {
133: fieldName = (String) fields.elementAt(j);
134: if (formHasField(formName, fieldName)) {
135: result[i].setFieldName(fieldName);
136: }
137: }
138: }
139: return result;
140: }
141: return new FormMessage[0];
142: }
143:
144: /**
145: * Get form messages for a given form and field.
146: *
147: * @param formName A String with the form name.
148: * @param fieldName A String with the field name.
149: * @return A FormMessage[].
150: */
151: public FormMessage[] getFormMessages(String formName,
152: String fieldName) {
153: String key = formName + "-" + fieldName;
154:
155: Vector messages = getValues(fields_messages, key);
156: String messageName;
157:
158: if (messages != null) {
159: FormMessage[] result = new FormMessage[messages.size()];
160: for (int i = 0; i < messages.size(); i++) {
161: result[i] = new FormMessage(formName, fieldName);
162: messageName = (String) messages.elementAt(i);
163: result[i].setMessage(messageName);
164: }
165: return result;
166: }
167: return new FormMessage[0];
168: }
169:
170: /**
171: * Check whether a form as a field.
172: *
173: * @param formName A String with the form name.
174: * @param fieldName A String with the field name.
175: * @return True if form has the field.
176: */
177: private boolean formHasField(String formName, String fieldName) {
178: List fields = getValues(forms_fields, formName);
179: for (Iterator iter = fields.iterator(); iter.hasNext();) {
180: if (fieldName.equals(iter.next().toString())) {
181: return true;
182: }
183: }
184: return false;
185: }
186: }
|