001: package org.andromda.core.metafacade;
002:
003: import java.io.Serializable;
004:
005: import java.util.List;
006:
007: import org.andromda.core.common.ClassUtils;
008: import org.andromda.core.common.ExceptionUtils;
009: import org.apache.commons.lang.StringUtils;
010:
011: /**
012: * Stores the validation messages that are collected during model validation.
013: *
014: * @author Chad Brandon
015: */
016: public class ModelValidationMessage implements Serializable {
017: /**
018: * Constructs a new instance of MetafacadeValidationMessage taking a
019: * <code>metafacade</code> instance and a <code>message</code>
020: * indicating what has been violated.
021: *
022: * @param metafacade the metafacade being validated.
023: * @param message the message to to communitate about the validation.
024: */
025: public ModelValidationMessage(final MetafacadeBase metafacade,
026: final String message) {
027: this (metafacade, null, message);
028: }
029:
030: /**
031: * Constructs a new instance of MetafacadeValidationMessage taking a
032: * <code>metafacade</code> instance the <code>name</code> of the
033: * validation constraint and the actual <code>message</code> text indicating
034: * what has been violated.
035: *
036: * @param metafacade the metafacade being validated.
037: * @param name the name of the model element being validated.
038: * @param message the message to to communitate about the validation.
039: */
040: public ModelValidationMessage(final MetafacadeBase metafacade,
041: final String name, final String message) {
042: ExceptionUtils.checkNull("metafacade", metafacade);
043: ExceptionUtils.checkEmpty("message", message);
044: this .metafacade = metafacade;
045: this .name = name;
046: this .message = message;
047: }
048:
049: /**
050: * Stores the actual name of the constraint (if there is one).
051: */
052: private String name;
053:
054: /**
055: * Gets the name of the validation constraint.
056: *
057: * @return the constraint name.
058: */
059: public String getName() {
060: return this .name;
061: }
062:
063: /**
064: * Stores the actual message text.
065: */
066: private String message;
067:
068: /**
069: * Gets the actual message text.
070: *
071: * @return Returns the message.
072: */
073: public String getMessage() {
074: return message;
075: }
076:
077: /**
078: * Stores the actual metafacade to which this validation message applies.
079: */
080: private MetafacadeBase metafacade;
081:
082: /**
083: * Stores the metafacade name which is only constructed the very first time.
084: */
085: private String metafacadeName = null;
086:
087: /**
088: * Gets the name of the metafacade to which this validation message applies.
089: *
090: * @return Returns the metafacade.
091: */
092: public String getMetafacadeName() {
093: if (this .metafacadeName == null) {
094: final String seperator = MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR;
095: final StringBuffer name = new StringBuffer();
096: for (MetafacadeBase metafacade = this .metafacade; metafacade != null; metafacade = (MetafacadeBase) metafacade
097: .getValidationOwner()) {
098: if (StringUtils.isNotBlank(metafacade
099: .getValidationName())) {
100: String validationName = metafacade
101: .getValidationName();
102: if (metafacade.getValidationOwner() != null) {
103: // remove package if we have an owner
104: validationName = validationName.replaceAll(".*"
105: + seperator, "");
106: }
107: if (name.length() > 0) {
108: name.insert(0, seperator);
109: }
110: name.insert(0, validationName);
111: }
112: }
113: this .metafacadeName = name.toString();
114: }
115: return metafacadeName;
116: }
117:
118: /**
119: * Stores the metafacade class displayed within the message, this is only retrieved the very first time.
120: */
121: private Class metafacadeClass = null;
122:
123: /**
124: * Gets the class of the metafacade to which this validation message applies.
125: *
126: * @return the metafacade Class.
127: */
128: public Class getMetafacadeClass() {
129: if (metafacadeClass == null) {
130: this .metafacadeClass = this .metafacade.getClass();
131: final List interfaces = ClassUtils
132: .getAllInterfaces(this .metafacade.getClass());
133: if (interfaces != null && !interfaces.isEmpty()) {
134: this .metafacadeClass = (Class) interfaces.iterator()
135: .next();
136: }
137: }
138: return this .metafacadeClass;
139: }
140:
141: /**
142: * @see java.lang.Object#toString()
143: */
144: public String toString() {
145: final StringBuffer toString = new StringBuffer();
146: toString.append("[");
147: toString.append(this .getMetafacadeName());
148: toString.append("]");
149: toString.append(":");
150: toString.append(this .message);
151: return toString.toString();
152: }
153:
154: /**
155: * @see java.lang.Object#hashCode()
156: */
157: public int hashCode() {
158: return this .toString().hashCode();
159: }
160:
161: /**
162: * @see java.lang.Object#equals(java.lang.Object)
163: */
164: public boolean equals(Object object) {
165: boolean equals = object != null
166: && ModelValidationMessage.class == object.getClass();
167: if (equals) {
168: final ModelValidationMessage message = (ModelValidationMessage) object;
169: equals = message.toString().equals(this.toString());
170: }
171: return equals;
172: }
173: }
|