01: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
02: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
03: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
04: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
05: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
06: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
07: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
08: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
09: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
10: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
11: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
12: // POSSIBILITY OF SUCH DAMAGE.
13: //
14: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
15: package com.metaboss.sdlctools.models.impl.metabossmodel;
16:
17: import javax.jmi.reflect.RefObject;
18: import javax.jmi.reflect.RefPackage;
19:
20: import com.metaboss.sdlctools.models.ModelElementResolver;
21: import com.metaboss.sdlctools.models.ModelRepositoryException;
22: import com.metaboss.sdlctools.models.ModelRepositoryIllegalArgumentException;
23: import com.metaboss.sdlctools.models.metabossmodel.MetaBossModelPackage;
24: import com.metaboss.sdlctools.models.metabossmodel.ModelElement;
25: import com.metaboss.sdlctools.models.metabossmodel.ModelUtils;
26: import com.metaboss.sdlctools.models.metabossmodel.designlibrarymodel.DesignLibrary;
27: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Enterprise;
28: import com.metaboss.util.StringUtils;
29:
30: /** The implementation of the ModelElementResolver as applicable to the MetaBoss models. */
31: public class ModelElementResolverImpl implements ModelElementResolver {
32: // The only instance of the resolver
33: private static ModelElementResolver sInstance = new ModelElementResolverImpl();
34:
35: /** The only way to obtain the instance of the resolver */
36: public static ModelElementResolver getInstance() {
37: return sInstance;
38: }
39:
40: // Hidden constructor, so not one can create an instance */
41: private ModelElementResolverImpl() {
42: }
43:
44: /** Looks at the kind of given model element and attempts to find the same model element in the
45: * given package. Note that the supplied model package and model elements must be of type MetaBossModelElement and
46: * MetaBossModelPackage.
47: */
48: public RefObject getMatchingModelElement(RefObject pOriginalObject,
49: RefPackage pTargetModelExtent)
50: throws ModelRepositoryException {
51: if ((pOriginalObject instanceof ModelElement) == false)
52: throw new ModelRepositoryIllegalArgumentException(
53: "SourceObject parameter must be of type "
54: + ModelElement.class.getName());
55: if ((pTargetModelExtent instanceof MetaBossModelPackage) == false)
56: throw new ModelRepositoryIllegalArgumentException(
57: "TargetExtent parameter must be of type "
58: + MetaBossModelPackage.class.getName());
59: ModelElement lSourceModelElement = (ModelElement) pOriginalObject;
60: MetaBossModelPackage lSourceModelPackage = (MetaBossModelPackage) lSourceModelElement
61: .refOutermostPackage();
62: ModelElement lSourceModelRootElement = ModelUtils
63: .getRootElement(lSourceModelPackage);
64: MetaBossModelPackage lTargetModelPackage = (MetaBossModelPackage) pTargetModelExtent;
65: ModelElement lTargetModelRootElement = ModelUtils
66: .getRootElement(lTargetModelPackage);
67:
68: // If the source and target model are one and the same - we just need to return source object
69: if (lSourceModelPackage.equals(lTargetModelPackage))
70: return pOriginalObject;
71: // If the source and target model are of the same type - we just need to find the same ref
72: if (lSourceModelRootElement.refMetaObject().equals(
73: lTargetModelRootElement.refMetaObject()))
74: return lTargetModelPackage.getModelElement().findByRef(
75: lSourceModelElement.getRef());
76: // If the source is design library and target is enterprise - we need to modify reference a little
77: if ((lSourceModelRootElement instanceof DesignLibrary)
78: && (lTargetModelRootElement instanceof Enterprise))
79: return lTargetModelPackage
80: .getModelElement()
81: .findByRef(
82: "Enterprise/"
83: + StringUtils
84: .toLowerCaseFirstCharacter(lSourceModelElement
85: .getRef()));
86: return null;
87: }
88: }
|