View Javadoc

1   /*
2    * Copyright (C) 2007 Alf Mikula
3    * 
4    * This file is part of PromoteGo.
5    *
6    * PromoteGo is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * PromoteGo is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with PromoteGo.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.promotego.validators;
20  
21  import org.promotego.beans.UserHolder;
22  import org.promotego.formbackingbeans.RadiusSearchBean;
23  import org.springframework.beans.factory.annotation.Required;
24  import org.springframework.validation.Errors;
25  import org.springframework.validation.ValidationUtils;
26  import org.springframework.validation.Validator;
27  
28  public class RadiusSearchValidator implements Validator
29  {
30      private UserHolder m_userHolder;
31      
32      public boolean supports(@SuppressWarnings("unchecked") Class clazz)
33      {
34          return RadiusSearchBean.class.isAssignableFrom(clazz);
35      }
36  
37      public void validate(Object target, Errors errors)
38      {
39          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address", "required", "Field is required.");
40          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "miles", "required", "Field is required.");
41          
42          assert m_userHolder.getUser() != null : "User object may not be null";
43          
44          RadiusSearchBean radiusSearch = (RadiusSearchBean)target;
45          if(!m_userHolder.getUser().equals(radiusSearch.getAddress().getUser()))
46          {
47              // Reject address if it doesn't belong to this user.  This can
48              // happen if somebody directly enters an address id instead of using
49              // our form.
50              errors.rejectValue("address", "invalidAddress", "Address is invalid");
51          }
52      }
53  
54      @Required
55      public void setUserHolder(UserHolder userHolder)
56      {
57          m_userHolder = userHolder;
58      }
59  }