View Javadoc

1   package org.apache.turbine.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Hashtable;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Vector;
26  
27  /***
28   * Used for adding and accessing messages that relate to a specific
29   * form and field.  Allows to query for messages by form name and
30   * field name.  Used together with FormMessage class.
31   *
32   * @author <a href="mailto:neeme@one.lv">Neeme Praks</a>
33   * @version $Id: FormMessages.java 534527 2007-05-02 16:10:59Z tv $
34   */
35  public class FormMessages
36  {
37      private Hashtable forms_messages;
38      private Hashtable fields_messages;
39      private Hashtable messages_fields;
40      private Hashtable forms_fields;
41  
42      /***
43       * Constructor.
44       */
45      public FormMessages()
46      {
47          forms_messages = new Hashtable();
48          fields_messages = new Hashtable();
49          messages_fields = new Hashtable();
50          forms_fields = new Hashtable();
51      }
52  
53      /***
54       * Sets a message for a field of a form.  The message is given as
55       * a long representing a return code.
56       *
57       * @param formName A String with the form name.
58       * @param fieldName A String with the field name.
59       * @param returnCode A long with the return code.
60       */
61      public void setMessage(String formName,
62                             String fieldName,
63                             long returnCode)
64      {
65          setMessage(formName, fieldName, String.valueOf(returnCode));
66      }
67  
68      /***
69       * Sets a message for a field of a form.  The message is given as
70       * a String.
71       *
72       * @param formName A String with the form name.
73       * @param fieldName A String with the field name.
74       * @param messageName A String with the message.
75       */
76      public void setMessage(String formName,
77                             String fieldName,
78                             String messageName)
79      {
80          fieldName = formName + "-" + fieldName;
81          addValue(forms_messages, formName, messageName);
82          addValue(fields_messages, fieldName, messageName);
83          addValue(messages_fields, messageName, fieldName);
84          addValue(forms_fields, formName, fieldName);
85      }
86  
87      /***
88       * Adds a pair key/value to a table, making sure not to add
89       * duplicate keys.
90       *
91       * @param table A Hastable.
92       * @param key A String with the key.
93       * @param value A String with value.
94       */
95      private void addValue(Hashtable table,
96                            String key,
97                            String value)
98      {
99          Vector values;
100 
101         if (!table.containsKey(key))
102         {
103             values = new Vector();
104             values.addElement(value);
105             table.put(key, values);
106         }
107         else
108         {
109             values = ((Vector) table.get(key));
110             if (!values.contains(value))
111             {
112                 values.addElement(value);
113             }
114         }
115     }
116 
117     /***
118      * Gets a pair key/value from a table.
119      *
120      * @param table A Hastable.
121      * @param key A String with the key.
122      * @return A Vector with the pair key/value, or null.
123      */
124     private Vector getValues(Hashtable table, String key)
125     {
126         return (Vector) table.get(key);
127     }
128 
129     /***
130      * Gets all form messages for a given form.
131      *
132      * @param formName A String with the form name.
133      * @return A FormMessage[].
134      */
135     public FormMessage[] getFormMessages(String formName)
136     {
137         Vector messages, fields;
138         String messageName, fieldName;
139         messages = getValues(forms_messages, formName);
140         if (messages != null)
141         {
142             FormMessage[] result = new FormMessage[messages.size()];
143             for (int i = 0; i < messages.size(); i++)
144             {
145                 result[i] = new FormMessage(formName);
146                 messageName = (String) messages.elementAt(i);
147                 result[i].setMessage(messageName);
148                 fields = getValues(messages_fields, messageName);
149                 for (int j = 0; j < fields.size(); j++)
150                 {
151                     fieldName = (String) fields.elementAt(j);
152                     if (formHasField(formName, fieldName))
153                     {
154                         result[i].setFieldName(fieldName);
155                     }
156                 }
157             }
158             return result;
159         }
160         return new FormMessage[0];
161     }
162 
163     /***
164      * Get form messages for a given form and field.
165      *
166      * @param formName A String with the form name.
167      * @param fieldName A String with the field name.
168      * @return A FormMessage[].
169      */
170     public FormMessage[] getFormMessages(String formName, String fieldName)
171     {
172         String key = formName + "-" + fieldName;
173 
174         Vector messages = getValues(fields_messages, key);
175         String messageName;
176 
177         if (messages != null)
178         {
179             FormMessage[] result = new FormMessage[messages.size()];
180             for (int i = 0; i < messages.size(); i++)
181             {
182                 result[i] = new FormMessage(formName, fieldName);
183                 messageName = (String) messages.elementAt(i);
184                 result[i].setMessage(messageName);
185             }
186             return result;
187         }
188         return new FormMessage[0];
189     }
190 
191     /***
192      * Check whether a form as a field.
193      *
194      * @param formName A String with the form name.
195      * @param fieldName A String with the field name.
196      * @return True if form has the field.
197      */
198     private boolean formHasField(String formName,
199                                  String fieldName)
200     {
201         List fields = getValues(forms_fields, formName);
202         for (Iterator iter = fields.iterator(); iter.hasNext();)
203         {
204             if (fieldName.equals(iter.next().toString()))
205             {
206                 return true;
207             }
208         }
209         return false;
210     }
211 }