View Javadoc

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