001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.sdlctools.models.modelassistant.metabossmodel.domainsupport;
016:
017: import java.util.ArrayList;
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import javax.jmi.reflect.ConstraintViolationException;
023: import javax.jmi.reflect.RefObject;
024:
025: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Message;
026: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.MessageField;
027: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.MessageTypeEnum;
028: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Servicemodule;
029: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Domain;
030: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Entity;
031:
032: /** This class containse helper methods dealing with the InstanceNotFound error message model element */
033: class TargetInstanceNotFoundMessageHelper {
034: private ModelAssistantImpl mModelAssistantImpl;
035:
036: TargetInstanceNotFoundMessageHelper(
037: ModelAssistantImpl pModelAssistantImpl) {
038: mModelAssistantImpl = pModelAssistantImpl;
039:
040: // Add entity lifecycle listener
041: mModelAssistantImpl.addLifecycleListener(Entity.class,
042: new ModelAssistantImpl.ModelElementLifecycleListener() {
043: public void onElementJustCreated(
044: RefObject pModelElementJustCreated) {
045: Entity lEntity = (Entity) pModelElementJustCreated;
046: String lEntityName = lEntity.getName();
047: if (lEntityName == null)
048: return; // Entity does not have a name
049: Domain lDomain = lEntity.getDomain();
050: String lDomainName = lDomain.getName();
051: if (lDomainName == null)
052: return; // Domain does not have a name
053: com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.System lSystem = lDomain
054: .getSystem();
055: if (lSystem == null)
056: return; // Domain is not associated with system
057: Servicemodule lServicemodule = lSystem
058: .findServicemodule(StylesheetImpl
059: .getDomainSupportServicemoduleName(lDomainName));
060: if (lServicemodule == null)
061: return; // There is no support servicemodule yet
062: ensurePresent(lServicemodule, lEntity,
063: lEntityName);
064: }
065:
066: public void onElementBeingDeleted(
067: RefObject pModelElementBeingDeleted) {
068: Entity lEntity = (Entity) pModelElementBeingDeleted;
069: String lEntityName = lEntity.getName();
070: if (lEntityName == null)
071: return; // Entity does not have a name
072: Domain lDomain = lEntity.getDomain();
073: String lDomainName = lDomain.getName();
074: if (lDomainName == null)
075: return; // Domain does not have a name
076: com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.System lSystem = lDomain
077: .getSystem();
078: if (lSystem == null)
079: return; // Domain is not associated with system
080: Servicemodule lServicemodule = lSystem
081: .findServicemodule(StylesheetImpl
082: .getDomainSupportServicemoduleName(lDomainName));
083: if (lServicemodule == null)
084: return; // There is no support servicemodule yet
085: ensureAbsent(lServicemodule, lEntityName);
086: }
087: });
088: // Add entity Name attribute change listener
089: mModelAssistantImpl
090: .addAttributeChangeListener(
091: Entity.class,
092: "Name",
093: new ModelAssistantImpl.ModelElementAttributeChangeListener() {
094: public void onAttributeBeingUpdated(
095: RefObject pModelElementBeingUpdated,
096: String pAttributeName,
097: Object pOldValue, Object pNewValue) {
098: Entity lEntity = (Entity) pModelElementBeingUpdated;
099: Domain lDomain = lEntity.getDomain();
100: if (lDomain == null)
101: return; // Entity is not associated with domain
102: String lDomainName = lDomain.getName();
103: if (lDomainName == null)
104: return; // Domain does not have a name
105: com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.System lSystem = lDomain
106: .getSystem();
107: if (lSystem == null)
108: return; // Domain is not associated with system
109: Servicemodule lServicemodule = lSystem
110: .findServicemodule(StylesheetImpl
111: .getDomainSupportServicemoduleName(lDomainName));
112: if (lServicemodule == null)
113: return; // There is no support servicemodule yet
114: if (pNewValue == null) {
115: // Only old value is known - ensure that the element is deleted
116: ensureAbsent(lServicemodule,
117: (String) pOldValue);
118: } else {
119: // The element must be present - rename or create
120: if (pOldValue != null)
121: ensureRenamedPresent(
122: lServicemodule,
123: lEntity,
124: (String) pOldValue,
125: (String) pNewValue);
126: else
127: ensurePresent(lServicemodule,
128: lEntity,
129: (String) pNewValue);
130: }
131: }
132: });
133:
134: }
135:
136: // This helper verifies constraints which are dealing with entity-related messages
137: void verifyConstraints(Collection pViolations,
138: Servicemodule pServicemodule, Entity pEntity,
139: Collection pUnclaimedMessages) {
140: // Work on the InstanceNotFound message
141: {
142: String lMessageName = StylesheetImpl
143: .getEntityInstanceNotFoundMessageName(pEntity
144: .getName());
145: Message lMessage = pServicemodule.findMessage(lMessageName);
146: if (lMessage == null)
147: pViolations
148: .add(new ConstraintViolationException(
149: pServicemodule,
150: pServicemodule.refMetaObject(),
151: "A Domain Support Servicemodule must have 'Instance Not Found' Message for every Entity in the corresponding Domain. The '"
152: + lMessageName
153: + "' Message not found."));
154: else
155: pUnclaimedMessages.remove(lMessage); // Claim the message
156: }
157: }
158:
159: // This helper is doing everything necessary to rectify errors reported in verifyConstraints()
160: void rectifyModel(Servicemodule pServicemodule, Entity pEntity,
161: Collection pUnclaimedMessages) {
162: // Check if we have a message and than just call ensurePresent
163: String lMessageName = StylesheetImpl
164: .getEntityInstanceNotFoundMessageName(pEntity.getName());
165: Message lMessage = pServicemodule.findMessage(lMessageName);
166: if (lMessage != null)
167: pUnclaimedMessages.remove(lMessage); // Claim the message
168: // Now just call ensurePresent
169: ensurePresent(pServicemodule, pEntity, pEntity.getName());
170: }
171:
172: // This helper renames the messages present for every entity
173: void ensureRenamedPresent(Servicemodule pServicemodule,
174: Entity pEntity, String pOldEntityName, String pNewEntityName) {
175: // Note that this method only deals with renaming and than calls the ensure present method
176:
177: // Work on the InstanceNotFound message
178: {
179: String lOldMessageName = StylesheetImpl
180: .getEntityInstanceNotFoundMessageName(pOldEntityName);
181: Message lOldMessage = pServicemodule
182: .findMessage(lOldMessageName);
183: String lNewMessageName = StylesheetImpl
184: .getEntityInstanceNotFoundMessageName(pNewEntityName);
185: Message lNewMessage = pServicemodule
186: .findMessage(lNewMessageName);
187: // Be relaxed here - allow for all sorts of mishaps
188: if (lOldMessage != null) {
189: if (lNewMessage != null) {
190: // New and old mesages are present - just delete the old one
191: lOldMessage.refDelete();
192: } else {
193: // Old message is present - new one is not - rename
194: lOldMessage.setName(lNewMessageName);
195: }
196: }
197: }
198: // Call the ensure present bit
199: ensurePresent(pServicemodule, pEntity, pNewEntityName);
200: }
201:
202: // This helper makes sure that the entity related messages exist and uptodate
203: void ensurePresent(Servicemodule pServicemodule, Entity pEntity,
204: String pEntityName) {
205: // Work on the InstanceNotFound message
206: {
207: String lMessageName = StylesheetImpl
208: .getEntityInstanceNotFoundMessageName(pEntityName);
209: String lMessageDescription = suggestInstanceNotFoundMessageDescription(pEntityName);
210: String lMessageText = suggestInstanceNotFoundMessageText(pEntityName);
211: Message lMessage = pServicemodule.findMessage(lMessageName);
212: if (lMessage == null) {
213: lMessage = mModelAssistantImpl.mMessageClass
214: .createMessage();
215: lMessage.setName(lMessageName);
216: lMessage.setServicemodule(pServicemodule);
217:
218: }
219: lMessage.setDescription(lMessageDescription);
220: lMessage.setType(MessageTypeEnum.ERROR);
221: lMessage.setDefaultText(lMessageText);
222: // This mesage has no fields
223: Collection lMessageFields = lMessage.getFields();
224: if (!lMessageFields.isEmpty()) {
225: // Unable to use iterator over the live collecgtion, because as we delete fields - they will
226: // be deleted from the list causing problems with the iterator
227: List lTempMessageFieldsCollection = new ArrayList();
228: lTempMessageFieldsCollection.addAll(lMessageFields);
229: for (Iterator lUnwantedMessageFieldsIterator = lTempMessageFieldsCollection
230: .iterator(); lUnwantedMessageFieldsIterator
231: .hasNext();) {
232: MessageField lMessageField = (MessageField) lUnwantedMessageFieldsIterator
233: .next();
234: lMessageField.refDelete();
235: }
236: }
237: }
238: }
239:
240: // This helper makes sure that the entity related messages are absent
241: void ensureAbsent(Servicemodule pServicemodule, String pEntityName) {
242: // Work on the InstanceNotFound message
243: Message lMessage = pServicemodule.findMessage(StylesheetImpl
244: .getEntityInstanceNotFoundMessageName(pEntityName));
245: if (lMessage != null)
246: lMessage.refDelete();
247: }
248:
249: // Helper. Returns the message description
250: private static String suggestInstanceNotFoundMessageDescription(
251: String pEntityName) {
252: // Use name 'as is' here
253: return "This error indicates that operation has failed because required "
254: + pEntityName + " instance was not found.";
255: }
256:
257: // Helper. Returns the message text
258: private static String suggestInstanceNotFoundMessageText(
259: String pEntityName) {
260: // Use name 'as is' here
261: return pEntityName + " instance not found.";
262: }
263: }
|