Java tutorial
package com.baidu.gcrm.customer.web.validator; import java.util.List; import java.util.Set; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.Errors; import org.springframework.validation.Validator; import com.baidu.gcrm.common.ServiceBeanFactory; import com.baidu.gcrm.common.auth.RequestThreadLocal; import com.baidu.gcrm.customer.model.Customer; import com.baidu.gcrm.customer.web.helper.CustomerBean; import com.baidu.gcrm.customer.web.helper.CustomerType; import com.baidu.gcrm.valuelistcache.model.AgentRegional; import com.baidu.gcrm.valuelistcache.model.Country; import com.baidu.gcrm.valuelistcache.service.AbstractValuelistCacheService; /** * ??gcrm ??CustomerAddValidator ?? ??? chenchunhui01 * 2014415 ?9:58:07 chenchunhui01 2014415 ?9:58:07 * * @version * */ public abstract class CustomerBaseValidator implements Validator { @Override public boolean supports(Class<?> clazz) { return clazz.equals(CustomerBean.class); } /** * * validatorNameAndLicense(???????) */ protected void validatorNameAndLicense(Customer customer, Errors errors) { /** * * 1.+?? ??? 2.??????? 3.????+???? ???? * */ Integer countryId = customer.getCountry(); List<Customer> existCustomers4Name = ServiceBeanFactory.getCustomerService() .findByCountryAndName(customer.getCountry(), customer.getCompanyName()); Long customerId = customer.getId(); if (existCustomers4Name != null) { if (existCustomers4Name.size() > 1) { errors.rejectValue("customer.companyName", "customer.companyName.notunique"); } else if (existCustomers4Name.size() == 1) { Long tempCustomerId = existCustomers4Name.get(0).getId(); if (customerId == null || !tempCustomerId.equals(customerId)) { errors.rejectValue("customer.companyName", "customer.companyName.notunique"); } } } if (errors.hasErrors()) { return; } // ??? String liscenseStr = customer.getBusinessLicense(); if (StringUtils.isBlank(liscenseStr)) { return; } List<Customer> existCustomers4License = ServiceBeanFactory.getCustomerService() .findByCountryAndLiscense(countryId, liscenseStr); if (existCustomers4License != null) { if (existCustomers4License.size() > 1) { errors.rejectValue("customer.businessLicense", "customer.businessLicense.notunique"); } else if (existCustomers4License.size() == 1) { Long tempCustomerId = existCustomers4License.get(0).getId(); if (customerId == null || !tempCustomerId.equals(customerId)) errors.rejectValue("customer.businessLicense", "customer.businessLicense.notunique"); } } } /** * * ??:??? * validatorAgentTypeAndAgentCountry * @: chenchunhui01 * @: 201474 ?3:19:30 * @param customer * @param errors * @return void * @exception * @version */ protected void validatorAgentTypeAndAgentCountry(Customer customer, Errors errors) { if (!CustomerType.offline.equals(customer.getCustomerType())) { return; } if (customer.getAgentRegional() != null && StringUtils.isNotBlank(customer.getAgentCountry())) { AgentRegional agentRegional = ServiceBeanFactory.getAgentRegionalService() .getByIdAndLocale(customer.getAgentRegional(), RequestThreadLocal.getLocale()); String[] agentCountryIds = customer.getAgentCountry().split(","); Set<Country> agentCountry = agentRegional.getAgentCountries(); boolean isLegal = false; for (String countryId : agentCountryIds) { isLegal = false; for (Country country : agentCountry) { if (country.getId().toString().endsWith(countryId)) { isLegal = true; break; } } if (!isLegal) { errors.rejectValue("customer.agentCountry", "customer.agentCountry.error"); break; } } } } }