View Javadoc

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