cmailleux    2005/05/18 14:18:57 CEST

  Added files:
    core/src/java/org/jahia/engines/validation 
                                               EngineValidationHelper.java 
                                               JahiaFieldChecks.java 
                                               JahiaMltHelper.java 
                                               ValidationError.java 
                                               ValidationErrorSorter.java 
  Log:
  add commons validation in 4.5
  
  Revision  Changes    Path
  1.1       +96 -0     
jahia/core/src/java/org/jahia/engines/validation/EngineValidationHelper.java 
(new)
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/engines/validation/EngineValidationHelper.java?rev=1.1&content-type=text/plain
  1.1       +256 -0    
jahia/core/src/java/org/jahia/engines/validation/JahiaFieldChecks.java (new)
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/engines/validation/JahiaFieldChecks.java?rev=1.1&content-type=text/plain
  1.1       +142 -0    
jahia/core/src/java/org/jahia/engines/validation/JahiaMltHelper.java (new)
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/engines/validation/JahiaMltHelper.java?rev=1.1&content-type=text/plain
  1.1       +70 -0     
jahia/core/src/java/org/jahia/engines/validation/ValidationError.java (new)
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/engines/validation/ValidationError.java?rev=1.1&content-type=text/plain
  1.1       +116 -0    
jahia/core/src/java/org/jahia/engines/validation/ValidationErrorSorter.java 
(new)
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/engines/validation/ValidationErrorSorter.java?rev=1.1&content-type=text/plain
  
  
  
  Index: EngineValidationHelper.java
  ====================================================================
  /*
   *                                   ____.
   *                       __/\ ______|    |__/\.     _______
   *            __   .____|    |       \   |    +----+       \
   *    _______|  /--|    |    |    -   \  _    |    :    -   \_________
   *   \\______: :---|    :    :           |    :    |         \________>
   *           |__\---\_____________:______:    :____|____:_____\
   *                                      /_____|
   *
   *              . . . i n   j a h i a   w e   t r u s t . . .
   *
   *
   *
   * ----- BEGIN LICENSE BLOCK -----
   * Version: JCSL 1.0
   *
   * The contents of this file are subject to the Jahia Community Source License
   * 1.0 or later (the "License"); you may not use this file except in
   * compliance with the License. You may obtain a copy of the License at
   * http://www.jahia.org/license
   *
   * Software distributed under the License is distributed on an "AS IS" basis,
   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
   * for the rights, obligations and limitations governing use of the contents
   * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
   * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
   * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
   *
   * The Shared Modifications are Jahia View Helper.
   *
   * The Developer of the Shared Modifications is Jahia Solution S�rl.
   * Portions created by the Initial Developer are Copyright (C) 2002 by the
   * Initial Developer. All Rights Reserved.
   *
   * Contributor(s):
   * 28-Feb-2005, Commaro, Benjamin Papez
   *
   * ----- END LICENSE BLOCK -----
   */
  
  
  package org.jahia.engines.validation;
  
  import java.util.*;
  
  
  /**
   *
   * <p>Title: EngineValidationHelper</p>
   * <p>Description: This class is used to ease the validation process and 
groups the 
   *                 validation errors per screen. </p>
   * <p>Copyright: Copyright (c) 2004</p>
   * <p>Company: Jahia Ltd</p>
   * @author not attributable
   * @version 1.0
   */
  public class EngineValidationHelper {
  
      private String previousScreen;
      private String nextScreen;
      private ArrayList validationErrors = new ArrayList();
  
      public EngineValidationHelper(){
      }
  
      public String getPreviousScreen(){
          return this.previousScreen;
      }
  
      public void setPreviousScreen(String screen){
          this.previousScreen = screen;
      }
  
      public String getNextScreen(){
          return this.nextScreen;
      }
  
      public void setNextScreen(String screen){
          this.nextScreen = screen;
      }
  
      public void addError(ValidationError ve){
          if ( ve != null ){
              this.validationErrors.add(ve);
          }
      }
  
      public ArrayList getErrors(){
          return this.validationErrors;
      }
  
      public boolean hasErrors(){
          return (this.validationErrors.size()>0);
      }
  }
  
  
  
  
  Index: JahiaFieldChecks.java
  ====================================================================
  /*
   *                                   ____.
   *                       __/\ ______|    |__/\.     _______
   *            __   .____|    |       \   |    +----+       \
   *    _______|  /--|    |    |    -   \  _    |    :    -   \_________
   *   \\______: :---|    :    :           |    :    |         \________>
   *           |__\---\_____________:______:    :____|____:_____\
   *                                      /_____|
   *
   *              . . . i n   j a h i a   w e   t r u s t . . .
   *
   *
   *
   * ----- BEGIN LICENSE BLOCK -----
   * Version: JCSL 1.0
   *
   * The contents of this file are subject to the Jahia Community Source License
   * 1.0 or later (the "License"); you may not use this file except in
   * compliance with the License. You may obtain a copy of the License at
   * http://www.jahia.org/license
   *
   * Software distributed under the License is distributed on an "AS IS" basis,
   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
   * for the rights, obligations and limitations governing use of the contents
   * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
   * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
   * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
   *
   * The Shared Modifications are Jahia View Helper.
   *
   * The Developer of the Shared Modifications is Jahia Solution S�rl.
   * Portions created by the Initial Developer are Copyright (C) 2002 by the
   * Initial Developer. All Rights Reserved.
   *
   * Contributor(s):
   * 28-Feb-2005, Commaro, Benjamin Papez
   *
   * ----- END LICENSE BLOCK -----
   */
  package org.jahia.engines.validation;
  
  import java.util.Iterator;
  
  import javax.servlet.http.HttpServletRequest;
  
  import org.apache.commons.beanutils.PropertyUtils;
  import org.apache.commons.validator.*;
  import org.apache.commons.validator.Field;
  import org.apache.commons.validator.ValidatorAction;
  import org.apache.struts.action.ActionMessages;
  import org.apache.struts.validator.FieldChecks;
  import org.apache.struts.validator.Resources;
  import org.jahia.services.sites.SiteLanguageSettings;
  
  /**
  *
  * <p>Title: JahiaFieldChecks</p>
  * <p>Description: This class implements the Jahia specific user input 
validation methods.</p>
  * <p>Copyright: Copyright (c) 2004</p>
  * <p>Company: Jahia Ltd</p>
  * @author not attributable
  * @version 1.0
  */
  public class JahiaFieldChecks extends FieldChecks {
      private static final org.apache.log4j.Logger logger =
          org.apache.log4j.Logger.getLogger(JahiaFieldChecks.class);
  
      /**
       * 
       */
      public JahiaFieldChecks() {
          super();
      }
  
      /**
       * Checks if texts for all languages are provided.
       * 
       * @param bean
       * @param va
       * @param field
       * @param errors
       * @param request
       * @return true if text in all languages is given, false if not
       */
      public static boolean validateMandatoryMLText(
          Object bean,
          ValidatorAction va,
          Field field,
          ActionMessages errors,
          HttpServletRequest request) {
              
          Arg mltProperty = field.getArg("mltProperty", 0);
          Object value = getPropertyValue(bean, 
              mltProperty != null && 
"mltProperty".equals(mltProperty.getName())? 
                  mltProperty.getKey()  : field.getProperty());
  
          if (!(value instanceof JahiaMltHelper)) {
              if (value != null) {
                  logger.error(
                      "property value must be an JahiaMltHelper "
                          + field.getProperty());
              }
              return false;
          } else {
              JahiaMltHelper mltHelper = (JahiaMltHelper)value;
              if (!isAllMandatoryLanguagesSet(mltHelper)) {
                  errors.add(
                      field.getKey(),
                      Resources.getActionMessage(request, va, field));
                  return false;
              }
              return true;
          }
      }
  
      /**
       * Checks if texts for all languages do not exceed a specified maximum 
length.
       * 
       * @param bean
       * @param va
       * @param field
       * @param errors
       * @param request
       * @return true if text in all languages are less than maxlength, false 
if not
       */
      public static boolean validateMaxLengthMLText(
          Object bean,
          ValidatorAction va,
          Field field,
          ActionMessages errors,
          HttpServletRequest request) {
              
          Arg mltProperty = field.getArg("mltProperty", 0);
          Object value = getPropertyValue(bean, 
              mltProperty != null && 
"mltProperty".equals(mltProperty.getName())? 
                  mltProperty.getKey()  : field.getProperty());            
  
          if (!(value instanceof JahiaMltHelper)) {
              if (value != null) {
                  logger.error(
                      "property value must be an JahiaMltHelper "
                          + field.getProperty());
              }
              return false;
          } else {
              JahiaMltHelper mltHelper = (JahiaMltHelper)value;
  
              int maxLength = Integer.parseInt(field.getVarValue("maxlength"));
              String[] texts = mltHelper.getText();
              for (int i = 0; i < texts.length; i++) {
                  if (texts[i] != null && texts[i].length() > maxLength) {
                      errors.add(
                          field.getKey(),
                          Resources.getActionMessage(request, va, field));
                      return false;
                  }
              }
              return true;
          }
      }
  
      /**
       * Checks if texts for all languages isn't less than a specified minimum 
length.
       * 
       * @param bean
       * @param va
       * @param field
       * @param errors
       * @param request
       * @return true if text in all languages are not less than minlength, 
false if they are
       */
      public static boolean validateMinLengthMLText(
          Object bean,
          ValidatorAction va,
          Field field,
          ActionMessages errors,
          HttpServletRequest request) {
              
          Arg mltProperty = field.getArg("mltProperty", 0);
          Object value = getPropertyValue(bean, 
              mltProperty != null && 
"mltProperty".equals(mltProperty.getName())? 
                  mltProperty.getKey()  : field.getProperty());
  
          if (!(value instanceof JahiaMltHelper)) {
              if (value != null) {
                  logger.error(
                      "property value must be an JahiaMltHelper "
                          + field.getProperty());
              }
              return false;
          } else {
              JahiaMltHelper mltHelper = (JahiaMltHelper)value;
  
              int minLength = Integer.parseInt(field.getVarValue("minlength"));
              String[] texts = mltHelper.getText();
              for (int i = 0; i < texts.length; i++) {
                  if (texts[i] != null && texts[i].length() < minLength) {
                      errors.add(
                          field.getKey(),
                          Resources.getActionMessage(request, va, field));
                      return false;
                  }
              }
              return true;
          }
      }    
      
      /**
       * Check if all mandatory languages are set within the passed 
JahiaMltHelper Object
       * 
       * @param mltHelper
       * @return true if text in all languages is given, false if not
       */
      public static boolean isAllMandatoryLanguagesSet(JahiaMltHelper 
mltHelper) {
          String[] texts = mltHelper.getText();
          String[] lang = mltHelper.getLanguage();
          for (int i = 0; i < texts.length; i++) {
              boolean found = false;
              for (Iterator languageIterator =
                  mltHelper.getLanguageSettings().listIterator();
                  languageIterator.hasNext() && !found;
                  ) {
                  SiteLanguageSettings siteLanguageSettings =
                      (SiteLanguageSettings)languageIterator.next();
  
                  if (lang[i].equals("shared") || 
                        lang[i].equals(siteLanguageSettings.getCode())) {
                      found = true;
                      if (siteLanguageSettings.isMandatory()
                          && (texts[i] == null || texts[i].length() == 0)) {
                          return false;
                      }
                  }
              }
          }
          return true;
      }
  
      /**
       * Uses PropertyUtils to get property from bean.
       * If PropertyUtils.getProperty() throws an exception, the exception is 
logged and null is returned.
       * @param bean
       * @param property
       * @return the bean property or null
       */
      private static Object getPropertyValue(Object bean, String property) {
          Object value = null;
          try {
              value = PropertyUtils.getProperty(bean, property);
          } catch (Exception ex) {
              logger.error("unable to get property value " + property, ex);
          }
          return value;
      }
  
  }
  
  
  
  Index: JahiaMltHelper.java
  ====================================================================
  /*
   *                                   ____.
   *                       __/\ ______|    |__/\.     _______
   *            __   .____|    |       \   |    +----+       \
   *    _______|  /--|    |    |    -   \  _    |    :    -   \_________
   *   \\______: :---|    :    :           |    :    |         \________>
   *           |__\---\_____________:______:    :____|____:_____\
   *                                      /_____|
   *
   *              . . . i n   j a h i a   w e   t r u s t . . .
   *
   *
   *
   * ----- BEGIN LICENSE BLOCK -----
   * Version: JCSL 1.0
   *
   * The contents of this file are subject to the Jahia Community Source License
   * 1.0 or later (the "License"); you may not use this file except in
   * compliance with the License. You may obtain a copy of the License at
   * http://www.jahia.org/license
   *
   * Software distributed under the License is distributed on an "AS IS" basis,
   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
   * for the rights, obligations and limitations governing use of the contents
   * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
   * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
   * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
   *
   * The Shared Modifications are Jahia View Helper.
   *
   * The Developer of the Shared Modifications is Jahia Solution S�rl.
   * Portions created by the Initial Developer are Copyright (C) 2002 by the
   * Initial Developer. All Rights Reserved.
   *
   * Contributor(s):
   * 28-Feb-2005, Commaro, Benjamin Papez
   *
   * ----- END LICENSE BLOCK -----
   */
  package org.jahia.engines.validation;
  
  import java.util.*;
  
  /**
  *
  * <p>Title: JahiaMltHelper</p>
  * <p>Description: This class converts all languages of a field to an array. 
This is then
  *                 used in the user input validation methods.</p>
  * <p>Copyright: Copyright (c) 2004</p>
  * <p>Company: Jahia Ltd</p>
  * @author not attributable
  * @version 1.0
  */
  public class JahiaMltHelper {
      private String[] text = null;
      private String[] language = null;
      private List languageSettings = null;
  
      /**
       * 
       */
      public JahiaMltHelper(List currentLanguageSettings) {
          super();
          languageSettings = currentLanguageSettings;
      }
  
      /**
       * Get the text for a given language key, returns empty string if not 
found
       * 
       * @param lang a language key
       * @return text
       */
      public String getText(String lang) {
          for (int i = 0; i < language.length; i++) {
              if (language[i].equals(lang)) {
                  return text[i];
              }
          }
          return "";
      }
  
      /**
       * @return language
       */
      public String[] getLanguage() {
          return language;
      }
  
      /**
       * @return text
       */
      public String[] getText() {
          return text;
      }
  
      /**
       * @param strings
       */
      public void setLanguage(String[] strings) {
          language = strings;
      }
  
      /**
       * @param strings
       */
      public void setText(String[] strings) {
          text = strings;
      }
  
      /**
       * 
       * @param lang
       * @param txt
       */
      public void addMltItem(String lang, String txt) {
          if (language != null && text != null) {
              int oldLen = language.length;
              String newLanguage[] = new String[oldLen + 1];
              String newText[] = new String[oldLen + 1];
  
              System.arraycopy(language, 0, newLanguage, 0, oldLen);
              System.arraycopy(text, 0, newText, 0, oldLen);
              newLanguage[oldLen] = lang;
              newText[oldLen] = txt;
              language = newLanguage;
              text = newText;
          } else {
              language = new String[1];
              language[0] = lang;
              text = new String[1];
              text[0] = txt;
          }
      }
  
      /**
       * @return languageSettings
       */
      public List getLanguageSettings() {
          return languageSettings;
      }
  
  }
  
  
  
  Index: ValidationError.java
  ====================================================================
  /*
   *                                   ____.
   *                       __/\ ______|    |__/\.     _______
   *            __   .____|    |       \   |    +----+       \
   *    _______|  /--|    |    |    -   \  _    |    :    -   \_________
   *   \\______: :---|    :    :           |    :    |         \________>
   *           |__\---\_____________:______:    :____|____:_____\
   *                                      /_____|
   *
   *              . . . i n   j a h i a   w e   t r u s t . . .
   *
   *
   *
   * ----- BEGIN LICENSE BLOCK -----
   * Version: JCSL 1.0
   *
   * The contents of this file are subject to the Jahia Community Source License
   * 1.0 or later (the "License"); you may not use this file except in
   * compliance with the License. You may obtain a copy of the License at
   * http://www.jahia.org/license
   *
   * Software distributed under the License is distributed on an "AS IS" basis,
   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
   * for the rights, obligations and limitations governing use of the contents
   * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
   * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
   * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
   *
   * The Shared Modifications are Jahia View Helper.
   *
   * The Developer of the Shared Modifications is Jahia Solution S�rl.
   * Portions created by the Initial Developer are Copyright (C) 2002 by the
   * Initial Developer. All Rights Reserved.
   *
   * Contributor(s):
   * 28-Feb-2005, Commaro, Benjamin Papez
   *
   * ----- END LICENSE BLOCK -----
   */
  
  package org.jahia.engines.validation;
  
  
  /**
   *
   * <p>Title: ValidationError</p>
   * <p>Description: The object of this class holds a validation error for a 
Jahia field.</p>
   * <p>Copyright: Copyright (c) 2004</p>
   * <p>Company: Jahia Ltd</p>
   * @author not attributable
   * @version 1.0
   */
  public class ValidationError {
  
    private Object source;
    private String msgError;
  
    public ValidationError(Object newSource, String newMsgError) {
      this.source = newSource;
      this.msgError = newMsgError;
    }
  
    public Object getSource() {
      return this.source;
    }
  
    public String getMsgError() {
      return this.msgError;
    }
  }
  
  
  
  Index: ValidationErrorSorter.java
  ====================================================================
  /*
   *                                   ____.
   *                       __/\ ______|    |__/\.     _______
   *            __   .____|    |       \   |    +----+       \
   *    _______|  /--|    |    |    -   \  _    |    :    -   \_________
   *   \\______: :---|    :    :           |    :    |         \________>
   *           |__\---\_____________:______:    :____|____:_____\
   *                                      /_____|
   *
   *              . . . i n   j a h i a   w e   t r u s t . . .
   *
   *
   *
   * ----- BEGIN LICENSE BLOCK -----
   * Version: JCSL 1.0
   *
   * The contents of this file are subject to the Jahia Community Source License
   * 1.0 or later (the "License"); you may not use this file except in
   * compliance with the License. You may obtain a copy of the License at
   * http://www.jahia.org/license
   *
   * Software distributed under the License is distributed on an "AS IS" basis,
   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
   * for the rights, obligations and limitations governing use of the contents
   * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
   * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
   * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
   *
   * The Shared Modifications are Jahia View Helper.
   *
   * The Developer of the Shared Modifications is Jahia Solution S�rl.
   * Portions created by the Initial Developer are Copyright (C) 2002 by the
   * Initial Developer. All Rights Reserved.
   *
   * Contributor(s):
   * 28-Feb-2005, Commaro, Benjamin Papez
   *
   * ----- END LICENSE BLOCK -----
   */
  
  package org.jahia.engines.validation;
  
  import java.util.Comparator;
  
  import org.jahia.data.containers.ContainerEditViewFieldGroup;
  import org.jahia.data.fields.JahiaField;
  import org.jahia.exceptions.JahiaException;
  
  /**
  *
  * <p>Title: ValidationErrorSorter</p>
  * <p>Description: This class sorts the validation messages. The messages, 
which are on the currently
  *                 processed page are the first items. Then the messages are 
sorted according to the
  *                 appearance on the screen (first tab, first field to last 
tab, last field).</p>
  * <p>Copyright: Copyright (c) 2004</p>
  * <p>Company: Jahia Ltd</p>
  * @author not attributable
  * @version 1.0
  */
  
  public class ValidationErrorSorter implements Comparator {
    ContainerEditViewFieldGroup fieldGroup = null;
    public ValidationErrorSorter(ContainerEditViewFieldGroup currentFieldGroup) 
{
      super();
      fieldGroup = currentFieldGroup;
    }
  
    public int compare(Object o1, Object o2) {
      Object leftObj = ((ValidationError)o1).getSource();
      Object rightObj = ((ValidationError)o2).getSource();
  
      if (leftObj != null
        && rightObj != null
        && leftObj instanceof JahiaField
        && rightObj instanceof JahiaField
        && fieldGroup != null) {
  
        JahiaField leftField = (JahiaField)leftObj;
        JahiaField rightField = (JahiaField)rightObj;
  
        try {
          boolean leftExists =
            fieldGroup.fieldExists(leftField.getDefinition().getName());
          boolean rightExists =
            fieldGroup.fieldExists(
              rightField.getDefinition().getName());
  
          if (leftExists != rightExists)
            return leftExists == true ? -1 : 1;
          else {
            return Math.abs(leftField.getID())
              < Math.abs(rightField.getID())
              ? -1
              : 1;
          }
        } catch (JahiaException e) {}
      }
      if (leftObj != null
        && leftObj instanceof JahiaField
        && (rightObj == null || !(rightObj instanceof JahiaField)))
        return -1;
  
      if (rightObj != null
        && rightObj instanceof JahiaField
        && (leftObj == null || !(leftObj instanceof JahiaField)))
        return 1;
  
      return leftObj.hashCode() < rightObj.hashCode()
        ? -1
        : (leftObj.equals(rightObj) ? 0 : 1);
    }
  
    public boolean equals(Object obj) {
      return obj instanceof ValidationErrorSorter ? true : false;
    }
  }
  

Reply via email to