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