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.List;
26
27 /**
28 * Used for adding and accessing messages that relate to a specific form and field. Allows to query for messages by form
29 * name and field name. Used together with FormMessage class.
30 *
31 * @author <a href="mailto:neeme@one.lv">Neeme Praks</a>
32 * @version $Id$
33 */
34 public class FormMessages
35 {
36 private final Hashtable<String, List<String>> forms_messages;
37
38 private final Hashtable<String, List<String>> fields_messages;
39
40 private final Hashtable<String, List<String>> messages_fields;
41
42 private final Hashtable<String, List<String>> forms_fields;
43
44 /**
45 * Constructor.
46 */
47 public FormMessages()
48 {
49 forms_messages = new Hashtable<>();
50 fields_messages = new Hashtable<>();
51 messages_fields = new Hashtable<>();
52 forms_fields = new Hashtable<>();
53 }
54
55 /**
56 * Sets a message for a field of a form. The message is given as a long representing a return code.
57 *
58 * @param formName A String with the form name.
59 * @param fieldName A String with the field name.
60 * @param returnCode A long with the return code.
61 */
62 public void setMessage( String formName, String fieldName, long returnCode )
63 {
64 setMessage( formName, fieldName, String.valueOf( returnCode ) );
65 }
66
67 /**
68 * Sets a message for a field of a form. The message is given as a String.
69 *
70 * @param formName A String with the form name.
71 * @param fieldName A String with the field name.
72 * @param messageName A String with the message.
73 */
74 public void setMessage( String formName, String fieldName, String messageName )
75 {
76 String formFieldName = formName + "-" + fieldName;
77 addValue( forms_messages, formName, messageName );
78 addValue( fields_messages, formFieldName, messageName );
79 addValue( messages_fields, messageName, formFieldName );
80 addValue( forms_fields, formName, formFieldName );
81 }
82
83 /**
84 * Adds a pair key/value to a table, making sure not to add duplicate keys.
85 *
86 * @param table A Hashtable.
87 * @param key A String with the key.
88 * @param value A String with value.
89 */
90 private void addValue( Hashtable<String, List<String>> table, String key, String value )
91 {
92 List<String> values;
93
94 if ( !table.containsKey( key ) )
95 {
96 values = new ArrayList<>();
97 values.add( value );
98 table.put( key, values );
99 }
100 else
101 {
102 values = table.get( key );
103 if ( !values.contains( value ) )
104 {
105 values.add( value );
106 }
107 }
108 }
109
110 /**
111 * Gets a pair key/value from a table.
112 *
113 * @param table A Hashtable.
114 * @param key A String with the key.
115 * @return A List with the pair key/value, or null.
116 */
117 private final List<String> getValues( Hashtable<String, List<String>> table, String key )
118 {
119 return table.get( key );
120 }
121
122 /**
123 * Gets all form messages for a given form.
124 *
125 * @param formName A String with the form name.
126 * @return A FormMessage[].
127 */
128 public FormMessage[] getFormMessages( String formName )
129 {
130 List<String> messages, fields;
131 String messageName, fieldName;
132 messages = getValues( forms_messages, formName );
133 if ( messages != null )
134 {
135 FormMessage.html#FormMessage">FormMessage[] result = new FormMessage[messages.size()];
136 for ( int i = 0; i < messages.size(); i++ )
137 {
138 result[i] = new FormMessage( formName );
139 messageName = messages.get( i );
140 result[i].setMessage( messageName );
141 fields = getValues( messages_fields, messageName );
142 for (String field : fields)
143 {
144 fieldName = field;
145 if ( formHasField( formName, fieldName ) )
146 {
147 result[i].setFieldName( fieldName );
148 }
149 }
150 }
151 return result;
152 }
153 return null;
154 }
155
156 /**
157 * Get form messages for a given form and field.
158 *
159 * @param formName A String with the form name.
160 * @param fieldName A String with the field name.
161 * @return A FormMessage[].
162 */
163 public FormMessage[] getFormMessages( String formName, String fieldName )
164 {
165 String key = formName + "-" + fieldName;
166
167 List<String> messages = getValues( fields_messages, key );
168 String messageName;
169
170 if ( messages != null )
171 {
172 FormMessage.html#FormMessage">FormMessage[] result = new FormMessage[messages.size()];
173 for ( int i = 0; i < messages.size(); i++ )
174 {
175 result[i] = new FormMessage( formName, fieldName );
176 messageName = messages.get( i );
177 result[i].setMessage( messageName );
178 }
179 return result;
180 }
181 return null;
182 }
183
184 /**
185 * Check whether a form as a field.
186 *
187 * @param formName A String with the form name.
188 * @param fieldName A String with the field name.
189 * @return True if form has the field.
190 */
191 private boolean formHasField( String formName, String fieldName )
192 {
193 List<String> fields = getValues( forms_fields, formName );
194 for (String field : fields)
195 {
196 if ( fieldName.equals( field.toString() ) )
197 {
198 return true;
199 }
200 }
201 return false;
202 }
203 }