View Javadoc

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