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