1 24 package org.ofbiz.base.util; 25 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Locale ; 31 import java.util.Set ; 32 import java.util.TreeSet ; 33 34 41 public class MessageString { 42 43 public static final String module = MessageString.class.getName(); 44 45 protected String message; 46 protected String fieldName; 47 protected String toFieldName; 48 protected Throwable sourceError; 49 protected Locale locale; 50 protected String propertyResource; 51 protected String propertyName; 52 protected boolean isError = true; 53 54 public static List getMessagesForField(String fieldName, boolean convertToStrings, List messageStringList) { 55 if (fieldName == null) { 56 return Collections.EMPTY_LIST; 57 } 58 Set fieldSet = new TreeSet (); 59 fieldSet.add(fieldName); 60 return getMessagesForField(fieldSet, convertToStrings, messageStringList); 61 } 62 public static List getMessagesForField(String fieldName1, String fieldName2, String fieldName3, String fieldName4, boolean convertToStrings, List messageStringList) { 63 Set fieldSet = new TreeSet (); 64 if (fieldName1 != null && fieldName1.length() > 0) fieldSet.add(fieldName1); 65 if (fieldName2 != null && fieldName2.length() > 0) fieldSet.add(fieldName2); 66 if (fieldName3 != null && fieldName3.length() > 0) fieldSet.add(fieldName3); 67 if (fieldName4 != null && fieldName4.length() > 0) fieldSet.add(fieldName4); 68 return getMessagesForField(fieldSet, convertToStrings, messageStringList); 69 } 70 public static List getMessagesForField(Set fieldNameSet, boolean convertToStrings, List messageStringList) { 71 if (messageStringList == null || fieldNameSet == null || fieldNameSet.size() == 0) { 72 return Collections.EMPTY_LIST; 73 } 74 List outList = new ArrayList (messageStringList.size()); 75 Iterator messageStringIter = messageStringList.iterator(); 76 while (messageStringIter.hasNext()) { 77 Object messageStringCur = messageStringIter.next(); 78 if (messageStringCur instanceof MessageString) { 79 MessageString messageString = (MessageString) messageStringCur; 80 if (messageString.isForField(fieldNameSet)) { 81 if (convertToStrings) { 82 outList.add(messageString.toString()); 83 } else { 84 outList.add(messageString); 85 } 86 } 87 } else { 88 continue; 90 } 91 } 92 return outList; 93 } 94 95 102 public MessageString(String message, String fieldName, String propertyResource, String propertyName, Locale locale, boolean isError) { 103 this.message = message; 104 this.fieldName = fieldName; 105 this.locale = locale; 106 this.propertyResource = propertyResource; 107 this.propertyName = propertyName; 108 this.isError = isError; 109 } 110 114 public MessageString(String message, String fieldName, boolean isError) { 115 this.message = message; 116 this.fieldName = fieldName; 117 this.isError = isError; 118 } 119 125 public MessageString(String message, String fieldName, String toFieldName, Throwable sourceError) { 126 this.message = message; 127 this.fieldName = fieldName; 128 this.toFieldName = toFieldName; 129 this.sourceError = sourceError; 130 this.isError = true; 131 } 132 136 public MessageString(String message, Throwable sourceError) { 137 this.message = message; 138 this.sourceError = sourceError; 139 this.isError = true; 140 } 141 142 145 public String getFieldName() { 146 return fieldName; 147 } 148 151 public void setFieldName(String fieldName) { 152 this.fieldName = fieldName; 153 } 154 public boolean isForField(Set fieldNameSet) { 155 if (fieldNameSet == null) { 156 return true; 157 } 158 return fieldNameSet.contains(this.fieldName); 159 } 160 public boolean isForField(String fieldName) { 161 if (this.fieldName == null) { 162 if (fieldName == null) { 163 return true; 164 } else { 165 return false; 166 } 167 } else { 168 return this.fieldName.equals(fieldName); 169 } 170 } 171 172 175 public String getMessage() { 176 return message; 177 } 178 181 public void setMessage(String message) { 182 this.message = message; 183 } 184 187 public Throwable getSourceError() { 188 return sourceError; 189 } 190 193 public void setSourceError(Throwable sourceError) { 194 this.sourceError = sourceError; 195 } 196 199 public String getToFieldName() { 200 return toFieldName; 201 } 202 205 public void setToFieldName(String toFieldName) { 206 this.toFieldName = toFieldName; 207 } 208 209 212 public Locale getLocale() { 213 return locale; 214 } 215 218 public void setLocale(Locale locale) { 219 this.locale = locale; 220 } 221 224 public String getPropertyName() { 225 return propertyName; 226 } 227 230 public void setPropertyName(String propertyName) { 231 this.propertyName = propertyName; 232 } 233 236 public String getPropertyResource() { 237 return propertyResource; 238 } 239 242 public void setPropertyResource(String propertyResource) { 243 this.propertyResource = propertyResource; 244 } 245 246 249 public boolean isError() { 250 return isError; 251 } 252 255 public void setError(boolean isError) { 256 this.isError = isError; 257 } 258 259 public String toString() { 260 return this.message; 261 } 262 } 263 | Popular Tags |