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;
016:
017: import java.util.Collection;
018: import java.util.Iterator;
019: import java.util.StringTokenizer;
020:
021: import javax.jmi.reflect.ConstraintViolationException;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.netbeans.mdr.handlers.ClassProxyHandler;
026: import org.netbeans.mdr.storagemodel.StorableClass;
027:
028: import com.metaboss.sdlctools.models.metabossmodel.MetaBossModelPackage;
029: import com.metaboss.sdlctools.models.metabossmodel.ModelElement;
030: import com.metaboss.sdlctools.models.metabossmodel.ModelElementClass;
031: import com.metaboss.sdlctools.models.metabossmodel.ModelVersion;
032: import com.metaboss.sdlctools.models.metabossmodel.designlibrarymodel.DesignLibrary;
033: import com.metaboss.sdlctools.models.metabossmodel.designlibrarymodel.DesignLibraryModelPackage;
034: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Enterprise;
035: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.EnterpriseModelPackage;
036: import com.metaboss.sdlctools.models.metabossmodel.technologylibrarymodel.TechnologyLibrary;
037: import com.metaboss.sdlctools.models.metabossmodel.technologylibrarymodel.TechnologyLibraryModelPackage;
038:
039: public abstract class ModelElementClassImpl extends ClassProxyHandler
040: implements ModelElementClass {
041: // Commons Logging instance.
042: private static final Log sLogger = LogFactory
043: .getLog(ModelElementClassImpl.class);
044:
045: // Required constructor
046: protected ModelElementClassImpl(StorableClass storable) {
047: super (storable);
048: }
049:
050: // Implement custom method, which gets the instance of the element by
051: // supplied reference or throws exception if element is not found
052: public ModelElement getByRef(String pRef) {
053: ModelElement lFoundElement = findByRef(pRef);
054: // Throw exception if nothing found
055: if (lFoundElement == null)
056: throw new IllegalArgumentException(
057: "Unable to locate ModelElement with Ref " + pRef);
058: return lFoundElement;
059: }
060:
061: // Implement custom method, which gets the instance of the element by
062: // supplied reference or returns null if element is not found
063: public ModelElement findByRef(String pRef) {
064: ModelElement lCurrentModelElement = null;
065: // Disassemble ref onto the tokens
066: StringTokenizer lTokenizer = new StringTokenizer(pRef, "/",
067: false);
068: while (lTokenizer.hasMoreTokens()) {
069: ModelElement lNewModelElement = null;
070: String lInstanceName = null;
071: String lTypeOrReference = lTokenizer.nextToken();
072: if (lTypeOrReference.endsWith("]")) {
073: int lNameStartBracePos = lTypeOrReference.indexOf("[");
074: if (lNameStartBracePos <= 0)
075: throw new IllegalArgumentException(
076: "Invalid format of ModelElement Ref (Found ']' character without opening '['): "
077: + pRef);
078: lInstanceName = lTypeOrReference.substring(
079: lNameStartBracePos + 1, lTypeOrReference
080: .length() - 1);
081: lTypeOrReference = lTypeOrReference.substring(0,
082: lNameStartBracePos);
083: }
084: if (lCurrentModelElement == null) {
085: // Approach top elements individually
086: if (lTypeOrReference.equals("Enterprise")) {
087: if (lInstanceName != null)
088: throw new IllegalArgumentException(
089: "Invalid format of ModelElement Ref (Enterprise element must not have a name): "
090: + pRef);
091: EnterpriseModelPackage lEnterpriseModelPackage = ((MetaBossModelPackage) refOutermostPackage())
092: .getEnterpriseModel();
093: Collection lEnteprises = lEnterpriseModelPackage
094: .getEnterprise().refAllOfType();
095: int lEnterprisesCount = lEnteprises.size();
096: if (lEnterprisesCount == 0)
097: return null; // Not found
098: else if (lEnterprisesCount > 1)
099: throw new ConstraintViolationException(
100: lEnterpriseModelPackage,
101: lEnterpriseModelPackage.refMetaObject(),
102: "There can only be at most one instance of Enterprise element in the model.");
103: lNewModelElement = (Enterprise) lEnteprises
104: .iterator().next();
105: } else if (lTypeOrReference.equals("DesignLibrary")) {
106: if (lInstanceName != null)
107: throw new IllegalArgumentException(
108: "Invalid format of ModelElement Ref (DesignLibrary element must not have a name): "
109: + pRef);
110: DesignLibraryModelPackage lDesignLibraryModelPackage = ((MetaBossModelPackage) refOutermostPackage())
111: .getDesignLibraryModel();
112: Collection lDesignLibraries = lDesignLibraryModelPackage
113: .getDesignLibrary().refAllOfType();
114: int lDesignLibrariesCount = lDesignLibraries.size();
115: if (lDesignLibrariesCount == 0)
116: return null; // Not found
117: else if (lDesignLibrariesCount > 1)
118: throw new ConstraintViolationException(
119: lDesignLibraryModelPackage,
120: lDesignLibraryModelPackage
121: .refMetaObject(),
122: "There can only be at most one instance of DesignLibrary element in the model.");
123: DesignLibrary lDesignLibrary = (DesignLibrary) lDesignLibraries
124: .iterator().next();
125: if (lDesignLibrary.getEnterprise() != null)
126: return null; // Found design library, but it is not a top level one
127: lNewModelElement = lDesignLibrary;
128: } else if (lTypeOrReference.equals("TechnologyLibrary")) {
129: if (lInstanceName != null)
130: throw new IllegalArgumentException(
131: "Invalid format of ModelElement Ref (TechnologyLibrary element must not have a name): "
132: + pRef);
133: TechnologyLibraryModelPackage lTechnologyLibraryModelPackage = ((MetaBossModelPackage) refOutermostPackage())
134: .getTechnologyLibraryModel();
135: Collection lTechnologyLibraries = lTechnologyLibraryModelPackage
136: .getTechnologyLibrary().refAllOfType();
137: int lTechnologyLibrariesCount = lTechnologyLibraries
138: .size();
139: if (lTechnologyLibrariesCount == 0)
140: return null; // Not found
141: else if (lTechnologyLibrariesCount > 1)
142: throw new ConstraintViolationException(
143: lTechnologyLibraryModelPackage,
144: lTechnologyLibraryModelPackage
145: .refMetaObject(),
146: "There can only be at most one instance of TechnologyLibrary element in the model.");
147: TechnologyLibrary lTechnologyLibrary = (TechnologyLibrary) lTechnologyLibraries
148: .iterator().next();
149: if (lTechnologyLibrary.getEnterprise() != null)
150: return null; // Found design library, but it is not a top level one
151: lNewModelElement = lTechnologyLibrary;
152: } else if (lTypeOrReference.equals("ModelVersion")) {
153: if (lInstanceName != null)
154: throw new IllegalArgumentException(
155: "Invalid format of ModelElement Ref (ModelVersion element must not have a name): "
156: + pRef);
157: MetaBossModelPackage lMetaBossModelPackage = (MetaBossModelPackage) refOutermostPackage();
158: Collection lModelVersions = lMetaBossModelPackage
159: .getModelVersion().refAllOfType();
160: int lModelVersionsCount = lModelVersions.size();
161: if (lModelVersionsCount == 0)
162: return null; // Not found
163: else if (lModelVersionsCount > 1)
164: throw new ConstraintViolationException(
165: lMetaBossModelPackage,
166: lMetaBossModelPackage.refMetaObject(),
167: "There can only be at most one instance of ModelVersion element in the model.");
168: ModelVersion lModelVersion = (ModelVersion) lModelVersions
169: .iterator().next();
170: lNewModelElement = lModelVersion;
171: } else
172: throw new IllegalArgumentException(
173: "Invalid format of ModelElement Ref (Expecting top element to be one of the 'Enterprise', 'DesignLibrary' or 'TechnologyLibrary'): "
174: + pRef);
175: } else {
176: try {
177: Object lReferencedValue = lCurrentModelElement
178: .refGetValue(lTypeOrReference);
179: if (lReferencedValue != null) {
180: if (lInstanceName == null
181: && lReferencedValue instanceof ModelElement)
182: lNewModelElement = (ModelElement) lReferencedValue;
183: else if (lInstanceName != null
184: && lReferencedValue instanceof Collection) {
185: for (Iterator lAllReferencedValuesIterator = ((Collection) lReferencedValue)
186: .iterator(); lAllReferencedValuesIterator
187: .hasNext();) {
188: ModelElement lModelElement = (ModelElement) lAllReferencedValuesIterator
189: .next();
190: if (lModelElement.getName().equals(
191: lInstanceName)) {
192: lNewModelElement = lModelElement;
193: break;
194: }
195: }
196: }
197: }
198: } catch (javax.jmi.reflect.InvalidNameException e) {
199: return null; // Path is refering to the notexistant element
200: }
201: }
202: if (lNewModelElement == null)
203: return null; // Path is refering to the notexistant element
204: lCurrentModelElement = lNewModelElement;
205: }
206: return lCurrentModelElement;
207: }
208: }
|