BOStudentCollectionImpl.java
001 //
002 // This file is generated by MetaBoss Ver 1.4.0001 at Thu Dec 15 22:36:21 EST 2005
003 // MetaBoss is Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
004 // Content of this file is Copyright 2000-2005 © Softaris Pty. Ltd. All Rights Reserved.
005 // This content is licensed under Open Source Gnu Public License (GPL)
006 // Full terms and conditions of GPL can be found found at www.gnu.org
007 //
008 package com.almamater.crs.domains.courses.impl;
009 
010 import com.metaboss.enterprise.datatypes.DataType;
011 import com.metaboss.enterprise.datatypes.DataTypeValidationException;
012 import com.metaboss.enterprise.datatypes.DataTypeInvalidOperationForEmptyInstanceException;
013 import com.metaboss.enterprise.datatypes.DataTypeInvalidOperationForConcealedInstanceException;
014 import com.metaboss.enterprise.ps.PSException;
015 import com.metaboss.enterprise.ps.STCollectionDetails;
016 import com.metaboss.enterprise.ps.STFilteringDetails;
017 import com.metaboss.enterprise.ps.STOrderingDetails;
018 import com.metaboss.enterprise.bo.BOException;
019 import com.metaboss.enterprise.bo.BOIllegalArgumentException;
020 import com.metaboss.enterprise.bo.BOUnexpectedProgramConditionException;
021 import com.metaboss.enterprise.bo.BOPersistenceServiceInvocationException;
022 import com.metaboss.enterprise.bo.BONamingAndDirectoryServiceInvocationException;
023 import com.metaboss.enterprise.bo.impl.BOObjectCollectionImpl;
024 
025 /* Collection implementation. Based on pluggable storage interface, which allows
026  * reuse of this implementation for many selectors. */
027 public class BOStudentCollectionImpl extends BOObjectCollectionImpl implements com.almamater.crs.domains.courses.BOStudentCollection
028 {
029     private static boolean sClassInitialised = false;
030     private static Object  sClassInitialisationSemaphore = new Object();
031     private static com.almamater.crs.domains.courses.storage.PSStudent sPersistenceService = null;
032     private STCollectionDetails[] mCollectionDetails = null;
033 
034     // Pseudo class initialiser. Main feature that we can throw exception from here (unable to do that from native java class initialiser)
035     protected static void initialiseStudentCollectionClassIfNecessary() throws BOException
036     {
037         if (!sClassInitialised)
038         {
039             synchronized(sClassInitialisationSemaphore)
040             {
041                 if (!sClassInitialised)
042                 {
043                     try
044                     {
045                         javax.naming.Context lCtx = new javax.naming.InitialContext();
046                         sPersistenceService = (com.almamater.crs.domains.courses.storage.PSStudent)lCtx.lookup(com.almamater.crs.domains.courses.storage.PSStudent.COMPONENT_URL);
047                     }
048                     catch(javax.naming.NamingException e)
049                     {
050                         throw new BONamingAndDirectoryServiceInvocationException("Unable to initialise BOStudentCollectionImpl",e);
051                     }
052                     sClassInitialised = true;
053                 }
054             }
055         }
056     }
057 
058     // Creates an instance of the filtered collection
059     public static com.almamater.crs.domains.courses.BOStudentCollection createBOStudentCollectionInstance(BODomainImpl pDomain, STCollectionDetails[] pCollectionDetailsthrows BOException
060     {
061         initialiseStudentCollectionClassIfNecessary();
062         if (pCollectionDetails == null || pCollectionDetails.length == 0)
063             throw new BOIllegalArgumentException("The collection filtering details can not be null or empty in call to this method.");
064         if (pCollectionDetails[pCollectionDetails.length-1].EntityRef.equals("Enterprise/systems[CRS]/domains[Courses]/entities[Student]"== false)
065             throw new BOIllegalArgumentException("The entity ref in the last element of collection details array passed to the com.almamater.crs.domains.courses.BOStudentCollection is not expected. Expecting Enterprise/systems[CRS]/domains[Courses]/entities[Student], have " + pCollectionDetails[pCollectionDetails.length-1].EntityRef);
066         BOStudentCollectionImpl lImpl = new BOStudentCollectionImpl(pDomain);
067         lImpl.mCollectionDetails = pCollectionDetails;
068         return lImpl;
069     }
070     // Creates an instance of the unfiltered entity collection
071     public static com.almamater.crs.domains.courses.BOStudentCollection createBOStudentCollectionInstance(BODomainImpl pDomainthrows BOException
072     {
073         initialiseStudentCollectionClassIfNecessary();
074         STCollectionDetails lCollectionDetails = new STCollectionDetails();
075         lCollectionDetails.EntityRef =  "Enterprise/systems[CRS]/domains[Courses]/entities[Student]";
076         lCollectionDetails.AllEntitiesInDomain = true;
077         BOStudentCollectionImpl lImpl = new BOStudentCollectionImpl(pDomain);
078         lImpl.mCollectionDetails = new STCollectionDetails[] { lCollectionDetails };
079         return lImpl;
080     }
081 
082 
083    /* Private constructor. Restricts direct creation of the object */
084    private BOStudentCollectionImplBODomainImpl pDomainthrows BOException
085    {
086        super(pDomain, "Enterprise/systems[CRS]/domains[Courses]/entities[Student]");
087    }
088 
089    /* Getter for domain object */
090    public com.almamater.crs.domains.courses.BODomain getDomain() throws BOException
091    {
092        return (com.almamater.crs.domains.courses.BODomain)getObjectDomain();
093    }
094 
095    /* Constructs the collection consisting of the same elements, but ordered in the requested manner. */
096    public com.almamater.crs.domains.courses.BOStudentCollection sortStudents(com.almamater.crs.types.system.courses.StudentOrderingInstruction pOrderingInstructionthrows BOException
097    {
098         if (pOrderingInstruction.isConcealed())
099            throw new BOIllegalArgumentException("Concealed ordering instruction is not valid for the sort operation.");
100         STCollectionDetails[] lNewCollectionDetails = getTypecastedCollection(this,true);
101         STCollectionDetails lNewCurrentCollectionDetails = lNewCollectionDetails[lNewCollectionDetails.length-1];
102         if (pOrderingInstruction == null || pOrderingInstruction.isEmpty())
103         {
104             lNewCurrentCollectionDetails.Sorters = null;
105         }
106         else
107         {
108             STOrderingDetails lOrderingDetails = new STOrderingDetails();
109             if (pOrderingInstruction.isByFamilyName())
110                 lOrderingDetails.ByAttributeRef = "Enterprise/systems[CRS]/domains[Courses]/entities[Student]/attributes[FamilyName]";
111             else
112             if (pOrderingInstruction.isByDateOfBirth())
113                 lOrderingDetails.ByAttributeRef = "Enterprise/systems[CRS]/domains[Courses]/entities[Student]/attributes[DateOfBirth]";
114             else
115                 throw new BOUnexpectedProgramConditionException("Unsupported attribute in ordering instruction: " + pOrderingInstruction);
116             if (pOrderingInstruction.isAscending())
117                 lOrderingDetails.Direction = STOrderingDetails.DIRECTION_ASCENDING;
118             else
119             if (pOrderingInstruction.isDescending())
120                 lOrderingDetails.Direction = STOrderingDetails.DIRECTION_DESCENDING;
121             else
122                 throw new BOUnexpectedProgramConditionException("Unsupported direction in ordering instruction: " + pOrderingInstruction);
123             if (lNewCurrentCollectionDetails.Sorters != null)
124             {
125                STOrderingDetails[] lExistingOrderingDetails = lNewCurrentCollectionDetails.Sorters;
126                lNewCurrentCollectionDetails.Sorters = new STOrderingDetails[lExistingOrderingDetails.length + 1];
127                System.arraycopy(lExistingOrderingDetails, 0, lNewCurrentCollectionDetails.Sorters, 0, lExistingOrderingDetails.length);
128                lNewCurrentCollectionDetails.Sorters[lNewCurrentCollectionDetails.Sorters.length-1= lOrderingDetails;
129             }
130             else
131                lNewCurrentCollectionDetails.Sorters = new STOrderingDetails[] { lOrderingDetails };
132         }
133         return BOStudentCollectionImpl.createBOStudentCollectionInstance((BODomainImpl)getObjectDomain(), lNewCollectionDetails);
134    }
135 
136    /* Constructs the collection which represents the union of this and passed collection */
137    public com.almamater.crs.domains.courses.BOStudentCollection union(com.almamater.crs.domains.courses.BOStudentCollection pOtherCollectionthrows BOException
138    {
139         if (pOtherCollection == null)
140             throw new BOIllegalArgumentException("pOtherCollection argument can not be null");
141         STCollectionDetails lCombinedCollectionDetails = new STCollectionDetails();
142         lCombinedCollectionDetails.EntityRef = "Enterprise/systems[CRS]/domains[Courses]/entities[Student]";
143         lCombinedCollectionDetails.SetOperationName = STCollectionDetails.SETOPERATION_UNION;
144         lCombinedCollectionDetails.FirstSetDetails = getTypecastedCollection(this,true);
145         lCombinedCollectionDetails.SecondSetDetails = getTypecastedCollection((BOStudentCollectionImpl)pOtherCollection,true);
146         return createBOStudentCollectionInstance((BODomainImpl)getObjectDomain()new STCollectionDetails[] { lCombinedCollectionDetails });
147    }
148 
149    /* Constructs the collection which represents the intersection of this and passed collection */
150    public com.almamater.crs.domains.courses.BOStudentCollection intersection(com.almamater.crs.domains.courses.BOStudentCollection pOtherCollectionthrows BOException
151    {
152         if (pOtherCollection == null)
153             throw new BOIllegalArgumentException("pOtherCollection argument can not be null");
154         STCollectionDetails lCombinedCollectionDetails = new STCollectionDetails();
155         lCombinedCollectionDetails.EntityRef = "Enterprise/systems[CRS]/domains[Courses]/entities[Student]";
156         lCombinedCollectionDetails.SetOperationName = STCollectionDetails.SETOPERATION_INTERSECTION;
157         lCombinedCollectionDetails.FirstSetDetails = getTypecastedCollection(this,true);
158         lCombinedCollectionDetails.SecondSetDetails = getTypecastedCollection((BOStudentCollectionImpl)pOtherCollection,true);
159         return createBOStudentCollectionInstance((BODomainImpl)getObjectDomain()new STCollectionDetails[] { lCombinedCollectionDetails });
160    }
161 
162    /* Constructs the collection which represents the difference between this and passed collection */
163    public com.almamater.crs.domains.courses.BOStudentCollection difference(com.almamater.crs.domains.courses.BOStudentCollection pOtherCollectionthrows BOException
164    {
165         if (pOtherCollection == null)
166             throw new BOIllegalArgumentException("pOtherCollection argument can not be null");
167         STCollectionDetails lCombinedCollectionDetails = new STCollectionDetails();
168         lCombinedCollectionDetails.EntityRef = "Enterprise/systems[CRS]/domains[Courses]/entities[Student]";
169         lCombinedCollectionDetails.SetOperationName = STCollectionDetails.SETOPERATION_DIFFERENCE;
170         lCombinedCollectionDetails.FirstSetDetails = getTypecastedCollection(this,true);
171         lCombinedCollectionDetails.SecondSetDetails = getTypecastedCollection((BOStudentCollectionImpl)pOtherCollection,true);
172         return createBOStudentCollectionInstance((BODomainImpl)getObjectDomain()new STCollectionDetails[] { lCombinedCollectionDetails });
173    }
174 
175     /* Constructs the collection which represents the subset of this collection */
176     public com.almamater.crs.domains.courses.BOStudentCollection selectStudents(com.almamater.types.core.CollectionOffset pStartOffset, com.almamater.types.core.SubsetSize pMaximumSizethrows BOException
177     {
178         if (pStartOffset == null)
179             throw new BOIllegalArgumentException("pStartOffset argument can not be null");
180         if (pStartOffset.isEmpty())
181             throw new BOIllegalArgumentException("pStartOffset argument can not be empty");
182         if (pStartOffset.isConcealed())
183             throw new BOIllegalArgumentException("pStartOffset argument can not be concealed");
184         if (pMaximumSize == null)
185             throw new BOIllegalArgumentException("pMaximumSize argument can not be null");
186         if (pMaximumSize.isEmpty())
187             throw new BOIllegalArgumentException("pMaximumSize argument can not be empty");
188         if (pMaximumSize.isConcealed())
189             throw new BOIllegalArgumentException("pMaximumSize argument can not be concealed");
190         STCollectionDetails lSubsetCollectionDetails = new STCollectionDetails();
191         lSubsetCollectionDetails.EntityRef = "Enterprise/systems[CRS]/domains[Courses]/entities[Student]";
192         lSubsetCollectionDetails.SetOperationName = STCollectionDetails.SETOPERATION_SUBSET;
193         lSubsetCollectionDetails.ResultStartOffset = pStartOffset;
194         lSubsetCollectionDetails.ResultMaximumSize = pMaximumSize;
195         lSubsetCollectionDetails.FirstSetDetails = getTypecastedCollection(this,true);
196         return createBOStudentCollectionInstance((BODomainImpl)getObjectDomain()new STCollectionDetails[] { lSubsetCollectionDetails });
197     }
198 
199     /* Returns total number of elements in this collection. */
200     public com.almamater.types.core.CollectionSize size() throws BOException
201     {
202         try
203         {
204             return sPersistenceService.getRecordsCollectionSize(getTypecastedCollection(this,false));
205         }
206         catch(PSException e)
207         {
208             throw new BOPersistenceServiceInvocationException("Error getting collection size from storage.", e);
209         }
210     }
211 
212     /* Returns true if collection is empty or false if collection has one or more elements. */
213     public com.almamater.types.core.BooleanField isEmpty() throws BOException
214     {
215         try
216         {
217             com.almamater.types.core.CollectionSize lSize =  sPersistenceService.getRecordsCollectionSize(getTypecastedCollection(this,false));
218             return com.almamater.types.core.BooleanField.createFromBoolean(lSize.getAsLong() == 0);
219         }
220         catch(DataTypeInvalidOperationForEmptyInstanceException e)
221         {
222             throw new BOUnexpectedProgramConditionException("Empty instance is specially caterd for, so this error should never occur.", e);
223         }
224         catch(DataTypeInvalidOperationForConcealedInstanceException e)
225         {
226             throw new BOUnexpectedProgramConditionException("Concealed instance is specially caterd for, so this error should never occur.", e);
227         }
228         catch(PSException e)
229         {
230             throw new BOPersistenceServiceInvocationException("Error getting collection size from storage.", e);
231         }
232     }
233 
234     /* Returns true if collection contains given instance or false otherwise. */
235     public boolean containscom.almamater.crs.domains.courses.BOStudent pStudentthrows BOException
236     {
237         try
238         {
239             if (pStudent == null)
240                 throw new BOIllegalArgumentException("pStudent argument can not be null");
241             return sPersistenceService.getByInstanceIdpStudent.getStudentInstanceId(), getTypecastedCollection(this,false)) != null;
242         }
243         catch(PSException e)
244         {
245             throw new BOPersistenceServiceInvocationException(e);
246         }
247     }
248 
249     /* Returns true if collection contains an instance with given Instance Id or false otherwise. */
250     public boolean containscom.almamater.crs.types.system.courses.StudentInstanceId pStudentInstanceIdthrows BOException
251     {
252         try
253         {
254             if (pStudentInstanceId == null)
255                 throw new BOIllegalArgumentException("pStudentInstanceId argument can not be null");
256             if (pStudentInstanceId.isEmpty())
257                 throw new BOIllegalArgumentException("pStudentInstanceId argument can not be empty");
258             if (pStudentInstanceId.isConcealed())
259                 throw new BOIllegalArgumentException("pStudentInstanceId argument can not be concealed");
260             return sPersistenceService.getByInstanceIdpStudentInstanceId, getTypecastedCollection(this,false)) != null;
261         }
262         catch(PSException e)
263         {
264             throw new BOPersistenceServiceInvocationException(e);
265         }
266     }
267 
268     /* Returns true if collection contains an instance with given primary key or false otherwise. */
269     public boolean contains(com.almamater.types.core.StudentNumber pStudentNothrows BOException
270     {
271         try
272         {
273             if (pStudentNo == null)
274                 throw new BOIllegalArgumentException("pStudentNo argument can not be null");
275             if (pStudentNo.isEmpty())
276                 throw new BOIllegalArgumentException("pStudentNo argument can not be empty");
277             if (pStudentNo.isConcealed())
278                 throw new BOIllegalArgumentException("pStudentNo argument can not be concealed");
279             return sPersistenceService.getByPrimaryKeypStudentNo, getTypecastedCollection(this,false)) != null;
280         }
281         catch(PSException e)
282         {
283             throw new BOPersistenceServiceInvocationException(e);
284         }
285     }
286 
287     /* Returns entity object with matching Id only if this collection contains it or null otherwise. */
288     public com.almamater.crs.domains.courses.BOStudent getcom.almamater.crs.types.system.courses.StudentInstanceId pStudentInstanceIdthrows BOException
289     {
290         try
291         {
292             com.almamater.crs.domains.courses.storage.STStudentRecord lRecord = sPersistenceService.getByInstanceId(pStudentInstanceId, getTypecastedCollection(this,false));
293             if (lRecord == null)
294                 return null;
295             return BOStudentImpl.createInstanceForExisting((BODomainImpl)getObjectDomain(), lRecord);
296         }
297         catch(PSException e)
298         {
299             throw new BOPersistenceServiceInvocationException(e);
300         }
301     }
302 
303     /* Returns an instance of entity with matching primary key key only if this collection contains it or null otherwise. */
304     public com.almamater.crs.domains.courses.BOStudent get(com.almamater.types.core.StudentNumber pStudentNothrows BOException
305     {
306         try
307         {
308             com.almamater.crs.domains.courses.storage.STStudentRecord lRecord = sPersistenceService.getByPrimaryKeypStudentNo, getTypecastedCollection(this,false));
309             if (lRecord == null)
310                 return null;
311             return BOStudentImpl.createInstanceForExisting((BODomainImpl)getObjectDomain(), lRecord);
312         }
313         catch(PSException e)
314         {
315             throw new BOPersistenceServiceInvocationException(e);
316         }
317     }
318 
319    /* Returns all elements in this collection as an array. */
320    public com.almamater.crs.domains.courses.BOStudent[] toStudentsArray() throws BOException
321     {
322         try
323         {
324             com.almamater.crs.domains.courses.storage.STStudentRecord[] lRecords = sPersistenceService.getAllRecords(getTypecastedCollection(this,false));
325             if (lRecords == null)
326                 return new com.almamater.crs.domains.courses.BOStudent[0];
327             return BOStudentImpl.createInstanceForExisting((BODomainImpl)getObjectDomain(), lRecords);
328         }
329         catch(PSException e)
330         {
331             throw new BOPersistenceServiceInvocationException(e);
332         }
333     }
334 
335     /* Returns collection, which represents all Courses associated with Students in this collection. */
336     public com.almamater.crs.domains.courses.BOCourseCollection getAllCourses() throws BOException
337     {
338         STCollectionDetails lCollectionDetails = new STCollectionDetails();
339         lCollectionDetails.EntityRef = "Enterprise/systems[CRS]/domains[Courses]/entities[Course]";
340         lCollectionDetails.AssociationRoleRef = "Enterprise/systems[CRS]/domains[Courses]/associations[StudentEnrollsIntoCourse]/roles[Course]";
341         STCollectionDetails[] lNewCollectionDetails = null;
342         STCollectionDetails[] lExistingCollectionDetails = getTypecastedCollection(this,true);
343         if (lExistingCollectionDetails == null || lExistingCollectionDetails.length == 0)
344         {
345             lNewCollectionDetails = new STCollectionDetails[] { lCollectionDetails };
346         }
347         else
348         {
349             lNewCollectionDetails = new STCollectionDetails[lExistingCollectionDetails.length + 1];
350             System.arraycopy(lExistingCollectionDetails, 0, lNewCollectionDetails, 0, lExistingCollectionDetails.length);
351             lNewCollectionDetails[lNewCollectionDetails.length-1= lCollectionDetails;
352         }
353         return BOCourseCollectionImpl.createBOCourseCollectionInstance((BODomainImpl)getObjectDomain(), lNewCollectionDetails);
354     }
355     private static STCollectionDetails[] getTypecastedCollection(BOStudentCollectionImpl pCollectionImpl, boolean pCreateNewCopythrows BOException
356     {
357         if (pCreateNewCopy && pCollectionImpl.mCollectionDetails != null && pCollectionImpl.mCollectionDetails.length > 0)
358         {
359             STCollectionDetails[] lNewCollectionDetails = new STCollectionDetails[pCollectionImpl.mCollectionDetails.length];
360             for (int i = 0; i < pCollectionImpl.mCollectionDetails.length;i++)
361                 lNewCollectionDetails[inew STCollectionDetails(pCollectionImpl.mCollectionDetails[i]);
362             return lNewCollectionDetails;
363         }
364         return pCollectionImpl.mCollectionDetails;
365     }
366 }