1 31 32 package org.opencms.frontend.templateone.form; 33 34 import org.opencms.main.CmsLog; 35 import org.opencms.util.CmsStringUtil; 36 37 import java.util.ArrayList ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 import java.util.regex.Pattern ; 41 import java.util.regex.PatternSyntaxException ; 42 43 import org.apache.commons.logging.Log; 44 45 54 public abstract class A_CmsField implements I_CmsField { 55 56 57 private static final Log LOG = CmsLog.getLog(CmsFormHandler.class); 58 59 private List m_items; 60 private String m_label; 61 private boolean m_mandatory; 62 private String m_name; 63 private String m_validationExpression; 64 private String m_value; 65 private String m_errorMessage; 66 67 70 public A_CmsField() { 71 72 super(); 73 74 m_items = new ArrayList (); 75 m_mandatory = false; 76 m_value = ""; 77 m_validationExpression = ""; 78 } 79 80 83 protected void finalize() throws Throwable { 84 85 try { 86 87 if (m_items != null) { 88 m_items.clear(); 89 } 90 91 m_label = null; 92 m_name = null; 93 m_validationExpression = null; 94 m_value = null; 95 } catch (Throwable t) { 96 } 98 99 super.finalize(); 100 } 101 102 105 public List getItems() { 106 107 return m_items; 108 } 109 110 113 public String getLabel() { 114 115 return m_label; 116 } 117 118 121 public String getName() { 122 123 return m_name; 124 } 125 126 129 public String getValidationExpression() { 130 131 return m_validationExpression; 132 } 133 134 137 public String getValue() { 138 139 return m_value; 140 } 141 142 145 public boolean isMandatory() { 146 147 return m_mandatory; 148 } 149 150 153 public boolean needsItems() { 154 155 return (CmsCheckboxField.class.isAssignableFrom(getClass()) 156 || CmsSelectionField.class.isAssignableFrom(getClass()) || CmsRadioButtonField.class.isAssignableFrom(getClass())); 157 } 158 159 171 protected void setItems(List items) { 172 173 m_items = items; 174 } 175 176 181 protected void setLabel(String description) { 182 183 m_label = description; 184 } 185 186 191 protected void setMandatory(boolean mandatory) { 192 193 m_mandatory = mandatory; 194 } 195 196 201 protected void setName(String name) { 202 203 m_name = name; 204 } 205 206 211 protected void setValidationExpression(String expression) { 212 213 m_validationExpression = expression; 214 } 215 216 221 protected void setValue(String value) { 222 223 m_value = value; 224 } 225 226 231 protected String validateConstraints() { 232 233 if (isMandatory()) { 234 235 if (needsItems()) { 237 238 Iterator k = m_items.iterator(); 240 boolean isSelected = false; 241 while (k.hasNext()) { 242 243 CmsFieldItem currentItem = (CmsFieldItem)k.next(); 244 if (currentItem.isSelected()) { 245 isSelected = true; 246 continue; 247 } 248 } 249 250 if (!isSelected) { 251 return CmsFormHandler.ERROR_MANDATORY; 253 } 254 } else { 255 256 if (CmsStringUtil.isEmpty(m_value)) { 258 return CmsFormHandler.ERROR_MANDATORY; 259 } 260 } 261 } 262 263 return null; 264 } 265 266 271 protected String validateValue() { 272 273 if (CmsStringUtil.isNotEmpty(m_value) && (!"".equals(m_validationExpression))) { 275 276 Pattern pattern = null; 277 try { 278 279 pattern = Pattern.compile(m_validationExpression); 280 if (!pattern.matcher(m_value).matches()) { 281 return CmsFormHandler.ERROR_VALIDATION; 282 } 283 } catch (PatternSyntaxException e) { 284 285 if (LOG.isErrorEnabled()) { 287 LOG.error(Messages.get().getBundle().key(Messages.LOG_ERR_PATTERN_SYNTAX_0), e); 288 } 289 } 290 } 291 292 return null; 293 } 294 295 298 public String validate(CmsFormHandler formHandler) { 299 300 String validationError = validateConstraints(); 302 if (CmsStringUtil.isEmpty(validationError)) { 303 304 validationError = validateValue(); 306 } 307 308 return validationError; 309 } 310 311 316 protected void setErrorMessage(String errorMessage) { 317 318 m_errorMessage = errorMessage; 319 } 320 321 324 public String getErrorMessage() { 325 326 return m_errorMessage; 327 } 328 329 333 public String toString() { 334 335 String result; 336 if (needsItems()) { 337 StringBuffer fieldValue = new StringBuffer (8); 339 Iterator k = getItems().iterator(); 340 boolean isSelected = false; 341 while (k.hasNext()) { 342 CmsFieldItem currentItem = (CmsFieldItem)k.next(); 343 if (currentItem.isSelected()) { 344 if (isSelected) { 345 fieldValue.append(", "); 346 } 347 fieldValue.append(currentItem.getLabel()); 348 isSelected = true; 349 } 350 } 351 result = fieldValue.toString(); 352 } else { 353 result = getValue(); 355 } 356 357 return result; 358 } 359 } 360 | Popular Tags |