View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.library.web.spring;
17  
18  import org.springframework.validation.Errors;
19  import org.springframework.validation.Validator;
20  
21  /***
22   * @author jmccrindle
23   */
24  public class ImportValidator implements Validator {
25  
26      /* (non-Javadoc)
27       * @see org.springframework.validation.Validator#supports(java.lang.Class)
28       */
29      public boolean supports(Class commandClass) {
30          return commandClass.equals(ImportCommand.class);
31      }
32  
33      protected boolean isBlank(String value) {
34          if(value == null) return true;
35          value = value.trim();
36          return value.length() == 0;
37      }
38      
39      /* (non-Javadoc)
40       * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
41       */
42      public void validate(Object command, Errors errors) {
43          ImportCommand importCommand = (ImportCommand) command;
44          if(isBlank(importCommand.getSystem())) {
45              errors.rejectValue("system", "errors.required", null, "Value required");
46          }
47          if(importCommand.getFile() == null) {
48              errors.rejectValue("file", "errors.required", null, "Value required");
49          }
50      }
51  
52  }