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.implicitmessages;
016:
017: import java.util.Collection;
018:
019: import javax.jmi.reflect.ConstraintViolationException;
020: import javax.jmi.reflect.RefObject;
021:
022: import com.metaboss.sdlctools.models.metabossmodel.ModelElementConstraint;
023: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.AbstractOperation;
024: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.EventSubscription;
025: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.EventSubscriptionOperation;
026: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Message;
027: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Operation;
028: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.OperationInputField;
029: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.OperationOutputMessage;
030: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Service;
031: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Servicemodule;
032:
033: /** This class containse helper methods dealing with the Entity key structure model element */
034: class TargetInvalidOperationInputErrorOperationOutputMessageHelper {
035: private ModelAssistantImpl mModelAssistantImpl;
036:
037: TargetInvalidOperationInputErrorOperationOutputMessageHelper(
038: ModelAssistantImpl pModelAssistantImpl) {
039: mModelAssistantImpl = pModelAssistantImpl;
040:
041: // Add listener for the operation input fields
042: mModelAssistantImpl
043: .addReferenceChangeListener(
044: Operation.class,
045: "inputFields",
046: new ModelAssistantImpl.ModelElementReferenceChangeListener() {
047: public void onReferenceBeingUpdated(
048: RefObject pModelElementBeingUpdated,
049: String pReferenceName,
050: RefObject pReferencedModelElementToRemove,
051: RefObject pReferencedModelElementToAdd) {
052: Operation lOperation = (Operation) pModelElementBeingUpdated;
053: Service lService = lOperation
054: .getService();
055: if (lService == null)
056: return; // Operation is not associated with the service yet
057: Servicemodule lServicemodule = lService
058: .getServicemodule();
059: if (lServicemodule == null)
060: return; // Service is not associated wit hthe servicemodule yet
061: if (pReferencedModelElementToAdd != null) {
062: // If we are adding input parameter - we need to ensure that the message is present
063: ensurePresent(lOperation);
064: } else if (pReferencedModelElementToRemove != null) {
065: if (doesOperationNeedsMessageAfterFieldRemoval(
066: lOperation,
067: (OperationInputField) pReferencedModelElementToRemove))
068: ensurePresent(lOperation);
069: else
070: ensureAbsent(lOperation);
071: }
072: }
073: });
074:
075: // Add listener for the operation input constraints
076: mModelAssistantImpl
077: .addReferenceChangeListener(
078: Operation.class,
079: "inputConstraints",
080: new ModelAssistantImpl.ModelElementReferenceChangeListener() {
081: public void onReferenceBeingUpdated(
082: RefObject pModelElementBeingUpdated,
083: String pReferenceName,
084: RefObject pReferencedModelElementToRemove,
085: RefObject pReferencedModelElementToAdd) {
086: Operation lOperation = (Operation) pModelElementBeingUpdated;
087: Service lService = lOperation
088: .getService();
089: if (lService == null)
090: return; // Operation is not associated with the service yet
091: Servicemodule lServicemodule = lService
092: .getServicemodule();
093: if (lServicemodule == null)
094: return; // Service is not associated wit hthe servicemodule yet
095: if (pReferencedModelElementToAdd != null) {
096: // If we are adding input parameter - we need to ensure that the message is present
097: ensurePresent(lOperation);
098: } else if (pReferencedModelElementToRemove != null) {
099: if (doesOperationNeedsMessageAfterConstraintRemoval(
100: lOperation,
101: (ModelElementConstraint) pReferencedModelElementToRemove))
102: ensurePresent(lOperation);
103: else
104: ensureAbsent(lOperation);
105: }
106: }
107: });
108:
109: // Add listener for the susbscription operation input fields
110: mModelAssistantImpl
111: .addReferenceChangeListener(
112: EventSubscriptionOperation.class,
113: "inputFields",
114: new ModelAssistantImpl.ModelElementReferenceChangeListener() {
115: public void onReferenceBeingUpdated(
116: RefObject pModelElementBeingUpdated,
117: String pReferenceName,
118: RefObject pReferencedModelElementToRemove,
119: RefObject pReferencedModelElementToAdd) {
120: EventSubscriptionOperation lOperation = (EventSubscriptionOperation) pModelElementBeingUpdated;
121: EventSubscription lEventSubscription = lOperation
122: .getSubscription();
123: if (lEventSubscription == null)
124: return; // Operation is not attached to the susbcription yet
125: Servicemodule lServicemodule = lEventSubscription
126: .getServicemodule();
127: if (lServicemodule == null)
128: return; // Service is not associated wit hthe servicemodule yet
129: if (pReferencedModelElementToAdd != null) {
130: // If we are adding input parameter - we need to ensure that the message is present
131: ensurePresent(lOperation);
132: } else if (pReferencedModelElementToRemove != null) {
133: if (doesOperationNeedsMessageAfterFieldRemoval(
134: lOperation,
135: (OperationInputField) pReferencedModelElementToRemove))
136: ensurePresent(lOperation);
137: else
138: ensureAbsent(lOperation);
139: }
140: }
141: });
142:
143: // Add listener for the operation input constraints
144: mModelAssistantImpl
145: .addReferenceChangeListener(
146: EventSubscriptionOperation.class,
147: "inputConstraints",
148: new ModelAssistantImpl.ModelElementReferenceChangeListener() {
149: public void onReferenceBeingUpdated(
150: RefObject pModelElementBeingUpdated,
151: String pReferenceName,
152: RefObject pReferencedModelElementToRemove,
153: RefObject pReferencedModelElementToAdd) {
154: EventSubscriptionOperation lOperation = (EventSubscriptionOperation) pModelElementBeingUpdated;
155: EventSubscription lEventSubscription = lOperation
156: .getSubscription();
157: if (lEventSubscription == null)
158: return; // Operation is not attached to the susbcription yet
159: Servicemodule lServicemodule = lEventSubscription
160: .getServicemodule();
161: if (lServicemodule == null)
162: return; // Service is not associated wit hthe servicemodule yet
163: if (pReferencedModelElementToAdd != null) {
164: // If we are adding input parameter - we need to ensure that the message is present
165: ensurePresent(lOperation);
166: } else if (pReferencedModelElementToRemove != null) {
167: if (doesOperationNeedsMessageAfterConstraintRemoval(
168: lOperation,
169: (ModelElementConstraint) pReferencedModelElementToRemove))
170: ensurePresent(lOperation);
171: else
172: ensureAbsent(lOperation);
173: }
174: }
175: });
176: }
177:
178: // This helper verifies constraints which are dealing with entity structures
179: void verifyConstraints(Collection pViolations, Operation pOperation) {
180: // See if we have the input parameters
181: String lOperationMessageName = StylesheetImpl
182: .getInvalidOperationInputErrorOperationOutputMessageName();
183: OperationOutputMessage lOperationMessage = pOperation
184: .findOutputMessage(lOperationMessageName);
185: // Check if we need this message at all
186: if (!doesOperationNeedsMessage(pOperation)) {
187: if (lOperationMessage != null)
188: pViolations
189: .add(new ConstraintViolationException(
190: pOperation,
191: pOperation.refMetaObject(),
192: "Operation must have the '"
193: + lOperationMessageName
194: + "' Output Message if and only if it has any input fields or input constraints. The '"
195: + lOperationMessageName
196: + "' Output Message is not required for the '"
197: + pOperation.getName()
198: + " operation."));
199: return;
200: }
201: // Only the cases where we need it fall here.
202: if (lOperationMessage == null) {
203: pViolations
204: .add(new ConstraintViolationException(
205: pOperation,
206: pOperation.refMetaObject(),
207: "Operation must have the '"
208: + lOperationMessageName
209: + "' Output Message if and only if it has any input fields or input constraints. The '"
210: + lOperationMessageName
211: + "' Output Message is missing from the '"
212: + pOperation.getName()
213: + " operation."));
214: return;
215: }
216:
217: Message lInvalidOperationInputErrorMessage = (Message) mModelAssistantImpl.mRootPackage
218: .getModelElement()
219: .findByRef(
220: StylesheetImpl
221: .getInvalidOperationInputErrorMessageRef());
222: if (lInvalidOperationInputErrorMessage == null)
223: pViolations
224: .add(new ConstraintViolationException(
225: lOperationMessage,
226: lOperationMessage.refMetaObject(),
227: "Operation must have the '"
228: + lOperationMessageName
229: + "' Output Message if and only if it has any input fields or input constraints. The model does not contain necessary message type (looking for MessageRef "
230: + StylesheetImpl
231: .getInvalidOperationInputErrorMessageRef()
232: + ")"));
233: else if (lInvalidOperationInputErrorMessage
234: .equals(lOperationMessage.getMessageType()) == false)
235: pViolations
236: .add(new ConstraintViolationException(
237: lOperationMessage,
238: lOperationMessage.refMetaObject(),
239: "Operation must have the '"
240: + lOperationMessageName
241: + "' Output Message if and only if it has any input fields or input constraints. The '"
242: + lOperationMessageName
243: + "' Output Message has wrong type."));
244:
245: if (lOperationMessage.isArray())
246: pViolations
247: .add(new ConstraintViolationException(
248: lOperationMessage,
249: lOperationMessage.refMetaObject(),
250: "Operation must have the '"
251: + lOperationMessageName
252: + "' Output Message if and only if it has any input fields or input constraints. The '"
253: + lOperationMessageName
254: + "' Output Message is an array, which is wrong."));
255: }
256:
257: // This helper verifies constraints which are dealing with entity structures
258: void verifyConstraints(Collection pViolations,
259: EventSubscriptionOperation pOperation) {
260: // See if we have the input parameters
261: String lOperationMessageName = StylesheetImpl
262: .getInvalidOperationInputErrorOperationOutputMessageName();
263: OperationOutputMessage lOperationMessage = pOperation
264: .findOutputMessage(lOperationMessageName);
265: EventSubscription lEventSubscription = pOperation
266: .getSubscription();
267: if (lEventSubscription != null) {
268:
269: // Check if we need this message at all
270: if (!doesOperationNeedsMessage(pOperation)) {
271: if (lOperationMessage != null)
272: pViolations
273: .add(new ConstraintViolationException(
274: pOperation,
275: pOperation.refMetaObject(),
276: "Event Susbscription Operation must have the '"
277: + lOperationMessageName
278: + "' Output Message if and only if it has any input fields or input constraints. The '"
279: + lOperationMessageName
280: + "' Output Message is not required for the '"
281: + lEventSubscription
282: .getName()
283: + " event subscription operation."));
284: return;
285: }
286: // Only the cases where we need it fall here.
287: if (lOperationMessage == null) {
288: pViolations
289: .add(new ConstraintViolationException(
290: pOperation,
291: pOperation.refMetaObject(),
292: "Event Susbscription Operation must have the '"
293: + lOperationMessageName
294: + "' Output Message if and only if it has any input fields or input constraints. The '"
295: + lOperationMessageName
296: + "' Output Message is missing from the '"
297: + lEventSubscription.getName()
298: + " event subscription operation."));
299: return;
300: }
301: Message lInvalidOperationInputErrorMessage = (Message) mModelAssistantImpl.mRootPackage
302: .getModelElement()
303: .findByRef(
304: StylesheetImpl
305: .getInvalidOperationInputErrorMessageRef());
306: if (lInvalidOperationInputErrorMessage == null)
307: pViolations
308: .add(new ConstraintViolationException(
309: lOperationMessage,
310: lOperationMessage.refMetaObject(),
311: "Event Susbscription Operation must have the '"
312: + lOperationMessageName
313: + "' Output Message if and only if it has any input fields or input constraints. The model does not contain necessary message type (looking for MessageRef "
314: + StylesheetImpl
315: .getInvalidOperationInputErrorMessageRef()
316: + ")"));
317: else if (lInvalidOperationInputErrorMessage
318: .equals(lOperationMessage.getMessageType()) == false)
319: pViolations
320: .add(new ConstraintViolationException(
321: lOperationMessage,
322: lOperationMessage.refMetaObject(),
323: "Event Susbscription Operation must have the '"
324: + lOperationMessageName
325: + "' Output Message if and only if it has any input fields or input constraints. The '"
326: + lOperationMessageName
327: + "' Output Message has wrong type."));
328: }
329: if (lOperationMessage.isArray())
330: pViolations
331: .add(new ConstraintViolationException(
332: lOperationMessage,
333: lOperationMessage.refMetaObject(),
334: "Event Susbscription Operation must have the '"
335: + lOperationMessageName
336: + "' Output Message if and only if it has any input fields or input constraints. The '"
337: + lOperationMessageName
338: + "' Output Message is an array, which is wrong."));
339: }
340:
341: // This helper is doing everything necessary to rectify errors reported in verifyConstraints()
342: void rectifyModel(Operation pOperation) {
343: Service lService = pOperation.getService();
344: if (lService == null)
345: return; // Operation is not associated with the service yet
346: Servicemodule lServicemodule = lService.getServicemodule();
347: if (lServicemodule == null)
348: return; // Service is not associated wit hthe servicemodule yet
349: // See if we have the input parameters
350: String lOperationMessageName = StylesheetImpl
351: .getInvalidOperationInputErrorOperationOutputMessageName();
352: OperationOutputMessage lOperationMessage = pOperation
353: .findOutputMessage(lOperationMessageName);
354: // Check if we need this message at all
355: if (doesOperationNeedsMessage(pOperation))
356: ensurePresent(pOperation);
357: else
358: ensureAbsent(pOperation);
359: }
360:
361: // This helper is doing everything necessary to rectify errors reported in verifyConstraints()
362: void rectifyModel(EventSubscriptionOperation pOperation) {
363: EventSubscription lEventSubscription = pOperation
364: .getSubscription();
365: if (lEventSubscription == null)
366: return; // Operation is not attached to the susbcription yet
367: Servicemodule lServicemodule = lEventSubscription
368: .getServicemodule();
369: if (lServicemodule == null)
370: return; // Service is not associated wit hthe servicemodule yet
371: // See if we have the input parameters
372: String lOperationMessageName = StylesheetImpl
373: .getInvalidOperationInputErrorOperationOutputMessageName();
374: OperationOutputMessage lOperationMessage = pOperation
375: .findOutputMessage(lOperationMessageName);
376: // Check if we need this message at all
377: if (doesOperationNeedsMessage(pOperation))
378: ensurePresent(pOperation);
379: else
380: ensureAbsent(pOperation);
381: }
382:
383: // This helper makes sure that the key and details structures exist and uptodate
384: private void ensurePresent(AbstractOperation pOperation) {
385: // Work on the Key structure
386: String lIlegalInputOperationOutputMessageName = StylesheetImpl
387: .getInvalidOperationInputErrorOperationOutputMessageName();
388: OperationOutputMessage lIlegalInputOperationOutputMessage = pOperation
389: .findOutputMessage(lIlegalInputOperationOutputMessageName);
390: if (lIlegalInputOperationOutputMessage == null) {
391: lIlegalInputOperationOutputMessage = mModelAssistantImpl.mOperationOutputMessageClass
392: .createOperationOutputMessage();
393: lIlegalInputOperationOutputMessage
394: .setName(lIlegalInputOperationOutputMessageName);
395: lIlegalInputOperationOutputMessage.setOperation(pOperation);
396: }
397: lIlegalInputOperationOutputMessage
398: .setOwnerIdentifier(ModelAssistantImpl.ASSISTANT_IDENTIFIER);
399: lIlegalInputOperationOutputMessage
400: .setDescription("This error message indicates that operation has failed because it was invoked with invalid combination of input fields. In other words it indicates that the input data validation constraint has been violated.");
401: lIlegalInputOperationOutputMessage.setArray(false);
402: lIlegalInputOperationOutputMessage
403: .setMessageType((Message) mModelAssistantImpl.mRootPackage
404: .getModelElement()
405: .findByRef(
406: StylesheetImpl
407: .getInvalidOperationInputErrorMessageRef()));
408: }
409:
410: // This helper makes sure that the structure is absent
411: private void ensureAbsent(AbstractOperation pOperation) {
412: // Work on the key structure
413: String lOperationMessageName = StylesheetImpl
414: .getInvalidOperationInputErrorOperationOutputMessageName();
415: OperationOutputMessage lOperationMessage = pOperation
416: .findOutputMessage(lOperationMessageName);
417: if (lOperationMessage != null)
418: lOperationMessage.refDelete();
419: }
420:
421: // Helper. Finds out if operation needs message or not
422: private boolean doesOperationNeedsMessage(
423: AbstractOperation pOperation) {
424: if (!pOperation.getInputFields().isEmpty())
425: return true;
426: if (!pOperation.getInputConstraints().isEmpty())
427: return true;
428: return false;
429: }
430:
431: // Helper. Finds out if operation needs message after removal of the given input field
432: private boolean doesOperationNeedsMessageAfterFieldRemoval(
433: AbstractOperation pOperation,
434: OperationInputField pFieldBeingRemoved) {
435: if (!pOperation.getInputConstraints().isEmpty())
436: return true;
437: Collection lOperationInputFields = pOperation.getInputFields();
438: int lOperationInputFieldsSize = lOperationInputFields.size();
439: if (lOperationInputFieldsSize > 1
440: || (lOperationInputFieldsSize == 1 && lOperationInputFields
441: .contains(pFieldBeingRemoved) == false))
442: return true; // More than one field or only one, but not the one being removed
443: return false;
444: }
445:
446: // Helper. Finds out if operation needs message after removal of the given input constraint
447: private boolean doesOperationNeedsMessageAfterConstraintRemoval(
448: AbstractOperation pOperation,
449: ModelElementConstraint pConstraintBeingRemoved) {
450: if (!pOperation.getInputFields().isEmpty())
451: return true;
452: Collection lOperationInputConstraints = pOperation
453: .getInputConstraints();
454: int lOperationInputConstraintsSize = lOperationInputConstraints
455: .size();
456: if (lOperationInputConstraintsSize > 1
457: || (lOperationInputConstraintsSize == 1 && lOperationInputConstraints
458: .contains(pConstraintBeingRemoved) == false))
459: return true; // More than one constraint or only one, but not the one being removed
460: return false;
461: }
462: }
|