/**
 * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Component of: Red Hat Application Server
 *
 * Initial Developers: Aizaz Ahmed
 *                     Vivek Lakshmanan
 *                     Andrew Overholt
 *                     Matthew Wringe
 *
 */
package olstore.validation;


import java.io.Serializable;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.util.ValidatorUtils;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.Resources;

public class Validator implements Serializable {


    /**
     * This validateTwoFields method originates from http://struts.apache.org/userGuide/dev_validator.html
     * and has been slightly alterted.
     * <p>
     * This method validates a text field only if it matches another text field and is not an empty string.
     *
     */
    public static boolean validateTwoFields(
            Object bean,
            ValidatorAction va,
            Field field,
            ActionMessages errors,
            org.apache.commons.validator.Validator validator,
            HttpServletRequest request) {

        String value = ValidatorUtils.getValueAsString(
                bean,
                field.getProperty());
        String sProperty2 = field.getVarValue("secondProperty");
        String value2 = ValidatorUtils.getValueAsString(
                bean,
                sProperty2);

        if (!GenericValidator.isBlankOrNull(value)) {
            try {
                if (!value.equals(value2)) {
                    errors.add(field.getKey(),
                            Resources.getActionError(request,va,field));

                    return false;
                }
            } catch (Exception e) {
                errors.add(field.getKey(),
                        Resources.getActionError(request, va,field));
                return false;
            }
        }

        return true;
    }

}
