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.util.ArrayList;
018: import java.util.Arrays;
019: import java.util.Collection;
020: import java.util.Collections;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Set;
025:
026: import org.netbeans.mdr.storagemodel.StorableObject;
027:
028: import com.metaboss.sdlctools.models.impl.metabossmodel.ModelElementImpl;
029: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.AbstractNamespace;
030: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.DataType;
031: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.Namespace;
032: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.Structure;
033: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.StructureField;
034: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.TypeTemplate;
035: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Message;
036:
037: public abstract class AbstractNamespaceImpl extends ModelElementImpl
038: implements AbstractNamespace {
039: // Required constructor
040: protected AbstractNamespaceImpl(StorableObject storable) {
041: super (storable);
042: }
043:
044: // Returns array of all data types
045: public Collection getCombinedDataTypes() {
046: List lDataTypes = new ArrayList();
047: lDataTypes.addAll(getDataTypes());
048: Collection lNamespaces = getSubNamespaces();
049: if (!lNamespaces.isEmpty()) {
050: for (Iterator lNamespacesIterator = lNamespaces.iterator(); lNamespacesIterator
051: .hasNext();) {
052: Namespace lNamespace = (Namespace) lNamespacesIterator
053: .next();
054: lDataTypes.addAll(lNamespace.getCombinedDataTypes());
055: }
056: }
057: return Collections.unmodifiableCollection(lDataTypes);
058: }
059:
060: // Returns array of all type templates
061: public Collection getCombinedTypeTemplates() {
062: List lTypeTemplates = new ArrayList();
063: lTypeTemplates.addAll(getTypeTemplates());
064: Collection lNamespaces = getSubNamespaces();
065: if (!lNamespaces.isEmpty()) {
066: for (Iterator lNamespacesIterator = lNamespaces.iterator(); lNamespacesIterator
067: .hasNext();) {
068: Namespace lNamespace = (Namespace) lNamespacesIterator
069: .next();
070: lTypeTemplates.addAll(lNamespace
071: .getCombinedTypeTemplates());
072: }
073: }
074: return Collections.unmodifiableCollection(lTypeTemplates);
075: }
076:
077: // Returns array of all sub namespaces
078: public Collection getCombinedSubNamespaces() {
079: List lSubNamespaces = new ArrayList();
080: Collection lNamespaces = getSubNamespaces();
081: if (!lNamespaces.isEmpty()) {
082: lSubNamespaces.addAll(lNamespaces);
083: for (Iterator lNamespacesIterator = lNamespaces.iterator(); lNamespacesIterator
084: .hasNext();) {
085: Namespace lNamespace = (Namespace) lNamespacesIterator
086: .next();
087: lSubNamespaces.addAll(lNamespace
088: .getCombinedSubNamespaces());
089: }
090: }
091: return Collections.unmodifiableCollection(lSubNamespaces);
092: }
093:
094: // Returns collection of of all Structures contained within this namespace and it's subnamespaces
095: public Collection getCombinedStructures() {
096: List lStructures = new ArrayList();
097: lStructures.addAll(getStructures());
098: Collection lNamespaces = getSubNamespaces();
099: if (!lNamespaces.isEmpty()) {
100: for (Iterator lNamespacesIterator = lNamespaces.iterator(); lNamespacesIterator
101: .hasNext();) {
102: Namespace lNamespace = (Namespace) lNamespacesIterator
103: .next();
104: lStructures.addAll(lNamespace.getCombinedStructures());
105: }
106: }
107: return Collections.unmodifiableCollection(lStructures);
108: }
109:
110: // Returns collection of of all Messages contained within this namespace and it's subnamespaces
111: public Collection getCombinedMessages() {
112: List lMessages = new ArrayList();
113: lMessages.addAll(getMessages());
114: Collection lNamespaces = getSubNamespaces();
115: if (!lNamespaces.isEmpty()) {
116: for (Iterator lNamespacesIterator = lNamespaces.iterator(); lNamespacesIterator
117: .hasNext();) {
118: Namespace lNamespace = (Namespace) lNamespacesIterator
119: .next();
120: lMessages.addAll(lNamespace.getCombinedMessages());
121: }
122: }
123: return Collections.unmodifiableCollection(lMessages);
124: }
125:
126: // Returns list of of Structures directly within this namespace in its dependency order. Any external structures are ignored
127: public List getStructuresInDependencyOrder() {
128: Collection lStructures = getStructures();
129: Structure[] lReturnArray = new Structure[lStructures.size()];
130: HashSet lUnprocessedStructRefs = new HashSet();
131: {
132: int i = 0;
133: for (Iterator lStructsIterator = lStructures.iterator(); lStructsIterator
134: .hasNext();) {
135: Structure lStruct = (Structure) lStructsIterator.next();
136: lReturnArray[i++] = lStruct;
137: lUnprocessedStructRefs.add(lStruct.getRef());
138: }
139: }
140: HashSet lProcessedStructRefs = new HashSet();
141: ArrayList lProcessedStructs = new ArrayList();
142: for (int i = 0; i < lReturnArray.length;) {
143: Structure lStructure = lReturnArray[i];
144: boolean lMustBeMoved = false;
145: StructureField[] lFields = (StructureField[]) lStructure
146: .getFields().toArray(new StructureField[0]);
147: for (int j = 0; j < lFields.length; j++) {
148: StructureField lField = lFields[j];
149: Structure lFieldStructureType = lField
150: .getStructureType();
151: if (lFieldStructureType != null
152: && lStructures.contains(lFieldStructureType)) {
153: if (lFieldStructureType.equals(lStructure))
154: continue; // Structure is referring to itself - already defined
155: if (lProcessedStructRefs
156: .contains(lFieldStructureType.getRef()))
157: continue; // Structure is referring to already defined structure
158: if (!lUnprocessedStructRefs
159: .contains(lFieldStructureType.getRef()))
160: throw new RuntimeException(
161: "Unexpected situation. Structure reference not found.");
162: lMustBeMoved = true;
163: break;
164: }
165: }
166: if (!lMustBeMoved) {
167: lUnprocessedStructRefs.remove(lStructure.getRef());
168: lProcessedStructRefs.add(lStructure.getRef());
169: i++;
170: continue; // Only has already defined fields
171: }
172: // Shift this structure to the back and do not increment index
173: System.arraycopy(lReturnArray, i + 1, lReturnArray, i,
174: lReturnArray.length - i - 1);
175: lReturnArray[lReturnArray.length - 1] = lStructure;
176: }
177: return Arrays.asList(lReturnArray);
178: }
179:
180: // Returns path to this namespace including itself and including DataDictionary
181: public List getPathWithDictionary() {
182: List lNamespaces = new ArrayList();
183: for (AbstractNamespace lNamespace = this ; lNamespace != null;) {
184: lNamespaces.add(0, lNamespace);
185: if ((lNamespace instanceof Namespace) == false)
186: break; // not a namespace (i.e. DataDictionary) will force break from the loop
187: lNamespace = ((Namespace) lNamespace).getNamespace();
188: }
189: return Collections.unmodifiableList(lNamespaces);
190: }
191:
192: // Returns path to this namespace including itself and including DataDictionary
193: public List getPathWithoutDictionary() {
194: List lNamespaces = new ArrayList();
195: for (AbstractNamespace lNamespace = this ; lNamespace != null;) {
196: if ((lNamespace instanceof Namespace) == false)
197: break; // not a namespace (i.e. DataDictionary) will force break from the loop
198: lNamespaces.add(0, lNamespace);
199: lNamespace = ((Namespace) lNamespace).getNamespace();
200: }
201: return Collections.unmodifiableList(lNamespaces);
202: }
203:
204: // Returns Namespace with specified name or throws exception if none found
205: public Namespace getSubNamespace(String pSubNamespaceName) {
206: Namespace lFoundSubNamespace = findSubNamespace(pSubNamespaceName);
207: // Throw exception if nothing found
208: if (lFoundSubNamespace == null)
209: throw new IllegalArgumentException(
210: "Unable to locate sub Namespace named '"
211: + pSubNamespaceName
212: + "' in Namespace. NamespaceRef: "
213: + getRef());
214: return lFoundSubNamespace;
215: }
216:
217: // Returns Namespace with specified name or null if none found
218: public Namespace findSubNamespace(String pSubNamespaceName) {
219: Collection lSubNamespaces = getSubNamespaces();
220: if (!lSubNamespaces.isEmpty()) {
221: for (Iterator lSubNamespacesIterator = lSubNamespaces
222: .iterator(); lSubNamespacesIterator.hasNext();) {
223: Namespace lSubNamespace = (Namespace) lSubNamespacesIterator
224: .next();
225: if (lSubNamespace.getName().equals(pSubNamespaceName))
226: return lSubNamespace;
227: }
228: }
229: return null;
230: }
231:
232: // Returns TypeTemplate with specified name or throws exception if none found
233: public TypeTemplate getTypeTemplate(String pTypeTemplateName) {
234: TypeTemplate lFoundTypeTemplate = findTypeTemplate(pTypeTemplateName);
235: // Throw exception if nothing found
236: if (lFoundTypeTemplate == null)
237: throw new IllegalArgumentException(
238: "Unable to locate TypeTemplate named '"
239: + pTypeTemplateName
240: + "' in Namespace. NamespaceRef: "
241: + getRef());
242: return lFoundTypeTemplate;
243: }
244:
245: // Returns TypeTemplate with specified name or null if none found
246: public TypeTemplate findTypeTemplate(String pTypeTemplateName) {
247: Collection lTypeTemplates = getTypeTemplates();
248: if (!lTypeTemplates.isEmpty()) {
249: for (Iterator lTypeTemplatesIterator = lTypeTemplates
250: .iterator(); lTypeTemplatesIterator.hasNext();) {
251: TypeTemplate lTypeTemplate = (TypeTemplate) lTypeTemplatesIterator
252: .next();
253: if (lTypeTemplate.getName().equals(pTypeTemplateName))
254: return lTypeTemplate;
255: }
256: }
257: return null;
258: }
259:
260: // Return DataType with specified name or throws exception if none found
261: public DataType getDataType(String pDataTypeName) {
262: DataType lFoundDataType = findDataType(pDataTypeName);
263: // Throw exception if nothing found
264: if (lFoundDataType == null)
265: throw new IllegalArgumentException(
266: "Unable to locate DataType named '" + pDataTypeName
267: + "' in Namespace. NamespaceRef: "
268: + getRef());
269: return lFoundDataType;
270: }
271:
272: // Return DataType with specified name or null if none found
273: public DataType findDataType(String pDataTypeName) {
274: Collection lDataTypes = getDataTypes();
275: if (!lDataTypes.isEmpty()) {
276: for (Iterator lDataTypesIterator = lDataTypes.iterator(); lDataTypesIterator
277: .hasNext();) {
278: DataType lDataType = (DataType) lDataTypesIterator
279: .next();
280: if (lDataType.getName().equals(pDataTypeName))
281: return lDataType;
282: }
283: }
284: return null;
285: }
286:
287: // Return Structure with specified name or throws exception if none found
288: public Structure getStructure(String pStructureName) {
289: Structure lFoundStructure = findStructure(pStructureName);
290: // Throw exception if nothing found
291: if (lFoundStructure == null)
292: throw new IllegalArgumentException(
293: "Unable to locate Structure named '"
294: + pStructureName
295: + "' in Namespace. NamespaceRef: "
296: + getRef());
297: return lFoundStructure;
298: }
299:
300: // Return Structure with specified name or null if none found
301: public Structure findStructure(String pStructureName) {
302: Collection lStructures = getStructures();
303: if (!lStructures.isEmpty()) {
304: for (Iterator lStructuresIterator = lStructures.iterator(); lStructuresIterator
305: .hasNext();) {
306: Structure lStructure = (Structure) lStructuresIterator
307: .next();
308: if (lStructure.getName().equals(pStructureName))
309: return lStructure;
310: }
311: }
312: return null;
313: }
314:
315: // Return Message with specified name or throws exception if none found
316: public Message getMessage(String pMessageName) {
317: Message lFoundMessage = findMessage(pMessageName);
318: // Throw exception if nothing found
319: if (lFoundMessage == null)
320: throw new IllegalArgumentException(
321: "Unable to locate Message named '" + pMessageName
322: + "' in Namespace. NamespaceRef: "
323: + getRef());
324: return lFoundMessage;
325: }
326:
327: // Return Message with specified name or null if none found
328: public Message findMessage(String pMessageName) {
329: Collection lMessages = getMessages();
330: if (!lMessages.isEmpty()) {
331: for (Iterator lMessagesIterator = lMessages.iterator(); lMessagesIterator
332: .hasNext();) {
333: Message lMessage = (Message) lMessagesIterator.next();
334: if (lMessage.getName().equals(pMessageName))
335: return lMessage;
336: }
337: }
338: return null;
339: }
340:
341: // Returns list of DataTypes, Structures and Messages used in the namespace. This includes owned and referenced elements
342: public Collection getCombinedTypes() {
343: Set lCombinedTypes = new HashSet();
344: // Add all datatypes from this namespace
345: lCombinedTypes.addAll(getDataTypes());
346: // Add all structures and datatypes used in them
347: for (Iterator lStructuresIterator = getStructures().iterator(); lStructuresIterator
348: .hasNext();) {
349: Structure lStructure = (Structure) lStructuresIterator
350: .next();
351: lCombinedTypes.add(lStructure);
352: lCombinedTypes.addAll(lStructure.getCombinedTypes());
353: }
354: // Add all messages and datatypes used in them
355: for (Iterator lMessagesIterator = getMessages().iterator(); lMessagesIterator
356: .hasNext();) {
357: Message lMessage = (Message) lMessagesIterator.next();
358: lCombinedTypes.add(lMessage);
359: lCombinedTypes.addAll(lMessage.getCombinedTypes());
360: }
361: // Call subnamespaces
362: for (Iterator lSubNamespacesIterator = getSubNamespaces()
363: .iterator(); lSubNamespacesIterator.hasNext();) {
364: Namespace lSubNamespace = (Namespace) lSubNamespacesIterator
365: .next();
366: lCombinedTypes.addAll(lSubNamespace.getCombinedTypes());
367: }
368: return java.util.Collections
369: .unmodifiableCollection(lCombinedTypes);
370: }
371:
372: }
|