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.datadictionarymodel;
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.AbstractNamespace;
039: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.DataDictionary;
040: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.DataType;
041: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.Namespace;
042: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.Structure;
043: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.StructureField;
044: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.ModelFacadeForOCLCompiler;
045:
046: public abstract class StructureImpl extends ModelElementImpl implements
047: Structure {
048: // Commons Logging instance.
049: private static final Log sLogger = LogFactory
050: .getLog(StructureImpl.class);
051:
052: // Required constructor
053: protected StructureImpl(StorableObject storable) {
054: super (storable);
055: }
056:
057: // Verify certain constraints
058: protected Collection _verify(Collection pViolations) {
059: // First call superclass
060: Collection lViolations = super ._verify(pViolations);
061:
062: // Constraint #1 - Compile every constraint and check syntax
063: if (!getConstraints().isEmpty()) {
064: ModelFacade lModelFacade = ModelFacadeForOCLCompiler
065: .createForStructureContext(this );
066: NameCreator lNameCreator = new NameCreator();
067: String lStructureName = getName();
068: String lContextName = lStructureName.substring(0, 1)
069: .toUpperCase()
070: + lStructureName.substring(1);
071: String lConstraintStringStart = "context " + lContextName
072: + " inv : ";
073: for (Iterator lConstraintsIterator = getConstraints()
074: .iterator(); lConstraintsIterator.hasNext();) {
075: ModelElementConstraint lModelElementConstraint = (ModelElementConstraint) lConstraintsIterator
076: .next();
077: String lExpressionString = lModelElementConstraint
078: .getOclExpression();
079: if (lExpressionString != null
080: && lExpressionString.trim().length() > 0) {
081: String lConstraintString = lConstraintStringStart
082: + lModelElementConstraint
083: .getOclExpression();
084: try {
085: OclTree lConstraintTree = MetaBossOclTree
086: .createTree(lConstraintString,
087: lModelFacade);
088: lConstraintTree.setNameCreator(lNameCreator);
089: lConstraintTree.assureTypes();
090: } catch (IOException e) {
091: sLogger
092: .error(
093: "Caught unexpected exception during model validation",
094: e);
095: } catch (OclException e) {
096: lViolations
097: .add(new ConstraintViolationException(
098: lModelElementConstraint,
099: lModelElementConstraint
100: .refMetaObject(), e
101: .getMessage()
102: + ". Constraint text: "
103: + lConstraintString));
104: } catch (OclParserException e) {
105: lViolations
106: .add(new ConstraintViolationException(
107: lModelElementConstraint,
108: lModelElementConstraint
109: .refMetaObject(), e
110: .getMessage()
111: + ". Constraint text: "
112: + lConstraintString));
113: }
114: }
115: }
116: }
117: return lViolations;
118: }
119:
120: // Returns owner data dictionary, if this message belongs to the dictionary or null if none found
121: public DataDictionary getOwnerDataDictionary() {
122: AbstractNamespace lNamespace = getNamespace();
123: if (lNamespace == null)
124: return null; // May belong to the Servicemodule
125: if (lNamespace instanceof DataDictionary)
126: return (DataDictionary) lNamespace; // Found data dictionary - just return it
127: return ((Namespace) lNamespace).getOwnerDataDictionary();
128: }
129:
130: /**
131: * @param pFieldName
132: * @return requested StructureField or throws exception if none found
133: */
134: public StructureField getField(String pFieldName) {
135: StructureField lFoundField = findField(pFieldName);
136: // Throw exception if nothing found
137: if (lFoundField == null)
138: throw new IllegalArgumentException(
139: "Unable to locate StructureField named '"
140: + pFieldName
141: + "' in Structure. StructureRef: "
142: + getRef());
143: return lFoundField;
144: }
145:
146: /**
147: * @param pStructureFieldName
148: * @return requested StructureField or null if none found
149: */
150: public StructureField findField(String pStructureFieldName) {
151: Collection lStructureFields = getFields();
152: if (!lStructureFields.isEmpty()) {
153: for (Iterator lStructureFieldsIterator = lStructureFields
154: .iterator(); lStructureFieldsIterator.hasNext();) {
155: StructureField lStructureField = (StructureField) lStructureFieldsIterator
156: .next();
157: if (lStructureField.getName().equals(
158: pStructureFieldName))
159: return lStructureField;
160: }
161: }
162: return null;
163: }
164:
165: /** @return All combined structures and types used in this structure */
166: public Collection getCombinedTypes() {
167: Set lCombinedTypes = new HashSet();
168: populateCombinedTypes(lCombinedTypes);
169: return java.util.Collections
170: .unmodifiableCollection(lCombinedTypes);
171: }
172:
173: /** Populates passed set with all combined structures and types used in this structure */
174: public void populateCombinedTypes(Set pAlredyKnownTypes) {
175: Collection lStructureFields = getFields();
176: if (!lStructureFields.isEmpty()) {
177: for (Iterator lStructureFieldsIterator = lStructureFields
178: .iterator(); lStructureFieldsIterator.hasNext();) {
179: StructureField lStructureField = (StructureField) lStructureFieldsIterator
180: .next();
181: DataType lDataType = lStructureField.getDataType();
182: if (lDataType != null) {
183: if (!pAlredyKnownTypes.contains(lDataType))
184: pAlredyKnownTypes.add(lDataType);
185: } else {
186: Structure lStructureType = lStructureField
187: .getStructureType();
188: if (lStructureType != null) {
189: if (!pAlredyKnownTypes.contains(lStructureType)) {
190: // Register this structure and populate all types this structure depends on
191: // note that this will recursively populate all dependent types
192: pAlredyKnownTypes.add(lStructureType);
193: ((StructureImpl) lStructureType)
194: .populateCombinedTypes(pAlredyKnownTypes);
195: }
196: }
197: }
198: }
199: }
200: }
201:
202: /**
203: * @param pConstraintName
204: * @return Constraint with the given name or throws exception if none found
205: */
206: public ModelElementConstraint getConstraint(String pConstraintName) {
207: ModelElementConstraint lFoundConstraint = findConstraint(pConstraintName);
208: // Throw exception if nothing found
209: if (lFoundConstraint == null)
210: throw new IllegalArgumentException(
211: "Unable to locate Constraint named '"
212: + pConstraintName
213: + "' in Entity. EntityRef: " + getRef());
214: return lFoundConstraint;
215: }
216:
217: /**
218: * @param pConstraintName
219: * @return Constraint with the given name or null if none found
220: */
221: public ModelElementConstraint findConstraint(String pConstraintName) {
222: Collection lConstraints = getConstraints();
223: if (!lConstraints.isEmpty()) {
224: for (Iterator lConstraintsIterator = lConstraints
225: .iterator(); lConstraintsIterator.hasNext();) {
226: ModelElementConstraint lConstraint = (ModelElementConstraint) lConstraintsIterator
227: .next();
228: if (lConstraint.getName().equals(pConstraintName))
229: return lConstraint;
230: }
231: }
232: return null;
233: }
234: }
|