View Javadoc

1   package com.melloware.jukes.gui.view.validation;
2   
3   import com.jgoodies.validation.ValidationResult;
4   import com.jgoodies.validation.Validator;
5   import com.jgoodies.validation.util.PropertyValidationSupport;
6   import com.jgoodies.validation.util.ValidationUtils;
7   import com.melloware.jukes.db.orm.Disc;
8   
9   /**
10   * Validates Discs.
11   * <p>
12   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
13   * @author Emil A. Lefkof III <info@melloware.com>
14   * @version 4.0
15   */
16  public final class DiscValidator
17      implements Validator {
18  	
19  	private static final String NAME = "Name";
20  	private static final String GENRE = "Genre";
21  	private static final String YEAR = "Year";
22  	private static final String MESSAGE_MANDATORY = "is mandatory";
23  
24      /**
25       * Holds the disc to be validated.
26       */
27      private final Disc disc;
28  
29      /**
30       * Constructs a DiscValidator on the given Disc.
31       *
32       * @param disc    the disc to be validated
33       */
34      public DiscValidator(Disc disc) {
35          this.disc = disc;
36      }
37  
38      /**
39       * Validates this Validator's Order and returns the result
40       * as an instance of {@link ValidationResult}.
41       *
42       * @return the ValidationResult of the disc validation
43       */
44      public ValidationResult validate() {
45          final PropertyValidationSupport support = new PropertyValidationSupport(disc, "Disc");
46  
47          if (ValidationUtils.isBlank(disc.getName())) {
48              support.addError(NAME, MESSAGE_MANDATORY);
49          } else if (!ValidationUtils.hasMaximumLength(disc.getName(), 100)) {
50              support.addError(NAME, "length shall be in [0, 100]");
51          }
52  
53          if (ValidationUtils.isBlank(disc.getGenre())) {
54              support.addError(GENRE, MESSAGE_MANDATORY);
55          } else if (!ValidationUtils.hasMaximumLength(disc.getGenre(), 100)) {
56              support.addError(GENRE, "length shall be in [0, 100]");
57          }
58  
59          if ((disc.getYear() == null)) {
60              support.addError(YEAR, MESSAGE_MANDATORY);
61          } else if (ValidationUtils.isBlank(disc.getYear())) {
62              support.addError(YEAR, MESSAGE_MANDATORY);
63          } else if (!ValidationUtils.hasBoundedLength(disc.getYear(), 4, 4)) { //NOPMD
64              support.addError(YEAR, "length shall be 4 digits.");
65          } else if (!ValidationUtils.isNumeric(disc.getYear())) {  //NOPMD
66              support.addError(YEAR, "must be a number");
67          }
68  
69          return support.getResult();
70      }
71  
72  }