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