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.impl.metabossmodel.enterprisemodel;
016:
017: import java.io.IOException;
018: import java.util.Collection;
019: import java.util.HashSet;
020: import java.util.Iterator;
021: import java.util.Set;
022:
023: import javax.jmi.reflect.ConstraintViolationException;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.netbeans.mdr.storagemodel.StorableObject;
028:
029: import tudresden.ocl.MetaBossOclTree;
030: import tudresden.ocl.NameCreator;
031: import tudresden.ocl.OclException;
032: import tudresden.ocl.OclTree;
033: import tudresden.ocl.check.types.ModelFacade;
034: import tudresden.ocl.parser.OclParserException;
035:
036: import com.metaboss.sdlctools.models.impl.metabossmodel.ModelElementImpl;
037: import com.metaboss.sdlctools.models.metabossmodel.ModelElementConstraint;
038: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.DataType;
039: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.Structure;
040: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.AbstractOperation;
041: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Message;
042: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.ModelFacadeForOCLCompiler;
043: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.OperationInputField;
044: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.OperationOutputField;
045: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.OperationOutputMessage;
046:
047: public abstract class AbstractOperationImpl extends ModelElementImpl
048: implements AbstractOperation {
049: // Commons Logging instance.
050: private static final Log sLogger = LogFactory
051: .getLog(AbstractOperationImpl.class);
052:
053: // Required constructor
054: protected AbstractOperationImpl(StorableObject storable) {
055: super (storable);
056: }
057:
058: // Verify certain constraints
059: protected Collection _verify(Collection pViolations) {
060: // First call superclass
061: Collection lViolations = super ._verify(pViolations);
062:
063: // Constraint #1 - Compile every constraint and check syntax
064: if (!getInputConstraints().isEmpty()) {
065: ModelFacade lModelFacade = ModelFacadeForOCLCompiler
066: .createForOperationInputContext(this );
067: NameCreator lNameCreator = new NameCreator();
068: String lOperationName = getName();
069: String lContextName = lOperationName.substring(0, 1)
070: .toUpperCase()
071: + lOperationName.substring(1);
072: String lConstraintStringStart = "context " + lContextName
073: + " inv : ";
074: for (Iterator lConstraintsIterator = getInputConstraints()
075: .iterator(); lConstraintsIterator.hasNext();) {
076: ModelElementConstraint lModelElementConstraint = (ModelElementConstraint) lConstraintsIterator
077: .next();
078: String lExpressionString = lModelElementConstraint
079: .getOclExpression();
080: if (lExpressionString != null
081: && lExpressionString.trim().length() > 0) {
082: String lConstraintString = lConstraintStringStart
083: + lModelElementConstraint
084: .getOclExpression();
085: try {
086: OclTree lConstraintTree = MetaBossOclTree
087: .createTree(lConstraintString,
088: lModelFacade);
089: lConstraintTree.setNameCreator(lNameCreator);
090: lConstraintTree.assureTypes();
091: } catch (IOException e) {
092: sLogger
093: .error(
094: "Caught unexpected exception during model validation",
095: e);
096: } catch (OclException e) {
097: lViolations
098: .add(new ConstraintViolationException(
099: lModelElementConstraint,
100: lModelElementConstraint
101: .refMetaObject(), e
102: .getMessage()
103: + ". Constraint text: "
104: + lConstraintString));
105: } catch (OclParserException e) {
106: lViolations
107: .add(new ConstraintViolationException(
108: lModelElementConstraint,
109: lModelElementConstraint
110: .refMetaObject(), e
111: .getMessage()
112: + ". Constraint text: "
113: + lConstraintString));
114: }
115: }
116: }
117: }
118: return lViolations;
119: }
120:
121: /**
122: * @param pFieldName
123: * @return requested OperationInputField or throws exception if none found
124: */
125: public OperationInputField getInputField(String pFieldName) {
126: OperationInputField lFoundInputField = findInputField(pFieldName);
127: // Throw exception if nothing found
128: if (lFoundInputField == null)
129: throw new IllegalArgumentException(
130: "Unable to locate OperationInputField named '"
131: + pFieldName
132: + "' in Operation. OperationRef: "
133: + getRef());
134: return lFoundInputField;
135: }
136:
137: /**
138: * @param pFieldName
139: * @return requested OperationInputField or null if none found
140: */
141: public OperationInputField findInputField(String pFieldName) {
142: Collection lInputFields = getInputFields();
143: if (!lInputFields.isEmpty()) {
144: for (Iterator lInputFieldsIterator = lInputFields
145: .iterator(); lInputFieldsIterator.hasNext();) {
146: OperationInputField lOperationInputField = (OperationInputField) lInputFieldsIterator
147: .next();
148: if (lOperationInputField.getName().equals(pFieldName))
149: return lOperationInputField;
150: }
151: }
152: return null;
153: }
154:
155: /**
156: * @param pConstraintName
157: * @return Constraint with the given name or throws exception if none found
158: */
159: public ModelElementConstraint getInputConstraint(
160: String pConstraintName) {
161: ModelElementConstraint lFoundConstraint = findInputConstraint(pConstraintName);
162: // Throw exception if nothing found
163: if (lFoundConstraint == null)
164: throw new IllegalArgumentException(
165: "Unable to locate Constraint named '"
166: + pConstraintName
167: + "' in Entity. EntityRef: " + getRef());
168: return lFoundConstraint;
169: }
170:
171: /**
172: * @param pConstraintName
173: * @return Constraint with the given name or null if none found
174: */
175: public ModelElementConstraint findInputConstraint(
176: String pConstraintName) {
177: Collection lConstraints = getInputConstraints();
178: if (!lConstraints.isEmpty()) {
179: for (Iterator lConstraintsIterator = lConstraints
180: .iterator(); lConstraintsIterator.hasNext();) {
181: ModelElementConstraint lConstraint = (ModelElementConstraint) lConstraintsIterator
182: .next();
183: if (lConstraint.getName().equals(pConstraintName))
184: return lConstraint;
185: }
186: }
187: return null;
188: }
189:
190: /**
191: * @param pFieldName
192: * @return requested OperationOutputField or throws exception if none found
193: */
194: public OperationOutputField getOutputField(String pFieldName) {
195: OperationOutputField lFoundOutputField = findOutputField(pFieldName);
196: // Throw exception if nothing found
197: if (lFoundOutputField == null)
198: throw new IllegalArgumentException(
199: "Unable to locate OperationOutputField named '"
200: + pFieldName
201: + "' in Operation. OperationRef: "
202: + getRef());
203: return lFoundOutputField;
204: }
205:
206: /**
207: * @param pFieldName
208: * @return requested OperationOutputField or null if none found
209: */
210: public OperationOutputField findOutputField(String pFieldName) {
211: Collection lOutputFields = getOutputFields();
212: if (!lOutputFields.isEmpty()) {
213: for (Iterator lOutputFieldsIterator = lOutputFields
214: .iterator(); lOutputFieldsIterator.hasNext();) {
215: OperationOutputField lOperationOutputField = (OperationOutputField) lOutputFieldsIterator
216: .next();
217: if (lOperationOutputField.getName().equals(pFieldName))
218: return lOperationOutputField;
219: }
220: }
221: return null;
222: }
223:
224: /**
225: * @param pMessageName
226: * @return requested OperationOutputMessage or throws exception if none found
227: */
228: public OperationOutputMessage getOutputMessage(String pMessageName) {
229: OperationOutputMessage lFoundOutputMessage = findOutputMessage(pMessageName);
230: // Throw exception if nothing found
231: if (lFoundOutputMessage == null)
232: throw new IllegalArgumentException(
233: "Unable to locate OperationOutputMessage named '"
234: + pMessageName
235: + "' in Operation. OperationRef: "
236: + getRef());
237: return lFoundOutputMessage;
238: }
239:
240: /**
241: * @param pMessageName
242: * @return requested OperationOutputMessage or null if none found
243: */
244: public OperationOutputMessage findOutputMessage(String pMessageName) {
245: Collection lOutputMessages = getOutputMessages();
246: if (!lOutputMessages.isEmpty()) {
247: for (Iterator lOutputMessagesIterator = lOutputMessages
248: .iterator(); lOutputMessagesIterator.hasNext();) {
249: OperationOutputMessage lOperationOutputMessage = (OperationOutputMessage) lOutputMessagesIterator
250: .next();
251: if (lOperationOutputMessage.getName().equals(
252: pMessageName))
253: return lOperationOutputMessage;
254: }
255: }
256: return null;
257: }
258:
259: /** @return All combined structures and types used in this structure */
260: public Collection getCombinedTypes() {
261: Set lCombinedTypes = new HashSet();
262: populateCombinedTypes(lCombinedTypes);
263: return java.util.Collections
264: .unmodifiableCollection(lCombinedTypes);
265: }
266:
267: /** Populates passed set with all combined structures, messages and types used in this structure */
268: public void populateCombinedTypes(Set pAlredyKnownTypes) {
269: // Work on input fields
270: Collection lInputFields = getInputFields();
271: if (!lInputFields.isEmpty()) {
272: for (Iterator lInputFieldsIterator = lInputFields
273: .iterator(); lInputFieldsIterator.hasNext();) {
274: OperationInputField lInputField = (OperationInputField) lInputFieldsIterator
275: .next();
276: DataType lDataType = lInputField.getDataType();
277: if (lDataType != null) {
278: if (!pAlredyKnownTypes.contains(lDataType))
279: pAlredyKnownTypes.add(lDataType);
280: } else {
281: Structure lStructureType = lInputField
282: .getStructureType();
283: if (lStructureType != null) {
284: if (!pAlredyKnownTypes.contains(lStructureType)) {
285: // Register this structure and populate all types this structure depends on
286: // note that this will recursively populate all dependent types
287: pAlredyKnownTypes.add(lStructureType);
288: pAlredyKnownTypes.addAll(lStructureType
289: .getCombinedTypes());
290: }
291: }
292: }
293: }
294: }
295: // Work on output fields
296: Collection lOutputFields = getOutputFields();
297: if (!lOutputFields.isEmpty()) {
298: for (Iterator lOutputFieldsIterator = lOutputFields
299: .iterator(); lOutputFieldsIterator.hasNext();) {
300: OperationOutputField lOutputField = (OperationOutputField) lOutputFieldsIterator
301: .next();
302: DataType lDataType = lOutputField.getDataType();
303: if (lDataType != null) {
304: if (!pAlredyKnownTypes.contains(lDataType))
305: pAlredyKnownTypes.add(lDataType);
306: } else {
307: Structure lStructureType = lOutputField
308: .getStructureType();
309: if (lStructureType != null) {
310: if (!pAlredyKnownTypes.contains(lStructureType)) {
311: // Register this structure and populate all types this structure depends on
312: // note that this will recursively populate all dependent types
313: pAlredyKnownTypes.add(lStructureType);
314: pAlredyKnownTypes.addAll(lStructureType
315: .getCombinedTypes());
316: }
317: }
318: }
319: }
320: }
321: // Work on output messages
322: Collection lOutputMessages = getOutputMessages();
323: if (!lOutputMessages.isEmpty()) {
324: for (Iterator lOutputMessagesIterator = lOutputMessages
325: .iterator(); lOutputMessagesIterator.hasNext();) {
326: OperationOutputMessage lOutputMessage = (OperationOutputMessage) lOutputMessagesIterator
327: .next();
328: Message lMessageType = lOutputMessage.getMessageType();
329: if (!pAlredyKnownTypes.contains(lMessageType)) {
330: // Register this structure and populate all types this structure depends on
331: // note that this will recursively populate all dependent types
332: pAlredyKnownTypes.add(lMessageType);
333: pAlredyKnownTypes.addAll(lMessageType
334: .getCombinedTypes());
335: }
336: }
337: }
338: }
339: }
|