1 package org.apache.turbine.util; 2 3 18 19 import java.util.Hashtable ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Vector ; 23 24 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 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 58 public void setMessage(String formName, 59 String fieldName, 60 long returnCode) 61 { 62 setMessage(formName, fieldName, String.valueOf(returnCode)); 63 } 64 65 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 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 values.addElement(value); 109 } 110 } 111 112 119 private final Vector getValues(Hashtable table, String key) 120 { 121 return (Vector ) table.get(key); 122 } 123 124 130 public FormMessage[] getFormMessages(String formName) 131 { 132 Vector messages, fields; 133 String messageName, fieldName; 134 messages = getValues(forms_messages, formName); 135 if (messages != null) 136 { 137 FormMessage[] result = new FormMessage[messages.size()]; 138 for (int i = 0; i < messages.size(); i++) 139 { 140 result[i] = new FormMessage(formName); 141 messageName = (String ) messages.elementAt(i); 142 result[i].setMessage(messageName); 143 fields = getValues(messages_fields, messageName); 144 for (int j = 0; j < fields.size(); j++) 145 { 146 fieldName = (String ) fields.elementAt(j); 147 if (formHasField(formName, fieldName)) 148 { 149 result[i].setFieldName(fieldName); 150 } 151 } 152 } 153 return result; 154 } 155 return null; 156 } 157 158 165 public FormMessage[] getFormMessages(String formName, String fieldName) 166 { 167 String key = formName + "-" + fieldName; 168 169 Vector messages = getValues(fields_messages, key); 170 String messageName; 171 172 if (messages != null) 173 { 174 FormMessage[] result = new FormMessage[messages.size()]; 175 for (int i = 0; i < messages.size(); i++) 176 { 177 result[i] = new FormMessage(formName, fieldName); 178 messageName = (String ) messages.elementAt(i); 179 result[i].setMessage(messageName); 180 } 181 return result; 182 } 183 return null; 184 } 185 186 193 private boolean formHasField(String formName, 194 String fieldName) 195 { 196 List fields = getValues(forms_fields, formName); 197 for (Iterator iter = fields.iterator(); iter.hasNext();) 198 { 199 if (fieldName.equals(iter.next().toString())) 200 { 201 return true; 202 } 203 } 204 return false; 205 } 206 } 207 | Popular Tags |