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.BOEnterpriseSystemList;
036: import com.metaboss.sdlctools.domains.enterprisemodel.BOSystem;
037: import com.metaboss.sdlctools.domains.enterprisemodel.storage.PSEnterprise;
038: import com.oldboss.framework.bo.impl.BOObjectImpl;
039:
040: public class BOEnterpriseSystemListImpl extends BOObjectImpl implements
041: BOEnterpriseSystemList {
042: private BOMetaBossDomainImpl mMetaBossDomainImpl = null;
043: private BOEnterprise mEnterprise = null;
044: private BOSystem[] mDetailsBackup = null;
045: private List mDetails = null;
046:
047: /* Instance creator */
048: public static BOEnterpriseSystemList createInstanceForExisting(
049: BOMetaBossDomainImpl pMetaBossDomainImpl,
050: BOEnterprise pEnterprise) throws BOException {
051: // List is cached individually by it's owner
052: BOEnterpriseSystemListImpl lImpl = new BOEnterpriseSystemListImpl();
053: lImpl.mMetaBossDomainImpl = pMetaBossDomainImpl;
054: lImpl.mEnterprise = pEnterprise;
055: lImpl.setupForExisting();
056: return lImpl;
057: }
058:
059: /* Private constructor restricts instance creation from outside */
060: private BOEnterpriseSystemListImpl() throws BOException {
061: }
062:
063: /* Retrieves all systems defined in this enterprise */
064: public BOSystem[] getAllSystems() throws BOException {
065: loadDetailsIfNecessary();
066: return (BOSystem[]) mDetails.toArray(new BOSystem[mDetails
067: .size()]);
068: }
069:
070: /* Retrieves the system corresponding to given reference from this enteprise */
071: public BOSystem getSystem(String pSystemRef) throws BOException {
072: if (pSystemRef == null
073: || pSystemRef.startsWith(mEnterprise.getRef()) == false)
074: return null; // No way this enteprise has given datatype
075: loadDetailsIfNecessary();
076: Iterator lIter = mDetails.iterator();
077: while (lIter.hasNext()) {
078: BOSystem lSystem = (BOSystem) lIter.next();
079: if (lSystem.getRef().equals(pSystemRef))
080: return lSystem;
081: }
082: return null;
083: }
084:
085: /* Creates new system in this enterprise. */
086: public BOSystem createSystem(String pSystemRef) throws BOException {
087: // Check if this servicemodule even belongs to this enteprise
088: if (!pSystemRef.startsWith(mEnterprise.getRef() + "."))
089: throw new BOIllegalArgumentException("SystemRef "
090: + pSystemRef
091: + " is not valid within enterprise. SystemRef: "
092: + pSystemRef + " EnterpriseRef: "
093: + mEnterprise.getRef());
094: if (!isBeingEdited())
095: throw new BOInvalidOperationForReadOnlyObjectException();
096: // Check if we can create system with the given ref
097: BOSystem[] lSystems = getAllSystems();
098: for (int i = 0; i < lSystems.length; i++) {
099: if (lSystems[i].getRef().equals(pSystemRef))
100: throw new BOIllegalArgumentException(
101: "Unable to create System with ref '"
102: + pSystemRef
103: + ". System with the same ref already exists.");
104: }
105: BOSystem lSystem = BOSystemImpl.createInstanceForNew(
106: fetchTransaction(), mMetaBossDomainImpl, pSystemRef);
107: mDetails.add(lSystem);
108: return lSystem;
109: }
110:
111: private void loadDetailsIfNecessary() throws BOException {
112: if (mDetails == null) {
113: try {
114: // Get the instance of the enterprise ps home via jndi
115: Context ctx = new InitialContext();
116: PSEnterprise lPs = (PSEnterprise) ctx
117: .lookup(PSEnterprise.COMPONENT_URL);
118: String[] lSystemRefs = lPs.getAllSystemRefs(mEnterprise
119: .getRef());
120: mDetails = new ArrayList();
121: if (lSystemRefs != null && lSystemRefs.length > 0) {
122: for (int i = 0; i < lSystemRefs.length; i++)
123: mDetails.add(BOSystemImpl
124: .createInstanceForExisting(
125: mMetaBossDomainImpl,
126: lSystemRefs[i]));
127: }
128: } catch (NamingException e) {
129: throw new BONamingAndDirectoryServiceInvocationException(
130: "Error while loading all systems. EnterpriseRef: "
131: + mEnterprise.getRef(), e);
132: } catch (PSException e) {
133: throw new BOPersistenceServiceInvocationException(
134: "Error while loading all systems. EnterpriseRef: "
135: + mEnterprise.getRef(), e);
136: }
137: }
138: }
139:
140: // Internal method<which allows to delete an attribute
141: void onDeleteSystem(BOSystem pSystemToDelete) throws BOException {
142: if (!isBeingEdited())
143: throw new BOInvalidOperationForReadOnlyObjectException();
144: if (!mDetails.remove(pSystemToDelete))
145: throw new BOUnexpectedProgramConditionException(
146: "Attempt to remove system, which is not in the EnterpriseSystemList");
147: }
148:
149: // Overriden to refuse deletion. This is true for all lists
150: protected void onDelete() throws BOException {
151: throw new BOInvalidOperationForObjectException(
152: "EnterpriseSystem list can not be deleted. To empty the list - delete all systems");
153: }
154:
155: // Overriden to load details and backup original list
156: protected void onBeginEdit() throws BOException {
157: // Fully load the object and preserve old values
158: loadDetailsIfNecessary();
159: mDetailsBackup = (BOSystem[]) mDetails
160: .toArray(new BOSystem[mDetails.size()]);
161: }
162:
163: // Encapsulates an action after successfull commit
164: public void onCommitSucceeded() throws BOException {
165: mDetailsBackup = null;
166: }
167:
168: // Overriddden to restore this object's status to pre-begin edit state
169: protected void onRollbackUpdate() throws BOException {
170: mDetails.clear();
171: mDetails.addAll(Arrays.asList(mDetailsBackup));
172: mDetailsBackup = null;
173: }
174: }
|