BSMiscellaneousQueriesImpl.java
001 package com.almamater.crs.services.reporting.impl;
002 
003 import java.util.Arrays;
004 import java.util.HashSet;
005 import java.util.Set;
006 
007 import javax.naming.Context;
008 import javax.naming.InitialContext;
009 
010 import org.apache.commons.logging.Log;
011 import org.apache.commons.logging.LogFactory;
012 
013 import com.almamater.crs.domains.courses.BOCourseCollection;
014 import com.almamater.crs.domains.courses.BODomain;
015 import com.almamater.crs.domains.courses.BOStudent;
016 import com.almamater.crs.domains.courses.BOStudentCollection;
017 import com.almamater.crs.domains.courses.BOTeacher;
018 import com.almamater.crs.domains.courses.BOTeacherCollection;
019 import com.almamater.crs.services.coursesdomainsupport.MOStudentInstanceNotFoundError;
020 import com.almamater.crs.services.coursesdomainsupport.STStudentDetails;
021 import com.almamater.crs.services.coursesdomainsupport.STStudentKey;
022 import com.almamater.crs.services.coursesdomainsupport.STTeacherDetails;
023 import com.almamater.crs.services.coursesdomainsupport.generatedimpl.TRBOStudent;
024 import com.almamater.crs.services.coursesdomainsupport.generatedimpl.TRBOTeacher;
025 import com.almamater.crs.services.reporting.BSMiscellaneousQueries;
026 import com.metaboss.enterprise.bo.BOException;
027 import com.metaboss.enterprise.bo.BORecordNotFoundException;
028 import com.metaboss.enterprise.bs.BSDomainObjectInvocationException;
029 import com.metaboss.enterprise.bs.BSException;
030 import com.metaboss.enterprise.bs.BSIllegalArgumentException;
031 import com.metaboss.enterprise.bs.BSNamingAndDirectoryServiceInvocationException;
032 
033 /** This class contains handcoded 'real' implementation of the services, which uses 
034  *  domain objects from the com.almamater.crs.domains.courses package. Similarly to other implementations
035  *  this one is required to implement corresponding interface. In our case it is the
036  *  com.almamater.crs.services.reporting.BSMiscellaneousQueries interface. */
037 public class BSMiscellaneousQueriesImpl implements com.almamater.crs.services.reporting.BSMiscellaneousQueries
038 {
039     private static final Log sLogger = LogFactory.getLog(BSMiscellaneousQueriesImpl.class);
040 
041     /** Constructor. Has package visibility. Instances of this class must be created via object factory class located in the same package */
042     BSMiscellaneousQueriesImpl(java.util.Hashtable pEnvironmentthrows Exception
043     {
044     }
045 
046     /** Implementation of the method */
047     public BSMiscellaneousQueries.STFindFreeTeachersResult findFreeTeachersBSMiscellaneousQueries.STFindFreeTeachersInput pInputthrows BSException
048     {
049         // Precreate the result structure ahead of processing
050         BSMiscellaneousQueries.STFindFreeTeachersResult lOperationResult = new BSMiscellaneousQueries.STFindFreeTeachersResult();
051         try
052         {
053             // ===== Section 1
054             // Obtain the courses domain object via JNDI. This object acts as an entry point
055             // to the database navigation and modification operations  
056             Context lContext = new InitialContext();
057             BODomain lDomain = (BODomain)lContext.lookup(BODomain.COMPONENT_URL);
058       
059             // ===== Section 2
060             // Build query as follows
061             // 1. Find all busy Teachers by combining all Course Supervisors with all Course Lecturers
062             BOCourseCollection lAllCourses = lDomain.getAllCourses();
063             BOTeacherCollection lAllCourseSupervisors = lAllCourses.getAllSupervisors();
064             BOTeacherCollection lAllCourseLecturers = lAllCourses.getAllLecturers();
065             BOTeacherCollection lAllAllocatedTeachers = lAllCourseSupervisors.union(lAllCourseLecturers);  
066 
067             // 2. Find all free Teachers by finding a difference between
068             // collection of all Teachers and the collection of busy Teachers
069             BOTeacherCollection lAllTeachers = lDomain.getAllTeachers();
070             BOTeacherCollection lAllFreeTeachers = lAllTeachers.difference(lAllAllocatedTeachers);
071       
072             // ===== Section 3
073             // Now that query is built, obtain the result as follows
074             // 1. Obtain the resulting array of Teacher domain objects,
075             BOTeacher[] lResultTeacherObjectArray = lAllFreeTeachers.toTeachersArray();
076                   
077             // 2. Convert to array of Teacher detail structures using generated helper class
078             STTeacherDetails[] lResultTeacherDetailsArray = TRBOTeacher.convertToDetailsStructureArray(lResultTeacherObjectArray);
079       
080             // 3. Populate the operation result structure
081             lOperationResult.setFreeTeachers(lResultTeacherDetailsArray);
082 
083             // That's all      
084             return lOperationResult; // Returning the result structure containing required data 
085         }
086         catch (javax.naming.NamingException e)
087         {
088             throw new BSNamingAndDirectoryServiceInvocationException("Unable to complete findFreeTeachers operation.", e);
089         }
090         catch (BOException e)
091         {
092             throw new BSDomainObjectInvocationException("Unable to complete findFreeTeachers operation.", e);
093         }
094     }
095 
096     /** Implementation of the method */
097     public BSMiscellaneousQueries.STFindStudentAcquaintancesResult findStudentAcquaintancesBSMiscellaneousQueries.STFindStudentAcquaintancesInput pInputthrows BSException
098     {
099         // Extract the student identification form the input
100         STStudentKey lStudentKey = pInput.getStudent();
101         if (lStudentKey == null)
102             throw new BSIllegalArgumentException("StudentKey parameter can not be null when invoking findStudentAcquaintances() operation.");
103 
104         // Precreate the result structure ahead of processing
105         BSMiscellaneousQueries.STFindStudentAcquaintancesResult lOperationResult = new BSMiscellaneousQueries.STFindStudentAcquaintancesResult();
106         try
107         {
108             // ===== Section 1
109             // Obtain the courses domain object via JNDI. This object acts as an entry point
110             // to the database navigation and modification operations  
111             Context lContext = new InitialContext();
112             BODomain lDomain = (BODomain)lContext.lookup(BODomain.COMPONENT_URL);
113       
114             // ===== Section 2
115             // Obtain an instance of the student using generated helper class
116             // Cater for the possibility that the studen may not be found
117             BOStudent lStudent = null;
118             try
119             {
120                 lStudent = TRBOStudent.getBOStudent(lDomain,lStudentKey);
121             }
122             catch(BORecordNotFoundException e)
123             {
124                 // Specified Student has not been found. Build and return error message result
125                 MOStudentInstanceNotFoundError lErrorMessage = new MOStudentInstanceNotFoundError();
126                 lOperationResult.setStudentNotFoundError(lErrorMessage);
127                 return lOperationResult; // Returning the result structure containing error message 
128             }
129       
130             // ===== Section 3
131             // Build queries as follows
132             // 1. Find all Courses our Student is enrolled in
133             BOCourseCollection lAllStudentCourses = lStudent.getCourses();
134       
135             // 2. Find Teacher Acquaintances as a combination of all Lecturers and Supervisors for the Courses our sudent is enrolled in
136             BOTeacherCollection lAllCourseSupervisors = lAllStudentCourses.getAllSupervisors();
137             BOTeacherCollection lAllCourseLecturers = lAllStudentCourses.getAllLecturers();
138             BOTeacherCollection lAllTeacherAcquaintances = lAllCourseSupervisors.union(lAllCourseLecturers);  
139       
140             // 3. Find Student Acquaintances as a combination of all Students enrolled in the Courses our sudent is enrolled in
141             BOStudentCollection lAllCourseAttendees = lAllStudentCourses.getAllAttendees();
142       
143             // ===== Section 3
144             // Now that queries are built, obtain the teachers result as follows
145             // 1. Obtain the resulting array of Teachers
146             BOTeacher[] lResultTeacherObjectArray = lAllTeacherAcquaintances.toTeachersArray();
147       
148             // 2. Convert to array of Teacher detail structures using generated helper class
149             STTeacherDetails[] lResultTeacherDetailsArray = TRBOTeacher.convertToDetailsStructureArray(lResultTeacherObjectArray);
150       
151             // 3. Populate the operation result structure
152             lOperationResult.setTeachers(lResultTeacherDetailsArray);
153       
154             // ===== Section 4
155             // Now that queries are built, obtain the students result as follows
156             // 1. Obtain the resulting array of Student attendees
157             BOStudent[] lAllCourseAttendeesObjectArray = lAllCourseAttendees.toStudentsArray();
158       
159             // 2. Exclude the student himself/herself from the list of all attendees, using HashSet facilities
160             Set lTempSet = new HashSet(Arrays.asList(lAllCourseAttendeesObjectArray));
161             lTempSet.remove(lStudent);
162             BOStudent[] lResultStudentsObjectArray = (BOStudent[])lTempSet.toArray(new BOStudent[lTempSet.size()])
163                   
164             // 3. Convert to array of Student detail structures using generated helper class
165             STStudentDetails[] lResultStudentDetailsArray = TRBOStudent.convertToDetailsStructureArray(lResultStudentsObjectArray);
166       
167             // 4. Populate the operation result structure
168             lOperationResult.setStudents(lResultStudentDetailsArray);
169       
170             // That's all      
171             return lOperationResult; // Returning the result structure containing required data 
172         }
173         catch (javax.naming.NamingException e)
174         {
175             throw new BSNamingAndDirectoryServiceInvocationException("Unable to complete findStudentAcquaintances operation.", e);
176         }
177         catch (BOException e)
178         {
179             throw new BSDomainObjectInvocationException("Unable to complete findStudentAcquaintances operation.", e);
180         }
181     }
182 }