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.BOIllegalArgumentException;
023: import com.metaboss.enterprise.bo.BOInvalidOperationForObjectException;
024: import com.metaboss.enterprise.bo.BOInvalidOperationForReadOnlyObjectException;
025: import com.metaboss.enterprise.bo.BONamingAndDirectoryServiceInvocationException;
026: import com.metaboss.enterprise.bo.BOPersistenceServiceInvocationException;
027: import com.metaboss.enterprise.ps.PSException;
028: import com.metaboss.sdlctools.domains.enterprisemodel.BOAssociation;
029: import com.metaboss.sdlctools.domains.enterprisemodel.BOAssociationRole;
030: import com.metaboss.sdlctools.domains.enterprisemodel.BOEntity;
031: import com.metaboss.sdlctools.domains.enterprisemodel.storage.PSAssociationRole;
032: import com.metaboss.sdlctools.domains.enterprisemodel.storage.STAssociationRole;
033: import com.metaboss.sdlctools.types.enterprisemodel.AssociationRoleCardinality;
034: import com.metaboss.sdlctools.types.enterprisemodel.AssociationRoleStereotype;
035: import com.oldboss.framework.bo.BOTransaction;
036: import com.oldboss.framework.bo.impl.BOObjectImpl;
037:
038: public class BOAssociationRoleImpl extends BOObjectImpl implements
039: BOAssociationRole {
040: private BOMetaBossDomainImpl mMetaBossDomainImpl = null;
041: private BOAssociation mAssociation = null;
042: private Boolean mIsRoleA = null; // Together with association forms the unique key for the role
043: private STAssociationRole mDetails = null;
044:
045: /* Instance creator */
046: public static BOAssociationRole createInstanceForExisting(
047: BOMetaBossDomainImpl pMetaBossDomainImpl,
048: BOAssociation pAssociation, boolean pIsRoleA)
049: throws BOException {
050: // Validate essential input arguments
051: if (pAssociation == null)
052: throw new BOIllegalArgumentException(
053: "Empty association reference");
054: String lCacheKey = pAssociation.getRef()
055: + (pIsRoleA ? ".A" : ".B");
056: // See if we have this object in cache
057: BOAssociationRoleImpl lImpl = (BOAssociationRoleImpl) pMetaBossDomainImpl
058: .retrieveExistingBOInstance(BOAssociationRole.class,
059: lCacheKey);
060: if (lImpl != null)
061: return lImpl;
062: lImpl = new BOAssociationRoleImpl();
063: lImpl.mMetaBossDomainImpl = pMetaBossDomainImpl;
064: lImpl.mAssociation = pAssociation;
065: lImpl.mIsRoleA = new Boolean(pIsRoleA);
066: lImpl.setupForExisting();
067: pMetaBossDomainImpl.saveNewBOInstance(BOAssociationRole.class,
068: lCacheKey, lImpl);
069: return lImpl;
070: }
071:
072: /* Instance creator */
073: public static BOAssociationRole createInstanceForExisting(
074: BOMetaBossDomainImpl pMetaBossDomainImpl, String pRoleRef)
075: throws BOException {
076: // Validate essential input arguments
077: if (pRoleRef == null || pRoleRef.length() == 0)
078: throw new BOIllegalArgumentException(
079: "Empty AssociationRoleRef");
080: // See if we have this object in cache
081: // Key is AssociationRoleRef.A or AssociationRoleRef.B
082: int lLastDot = pRoleRef.lastIndexOf('.');
083: if (lLastDot <= 0)
084: throw new BOIllegalArgumentException(
085: "Invalid format of AssociationRoleRef: " + pRoleRef);
086: String lAssociationRef = pRoleRef.substring(0, lLastDot);
087: // Load the role details from the database and create the BO if necessary
088: STAssociationRole lDetails = null;
089: try {
090: Context ctx = new InitialContext();
091: PSAssociationRole lPs = (PSAssociationRole) ctx
092: .lookup(PSAssociationRole.COMPONENT_URL);
093: if ((lDetails = lPs.getAssociationRole(pRoleRef)) == null)
094: throw new BOException(
095: "AssociationRole not found. AssociationRoleRef: "
096: + pRoleRef);
097: } catch (NamingException e) {
098: throw new BONamingAndDirectoryServiceInvocationException(
099: "", e);
100: } catch (PSException e) {
101: throw new BOPersistenceServiceInvocationException("", e);
102: }
103: String lCacheKey = lAssociationRef
104: + (lDetails.IsRoleA ? ".A" : ".B");
105: BOAssociationRoleImpl lImpl = (BOAssociationRoleImpl) pMetaBossDomainImpl
106: .retrieveExistingBOInstance(BOAssociationRole.class,
107: lCacheKey);
108: if (lImpl != null)
109: return lImpl;
110: lImpl = new BOAssociationRoleImpl();
111: lImpl.mMetaBossDomainImpl = pMetaBossDomainImpl;
112: lImpl.mIsRoleA = new Boolean(lDetails.IsRoleA);
113: lImpl.mDetails = lDetails;
114: lImpl.mAssociation = BOAssociationImpl
115: .createInstanceForExisting(pMetaBossDomainImpl,
116: lAssociationRef);
117: lImpl.setupForExisting();
118: pMetaBossDomainImpl.saveNewBOInstance(BOAssociationRole.class,
119: lCacheKey, lImpl);
120: return lImpl;
121: }
122:
123: /* Instance creator */
124: public static BOAssociationRole createInstanceForNew(
125: BOTransaction pTransaction,
126: BOMetaBossDomainImpl pMetaBossDomainImpl,
127: BOAssociation pAssociation, boolean pIsRoleA)
128: throws BOException {
129: String lCacheKey = pAssociation.getRef()
130: + (pIsRoleA ? ".A" : ".B");
131: // See if we have this object in cache already
132: BOAssociationRoleImpl lImpl = (BOAssociationRoleImpl) pMetaBossDomainImpl
133: .retrieveExistingBOInstance(BOAssociationRole.class,
134: lCacheKey);
135: if (lImpl != null)
136: throw new BOIllegalArgumentException("AssociationRole"
137: + (pIsRoleA ? "A" : "B")
138: + " already exists. AssociationRef: "
139: + pAssociation.getRef());
140: lImpl = new BOAssociationRoleImpl();
141: lImpl.mMetaBossDomainImpl = pMetaBossDomainImpl;
142: lImpl.mAssociation = pAssociation;
143: lImpl.mIsRoleA = new Boolean(pIsRoleA);
144: lImpl.setupForNew(pTransaction);
145: pMetaBossDomainImpl.saveNewBOInstance(BOAssociationRole.class,
146: lCacheKey, lImpl);
147: return lImpl;
148: }
149:
150: /* Retrieves association this role belongs to. Together with name
151: * forms primary key for this object */
152: public BOAssociation getAssociation() throws BOException {
153: return mAssociation;
154: }
155:
156: /* Retrieves role's ref. Ref is globally unique */
157: public String getRef() throws BOException {
158: return getAssociation().getRef() + "." + getName();
159: }
160:
161: /* Retrieves role's position. */
162: public boolean isRoleA() throws BOException {
163: return mIsRoleA.booleanValue();
164: }
165:
166: /* Retrieves element's name. Name is only unique within parent entity */
167: public String getName() throws BOException {
168: loadDetailsIfNecessary();
169: return mDetails.Name;
170: }
171:
172: /* Sets singular role name. */
173: public void setName(String pName) throws BOException {
174: if (!isBeingEdited())
175: throw new BOInvalidOperationForReadOnlyObjectException();
176: mDetails.Name = pName;
177: }
178:
179: /* Retrieves plural role name */
180: public String getPluralName() throws BOException {
181: loadDetailsIfNecessary();
182: return mDetails.PluralName;
183: }
184:
185: /* Sets plural role name. */
186: public void setPluralName(String pPluralName) throws BOException {
187: if (!isBeingEdited())
188: throw new BOInvalidOperationForReadOnlyObjectException();
189: mDetails.PluralName = pPluralName;
190: }
191:
192: /* Retrieves entity for which this role is described */
193: public BOEntity getEntity() throws BOException {
194: loadDetailsIfNecessary();
195: return BOEntityImpl.createInstanceForExisting(
196: mMetaBossDomainImpl, mDetails.EntityRef);
197: }
198:
199: /* Sets entity for which this role is described. This method
200: * will remove the role from the entity it was previously associated with and
201: * add this role to the entity passed to it. */
202: public void setEntity(BOEntity pEntity) throws BOException {
203: if (!isBeingEdited())
204: throw new BOInvalidOperationForReadOnlyObjectException();
205: if (pEntity == null)
206: throw new BOIllegalArgumentException(
207: "Entity reference in AssociationRole is mandatory and can not be set to null");
208: // We need to remove our sibling role from the old entity
209: BOAssociationRole lSiblingAssociationRole = isRoleA() ? mAssociation
210: .getRoleB()
211: : mAssociation.getRoleA();
212: BOEntityAssociationRoleListImpl lOldEntityAssociationRoleList = (BOEntityAssociationRoleListImpl) BOEntityImpl
213: .createInstanceForExisting(mMetaBossDomainImpl,
214: mDetails.EntityRef).getAssociationRoles();
215: lOldEntityAssociationRoleList.beginEdit(fetchTransaction());
216: lOldEntityAssociationRoleList
217: .removeAssociationRole(lSiblingAssociationRole);
218: // Now we need to add sibling association role to the new entity
219: mDetails.EntityRef = pEntity.getRef();
220: BOEntityAssociationRoleListImpl lNewEntityAssociationRoleList = (BOEntityAssociationRoleListImpl) pEntity
221: .getAssociationRoles();
222: lNewEntityAssociationRoleList.beginEdit(fetchTransaction());
223: lNewEntityAssociationRoleList
224: .addAssociationRole(lSiblingAssociationRole);
225: }
226:
227: /* Returns element's description */
228: public String getDescription() throws BOException {
229: loadDetailsIfNecessary();
230: return mDetails.Description;
231: }
232:
233: /* Sets description */
234: public void setDescription(String pDescription) throws BOException {
235: if (!isBeingEdited())
236: throw new BOInvalidOperationForReadOnlyObjectException();
237: mDetails.Description = pDescription;
238: }
239:
240: /** Retrieves cardinality for this role */
241: public AssociationRoleCardinality getCardinality()
242: throws BOException {
243: loadDetailsIfNecessary();
244: return mDetails.Cardinality;
245: }
246:
247: /** Sets cardinality */
248: public void setCardinality(AssociationRoleCardinality pCardinality)
249: throws BOException {
250: if (!isBeingEdited())
251: throw new BOInvalidOperationForReadOnlyObjectException();
252: mDetails.Cardinality = pCardinality;
253: }
254:
255: /* Returns stereotype for this association role */
256: public AssociationRoleStereotype getStereotype() throws BOException {
257: loadDetailsIfNecessary();
258: return mDetails.Stereotype;
259: }
260:
261: /** Sets stereotype */
262: public void setStereotype(AssociationRoleStereotype pStereotype)
263: throws BOException {
264: if (!isBeingEdited())
265: throw new BOInvalidOperationForReadOnlyObjectException();
266: mDetails.Stereotype = pStereotype;
267: }
268:
269: protected void onBeginEdit() throws BOException {
270: // Fully load the object
271: loadDetailsIfNecessary();
272: }
273:
274: protected void onDelete() throws BOException {
275: throw new BOInvalidOperationForObjectException(
276: "BOAssociationRole can not be deleted or created separately from BOAssociation.");
277: }
278:
279: private void loadDetailsIfNecessary() throws BOException {
280: if (mDetails == null) {
281: try {
282: // Get the instance of the enterprise ps home via jndi
283: Context lCtx = new InitialContext();
284: PSAssociationRole lPs = (PSAssociationRole) lCtx
285: .lookup(PSAssociationRole.COMPONENT_URL);
286: if ((mDetails = lPs.getAssociationRole(mAssociation
287: .getRef(), mIsRoleA.booleanValue())) == null)
288: throw new BOException("AssociationRole"
289: + (mIsRoleA.booleanValue() ? "A" : "B")
290: + " not found. AssociationRef: "
291: + mAssociation.getRef());
292: } catch (NamingException e) {
293: throw new BONamingAndDirectoryServiceInvocationException(
294: "", e);
295: } catch (PSException e) {
296: throw new BOPersistenceServiceInvocationException("", e);
297: }
298: }
299: }
300:
301: /* Encapsulates commit action by this object */
302: protected void onCommitCreation() throws BOException {
303: try {
304: // Get the instance of the enterprise ps home via jndi
305: Context lCtx = new InitialContext();
306: PSAssociationRole lPs = (PSAssociationRole) lCtx
307: .lookup(PSAssociationRole.COMPONENT_URL);
308: lPs.insertAssociationRole(mAssociation.getRef(), mDetails);
309: } catch (NamingException e) {
310: throw new BONamingAndDirectoryServiceInvocationException(
311: "", e);
312: } catch (PSException e) {
313: throw new BOPersistenceServiceInvocationException("", e);
314: }
315: }
316:
317: /* Encapsulates commit action by this object */
318: protected void onCommitUpdate() throws BOException {
319: try {
320: // Get the instance of the enterprise ps home via jndi
321: Context lCtx = new InitialContext();
322: PSAssociationRole lPs = (PSAssociationRole) lCtx
323: .lookup(PSAssociationRole.COMPONENT_URL);
324: lPs.updateAssociationRole(mAssociation.getRef(), mDetails);
325: } catch (NamingException e) {
326: throw new BONamingAndDirectoryServiceInvocationException(
327: "", e);
328: } catch (PSException e) {
329: throw new BOPersistenceServiceInvocationException("", e);
330: }
331: }
332:
333: /** Overriden to provide check reference equality */
334: public boolean equals(Object pOther) {
335: if (pOther == null)
336: return false;
337: if (!(pOther instanceof BOAssociationRoleImpl))
338: return false;
339: return ((BOAssociationRoleImpl) pOther).mAssociation
340: .equals(mAssociation)
341: && ((BOAssociationRoleImpl) pOther).mIsRoleA
342: .equals(mIsRoleA);
343: }
344: }
|