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.BOAssociationRole;
035: import com.metaboss.sdlctools.domains.enterprisemodel.BOAttribute;
036: import com.metaboss.sdlctools.domains.enterprisemodel.BOEntity;
037: import com.metaboss.sdlctools.domains.enterprisemodel.BOEntityAttributeList;
038: import com.metaboss.sdlctools.domains.enterprisemodel.storage.PSEntity;
039: import com.metaboss.sdlctools.domains.enterprisemodel.storage.STAttribute;
040: import com.oldboss.framework.bo.BOTransaction;
041: import com.oldboss.framework.bo.impl.BOObjectImpl;
042:
043: public class BOEntityAttributeListImpl extends BOObjectImpl implements
044: BOEntityAttributeList {
045: private BOMetaBossDomainImpl mMetaBossDomainImpl = null;
046: private BOEntity mEntity = null;
047: private BOAttribute[] mDetailsBackup = null;
048: private List mDetails = null;
049:
050: /* Instance creator */
051: public static BOEntityAttributeList createInstanceForExisting(
052: BOMetaBossDomainImpl pMetaBossDomainImpl, BOEntity pEntity)
053: throws BOException {
054: BOEntityAttributeListImpl lImpl = (BOEntityAttributeListImpl) pMetaBossDomainImpl
055: .retrieveExistingBOInstance(
056: BOEntityAttributeList.class, pEntity.getRef());
057: if (lImpl != null)
058: return lImpl;
059: lImpl = new BOEntityAttributeListImpl();
060: lImpl.mMetaBossDomainImpl = pMetaBossDomainImpl;
061: lImpl.mEntity = pEntity;
062: lImpl.setupForExisting();
063: pMetaBossDomainImpl.saveNewBOInstance(
064: BOEntityAttributeList.class, pEntity.getRef(), lImpl);
065: return lImpl;
066: }
067:
068: /* Instance creator */
069: public static BOEntityAttributeList createInstanceForNew(
070: BOTransaction pTransaction,
071: BOMetaBossDomainImpl pMetaBossDomainImpl, BOEntity pEntity)
072: throws BOException {
073: BOEntityAttributeListImpl lImpl = (BOEntityAttributeListImpl) pMetaBossDomainImpl
074: .retrieveExistingBOInstance(
075: BOEntityAttributeList.class, pEntity.getRef());
076: if (lImpl != null)
077: throw new BOIllegalArgumentException(
078: "EntityAttributeList already exists. EntityRef: "
079: + pEntity.getRef());
080: lImpl = new BOEntityAttributeListImpl();
081: lImpl.mMetaBossDomainImpl = pMetaBossDomainImpl;
082: lImpl.mEntity = pEntity;
083: lImpl.mDetailsBackup = new BOAttribute[0];
084: lImpl.mDetails = new ArrayList();
085: lImpl.setupForNew(pTransaction);
086: pMetaBossDomainImpl.saveNewBOInstance(
087: BOEntityAttributeList.class, pEntity.getRef(), lImpl);
088: return lImpl;
089: }
090:
091: /* Private constructor restricts instance creation from outside */
092: private BOEntityAttributeListImpl() throws BOException {
093: }
094:
095: /** Retrieves an attribute object by it's name or null if none exists */
096: public BOAttribute getAttribute(String pName) throws BOException {
097: loadDetailsIfNecessary();
098: Iterator lIter = mDetails.iterator();
099: while (lIter.hasNext()) {
100: BOAttribute lAttribute = (BOAttribute) lIter.next();
101: if (lAttribute.getName().equals(pName))
102: return lAttribute;
103: }
104: return null;
105: }
106:
107: /* Retrieves all attributes in this entity */
108: public BOAttribute[] getAllAttributes() throws BOException {
109: loadDetailsIfNecessary();
110: return (BOAttribute[]) mDetails
111: .toArray(new BOAttribute[mDetails.size()]);
112: }
113:
114: /* Retrieves all attributes in this entity, including any possible subtype attributes */
115: public BOAttribute[] getCombinedAttributes() throws BOException {
116: if (mEntity.getSupertype() == null)
117: return getAllAttributes();
118: List lCombinedAttributes = new ArrayList();
119: lCombinedAttributes.addAll(Arrays.asList(getAllAttributes()));
120: lCombinedAttributes.addAll(Arrays.asList(mEntity.getSupertype()
121: .getAttributes().getCombinedAttributes()));
122: return (BOAttribute[]) lCombinedAttributes
123: .toArray(new BOAttribute[lCombinedAttributes.size()]);
124: }
125:
126: /* Retrieves all attributes, which can be used in ordering */
127: public BOAttribute[] getAttributesToUseInOrdering()
128: throws BOException {
129: loadDetailsIfNecessary();
130: List lOrderableAttributes = new ArrayList();
131: Iterator lIter = mDetails.iterator();
132: while (lIter.hasNext()) {
133: BOAttribute lAttribute = (BOAttribute) lIter.next();
134: if (lAttribute.getCanUseForOrdering())
135: lOrderableAttributes.add(lAttribute);
136: }
137: return (BOAttribute[]) lOrderableAttributes
138: .toArray(new BOAttribute[lOrderableAttributes.size()]);
139: }
140:
141: /* Creates new attribute with given name */
142: public BOAttribute createAttribute(String pName) throws BOException {
143: if (!isBeingEdited())
144: throw new BOInvalidOperationForReadOnlyObjectException();
145: // Check if we can create attribute with the given name
146: BOAttribute[] lCombinedAttributes = getCombinedAttributes();
147: for (int i = 0; i < lCombinedAttributes.length; i++) {
148: if (lCombinedAttributes[i].getName().equals(pName))
149: throw new BOIllegalArgumentException(
150: "Unable to create Attribute with name '"
151: + pName
152: + "' in Entity "
153: + mEntity.getRef()
154: + ". Attribute with the same name already exists in this entity or it's supertype.");
155: }
156: BOAssociationRole[] lCombinedAssociationRoles = mEntity
157: .getAssociationRoles().getCombinedAssociationRoles();
158: for (int i = 0; i < lCombinedAssociationRoles.length; i++) {
159: if (lCombinedAssociationRoles[i].getName().equals(pName))
160: throw new BOIllegalArgumentException(
161: "Unable to create Attribute with name '"
162: + pName
163: + "' in Entity "
164: + mEntity.getRef()
165: + ". AssociationRole with the same name already exists in this entity or it's supertype.");
166: }
167: BOAttribute lAttribute = BOAttributeImpl
168: .createInstanceForNew(fetchTransaction(),
169: mMetaBossDomainImpl, mEntity, pName);
170: mDetails.add(lAttribute);
171: return lAttribute;
172: }
173:
174: private void loadDetailsIfNecessary() throws BOException {
175: if (mDetails == null) {
176: try {
177: // Get the instance of the enterprise ps home via jndi
178: Context ctx = new InitialContext();
179: PSEntity lPs = (PSEntity) ctx
180: .lookup(PSEntity.COMPONENT_URL);
181: STAttribute[] lAttributes = lPs.getAttributes(mEntity
182: .getRef());
183: mDetails = new ArrayList();
184: if (lAttributes != null || lAttributes.length > 0) {
185: for (int i = 0; i < lAttributes.length; i++)
186: mDetails.add(BOAttributeImpl
187: .createInstanceForExisting(
188: mMetaBossDomainImpl, mEntity,
189: lAttributes[i]));
190: }
191: } catch (NamingException e) {
192: throw new BONamingAndDirectoryServiceInvocationException(
193: "", e);
194: } catch (PSException e) {
195: throw new BOPersistenceServiceInvocationException("", e);
196: }
197: }
198: }
199:
200: // Internal method<which allows to delete an attribute
201: void onDeleteAttribute(BOAttribute pAttributeToDelete)
202: throws BOException {
203: if (!isBeingEdited())
204: throw new BOInvalidOperationForReadOnlyObjectException();
205: if (!mDetails.remove(pAttributeToDelete))
206: throw new BOUnexpectedProgramConditionException(
207: "Attempt to remove attribute, which is not in the EntityAttributeList");
208: }
209:
210: // Overriden to refuse deletion. This is true for all lists
211: protected void onDelete() throws BOException {
212: throw new BOInvalidOperationForObjectException(
213: "EntityAttribute list can not be deleted. To empty the list - delete all attributes");
214: }
215:
216: // Overriden to load details and backup original list
217: protected void onBeginEdit() throws BOException {
218: // Fully load the object and preserve old values
219: loadDetailsIfNecessary();
220: mDetailsBackup = (BOAttribute[]) mDetails
221: .toArray(new BOAttribute[mDetails.size()]);
222: }
223:
224: // Overridden to clean up saved copy
225: protected void onCommitCreation() throws BOException {
226: mDetailsBackup = null;
227: }
228:
229: // Overridden to clean up saved copy
230: protected void onCommitUpdate() throws BOException {
231: mDetailsBackup = null;
232: }
233:
234: // Overriddden to cleanup this object from cache
235: protected void onRollbackCreation() throws BOException {
236: mMetaBossDomainImpl.removeExistingBOInstance(
237: BOEntityAttributeList.class, mEntity.getRef());
238: }
239:
240: // Overriddden to restore this object's status to pre-begin edit state
241: protected void onRollbackUpdate() throws BOException {
242: mDetails.clear();
243: mDetails.addAll(Arrays.asList(mDetailsBackup));
244: mDetailsBackup = null;
245: }
246: }
|