1 package org.kuali.student.enrollment.class2.courseoffering.service.impl;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.kuali.rice.krad.inquiry.InquirableImpl;
5 import org.kuali.student.enrollment.class2.courseoffering.dto.ActivityOfferingWrapper;
6 import org.kuali.student.enrollment.class2.courseoffering.dto.SeatPoolWrapper;
7 import org.kuali.student.enrollment.class2.courseoffering.util.ActivityOfferingConstants;
8 import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingManagementUtil;
9 import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingInfo;
10 import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
11 import org.kuali.student.enrollment.courseoffering.dto.FormatOfferingInfo;
12 import org.kuali.student.enrollment.courseoffering.dto.SeatPoolDefinitionInfo;
13 import org.kuali.student.enrollment.coursewaitlist.dto.CourseWaitListInfo;
14 import org.kuali.student.r2.common.dto.ContextInfo;
15 import org.kuali.student.common.util.security.ContextUtils;
16 import org.kuali.student.r2.common.util.constants.CourseOfferingServiceConstants;
17 import org.kuali.student.r2.common.util.constants.CourseWaitListServiceConstants;
18 import org.kuali.student.r2.common.util.date.DateFormatters;
19 import org.kuali.student.r2.core.acal.dto.TermInfo;
20 import org.kuali.student.r2.core.class1.state.dto.StateInfo;
21 import org.kuali.student.r2.core.class1.type.dto.TypeInfo;
22 import org.kuali.student.r2.core.class1.type.dto.TypeTypeRelationInfo;
23 import org.kuali.student.r2.core.constants.TypeServiceConstants;
24 import org.kuali.student.r2.core.population.dto.PopulationInfo;
25 import org.kuali.student.r2.core.scheduling.dto.ScheduleRequestSetInfo;
26
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.Comparator;
30 import java.util.Formatter;
31 import java.util.List;
32 import java.util.Locale;
33 import java.util.Map;
34
35 public class ActivityOfferingWrapperInquirableImpl extends InquirableImpl {
36 private static final long serialVersionUID = 1L;
37
38 @Override
39 public ActivityOfferingWrapper retrieveDataObject(Map<String, String> dataObjectKeys) {
40 try {
41 ContextInfo contextInfo = ContextUtils.createDefaultContextInfo();
42
43 ActivityOfferingInfo aoInfo = CourseOfferingManagementUtil.getCourseOfferingService().getActivityOffering(dataObjectKeys.get(ActivityOfferingConstants.ACTIVITY_OFFERING_WRAPPER_ID), contextInfo);
44 ActivityOfferingWrapper aoWapper = new ActivityOfferingWrapper(aoInfo);
45
46
47 CourseOfferingInfo courseOfferingInfo = CourseOfferingManagementUtil.getCourseOfferingService().getCourseOffering(aoInfo.getCourseOfferingId(), contextInfo);
48
49
50 FormatOfferingInfo formatOfferingInfo = CourseOfferingManagementUtil.getCourseOfferingService().getFormatOffering(aoInfo.getFormatOfferingId(), contextInfo);
51 aoWapper.setFormatOffering(formatOfferingInfo);
52
53
54 aoWapper.setHasWaitlistCO(courseOfferingInfo.getHasWaitlist());
55
56 List <CourseWaitListInfo> courseWaitListInfoList = CourseOfferingManagementUtil.getCourseWaitListService().getCourseWaitListsByActivityOffering(aoInfo.getId(), contextInfo);
57 int firstCourseWaitListInfo = 0;
58 CourseWaitListInfo courseWaitListInfo = new CourseWaitListInfo();
59
60 if ( null != courseWaitListInfoList && courseWaitListInfoList.size() > 0 )
61 {
62 courseWaitListInfo = courseWaitListInfoList.get(firstCourseWaitListInfo);
63 }
64 aoWapper.setCourseWaitListInfo(courseWaitListInfo);
65 aoWapper.updateWaitListType();
66
67 aoWapper.setHasWaitlist(false);
68 if (CourseWaitListServiceConstants.COURSE_WAIT_LIST_ACTIVE_STATE_KEY.equals(courseWaitListInfo.getStateKey())){
69 aoWapper.setHasWaitlist(true);
70 }
71
72
73 TermInfo term = null;
74 TermInfo subTerm=null;
75 aoWapper.setSubTermName("None");
76 TermInfo termTemp = CourseOfferingManagementUtil.getAcademicCalendarService().getTerm(aoInfo.getTermId(), contextInfo);
77 List<TypeTypeRelationInfo> terms = CourseOfferingManagementUtil.getTypeService().getTypeTypeRelationsByRelatedTypeAndType(termTemp.getTypeKey(), TypeServiceConstants.TYPE_TYPE_RELATION_CONTAINS_TYPE_KEY, contextInfo);
78 if (terms == null || terms.isEmpty()) {
79 term = new TermInfo(termTemp);
80 } else {
81 subTerm = new TermInfo(termTemp);
82 term = CourseOfferingManagementUtil.getAcademicCalendarService().getContainingTerms(aoInfo.getTermId(), contextInfo).get(0);
83 TypeInfo subTermType = CourseOfferingManagementUtil.getTypeService().getType(subTerm.getTypeKey(), contextInfo);
84 aoWapper.setSubTermName(subTermType.getName());
85 }
86 aoWapper.setTerm(term);
87 if (term != null) {
88 aoWapper.setTermName(term.getName());
89 }
90 aoWapper.setTermDisplayString(getTermDisplayString(aoInfo.getTermId(), term));
91
92 if (subTerm!=null) {
93 aoWapper.setTermStartEndDate(getTermStartEndDate(aoInfo.getTermId(), subTerm));
94 } else {
95 aoWapper.setTermStartEndDate(getTermStartEndDate(aoInfo.getTermId(), term));
96 }
97
98
99 aoWapper.setCourseOfferingCode(aoInfo.getCourseOfferingCode());
100 aoWapper.setCourseOfferingTitle(aoInfo.getCourseOfferingTitle());
101
102 String sCredits = courseOfferingInfo.getCreditCnt();
103 if (sCredits == null) {
104 sCredits = "0";
105 }
106 aoWapper.setCredits(sCredits);
107
108 aoWapper.setActivityCode(aoInfo.getActivityCode());
109 aoWapper.setAbbreviatedCourseType(CourseOfferingManagementUtil.getTypeService().getType(aoInfo.getTypeKey(), contextInfo).getName().toUpperCase().substring(0, 3));
110
111 boolean readOnlyView = Boolean.parseBoolean(dataObjectKeys.get("readOnlyView"));
112 aoWapper.setReadOnlyView(readOnlyView);
113
114
115 StateInfo state = CourseOfferingManagementUtil.getStateService().getState(aoWapper.getAoInfo().getStateKey(), contextInfo);
116 aoWapper.setStateName(state.getName());
117 TypeInfo typeInfo = CourseOfferingManagementUtil.getTypeService().getType(aoWapper.getAoInfo().getTypeKey(), contextInfo);
118 aoWapper.setTypeName(typeInfo.getName());
119
120
121 List<SeatPoolDefinitionInfo> seatPoolDefinitionInfoList = CourseOfferingManagementUtil.getCourseOfferingService().getSeatPoolDefinitionsForActivityOffering(aoInfo.getId(), contextInfo);
122
123
124 Collections.sort(seatPoolDefinitionInfoList, new Comparator<SeatPoolDefinitionInfo>() {
125 @Override
126 public int compare(SeatPoolDefinitionInfo sp1, SeatPoolDefinitionInfo sp2) {
127 return sp1.getProcessingPriority().compareTo(sp2.getProcessingPriority());
128 }
129 });
130
131 List<SeatPoolWrapper> seatPoolWrapperList = new ArrayList<SeatPoolWrapper>();
132
133 for (SeatPoolDefinitionInfo seatPoolDefinitionInfo : seatPoolDefinitionInfoList) {
134 SeatPoolWrapper spWrapper = new SeatPoolWrapper();
135
136 PopulationInfo pInfo = CourseOfferingManagementUtil.getPopulationService().getPopulation(seatPoolDefinitionInfo.getPopulationId(), contextInfo);
137 spWrapper.setSeatPoolPopulation(pInfo);
138 spWrapper.setSeatPool(seatPoolDefinitionInfo);
139 spWrapper.setId(seatPoolDefinitionInfo.getId());
140 seatPoolWrapperList.add(spWrapper);
141 }
142 aoWapper.setSeatpools(seatPoolWrapperList);
143
144 CourseOfferingManagementUtil.getScheduleHelper().loadSchedules(aoWapper,contextInfo);
145
146 if (aoInfo.getIsColocated()){
147 List<ScheduleRequestSetInfo> scheduleRequestSets = CourseOfferingManagementUtil.getSchedulingService().getScheduleRequestSetsByRefObject(CourseOfferingServiceConstants.REF_OBJECT_URI_ACTIVITY_OFFERING, aoInfo.getId(), contextInfo);
148 int firstRequestSet = 0;
149 if (scheduleRequestSets != null && !scheduleRequestSets.isEmpty()) {
150
151 if (scheduleRequestSets.size() > 1) {
152 throw new RuntimeException(String.format("Activity %s:%s [%s] has multiple ScheduleRequestSets. This shouldn't happen.",
153 aoWapper.getCourseOfferingCode(), aoWapper.getActivityCode(), aoWapper.getId()));
154 } else {
155 aoWapper.setScheduleRequestSetInfo(scheduleRequestSets.get(firstRequestSet));
156 List colocateIds = new ArrayList(aoWapper.getScheduleRequestSetInfo().getRefObjectIds());
157 colocateIds.remove(aoInfo.getId());
158 List<ActivityOfferingInfo> aoInfos = CourseOfferingManagementUtil.getCourseOfferingService().getActivityOfferingsByIds(colocateIds,contextInfo);
159 StringBuilder builder = new StringBuilder();
160 for (ActivityOfferingInfo ao : aoInfos){
161 builder.append(ao.getCourseOfferingCode() + "-" + ao.getActivityCode() + ",");
162 }
163 aoWapper.setColocatedAoInfo(StringUtils.removeEnd(builder.toString(),","));
164 }
165 }
166 }
167 return aoWapper;
168 } catch (Exception e) {
169 throw new RuntimeException(e);
170 }
171 }
172
173 private String getTermDisplayString(String termId, TermInfo term) {
174
175 StringBuilder stringBuilder = new StringBuilder();
176 Formatter formatter = new Formatter(stringBuilder, Locale.US);
177 String displayString = termId;
178 if (term != null) {
179 String startDate = DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.format(term.getStartDate());
180 String endDate = DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.format(term.getEndDate());
181 String termType = term.getName();
182 formatter.format("%s (%s to %s)", termType, startDate, endDate);
183 displayString = stringBuilder.toString();
184 }
185 return displayString;
186 }
187
188 private String getTermStartEndDate(String termId, TermInfo term) {
189
190 StringBuilder stringBuilder = new StringBuilder();
191 Formatter formatter = new Formatter(stringBuilder, Locale.US);
192 String displayString = termId;
193 if (term != null) {
194 String startDate = DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.format(term.getStartDate());
195 String endDate = DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.format(term.getEndDate());
196 formatter.format("%s - %s", startDate, endDate);
197 displayString = stringBuilder.toString();
198 }
199 return displayString;
200 }
201 }