View Javadoc

1   /**
2    * Copyright 2012 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   * Created by David Yin on 6/27/12
16   */
17  package org.kuali.student.enrollment.class2.courseoffering.service.impl;
18  
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21  import org.kuali.rice.krad.inquiry.InquirableImpl;
22  import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingManagementUtil;
23  import org.kuali.student.enrollment.class2.courseoffering.dto.FormatOfferingWrapper;
24  import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingCrossListingInfo;
25  import org.kuali.student.r2.common.util.constants.LuServiceConstants;
26  import org.kuali.student.r2.core.acal.dto.TermInfo;
27  import org.kuali.student.r2.core.acal.service.AcademicCalendarService;
28  import org.kuali.student.enrollment.class2.courseoffering.dto.CourseOfferingEditWrapper;
29  import org.kuali.student.enrollment.class2.courseoffering.dto.OfferingInstructorWrapper;
30  import org.kuali.student.enrollment.class2.courseoffering.dto.OrganizationInfoWrapper;
31  import org.kuali.student.enrollment.class2.courseoffering.dto.ActivityOfferingWrapper;
32  import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingConstants;
33  import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingResourceLoader;
34  import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingViewHelperUtil;
35  import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
36  import org.kuali.student.enrollment.courseoffering.dto.FormatOfferingInfo;
37  import org.kuali.student.enrollment.courseoffering.dto.OfferingInstructorInfo;
38  import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingInfo;
39  import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
40  import org.kuali.student.r2.common.constants.CommonServiceConstants;
41  import org.kuali.student.r2.common.dto.ContextInfo;
42  import org.kuali.student.r2.common.util.ContextUtils;
43  import org.kuali.student.r2.core.class1.state.dto.StateInfo;
44  import org.kuali.student.r2.core.class1.state.service.StateService;
45  import org.kuali.student.r2.core.class1.type.dto.TypeInfo;
46  import org.kuali.student.r2.core.class1.type.service.TypeService;
47  import org.kuali.student.r2.core.constants.AcademicCalendarServiceConstants;
48  import org.kuali.student.r2.common.util.constants.CourseOfferingServiceConstants;
49  import org.kuali.student.r2.common.util.constants.LprServiceConstants;
50  import org.kuali.student.r2.core.constants.StateServiceConstants;
51  import org.kuali.student.r2.core.constants.TypeServiceConstants;
52  import org.kuali.student.r2.core.organization.dto.OrgInfo;
53  import org.kuali.student.r2.core.organization.service.OrganizationService;
54  import org.kuali.student.r2.lum.course.dto.CourseInfo;
55  import org.kuali.student.r2.lum.course.service.CourseService;
56  import org.kuali.student.r2.lum.course.service.assembler.CourseAssemblerConstants;
57  import org.kuali.student.r2.lum.lrc.dto.ResultValuesGroupInfo;
58  import org.kuali.student.r2.lum.lrc.service.LRCService;
59  import org.kuali.student.r2.lum.util.constants.CourseServiceConstants;
60  import org.kuali.student.r2.lum.util.constants.LrcServiceConstants;
61  
62  import javax.xml.namespace.QName;
63  import java.util.ArrayList;
64  import java.util.Arrays;
65  import java.util.HashSet;
66  import java.util.List;
67  import java.util.Map;
68  import java.util.Set;
69  
70  /**
71   * This class provides a Inquirable implementation for Course Offerings in the Course Offering Edit ui
72   *
73   * @author Kuali Student Team
74   */
75  public class CourseOfferingEditInquirableImpl extends InquirableImpl {
76      private CourseOfferingService courseOfferingService;
77      private CourseService courseService;
78      private LRCService lrcService;
79      private OrganizationService organizationService;
80      private TypeService typeService;
81      private StateService stateService;
82      private transient AcademicCalendarService acalService;
83  
84      @Override
85      public Object retrieveDataObject(Map<String, String> parameters) {
86          ContextInfo contextInfo = ContextUtils.createDefaultContextInfo();
87  
88          String coInfoId = parameters.get("coInfo.id");
89          if(coInfoId == null || "".equals(coInfoId)){
90              coInfoId = parameters.get("id");
91          }
92          if(coInfoId == null || "".equals(coInfoId)){
93              coInfoId = parameters.get("currentCourseOfferingWrapper.courseOfferingId");
94          }
95          if(coInfoId == null || "".equals(coInfoId)){
96              coInfoId = parameters.get("courseOfferingId");
97          }
98          //ResultValuesGroup rvGroup = null;
99  
100         try {
101             CourseOfferingInfo coInfo = getCourseOfferingService().getCourseOffering(coInfoId, contextInfo);
102 
103             //Display credit count
104             CourseInfo courseInfo = getCourseService().getCourse(coInfo.getCourseId(), contextInfo);
105 
106             CourseOfferingEditWrapper formObject = new CourseOfferingEditWrapper(coInfo);
107 
108             formObject.setCourse(courseInfo);
109 
110             //Display format offering
111             List<FormatOfferingInfo> formatOfferingInfos = getCourseOfferingService().getFormatOfferingsByCourseOffering(coInfoId, contextInfo);
112             List<FormatOfferingWrapper> foList = new ArrayList<FormatOfferingWrapper>();
113             for (FormatOfferingInfo fo : formatOfferingInfos){
114                 FormatOfferingWrapper wrapper = new FormatOfferingWrapper();
115                 wrapper.setFormatOfferingInfo(fo);
116                 wrapper.setCourseOfferingWrapper(formObject);
117 
118                 //set the reader friendly Final Exam Driver Activity
119                 if (!StringUtils.isEmpty(fo.getFinalExamLevelTypeKey()) && StringUtils.equals(formObject.getFinalExamDriver(), LuServiceConstants.LU_EXAM_DRIVER_AO_KEY)) {
120                     wrapper.setFinalExamUI(getTypeService().getType(fo.getFinalExamLevelTypeKey(), contextInfo).getName());
121                 } else {
122                     wrapper.setFinalExamUI(" ");
123                 }
124 
125                 foList.add(wrapper);
126             }
127             formObject.setFormatOfferingList(foList);
128             TermInfo term = getAcalService().getTerm(coInfo.getTermId(), contextInfo);
129             formObject.setTermName(term.getName());
130 
131             //Display instructors
132             List<OfferingInstructorInfo> offeringInstructorInfos = coInfo.getInstructors();
133             List<OfferingInstructorWrapper> instructorList = new ArrayList<OfferingInstructorWrapper>();
134 
135             for (OfferingInstructorInfo offeringInstructorInfo : offeringInstructorInfos) {
136                 OfferingInstructorWrapper instructor = new OfferingInstructorWrapper();
137                 instructor.setOfferingInstructorInfo(offeringInstructorInfo);
138                 if (offeringInstructorInfo.getTypeKey().equals(LprServiceConstants.INSTRUCTOR_MAIN_TYPE_KEY)) {
139                     //offeringInstructorInfo.setTypeKey("Instructor");
140                     instructor.setTypeName("Instructor");
141                 } else if (offeringInstructorInfo.getTypeKey().equals(LprServiceConstants.INSTRUCTOR_ASSISTANT_TYPE_KEY)) {
142                     //offeringInstructorInfo.setTypeKey("Teaching Assistant");
143                     instructor.setTypeName("Teaching Assistant");
144 //                } else if (offeringInstructorInfo.getTypeKey().equals(LprServiceConstants.INSTRUCTOR_SUPPORT_TYPE_KEY)) {
145                     //TODO: set support here
146                 }
147                 instructorList.add(instructor);
148             }
149             formObject.setInstructors(instructorList);
150 
151             //Display organization name
152             List<OrganizationInfoWrapper> orgList = new ArrayList<OrganizationInfoWrapper>();
153             if(coInfo.getUnitsDeploymentOrgIds() != null){
154                 for(String orgId: coInfo.getUnitsDeploymentOrgIds()){
155                     OrgInfo orgInfo = getOrganizationService().getOrg(orgId, contextInfo);
156                     orgList.add(new OrganizationInfoWrapper(orgInfo));
157                 }
158             }
159             formObject.setOrganizationNames(orgList);
160 
161             List<String> studentRegOptions = new ArrayList<String>();
162             List<String> crsGradingOptions = new ArrayList<String>();
163             if (coInfo.getCourseId() != null && courseInfo != null) {
164                 List<String> gradingOptions = courseInfo.getGradingOptions();
165                 Set<String> regOpts = new HashSet<String>(Arrays.asList(CourseOfferingServiceConstants.ALL_STUDENT_REGISTRATION_OPTION_TYPE_KEYS));
166                 for (String gradingOption : gradingOptions) {
167                     if (regOpts.contains(gradingOption)) {
168                         studentRegOptions.add(gradingOption);
169                     } else {
170                         crsGradingOptions.add(gradingOption);
171                     }
172                 }
173                 //Audit is pulled out into a dynamic attribute on course so map it back
174                 if("true".equals(courseInfo.getAttributeValue(CourseAssemblerConstants.COURSE_RESULT_COMP_ATTR_AUDIT))){
175                     studentRegOptions.add(LrcServiceConstants.RESULT_GROUP_KEY_GRADE_AUDIT);
176                 }
177             }
178             //retrieve StudentRegOptions from coInfo instead of calculating it
179             studentRegOptions = coInfo.getStudentRegistrationGradingOptions();
180 
181             formObject.setStudentRegOptions(studentRegOptions);
182             formObject.setCrsGradingOptions(crsGradingOptions);
183 
184             //TODO - please comment what the following section of code is supposed to do!
185             String selectedStudentRegOpts = "";
186             if (studentRegOptions != null) {
187                 ResultValuesGroupInfo rvg;
188                 StringBuilder sbStudentRegOpts = new StringBuilder();
189                 for(String studentGradingOption : studentRegOptions) {
190                     rvg = getLRCService().getResultValuesGroup(studentGradingOption, contextInfo);
191                     if (null != rvg) {
192                         sbStudentRegOpts.append(rvg.getName());
193                     } else {
194                         sbStudentRegOpts.append(studentGradingOption);
195                     }
196                     sbStudentRegOpts.append("; ");
197                 }
198                 selectedStudentRegOpts = sbStudentRegOpts.toString();
199             }
200             if (selectedStudentRegOpts.isEmpty()) {
201                 selectedStudentRegOpts = CourseOfferingConstants.COURSEOFFERING_TEXT_STD_REG_OPTS_EMPTY;
202             }
203             else {
204                 selectedStudentRegOpts = selectedStudentRegOpts.substring(0, selectedStudentRegOpts.length() - 2);
205             }
206             formObject.setSelectedStudentRegOpts(selectedStudentRegOpts);
207 
208             /**
209              * Sets the cross listed infos
210              */
211             for (CourseOfferingCrossListingInfo crossListingInfo : coInfo.getCrossListings()){
212                 formObject.getAlternateCOCodes().add(crossListingInfo.getCode());
213             }
214 
215             // set use final exam matrix toggle UI
216             if (formObject.isUseFinalExamMatrix()) {
217                 formObject.setUseFinalExamMatrixUI(CourseOfferingConstants.COURSEOFFERING_TEXT_USE_FINAL_EXAM_MATRIX);
218             } else {
219                 formObject.setUseFinalExamMatrixUI(CourseOfferingConstants.COURSEOFFERING_TEXT_NOT_USE_FINAL_EXAM_MATRIX);
220             }
221 
222             //load related AOs
223             loadActivityOfferingsByCourseOffering(coInfo, formObject);
224             return formObject;
225         } catch (Exception e) {
226             throw new RuntimeException(e);
227         }
228     }
229 
230     private void loadActivityOfferingsByCourseOffering(CourseOfferingInfo theCourseOfferingInfo, CourseOfferingEditWrapper formObject) throws Exception {
231         String courseOfferingId = theCourseOfferingInfo.getId();
232         List<ActivityOfferingInfo> activityOfferingInfoList;
233         List<ActivityOfferingWrapper> activityOfferingWrapperList;
234 
235         try {
236             activityOfferingInfoList = getCourseOfferingService().getActivityOfferingsByCourseOffering(courseOfferingId, ContextUtils.createDefaultContextInfo());
237             activityOfferingWrapperList = new ArrayList<ActivityOfferingWrapper>(activityOfferingInfoList.size());
238 
239             for (ActivityOfferingInfo info : activityOfferingInfoList) {
240                 ActivityOfferingWrapper aoWrapper = convertAOInfoToWrapper_Simple(info);
241                 activityOfferingWrapperList.add(aoWrapper);
242             }
243         } catch (Exception e) {
244             throw new RuntimeException(String.format("Could not load AOs for course offering [%s].", courseOfferingId), e);
245         }
246         formObject.setAoWrapperList(activityOfferingWrapperList);
247     }
248 
249     private ActivityOfferingWrapper convertAOInfoToWrapper_Simple(ActivityOfferingInfo aoInfo) throws Exception{
250 
251         ActivityOfferingWrapper aoWrapper = new ActivityOfferingWrapper(aoInfo);
252 
253         ContextInfo contextInfo = ContextUtils.createDefaultContextInfo();
254 
255         StateInfo state = getStateService().getState(aoInfo.getStateKey(), contextInfo);        
256         aoWrapper.setStateName(state.getName());
257 
258         TypeInfo typeInfo = getTypeService().getType(aoInfo.getTypeKey(), contextInfo);
259         aoWrapper.setTypeName(typeInfo.getName());
260 
261         FormatOfferingInfo fo = getCourseOfferingService().getFormatOffering(aoInfo.getFormatOfferingId(), contextInfo);
262         aoWrapper.setFormatOffering(fo);
263 
264         OfferingInstructorInfo displayInstructor = CourseOfferingViewHelperUtil.findDisplayInstructor(aoInfo.getInstructors());
265 
266         if(displayInstructor != null) {
267             aoWrapper.setFirstInstructorDisplayName(displayInstructor.getPersonName());
268         }
269 
270         //for multiple instructor display
271         List<OfferingInstructorInfo> instructorInfos = aoInfo.getInstructors();
272         if (instructorInfos != null) {
273             for (OfferingInstructorInfo offeringInstructorInfo : instructorInfos) {
274                 aoWrapper.setInstructorDisplayNames(offeringInstructorInfo.getPersonName(), true);
275             }
276         }
277 
278         //Check for sub-term or term and populate accordingly if AO belongs to a subterm
279         //If no change of AO.getTermId() > avoid service calls and populate sub-term info as it has been stored in aoWrapperStored
280         TermInfo term;
281         TermInfo subTerm;
282         aoWrapper.setHasSubTerms(false);
283         aoWrapper.setSubTermName("None");
284         aoWrapper.setSubTermId("");
285         //check if the term has any parent term
286         List<TermInfo> terms = getAcalService().getContainingTerms(aoInfo.getTermId(), contextInfo);
287         if (terms == null || terms.isEmpty()) { //AO belong to a parent term or a standard term
288             term = getAcalService().getTerm(aoWrapper.getAoInfo().getTermId(), contextInfo);
289             // checking if we can have sub-terms for giving term
290             List<TermInfo> subTerms = getAcalService().getIncludedTermsInTerm(aoWrapper.getAoInfo().getTermId(), contextInfo);
291             if(!subTerms.isEmpty()) {
292                 aoWrapper.setHasSubTerms(true);
293             }
294         } else {//AO belongs to a sub-term
295             subTerm = getAcalService().getTerm(aoInfo.getTermId(), contextInfo);
296             term = terms.get(0);
297             aoWrapper.setHasSubTerms(true);
298             aoWrapper.setSubTermId(subTerm.getId());
299             TypeInfo subTermType = getTypeService().getType(subTerm.getTypeKey(), contextInfo);
300             aoWrapper.setSubTermName(subTermType.getName());
301             aoWrapper.setTermStartEndDate(CourseOfferingManagementUtil.getTermStartEndDate(subTerm.getId(), subTerm));
302         }
303         aoWrapper.setTerm(term);
304         if (term != null) {
305             aoWrapper.setTermName(term.getName());
306         }
307         aoWrapper.setTermDisplayString(CourseOfferingManagementUtil.getTermDisplayString(aoInfo.getTermId(), term));
308         return aoWrapper;
309     }
310 
311 
312 
313     public CourseOfferingService getCourseOfferingService() {
314         if(courseOfferingService == null)
315             courseOfferingService= CourseOfferingResourceLoader.loadCourseOfferingService();
316         return courseOfferingService;
317     }
318 
319     protected CourseService getCourseService() {
320         if(courseService == null) {
321             courseService = (CourseService) GlobalResourceLoader.getService(new QName(CourseServiceConstants.COURSE_NAMESPACE, "CourseService"));
322         }
323         return this.courseService;
324     }
325 
326     protected LRCService getLRCService() {
327         if(lrcService == null) {
328             lrcService = (LRCService) GlobalResourceLoader.getService(new QName(LrcServiceConstants.NAMESPACE, LrcServiceConstants.SERVICE_NAME_LOCAL_PART));
329         }
330         return this.lrcService;
331     }
332 
333     protected StateService getStateService(){
334         if (stateService == null){
335             stateService = GlobalResourceLoader.getService(new QName(StateServiceConstants.NAMESPACE, StateServiceConstants.SERVICE_NAME_LOCAL_PART));
336         }
337         return stateService;
338     }
339 
340     protected TypeService getTypeService(){
341         if (typeService == null){
342             typeService = GlobalResourceLoader.getService(new QName(TypeServiceConstants.NAMESPACE, TypeServiceConstants.SERVICE_NAME_LOCAL_PART));
343         }
344         return typeService;
345     }
346 
347     private OrganizationService getOrganizationService(){
348         if(organizationService == null) {
349             organizationService = (OrganizationService) GlobalResourceLoader.getService(new QName(CommonServiceConstants.REF_OBJECT_URI_GLOBAL_PREFIX + "organization", "OrganizationService"));
350         }
351         return organizationService;
352     }
353     public AcademicCalendarService getAcalService() {
354         if(acalService == null) {
355             acalService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
356         }
357         return this.acalService;
358     }
359 
360 }