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.services.codegeneration;
016:
017: import java.util.ArrayList;
018: import java.util.HashMap;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022:
023: import javax.jmi.reflect.JmiException;
024:
025: import com.metaboss.enterprise.bs.BSException;
026: import com.metaboss.enterprise.bs.BSServiceProviderException;
027: import com.metaboss.sdlctools.models.metabossmodel.ModelElement;
028: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.DataDictionary;
029: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.DataType;
030: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.Namespace;
031: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.Structure;
032: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.TypeTemplate;
033: import com.metaboss.sdlctools.models.metabossmodel.designlibrarymodel.DesignLibrary;
034: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Message;
035: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.AggregationTypeEnum;
036: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Association;
037: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.AssociationRole;
038:
039: /** Set of useful enterprise model utilites */
040: public class CommonUtil {
041: /** Builds template reference to be used in the template processing */
042: private static Map sGetTemplateReferenceForTemplateProcessorOutput = new HashMap();
043:
044: public static String getTemplateReferenceForTemplateProcessor(
045: TypeTemplate pTypeTemplate) throws BSException {
046: String lResult = (String) sGetTemplateReferenceForTemplateProcessorOutput
047: .get(pTypeTemplate.getRef());
048: if (lResult == null) {
049: DataDictionary lDataDictionary = pTypeTemplate
050: .getOwnerDataDictionary();
051: com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.System lSystem = lDataDictionary
052: .getSystem();
053: DesignLibrary lDesignLibrary = lDataDictionary
054: .getDesignLibrary();
055: // Now that we have access to all elements - roll theminto string
056: StringBuffer lStringBuffer = new StringBuffer();
057: if (lSystem != null) {
058: lStringBuffer.append(lSystem.getEnterprise().getName());
059: lStringBuffer.append(".");
060: lStringBuffer.append(lSystem.getName());
061: } else {
062: lStringBuffer.append(lDesignLibrary.getEnterprise()
063: .getName());
064: }
065: lStringBuffer.append(".TypeTemplates.");
066: for (Iterator lNamespacesIterator = pTypeTemplate
067: .getNamespace().getPathWithoutDictionary()
068: .iterator(); lNamespacesIterator.hasNext();) {
069: Namespace lNamespace = (Namespace) lNamespacesIterator
070: .next();
071: lStringBuffer
072: .append(lNamespace.getName().toLowerCase());
073: lStringBuffer.append(".");
074: }
075: lStringBuffer.append(pTypeTemplate.getName());
076: sGetTemplateReferenceForTemplateProcessorOutput.put(
077: pTypeTemplate.getRef(), lResult = lStringBuffer
078: .toString());
079: }
080: return lResult;
081: }
082:
083: /** Returns the list of referenced generic datatypes (they do not belong to any system) */
084: public static DataType[] getReferencedPublicDatatypes(
085: com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.System pSystem)
086: throws BSException {
087: try {
088: // Get all and filter out the generic ones
089: ArrayList lReturn = new ArrayList();
090: for (Iterator lCombinedTypesIterator = pSystem
091: .getCombinedTypes().iterator(); lCombinedTypesIterator
092: .hasNext();) {
093: ModelElement lType = (ModelElement) lCombinedTypesIterator
094: .next();
095: if (lType instanceof DataType) {
096: DataType lDataType = (DataType) lType;
097: DataDictionary lOwnerDataDictionary = lDataType
098: .getOwnerDataDictionary();
099: if (lOwnerDataDictionary.getSystem() == null)
100: lReturn.add(lDataType);
101: }
102: }
103: return (DataType[]) lReturn.toArray(new DataType[lReturn
104: .size()]);
105: } catch (JmiException e) {
106: throw new BSServiceProviderException(
107: "Unable to execute getReferencedPublicDatatypes() method.",
108: e);
109: }
110: }
111:
112: /** Returns the list of referenced generic structures (they do not belong to any system) */
113: public static Structure[] getReferencedPublicStructures(
114: com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.System pSystem)
115: throws BSException {
116: try {
117: // Get all and filter out the generic ones
118: ArrayList lReturn = new ArrayList();
119: for (Iterator lCombinedTypesIterator = pSystem
120: .getCombinedTypes().iterator(); lCombinedTypesIterator
121: .hasNext();) {
122: ModelElement lType = (ModelElement) lCombinedTypesIterator
123: .next();
124: if (lType instanceof Structure) {
125: Structure lStructure = (Structure) lType;
126: // Check if mesage is in the datadictionary which does not belong to any system
127: DataDictionary lOwnerDataDictionary = lStructure
128: .getOwnerDataDictionary();
129: if (lOwnerDataDictionary != null
130: && lOwnerDataDictionary.getSystem() == null)
131: lReturn.add(lStructure);
132: }
133: }
134: return (Structure[]) lReturn.toArray(new Structure[lReturn
135: .size()]);
136: } catch (JmiException e) {
137: throw new BSServiceProviderException(
138: "Unable to execute getReferencedPublicStructures() method.",
139: e);
140: }
141: }
142:
143: /** Returns the list of referenced generic messages (they do not belong to any system) */
144: public static Message[] getReferencedPublicMessages(
145: com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.System pSystem)
146: throws BSException {
147: try {
148: // Get all and filter out the generic ones
149: ArrayList lReturn = new ArrayList();
150: for (Iterator lCombinedTypesIterator = pSystem
151: .getCombinedTypes().iterator(); lCombinedTypesIterator
152: .hasNext();) {
153: ModelElement lType = (ModelElement) lCombinedTypesIterator
154: .next();
155: if (lType instanceof Message) {
156: Message lMessage = (Message) lType;
157: // Check if mesage is in the datadictionary which does not belong to any system
158: DataDictionary lOwnerDataDictionary = lMessage
159: .getOwnerDataDictionary();
160: if (lOwnerDataDictionary != null
161: && lOwnerDataDictionary.getSystem() == null)
162: lReturn.add(lMessage);
163: }
164: }
165: return (Message[]) lReturn.toArray(new Message[lReturn
166: .size()]);
167: } catch (JmiException e) {
168: throw new BSServiceProviderException(
169: "Unable to execute getReferencedPublicMessages() method.",
170: e);
171: }
172: }
173:
174: /** Returns true if specified role results in entity referencing this role
175: * having referenced_instance_id attribute being included in the entity. */
176: public static boolean doesEntityStoreReference(
177: AssociationRole pReference) throws BSException {
178: try {
179: if (pReference.isPlural())
180: return false; // This role references more than one entity on the other side, therefore it can not have reference
181: if (pReference.equals(AggregationTypeEnum.COMPOSITION))
182: return true; // This is the reference to the owner - we will always have the reference
183: AssociationRole lEntityRole = pReference.getOppositeRole();
184: // In this association thie entity is an ownre - it will never have a reference
185: if (lEntityRole.getAggregationType().equals(
186: AggregationTypeEnum.COMPOSITION))
187: return false;
188: // If we are just associated (no aggregation), than entity has the reference
189: if (lEntityRole.getAggregationType().equals(
190: AggregationTypeEnum.NONE))
191: return true;
192: // We are now left with the case where this entity aggregates
193: // a single instance of the other entity. Since we would rather push the
194: // reference to the end with AggregationType NONE and it is only not possible
195: // if our role is plural - chec for that and give final answer
196: return lEntityRole.isPlural();
197: } catch (JmiException e) {
198: throw new BSServiceProviderException(
199: "Unable to execute doesEntityHaveReference() method.",
200: e);
201: }
202: }
203:
204: /** Returns true if in the specified association neither of entities is able to store the reference to the other. */
205: public static boolean doEntitiesStoreReference(
206: Association pAssociation) throws BSException {
207: try {
208: List lRoles = pAssociation.getRoles();
209: return doesEntityStoreReference((AssociationRole) lRoles
210: .get(0))
211: || doesEntityStoreReference((AssociationRole) lRoles
212: .get(1));
213: } catch (JmiException e) {
214: throw new BSServiceProviderException(
215: "Unable to execute doEntitiesHaveReference() method.",
216: e);
217: }
218: }
219: }
|