View Javadoc
1   /*
2    * #%L
3    * Nuiton Validator
4    * %%
5    * Copyright (C) 2013 - 2014 Code Lutin, Tony Chemit
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  package org.nuiton.validator.bean.simple;
23  
24  import org.nuiton.validator.NuitonValidatorScope;
25  
26  import java.io.Serializable;
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.StringTokenizer;
30  
31  import static org.nuiton.i18n.I18n.t;
32  
33  /**
34   * The object to box a validation message.
35   *
36   * @param <E> type of message (use for override {@link #compareTo(Object)}
37   *            method.
38   * @author Tony Chemit - chemit@codelutin.com
39   * @since 2.5.2
40   */
41  public class SimpleBeanValidatorMessage<E extends SimpleBeanValidatorMessage<?>> implements Comparable<E>, Serializable {
42  
43      private static final long serialVersionUID = 1L;
44  
45      /** the validator that produce the message */
46      protected SimpleBeanValidator<?> validator;
47  
48      /** the field that produce the message */
49      protected String field;
50  
51      /** the label of the message (to be displayed somewhere) */
52      protected String message;
53  
54      /** the scope of the message */
55      protected NuitonValidatorScope scope;
56  
57      public SimpleBeanValidatorMessage(SimpleBeanValidator<?> validator,
58                                        String field,
59                                        String message,
60                                        NuitonValidatorScope scope) {
61          this.field = field;
62          this.validator = validator;
63          this.message = message == null ? null : message.trim();
64          this.scope = scope;
65      }
66  
67      public SimpleBeanValidator<?> getValidator() {
68          return validator;
69      }
70  
71      public String getField() {
72          return field;
73      }
74  
75      public NuitonValidatorScope getScope() {
76          return scope;
77      }
78  
79      public String getMessage() {
80          return message;
81      }
82  
83      @Override
84      public int compareTo(E o) {
85          // sort on scope
86          int result = getScope().compareTo(o.getScope());
87          if (result == 0) {
88              // sort on field name
89              result = field.compareTo(o.field);
90              if (result == 0) {
91                  // sort on message
92                  result = message.compareTo(o.message);
93              }
94          }
95          return result;
96      }
97  
98      @Override
99      public boolean equals(Object o) {
100         if (this == o) {
101             return true;
102         }
103         if (!(o instanceof SimpleBeanValidatorMessage<?>)) {
104             return false;
105         }
106 
107         SimpleBeanValidatorMessage<?> that = (SimpleBeanValidatorMessage<?>) o;
108 
109         return field.equals(that.field) &&
110                (message != null ? !message.equals(that.message) : that.message == null) &&
111                scope == that.scope;
112     }
113 
114     @Override
115     public int hashCode() {
116         int result = field.hashCode();
117         result = 31 * result + (message != null ? message.hashCode() : 0);
118         result = 31 * result + (scope != null ? scope.hashCode() : 0);
119         return result;
120     }
121 
122     @Override
123     public String toString() {
124         return scope + " - " + getI18nError(message);
125     }
126 
127     public String getI18nError(String error) {
128         String text;
129         if (!error.contains("##")) {
130             text = t(error);
131         } else {
132             StringTokenizer stk = new StringTokenizer(error, "##");
133             String errorName = stk.nextToken();
134             List<String> args = new ArrayList<String>();
135             while (stk.hasMoreTokens()) {
136                 args.add(stk.nextToken());
137             }
138             text = t(errorName, args.toArray());
139         }
140         return text;
141     }
142 }