View Javadoc

1   package com.melloware.jukes.gui.view.validation;
2   
3   import java.text.MessageFormat;
4   import java.util.Iterator;
5   import java.util.List;
6   
7   import com.jgoodies.validation.ValidationResult;
8   import com.jgoodies.validation.Validator;
9   import com.jgoodies.validation.util.PropertyValidationSupport;
10  import com.jgoodies.validation.util.ValidationUtils;
11  import com.melloware.jukes.db.orm.Disc;
12  import com.melloware.jukes.file.tag.MusicTag;
13  import com.melloware.jukes.gui.tool.Resources;
14  
15  /**
16   * Validates Discs.
17   * <p>
18   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
19   * @author Emil A. Lefkof III <info@melloware.com>
20   * @version 4.0
21   */
22  public final class DiscValidator implements Validator {
23  
24     private static final String NAME = "Name";
25     private static final String GENRE = "Genre";
26     private static final String YEAR = "Year";
27     private static final String MESSAGE_MANDATORY = Resources.getString("messages.isMandatory");
28     private static final String NOTES = "Notes";
29     private static final List<String> GENRE_LIST = MusicTag.getGenreTypes();
30     private static final String MESSAGE_LENGTH_500 = Resources.getString("messages.Length500");
31     private static final String MESSAGE_LENGTH_100 = Resources.getString("messages.Length100");
32     private static final String MESSAGE_LENGTH_4 = Resources.getString("messages.Length4");
33     private static final String MESSAGE_MUST_BE_A_NUMBER = Resources.getString("messages.mustbeanumber");
34     private static final String MESSAGE_BASIC_GENRE = Resources.getString("messages.BasicGenre");
35  
36     /**
37      * Holds the disc to be validated.
38      */
39     private final Disc disc;
40  
41     /**
42      * Constructs a DiscValidator on the given Disc.
43      * @param disc the disc to be validated
44      */
45     public DiscValidator(Disc disc) {
46        this.disc = disc;
47     }
48  
49     public boolean containsIgnoreCase(List<String> l, String s) {
50        Iterator<String> it = l.iterator();
51        while (it.hasNext()) {
52           if (it.next().compareToIgnoreCase(s) == 0) {
53              return true;
54           }
55        }
56        return false;
57     }
58  
59     /**
60      * Validates this Validator's Order and returns the result as an instance of
61      * {@link ValidationResult}.
62      * @return the ValidationResult of the disc validation
63      */
64     @Override
65     public ValidationResult validate(Object validationTarget) {
66        final PropertyValidationSupport support = new PropertyValidationSupport(disc, "Disc");
67  
68        if (ValidationUtils.isBlank(disc.getName())) {
69           support.addError(NAME, MESSAGE_MANDATORY);
70        } else if (!ValidationUtils.hasMaximumLength(disc.getName(), 100)) {
71           support.addError(NAME, MESSAGE_LENGTH_100);
72        }
73        // AZ: Validate Notes length
74        if (!ValidationUtils.hasMaximumLength(disc.getNotes(), 500)) {
75           support.addError(NOTES, MESSAGE_LENGTH_500);
76        }
77        if (ValidationUtils.isBlank(disc.getGenre())) {
78           support.addError(GENRE, MESSAGE_MANDATORY);
79        } else {
80           if (!ValidationUtils.hasMaximumLength(disc.getGenre(), 100)) {
81              support.addError(GENRE, MESSAGE_LENGTH_100);
82           }
83           final String TmpString = disc.getGenre().toString();
84           final String BasicGenre;
85           if (TmpString.indexOf(":") > 0) {
86              BasicGenre = TmpString.substring(0, TmpString.indexOf(":")).trim();
87           } else if (TmpString.indexOf(",") > 0) {
88              BasicGenre = TmpString.substring(0, TmpString.indexOf(",")).trim();
89           } else if (TmpString.indexOf(";") > 0) {
90              BasicGenre = TmpString.substring(0, TmpString.indexOf(";")).trim();
91           } else if (TmpString.indexOf("-") > 0) {
92              BasicGenre = TmpString.substring(0, TmpString.indexOf("-")).trim();
93           } else if (TmpString.indexOf(".") > 0) {
94              BasicGenre = TmpString.substring(0, TmpString.indexOf(".")).trim();
95           } else if (TmpString.indexOf("/") > 0) {
96              BasicGenre = TmpString.substring(0, TmpString.indexOf("/")).trim();
97           } else
98              BasicGenre = TmpString.trim();
99  
100          if (!containsIgnoreCase(GENRE_LIST, BasicGenre)) {
101             final String message = MessageFormat.format(MESSAGE_BASIC_GENRE, new Object[] { BasicGenre });
102             support.addError(GENRE, message);
103          }
104       }
105       if ((disc.getYear() == null)) {
106          support.addError(YEAR, MESSAGE_MANDATORY);
107       } else if (ValidationUtils.isBlank(disc.getYear())) {
108          support.addError(YEAR, MESSAGE_MANDATORY);
109       } else if (!ValidationUtils.hasBoundedLength(disc.getYear(), 4, 4)) { // NOPMD
110          support.addError(YEAR, MESSAGE_LENGTH_4);
111       } else if (!ValidationUtils.isNumeric(disc.getYear())) { // NOPMD
112          support.addError(YEAR, MESSAGE_MUST_BE_A_NUMBER);
113       }
114 
115       return support.getResult();
116    }
117 
118 }