/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.spring.services.dwr.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.internna.iwebmvc.core.context.ContextHolder;
import org.internna.iwebmvc.core.crypto.Decipherer;
import org.internna.iwebmvc.core.dao.SecurityDAO;
import org.internna.iwebmvc.core.i18n.Translate;
import org.internna.iwebmvc.core.security.AJAXManageableImageCaptchaService;
import org.internna.iwebmvc.model.DomainEntity;
import org.internna.iwebmvc.spring.mvc.validators.AnnotatedValidator;
import org.internna.iwebmvc.spring.services.dwr.AJAXValidator;
import org.internna.iwebmvc.spring.util.RequestUtils;
import org.internna.iwebmvc.utils.ClassUtils;
import org.internna.iwebmvc.utils.StringUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.Errors;
/**
*
* @author Jose Noheda
* @since 1.0
*/
@RemoteProxy(name = "AJAXValidator")
public class AJAXValidatorImpl implements AJAXValidator {
@Autowired protected SecurityDAO dao;
@Autowired protected Decipherer decipherer;
@Autowired protected AnnotatedValidator validator;
@Autowired protected ContextHolder contextHolder;
@Autowired protected AJAXManageableImageCaptchaService captchaService;
/**
* Validates a field using the default AnnotationVisitor.
*
* @param entity a DomainEntity class name
* @param path any simple path of the parent entity
* @param value the object to validate
* @return a collection of errors in that path
* @throws java.lang.Exception if the parameters are incorrect
*/
@Translate
@RemoteMethod
@SuppressWarnings("unchecked")
public Object validate(Class clazz, String path, DomainEntity value) throws Exception {
Errors e = doValidation(clazz, path, value);
return e.getFieldErrors(path + "*");
}
@Translate
@RemoteMethod
@SuppressWarnings("unchecked")
public Object validatePrimitive(Class clazz, String path, String value) throws Exception {
Errors e = doValidation(clazz, path, value);
return e.getFieldErrors(path);
}
@SuppressWarnings("unchecked")
protected Errors doValidation(Class clazz, String path, Object value) throws Exception {
DomainEntity object = (DomainEntity) ClassUtils.instantiateClass(clazz);
object.initialize(contextHolder);
BeanWrapper validatable = new BeanWrapperImpl(object);
validatable.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
validatable.setPropertyValue(path, value);
Errors errors = new BeanPropertyBindingResult(object, "target");
validator.validate(object, errors);
return errors;
}
@RemoteMethod
public boolean validateCaptcha(String input, boolean dispose) {
boolean validation = false;
if (StringUtils.hasText(input)) {
String id = RequestUtils.getActiveRequest().getSession().getId();
validation = dispose ? captchaService.validateResponseForID(id, input) : captchaService.validateResponse(id, input);
}
return validation;
}
@RemoteMethod
public boolean validateUserName(String userName) throws Exception {
try {
return dao.findUser(userName) != null;
} catch (Exception e) {
return false;
}
}
}