View Javadoc
1   package org.andromda.core.metafacade;
2   
3   import java.io.Serializable;
4   import java.util.List;
5   import org.andromda.core.common.ClassUtils;
6   import org.andromda.core.common.ExceptionUtils;
7   import org.apache.commons.lang.StringUtils;
8   
9   /**
10   * Stores the validation messages that are collected during model validation.
11   *
12   * @author Chad Brandon
13   */
14  public class ModelValidationMessage
15      implements Serializable
16  {
17      private static final long serialVersionUID = 34L;
18  
19      /**
20       * Constructs a new instance of MetafacadeValidationMessage taking a
21       * <code>metafacade</code> instance and a <code>message</code>
22       * indicating what has been violated.
23       *
24       * @param metafacade the metafacade being validated.
25       * @param message the message to to communicate about the validation.
26       */
27      public ModelValidationMessage(
28          final MetafacadeBase metafacade,
29          final String message)
30      {
31          this(metafacade, null, message);
32      }
33  
34      /**
35       * Constructs a new instance of MetafacadeValidationMessage taking a
36       * <code>metafacade</code> instance the <code>name</code> of the
37       * validation constraint and the actual <code>message</code> text indicating
38       * what has been violated.
39       *
40       * @param metafacade the metafacade being validated.
41       * @param name the name of the model element being validated.
42       * @param message the message to communicate about the validation.
43       */
44      public ModelValidationMessage(
45          final MetafacadeBase metafacade,
46          final String name,
47          final String message)
48      {
49          ExceptionUtils.checkNull("metafacade", metafacade);
50          ExceptionUtils.checkEmpty("message", message);
51          this.metafacade = metafacade;
52          this.name = name;
53          this.message = message;
54      }
55  
56      /**
57       * Stores the actual name of the constraint (if there is one).
58       */
59      private String name;
60  
61      /**
62       * Gets the name of the validation constraint.
63       *
64       * @return the constraint name.
65       */
66      public String getName()
67      {
68          return this.name;
69      }
70  
71      /**
72       * Stores the actual message text.
73       */
74      private String message;
75  
76      /**
77       * Gets the actual message text.
78       *
79       * @return Returns the message.
80       */
81      public String getMessage()
82      {
83          return message;
84      }
85  
86      /**
87       * Stores the actual metafacade to which this validation message applies.
88       */
89      private MetafacadeBase metafacade;
90  
91      /**
92       * Stores the metafacade name which is only constructed the very first time.
93       */
94      private String metafacadeName = null;
95  
96      /**
97       * Gets the name of the metafacade to which this validation message applies.
98       *
99       * @return Returns the metafacade.
100      */
101     public String getMetafacadeName()
102     {
103         if (this.metafacadeName == null)
104         {
105             final String separator = MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR;
106             final StringBuilder name = new StringBuilder();
107             for (
108                 MetafacadeBase metafacade = this.metafacade; metafacade != null;
109                 metafacade = (MetafacadeBase)metafacade.getValidationOwner())
110             {
111                 if (StringUtils.isNotBlank(metafacade.getValidationName()))
112                 {
113                     String validationName = metafacade.getValidationName();
114                     if (metafacade.getValidationOwner() != null)
115                     {
116                         // remove package if we have an owner
117                         validationName = validationName.replaceAll(".*" + separator, "");
118                     }
119                     if (name.length()>0)
120                     {
121                         name.insert(0, separator);
122                     }
123                     name.insert(0, validationName);
124                 }
125             }
126             this.metafacadeName = name.toString();
127         }
128         return metafacadeName;
129     }
130 
131     /**
132      * Stores the metafacade class displayed within the message, this is only retrieved the very first time.
133      */
134     private Class metafacadeClass = null;
135 
136     /**
137      * Gets the class of the metafacade to which this validation message applies.
138      *
139      * @return the metafacade Class.
140      */
141     public Class getMetafacadeClass()
142     {
143         if (metafacadeClass == null)
144         {
145             this.metafacadeClass = this.metafacade.getClass();
146             final List interfaces = ClassUtils.getAllInterfaces(this.metafacade.getClass());
147             if (interfaces != null && !interfaces.isEmpty())
148             {
149                 this.metafacadeClass = (Class)interfaces.iterator().next();
150             }
151         }
152         return this.metafacadeClass;
153     }
154 
155     /**
156      * @see Object#toString()
157      */
158     public String toString()
159     {
160         final StringBuilder toString = new StringBuilder();
161         toString.append('[');
162         toString.append(this.getMetafacadeName());
163         toString.append(']');
164         toString.append(':');
165         toString.append(this.message);
166         return toString.toString();
167     }
168 
169     /**
170      * @see Object#hashCode()
171      */
172     public int hashCode()
173     {
174         return this.toString().hashCode();
175     }
176 
177     /**
178      * @see Object#equals(Object)
179      */
180     @SuppressWarnings("null") // Compiler doesn't not recognize that object cannot be null when using message
181     public boolean equals(Object object)
182     {
183         boolean equals = object != null && ModelValidationMessage.class == object.getClass();
184         if (equals)
185         {
186             final ModelValidationMessage message = (ModelValidationMessage)object;
187             // message can never be null at this point, because object cannot be null, despite the compiler warning
188             if (message != null)
189             {
190                 equals = message.toString().equals(this.toString());
191             }
192         }
193         return equals;
194     }
195 }