1 40 package org.jahia.engines.validation; 41 42 import java.util.HashMap ; 43 import java.util.Iterator ; 44 45 import javax.servlet.http.HttpServletRequest ; 46 47 import org.apache.commons.beanutils.PropertyUtils; 48 import org.apache.commons.validator.Arg; 49 import org.apache.commons.validator.Field; 50 import org.apache.commons.validator.ValidatorAction; 51 import org.apache.log4j.Logger; 52 import org.apache.struts.action.ActionMessages; 53 import org.apache.struts.validator.FieldChecks; 54 import org.apache.struts.validator.Resources; 55 import org.jahia.bin.Jahia; 56 import org.jahia.engines.shared.JahiaPageEngineTempBean; 57 import org.jahia.engines.shared.Page_Field; 58 import org.jahia.exceptions.JahiaSessionExpirationException; 59 import org.jahia.params.ParamBean; 60 import org.jahia.services.sites.SiteLanguageSettings; 61 62 71 public class JahiaFieldChecks extends FieldChecks { 72 73 private static final Logger logger = 74 Logger.getLogger(JahiaFieldChecks.class); 75 76 79 public JahiaFieldChecks() { 80 super(); 81 } 82 83 93 public static boolean validateMandatoryMLText( 94 Object bean, 95 ValidatorAction va, 96 Field field, 97 ActionMessages errors, 98 HttpServletRequest request) { 99 100 Arg mltProperty = field.getArg("mltProperty", 0); 101 Object value = getPropertyValue(bean, 102 mltProperty != null && "mltProperty".equals(mltProperty.getName())? 103 mltProperty.getKey() : field.getProperty()); 104 105 if (!(value instanceof JahiaMltHelper)) { 106 if (value != null) { 107 logger.error( 108 "property value must be an JahiaMltHelper " 109 + field.getProperty()); 110 } 111 return false; 112 } else { 113 JahiaMltHelper mltHelper = (JahiaMltHelper)value; 114 if (!isAllMandatoryLanguagesSet(mltHelper)) { 115 errors.add( 116 field.getKey(), 117 Resources.getActionMessage(request, va, field)); 118 return false; 119 } 120 return true; 121 } 122 } 123 124 135 public static boolean validateMandatoryMLTextIfSet( 136 Object bean, 137 ValidatorAction va, 138 Field field, 139 ActionMessages errors, 140 HttpServletRequest request) { 141 142 Arg mltProperty = field.getArg("mltProperty", 0); 143 Object value = getPropertyValue(bean, 144 mltProperty != null && "mltProperty".equals(mltProperty.getName())? 145 mltProperty.getKey() : field.getProperty()); 146 147 if (!(value instanceof JahiaMltHelper)) { 148 if (value != null) { 149 logger.error( 150 "property value must be an JahiaMltHelper " 151 + field.getProperty()); 152 } 153 return false; 154 } else { 155 JahiaMltHelper mltHelper = (JahiaMltHelper)value; 156 String [] text = mltHelper.getText(); 157 boolean allEmpty = true; 158 if (text != null) { 159 for (int i = 0; i < text.length && allEmpty; i++) { 160 if (text[i] != null && text[i].length() > 0) { 161 allEmpty = false; 162 } 163 } 164 } 165 if (!allEmpty && !isAllMandatoryLanguagesSet(mltHelper)) { 166 errors.add( 167 field.getKey(), 168 Resources.getActionMessage(request, va, field)); 169 return false; 170 } 171 return true; 172 } 173 } 174 175 186 public static boolean validateMandatoryTitleIfLinkValid(Object bean, 187 ValidatorAction va, Field field, ActionMessages errors, 188 HttpServletRequest request) { 189 boolean fieldValid = false; 190 String value = (String ) getPropertyValue(bean, field.getProperty()); 191 if (value != null) { 192 fieldValid = true; 193 } else { 194 ParamBean jParams = Jahia.getThreadParamBean(); 195 HashMap pageBeans = null; 196 if (jParams != null) { 197 try { 198 pageBeans = (HashMap ) jParams.getSession().getAttribute( 199 "Page_Field.PageBeans"); 200 } catch (JahiaSessionExpirationException e) { 201 logger.warn("Session expired - cannot validate", e); 202 } 203 } 204 if (pageBeans == null) { 205 pageBeans = new HashMap (); 206 } 207 208 JahiaPageEngineTempBean pageBean = (JahiaPageEngineTempBean) pageBeans 209 .get(field.getProperty()); 210 211 if (pageBean == null || Page_Field.RESET_LINK.equals(pageBean.getOperation())) { 212 fieldValid = true; 213 } 214 } 215 return fieldValid; 216 } 217 218 228 public static boolean validateMaxLengthMLText( 229 Object bean, 230 ValidatorAction va, 231 Field field, 232 ActionMessages errors, 233 HttpServletRequest request) { 234 235 Arg mltProperty = field.getArg("mltProperty", 0); 236 Object value = getPropertyValue(bean, 237 mltProperty != null && "mltProperty".equals(mltProperty.getName())? 238 mltProperty.getKey() : field.getProperty()); 239 240 if (!(value instanceof JahiaMltHelper)) { 241 if (value != null) { 242 logger.error( 243 "property value must be an JahiaMltHelper " 244 + field.getProperty()); 245 } 246 return false; 247 } else { 248 JahiaMltHelper mltHelper = (JahiaMltHelper)value; 249 250 int maxLength = Integer.parseInt(field.getVarValue("maxlength")); 251 String [] texts = mltHelper.getText(); 252 for (int i = 0; i < texts.length; i++) { 253 if (texts[i] != null && texts[i].length() > maxLength) { 254 errors.add( 255 field.getKey(), 256 Resources.getActionMessage(request, va, field)); 257 return false; 258 } 259 } 260 return true; 261 } 262 } 263 264 274 public static boolean validateMinLengthMLText( 275 Object bean, 276 ValidatorAction va, 277 Field field, 278 ActionMessages errors, 279 HttpServletRequest request) { 280 281 Arg mltProperty = field.getArg("mltProperty", 0); 282 Object value = getPropertyValue(bean, 283 mltProperty != null && "mltProperty".equals(mltProperty.getName())? 284 mltProperty.getKey() : field.getProperty()); 285 286 if (!(value instanceof JahiaMltHelper)) { 287 if (value != null) { 288 logger.error( 289 "property value must be an JahiaMltHelper " 290 + field.getProperty()); 291 } 292 return false; 293 } else { 294 JahiaMltHelper mltHelper = (JahiaMltHelper)value; 295 296 int minLength = Integer.parseInt(field.getVarValue("minlength")); 297 String [] texts = mltHelper.getText(); 298 for (int i = 0; i < texts.length; i++) { 299 if (texts[i] != null && texts[i].length() < minLength) { 300 errors.add( 301 field.getKey(), 302 Resources.getActionMessage(request, va, field)); 303 return false; 304 } 305 } 306 return true; 307 } 308 } 309 310 316 public static boolean isAllMandatoryLanguagesSet(JahiaMltHelper mltHelper) { 317 String [] texts = mltHelper.getText(); 318 String [] lang = mltHelper.getLanguage(); 319 320 for (Iterator languageIterator = mltHelper.getLanguageSettings() 321 .listIterator(); languageIterator.hasNext();) { 322 SiteLanguageSettings siteLanguageSettings = (SiteLanguageSettings) languageIterator 323 .next(); 324 if (siteLanguageSettings.isMandatory()) { 325 boolean found = false; 326 for (int i = 0; texts != null && i < texts.length && !found; i++) { 327 if (lang[i].equals("shared") 328 || lang[i].equals(siteLanguageSettings.getCode())) { 329 found = true; 330 if (texts[i] == null || texts[i].length() == 0) { 331 return false; 332 } 333 } 334 } 335 if (!found) 336 return false; 337 } 338 } 339 return true; 340 } 341 342 349 private static Object getPropertyValue(Object bean, String property) { 350 Object value = null; 351 try { 352 value = PropertyUtils.getProperty(bean, property); 353 } catch (Exception ex) { 354 logger.error("unable to get property value " + property, ex); 355 } 356 return value; 357 } 358 359 } 360 | Popular Tags |