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
038: com.almamater.crs.services.reporting.BSMiscellaneousQueries {
039: private static final Log sLogger = LogFactory
040: .getLog(BSMiscellaneousQueriesImpl.class);
041:
042: /** Constructor. Has package visibility. Instances of this class must be created via object factory class located in the same package */
043: BSMiscellaneousQueriesImpl(java.util.Hashtable pEnvironment)
044: throws Exception {
045: }
046:
047: /** Implementation of the method */
048: public BSMiscellaneousQueries.STFindFreeTeachersResult findFreeTeachers(
049: BSMiscellaneousQueries.STFindFreeTeachersInput pInput)
050: throws BSException {
051: // Precreate the result structure ahead of processing
052: BSMiscellaneousQueries.STFindFreeTeachersResult lOperationResult = new BSMiscellaneousQueries.STFindFreeTeachersResult();
053: try {
054: // ===== Section 1
055: // Obtain the courses domain object via JNDI. This object acts as an entry point
056: // to the database navigation and modification operations
057: Context lContext = new InitialContext();
058: BODomain lDomain = (BODomain) lContext
059: .lookup(BODomain.COMPONENT_URL);
060:
061: // ===== Section 2
062: // Build query as follows
063: // 1. Find all busy Teachers by combining all Course Supervisors with all Course Lecturers
064: BOCourseCollection lAllCourses = lDomain.getAllCourses();
065: BOTeacherCollection lAllCourseSupervisors = lAllCourses
066: .getAllSupervisors();
067: BOTeacherCollection lAllCourseLecturers = lAllCourses
068: .getAllLecturers();
069: BOTeacherCollection lAllAllocatedTeachers = lAllCourseSupervisors
070: .union(lAllCourseLecturers);
071:
072: // 2. Find all free Teachers by finding a difference between
073: // collection of all Teachers and the collection of busy Teachers
074: BOTeacherCollection lAllTeachers = lDomain.getAllTeachers();
075: BOTeacherCollection lAllFreeTeachers = lAllTeachers
076: .difference(lAllAllocatedTeachers);
077:
078: // ===== Section 3
079: // Now that query is built, obtain the result as follows
080: // 1. Obtain the resulting array of Teacher domain objects,
081: BOTeacher[] lResultTeacherObjectArray = lAllFreeTeachers
082: .toTeachersArray();
083:
084: // 2. Convert to array of Teacher detail structures using generated helper class
085: STTeacherDetails[] lResultTeacherDetailsArray = TRBOTeacher
086: .convertToDetailsStructureArray(lResultTeacherObjectArray);
087:
088: // 3. Populate the operation result structure
089: lOperationResult
090: .setFreeTeachers(lResultTeacherDetailsArray);
091:
092: // That's all
093: return lOperationResult; // Returning the result structure containing required data
094: } catch (javax.naming.NamingException e) {
095: throw new BSNamingAndDirectoryServiceInvocationException(
096: "Unable to complete findFreeTeachers operation.", e);
097: } catch (BOException e) {
098: throw new BSDomainObjectInvocationException(
099: "Unable to complete findFreeTeachers operation.", e);
100: }
101: }
102:
103: /** Implementation of the method */
104: public BSMiscellaneousQueries.STFindStudentAcquaintancesResult findStudentAcquaintances(
105: BSMiscellaneousQueries.STFindStudentAcquaintancesInput pInput)
106: throws BSException {
107: // Extract the student identification form the input
108: STStudentKey lStudentKey = pInput.getStudent();
109: if (lStudentKey == null)
110: throw new BSIllegalArgumentException(
111: "StudentKey parameter can not be null when invoking findStudentAcquaintances() operation.");
112:
113: // Precreate the result structure ahead of processing
114: BSMiscellaneousQueries.STFindStudentAcquaintancesResult lOperationResult = new BSMiscellaneousQueries.STFindStudentAcquaintancesResult();
115: try {
116: // ===== Section 1
117: // Obtain the courses domain object via JNDI. This object acts as an entry point
118: // to the database navigation and modification operations
119: Context lContext = new InitialContext();
120: BODomain lDomain = (BODomain) lContext
121: .lookup(BODomain.COMPONENT_URL);
122:
123: // ===== Section 2
124: // Obtain an instance of the student using generated helper class
125: // Cater for the possibility that the studen may not be found
126: BOStudent lStudent = null;
127: try {
128: lStudent = TRBOStudent.getBOStudent(lDomain,
129: lStudentKey);
130: } catch (BORecordNotFoundException e) {
131: // Specified Student has not been found. Build and return error message result
132: MOStudentInstanceNotFoundError lErrorMessage = new MOStudentInstanceNotFoundError();
133: lOperationResult.setStudentNotFoundError(lErrorMessage);
134: return lOperationResult; // Returning the result structure containing error message
135: }
136:
137: // ===== Section 3
138: // Build queries as follows
139: // 1. Find all Courses our Student is enrolled in
140: BOCourseCollection lAllStudentCourses = lStudent
141: .getCourses();
142:
143: // 2. Find Teacher Acquaintances as a combination of all Lecturers and Supervisors for the Courses our sudent is enrolled in
144: BOTeacherCollection lAllCourseSupervisors = lAllStudentCourses
145: .getAllSupervisors();
146: BOTeacherCollection lAllCourseLecturers = lAllStudentCourses
147: .getAllLecturers();
148: BOTeacherCollection lAllTeacherAcquaintances = lAllCourseSupervisors
149: .union(lAllCourseLecturers);
150:
151: // 3. Find Student Acquaintances as a combination of all Students enrolled in the Courses our sudent is enrolled in
152: BOStudentCollection lAllCourseAttendees = lAllStudentCourses
153: .getAllAttendees();
154:
155: // ===== Section 3
156: // Now that queries are built, obtain the teachers result as follows
157: // 1. Obtain the resulting array of Teachers
158: BOTeacher[] lResultTeacherObjectArray = lAllTeacherAcquaintances
159: .toTeachersArray();
160:
161: // 2. Convert to array of Teacher detail structures using generated helper class
162: STTeacherDetails[] lResultTeacherDetailsArray = TRBOTeacher
163: .convertToDetailsStructureArray(lResultTeacherObjectArray);
164:
165: // 3. Populate the operation result structure
166: lOperationResult.setTeachers(lResultTeacherDetailsArray);
167:
168: // ===== Section 4
169: // Now that queries are built, obtain the students result as follows
170: // 1. Obtain the resulting array of Student attendees
171: BOStudent[] lAllCourseAttendeesObjectArray = lAllCourseAttendees
172: .toStudentsArray();
173:
174: // 2. Exclude the student himself/herself from the list of all attendees, using HashSet facilities
175: Set lTempSet = new HashSet(Arrays
176: .asList(lAllCourseAttendeesObjectArray));
177: lTempSet.remove(lStudent);
178: BOStudent[] lResultStudentsObjectArray = (BOStudent[]) lTempSet
179: .toArray(new BOStudent[lTempSet.size()]);
180:
181: // 3. Convert to array of Student detail structures using generated helper class
182: STStudentDetails[] lResultStudentDetailsArray = TRBOStudent
183: .convertToDetailsStructureArray(lResultStudentsObjectArray);
184:
185: // 4. Populate the operation result structure
186: lOperationResult.setStudents(lResultStudentDetailsArray);
187:
188: // That's all
189: return lOperationResult; // Returning the result structure containing required data
190: } catch (javax.naming.NamingException e) {
191: throw new BSNamingAndDirectoryServiceInvocationException(
192: "Unable to complete findStudentAcquaintances operation.",
193: e);
194: } catch (BOException e) {
195: throw new BSDomainObjectInvocationException(
196: "Unable to complete findStudentAcquaintances operation.",
197: e);
198: }
199: }
200: }
|