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.BOInvalidOperationForReadOnlyObjectException;
024: import com.metaboss.enterprise.bo.BONamingAndDirectoryServiceInvocationException;
025: import com.metaboss.enterprise.bo.BOPersistenceServiceInvocationException;
026: import com.metaboss.enterprise.ps.PSException;
027: import com.metaboss.sdlctools.domains.enterprisemodel.BOAssociation;
028: import com.metaboss.sdlctools.domains.enterprisemodel.BOAssociationRole;
029: import com.metaboss.sdlctools.domains.enterprisemodel.BODomain;
030: import com.metaboss.sdlctools.domains.enterprisemodel.storage.PSAssociation;
031: import com.metaboss.sdlctools.domains.enterprisemodel.storage.STAssociation;
032: import com.oldboss.framework.bo.BOTransaction;
033: import com.oldboss.framework.bo.impl.BOObjectImpl;
034:
035: public class BOAssociationImpl extends BOObjectImpl implements
036: BOAssociation {
037: private BOMetaBossDomainImpl mMetaBossDomainImpl = null;
038: private String mAssociationRef = null;
039: private STAssociation mDetails = null;
040: private BOAssociationRole mRoleA = null;
041: private BOAssociationRole mRoleB = null;
042:
043: /* Instance creator */
044: public static BOAssociation createInstanceForExisting(
045: BOMetaBossDomainImpl pMetaBossDomainImpl,
046: STAssociation pDetails) throws BOException {
047: if (pDetails == null)
048: throw new BOIllegalArgumentException(
049: "Empty details structure");
050: // See if we have this object in cache already
051: BOAssociationImpl lImpl = (BOAssociationImpl) pMetaBossDomainImpl
052: .retrieveExistingBOInstance(BOAssociation.class,
053: pDetails.AssociationRef);
054: if (lImpl != null)
055: return lImpl;
056: lImpl = new BOAssociationImpl();
057: lImpl.mMetaBossDomainImpl = pMetaBossDomainImpl;
058: lImpl.mAssociationRef = pDetails.AssociationRef;
059: lImpl.mDetails = pDetails;
060: lImpl.setupForExisting();
061: pMetaBossDomainImpl.saveNewBOInstance(BOAssociation.class,
062: pDetails.AssociationRef, lImpl);
063: return lImpl;
064: }
065:
066: /* Instance creator */
067: public static BOAssociation createInstanceForExisting(
068: BOMetaBossDomainImpl pMetaBossDomainImpl,
069: String pAssociationRef) throws BOException {
070: if (pAssociationRef == null || pAssociationRef.length() == 0)
071: throw new BOIllegalArgumentException("Empty AssociationRef");
072: // See if we have this object in cache already
073: BOAssociationImpl lImpl = (BOAssociationImpl) pMetaBossDomainImpl
074: .retrieveExistingBOInstance(BOAssociation.class,
075: pAssociationRef);
076: if (lImpl != null)
077: return lImpl;
078: lImpl = new BOAssociationImpl();
079: lImpl.mMetaBossDomainImpl = pMetaBossDomainImpl;
080: lImpl.mAssociationRef = pAssociationRef;
081: lImpl.mDetails = null;
082: lImpl.setupForExisting();
083: pMetaBossDomainImpl.saveNewBOInstance(BOAssociation.class,
084: pAssociationRef, lImpl);
085: return lImpl;
086: }
087:
088: /* Instance creator */
089: public static BOAssociation createInstanceForNew(
090: BOTransaction pTransaction,
091: BOMetaBossDomainImpl pMetaBossDomainImpl,
092: String pAssociationRef) throws BOException {
093: // See if we have this object in cache already
094: BOAssociationImpl lImpl = (BOAssociationImpl) pMetaBossDomainImpl
095: .retrieveExistingBOInstance(BOAssociation.class,
096: pAssociationRef);
097: if (lImpl != null)
098: throw new BOIllegalArgumentException(
099: "Association already exists. AssociationRef: "
100: + pAssociationRef);
101: lImpl = new BOAssociationImpl();
102: lImpl.mMetaBossDomainImpl = pMetaBossDomainImpl;
103: lImpl.mAssociationRef = pAssociationRef;
104: lImpl.mDetails = new STAssociation();
105: lImpl.mDetails.AssociationRef = pAssociationRef;
106: lImpl.setupForNew(pTransaction);
107: pMetaBossDomainImpl.saveNewBOInstance(BOAssociation.class,
108: pAssociationRef, lImpl);
109: // Immediately create new roles
110: lImpl.mRoleA = BOAssociationRoleImpl.createInstanceForNew(
111: pTransaction, pMetaBossDomainImpl, lImpl, true);
112: lImpl.mRoleB = BOAssociationRoleImpl.createInstanceForNew(
113: pTransaction, pMetaBossDomainImpl, lImpl, false);
114: return lImpl;
115: }
116:
117: /* Private constructor restricts instance creation from outside */
118: private BOAssociationImpl() throws BOException {
119: }
120:
121: /* Retrieves unique reference */
122: public String getRef() throws BOException {
123: return mAssociationRef;
124: }
125:
126: /* Retrieves entity name */
127: public String getName() throws BOException {
128: String lAssociationName = getRef();
129: if (lAssociationName.lastIndexOf(".") >= 0)
130: lAssociationName = lAssociationName
131: .substring(lAssociationName.lastIndexOf(".") + 1);
132: return lAssociationName;
133: }
134:
135: /* Retrieves the domain which owns this entity.*/
136: public BODomain getDomain() throws BOException {
137: String lAssociationRef = getRef();
138: if (lAssociationRef.lastIndexOf(".") >= 0) {
139: String lDomainRef = lAssociationRef.substring(0,
140: lAssociationRef.lastIndexOf("."));
141: return BODomainImpl.createInstanceForExisting(
142: mMetaBossDomainImpl, lDomainRef);
143: }
144: throw new BOException(
145: "Invalid association reference - missing enterprise and domain components. (AssociationRef:"
146: + getRef() + ")");
147: }
148:
149: /* Retrieves description */
150: public String getDescription() throws BOException {
151: loadDetailsIfNecessary();
152: return mDetails.Description;
153: }
154:
155: /* Sets new description */
156: public void setDescription(String pDescription) throws BOException {
157: if (!isBeingEdited())
158: throw new BOInvalidOperationForReadOnlyObjectException();
159: mDetails.Description = pDescription;
160: }
161:
162: /* Retrieves the A role in this association */
163: public BOAssociationRole getRoleA() throws BOException {
164: if (mRoleA == null) {
165: loadDetailsIfNecessary();
166: mRoleA = BOAssociationRoleImpl.createInstanceForExisting(
167: mMetaBossDomainImpl, this , true);
168: }
169: return mRoleA;
170: }
171:
172: /* Retrieves the B role in this association */
173: public BOAssociationRole getRoleB() throws BOException {
174: if (mRoleB == null) {
175: loadDetailsIfNecessary();
176: mRoleB = BOAssociationRoleImpl.createInstanceForExisting(
177: mMetaBossDomainImpl, this , false);
178: }
179: return mRoleB;
180: }
181:
182: private void loadDetailsIfNecessary() throws BOException {
183: if (mDetails == null) {
184: try {
185: // Get the instance of the enterprise ps home via jndi
186: Context lCtx = new InitialContext();
187: PSAssociation lPs = (PSAssociation) lCtx
188: .lookup(PSAssociation.COMPONENT_URL);
189:
190: mDetails = lPs.getAssociation(mAssociationRef);
191: if (mDetails == null)
192: throw new BOException(
193: "Association not found. AssociationRef:"
194: + mAssociationRef);
195: } catch (NamingException e) {
196: throw new BONamingAndDirectoryServiceInvocationException(
197: "", e);
198: } catch (PSException e) {
199: throw new BOPersistenceServiceInvocationException("", e);
200: }
201: }
202: }
203:
204: protected void onBeginEdit() throws BOException {
205: // Fully load the object
206: loadDetailsIfNecessary();
207: }
208:
209: /* Encapsulates commit action by this object */
210: protected void onCommitCreation() throws BOException {
211: try {
212: // Get the instance of the enterprise ps home via jndi
213: Context lCtx = new InitialContext();
214: PSAssociation lPs = (PSAssociation) lCtx
215: .lookup(PSAssociation.COMPONENT_URL);
216: lPs.insertAssociation(mDetails);
217: } catch (NamingException e) {
218: throw new BONamingAndDirectoryServiceInvocationException(
219: "", e);
220: } catch (PSException e) {
221: throw new BOPersistenceServiceInvocationException("", e);
222: }
223: }
224:
225: /* Encapsulates commit action by this object */
226: protected void onCommitUpdate() throws BOException {
227: try {
228: // Get the instance of the enterprise ps home via jndi
229: Context lCtx = new InitialContext();
230: PSAssociation lPs = (PSAssociation) lCtx
231: .lookup(PSAssociation.COMPONENT_URL);
232: lPs.updateAssociation(mDetails);
233: } catch (NamingException e) {
234: throw new BONamingAndDirectoryServiceInvocationException(
235: "", e);
236: } catch (PSException e) {
237: throw new BOPersistenceServiceInvocationException("", e);
238: }
239: }
240:
241: protected void onCommitDeletion() throws BOException {
242: try {
243: // Get the instance of the enterprise ps home via jndi
244: Context lCtx = new InitialContext();
245: PSAssociation lPs = (PSAssociation) lCtx
246: .lookup(PSAssociation.COMPONENT_URL);
247: lPs.deleteAssociation(mAssociationRef);
248: } catch (NamingException e) {
249: throw new BONamingAndDirectoryServiceInvocationException(
250: "", e);
251: } catch (PSException e) {
252: throw new BOPersistenceServiceInvocationException("", e);
253: }
254: }
255:
256: /* Encapsulates an action after successfull commit */
257: public void onCommitSucceeded() throws BOException {
258: if (isDeleted())
259: mMetaBossDomainImpl.removeExistingBOInstance(
260: BOAssociation.class, mAssociationRef);
261: }
262:
263: /* Overriden to provide check reference equality */
264: public boolean equals(Object pOther) {
265: if (pOther == null)
266: return false;
267: if (!(pOther instanceof BOAssociationImpl))
268: return false;
269: return ((BOAssociationImpl) pOther).mAssociationRef
270: .equals(mAssociationRef);
271: }
272: }
|