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.domains.enterprisemodel.impl;
016:
017: import javax.naming.Context;
018: import javax.naming.InitialContext;
019: import javax.naming.NamingException;
020:
021: import com.metaboss.enterprise.bo.BOException;
022: import com.metaboss.enterprise.bo.BONamingAndDirectoryServiceInvocationException;
023: import com.metaboss.enterprise.bo.BOPersistenceServiceInvocationException;
024: import com.metaboss.enterprise.ps.PSException;
025: import com.metaboss.sdlctools.domains.enterprisemodel.BOAssociationRole;
026: import com.metaboss.sdlctools.domains.enterprisemodel.BOAttribute;
027: import com.metaboss.sdlctools.domains.enterprisemodel.BOEntity;
028: import com.metaboss.sdlctools.domains.enterprisemodel.BOEntityPrimaryKeyElementList;
029: import com.metaboss.sdlctools.domains.enterprisemodel.BOPrimaryKeyElement;
030: import com.metaboss.sdlctools.domains.enterprisemodel.storage.PSEntity;
031: import com.oldboss.framework.bo.impl.BOObjectImpl;
032:
033: public class BOEntityPrimaryKeyElementListImpl extends BOObjectImpl
034: implements BOEntityPrimaryKeyElementList {
035: private BOMetaBossDomainImpl mMetaBossDomainImpl = null;
036: private BOEntity mEntity = null;
037:
038: /* Instance creator */
039: public static BOEntityPrimaryKeyElementList createInstanceForExisting(
040: BOMetaBossDomainImpl pMetaBossDomainImpl, BOEntity pEntity)
041: throws BOException {
042: BOEntityPrimaryKeyElementListImpl lImpl = new BOEntityPrimaryKeyElementListImpl();
043: lImpl.initialiseForExisting(pMetaBossDomainImpl, pEntity);
044: return lImpl;
045: }
046:
047: /* Private constructor restricts instance creation from outside */
048: private BOEntityPrimaryKeyElementListImpl() throws BOException {
049: }
050:
051: /* Instance initialisation */
052: private void initialiseForExisting(
053: BOMetaBossDomainImpl pMetaBossDomainImpl, BOEntity pEntity)
054: throws BOException {
055: mMetaBossDomainImpl = pMetaBossDomainImpl;
056: mEntity = pEntity;
057: setupForExisting();
058: }
059:
060: /* Retrieves all elements comprising primary key for the entity.
061: * It may be of type BOAttribute or BORelationship_Association */
062: public BOPrimaryKeyElement[] getAllElements() throws BOException {
063: // First load names of the elements
064: try {
065: // Get the instance of the enterprise ps home via jndi
066: Context ctx = new InitialContext();
067: PSEntity lPs = (PSEntity) ctx
068: .lookup(PSEntity.COMPONENT_URL);
069:
070: String[] lElementNames = lPs
071: .getPrimaryKey(mEntity.getRef());
072: if (lElementNames == null || lElementNames.length == 0)
073: return new BOPrimaryKeyElement[0]; // Primary key is empty
074:
075: BOAttribute[] lCombinedAttributes = mEntity.getAttributes()
076: .getCombinedAttributes();
077: BOAssociationRole[] lCombinedRoles = mEntity
078: .getAssociationRoles()
079: .getCombinedAssociationRoles();
080: // Now iterate through and look for matching attribute or association
081: BOPrimaryKeyElement[] lReturn = new BOPrimaryKeyElement[lElementNames.length];
082: for (int i = 0; i < lElementNames.length; i++) {
083: String lElementName = lElementNames[i];
084: BOPrimaryKeyElement lElement = null;
085: // First see if element is an attribute
086: for (int j = 0; j < lCombinedAttributes.length; j++) {
087: BOAttribute lAttribute = lCombinedAttributes[j];
088: if (lElementName.equals(lAttribute.getName())) {
089: if (lAttribute
090: .getStereotype()
091: .equals(
092: com.metaboss.sdlctools.types.enterprisemodel.AttributeStereotype.OPTIONAL))
093: throw new BOException(
094: "Optional attribute can not be a part of the primary key. (EntityRef:"
095: + mEntity.getRef()
096: + " ElementName:"
097: + lElementName + ")");
098: lElement = BOPrimaryKeyElementImpl
099: .createInstanceForExisting(
100: mMetaBossDomainImpl, mEntity,
101: lAttribute);
102: break;
103: }
104: }
105: if (lElement == null) {
106: // Now see if it is the role
107: for (int j = 0; j < lCombinedRoles.length; j++) {
108: BOAssociationRole lRole = lCombinedRoles[j];
109: if (lElementName.equals(lRole.getName())) {
110: if (!lRole
111: .getCardinality()
112: .equals(
113: com.metaboss.sdlctools.types.enterprisemodel.AssociationRoleCardinality.ONE))
114: throw new BOException(
115: "Only association with \"exactly one\" cardinality can be part of primary key. (EntityRef:"
116: + mEntity.getRef()
117: + " ElementName:"
118: + lElementName + ")");
119: lElement = BOPrimaryKeyElementImpl
120: .createInstanceForExisting(
121: mMetaBossDomainImpl,
122: mEntity, lRole);
123: break;
124: }
125: }
126: }
127: if (lElement == null)
128: throw new BOException(
129: "Entity's Primary Key definition contains unknown element. (EntityRef:"
130: + mEntity.getRef()
131: + " ElementName:"
132: + lElementNames[i] + ")");
133: lReturn[i] = lElement;
134: }
135: return lReturn;
136: } catch (NamingException e) {
137: throw new BONamingAndDirectoryServiceInvocationException(
138: "", e);
139: } catch (PSException e) {
140: throw new BOPersistenceServiceInvocationException("", e);
141: }
142: }
143:
144: /* Returns number of elements comprising primary key */
145: public int size() throws BOException {
146: // First load names of the elements
147: try {
148: // Get the instance of the enterprise ps home via jndi
149: Context ctx = new InitialContext();
150: PSEntity lPs = (PSEntity) ctx
151: .lookup(PSEntity.COMPONENT_URL);
152:
153: String[] lElementNames = lPs
154: .getPrimaryKey(mEntity.getRef());
155: if (lElementNames == null)
156: return 0;
157: return lElementNames.length;
158: } catch (NamingException e) {
159: throw new BONamingAndDirectoryServiceInvocationException(
160: "", e);
161: } catch (PSException e) {
162: throw new BOPersistenceServiceInvocationException("", e);
163: }
164: }
165: }
|