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 java.util.ArrayList;
018: import java.util.Arrays;
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import javax.naming.Context;
023: import javax.naming.InitialContext;
024: import javax.naming.NamingException;
025:
026: import com.metaboss.enterprise.bo.BOException;
027: import com.metaboss.enterprise.bo.BOIllegalArgumentException;
028: import com.metaboss.enterprise.bo.BOInvalidOperationForObjectException;
029: import com.metaboss.enterprise.bo.BOInvalidOperationForReadOnlyObjectException;
030: import com.metaboss.enterprise.bo.BONamingAndDirectoryServiceInvocationException;
031: import com.metaboss.enterprise.bo.BOPersistenceServiceInvocationException;
032: import com.metaboss.enterprise.bo.BOUnexpectedProgramConditionException;
033: import com.metaboss.enterprise.ps.PSException;
034: import com.metaboss.sdlctools.domains.enterprisemodel.BOEnterprise;
035: import com.metaboss.sdlctools.domains.enterprisemodel.BOEnterpriseList;
036: import com.metaboss.sdlctools.domains.enterprisemodel.storage.PSEnterprise;
037: import com.oldboss.framework.bo.impl.BOObjectImpl;
038:
039: public class BOEnterpriseListImpl extends BOObjectImpl implements
040: BOEnterpriseList {
041: private BOMetaBossDomainImpl mMetaBossDomainImpl = null;
042: private BOEnterprise[] mDetailsBackup = null;
043: private List mDetails = null;
044:
045: /* Instance creator */
046: public static BOEnterpriseList createInstanceForExisting(
047: BOMetaBossDomainImpl pMetaBossDomainImpl)
048: throws BOException {
049: BOEnterpriseListImpl lImpl = new BOEnterpriseListImpl();
050: lImpl.mMetaBossDomainImpl = pMetaBossDomainImpl;
051: lImpl.setupForExisting();
052: return lImpl;
053: }
054:
055: /* Private constructor restricts instance creation from outside */
056: private BOEnterpriseListImpl() throws BOException {
057: }
058:
059: /* Retrieves enteprise definition or null if none found with supplied reference */
060: public BOEnterprise getEnterprise(String pEnterpriseRef)
061: throws BOException {
062: if (pEnterpriseRef == null)
063: return null; // No way this enteprise can be found
064: loadDetailsIfNecessary();
065: Iterator lIter = mDetails.iterator();
066: while (lIter.hasNext()) {
067: BOEnterprise lEnterprise = (BOEnterprise) lIter.next();
068: if (lEnterprise.getRef().equals(pEnterpriseRef))
069: return lEnterprise;
070: }
071: return null;
072: }
073:
074: /* Retrieves all enterprises defined within this list */
075: public BOEnterprise[] getAllEnterprises() throws BOException {
076: loadDetailsIfNecessary();
077: return (BOEnterprise[]) mDetails
078: .toArray(new BOEnterprise[mDetails.size()]);
079: }
080:
081: /* Creates a new enterprise with the given reference */
082: public BOEnterprise createEnterprise(String pEnterpriseRef)
083: throws BOException {
084: if (!isBeingEdited())
085: throw new BOInvalidOperationForReadOnlyObjectException();
086: // Check if we can create enterprise with the given ref
087: Iterator lIter = mDetails.iterator();
088: while (lIter.hasNext()) {
089: BOEnterprise lEnterprise = (BOEnterprise) lIter.next();
090: if (lEnterprise.getRef().equals(pEnterpriseRef))
091: throw new BOIllegalArgumentException(
092: "Unable to create Enterprise with ref '"
093: + pEnterpriseRef
094: + ". Enterprise with the same ref already exists.");
095: }
096: BOEnterprise lEnterprise = BOEnterpriseImpl
097: .createInstanceForNew(fetchTransaction(),
098: mMetaBossDomainImpl, pEnterpriseRef);
099: mDetails.add(lEnterprise);
100: return lEnterprise;
101: }
102:
103: private void loadDetailsIfNecessary() throws BOException {
104: if (mDetails == null) {
105: try {
106: // Get the instance of the enterprise ps home via jndi
107: Context lCtx = new InitialContext();
108: PSEnterprise lPs = (PSEnterprise) lCtx
109: .lookup(PSEnterprise.COMPONENT_URL);
110: String[] lEnterpriseRefs = lPs.getAllEnterpriseRefs();
111: mDetails = new ArrayList();
112: if (lEnterpriseRefs != null
113: && lEnterpriseRefs.length > 0) {
114: for (int i = 0; i < lEnterpriseRefs.length; i++)
115: mDetails.add(BOEnterpriseImpl
116: .createInstanceForExisting(
117: mMetaBossDomainImpl,
118: lEnterpriseRefs[i]));
119: }
120: } catch (NamingException e) {
121: throw new BONamingAndDirectoryServiceInvocationException(
122: "Error while loading all enterprises.", e);
123: } catch (PSException e) {
124: throw new BOPersistenceServiceInvocationException(
125: "Error while loading all enterprises.", e);
126: }
127: }
128: }
129:
130: // Internal method<which allows to delete an attribute
131: void onDeleteEnterpise(BOEnterprise pEnterpriseToDelete)
132: throws BOException {
133: if (!isBeingEdited())
134: throw new BOInvalidOperationForReadOnlyObjectException();
135: if (!mDetails.remove(pEnterpriseToDelete))
136: throw new BOUnexpectedProgramConditionException(
137: "Attempt to remove Enterprise, which is not in the EnterpriseList");
138: }
139:
140: // Overriden to refuse deletion. This is true for all lists
141: protected void onDelete() throws BOException {
142: throw new BOInvalidOperationForObjectException(
143: "Enterprise list can not be deleted. To empty the list - delete all enterprises");
144: }
145:
146: // Overriden to load details and backup original list
147: protected void onBeginEdit() throws BOException {
148: // Fully load the object and preserve old values
149: loadDetailsIfNecessary();
150: mDetailsBackup = (BOEnterprise[]) mDetails
151: .toArray(new BOEnterprise[mDetails.size()]);
152: }
153:
154: // Encapsulates an action after successfull commit
155: public void onCommitSucceeded() throws BOException {
156: mDetailsBackup = null;
157: }
158:
159: // Overriddden to restore this object's status to pre-begin edit state
160: protected void onRollbackUpdate() throws BOException {
161: mDetails.clear();
162: mDetails.addAll(Arrays.asList(mDetailsBackup));
163: mDetailsBackup = null;
164: }
165: }
|