1 package org.kuali.student.enrollment.class2.courseoffering.service.impl;
2
3 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
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.helper.impl.ActivityOfferingScheduleHelperImpl;
8 import org.kuali.student.enrollment.class2.courseoffering.util.ActivityOfferingConstants;
9 import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingResourceLoader;
10 import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingInfo;
11 import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
12 import org.kuali.student.enrollment.courseoffering.dto.FormatOfferingInfo;
13 import org.kuali.student.enrollment.courseoffering.dto.SeatPoolDefinitionInfo;
14 import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
15 import org.kuali.student.enrollment.coursewaitlist.dto.CourseWaitListInfo;
16 import org.kuali.student.enrollment.coursewaitlist.service.CourseWaitListService;
17 import org.kuali.student.r2.common.dto.ContextInfo;
18 import org.kuali.student.r2.common.util.ContextUtils;
19 import org.kuali.student.r2.common.util.constants.CourseWaitListServiceConstants;
20 import org.kuali.student.r2.common.util.date.DateFormatters;
21 import org.kuali.student.r2.core.acal.dto.TermInfo;
22 import org.kuali.student.r2.core.acal.service.AcademicCalendarService;
23 import org.kuali.student.r2.core.class1.state.dto.StateInfo;
24 import org.kuali.student.r2.core.class1.state.service.StateService;
25 import org.kuali.student.r2.core.class1.type.dto.TypeInfo;
26 import org.kuali.student.r2.core.class1.type.dto.TypeTypeRelationInfo;
27 import org.kuali.student.r2.core.class1.type.service.TypeService;
28 import org.kuali.student.r2.core.constants.AcademicCalendarServiceConstants;
29 import org.kuali.student.r2.core.constants.PopulationServiceConstants;
30 import org.kuali.student.r2.core.constants.TypeServiceConstants;
31 import org.kuali.student.r2.core.population.dto.PopulationInfo;
32 import org.kuali.student.r2.core.population.service.PopulationService;
33
34 import javax.xml.namespace.QName;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.Comparator;
38 import java.util.Formatter;
39 import java.util.List;
40 import java.util.Locale;
41 import java.util.Map;
42
43 public class ActivityOfferingWrapperInquirableImpl extends InquirableImpl {
44 private static final long serialVersionUID = 1L;
45 private transient TypeService typeService;
46 private transient StateService stateService;
47 private transient AcademicCalendarService acalService;
48 private transient PopulationService populationService;
49
50 @Override
51 public ActivityOfferingWrapper retrieveDataObject(Map<String, String> dataObjectKeys) {
52 try {
53 ContextInfo contextInfo = ContextUtils.createDefaultContextInfo();
54
55 ActivityOfferingInfo aoInfo = getCourseOfferingService().getActivityOffering(dataObjectKeys.get(ActivityOfferingConstants.ACTIVITY_OFFERING_WRAPPER_ID), contextInfo);
56 ActivityOfferingWrapper aoWapper = new ActivityOfferingWrapper(aoInfo);
57
58
59 CourseOfferingInfo courseOfferingInfo = getCourseOfferingService().getCourseOffering(aoInfo.getCourseOfferingId(), contextInfo);
60
61
62 FormatOfferingInfo formatOfferingInfo = getCourseOfferingService().getFormatOffering(aoInfo.getFormatOfferingId(), contextInfo);
63 aoWapper.setFormatOffering(formatOfferingInfo);
64
65
66 aoWapper.setHasWaitlistCO(courseOfferingInfo.getHasWaitlist());
67
68 List <CourseWaitListInfo> courseWaitListInfoList = getCourseWaitListService().getCourseWaitListsByActivityOffering(aoInfo.getId(), contextInfo);
69 int firstCourseWaitListInfo = 0;
70 CourseWaitListInfo courseWaitListInfo = new CourseWaitListInfo();
71
72 if ( null != courseWaitListInfoList && courseWaitListInfoList.size() > 0 )
73 {
74 courseWaitListInfo = courseWaitListInfoList.get(firstCourseWaitListInfo);
75 }
76 aoWapper.setCourseWaitListInfo(courseWaitListInfo);
77 aoWapper.updateWaitListType();
78
79 aoWapper.setHasWaitlist(false);
80 if (CourseWaitListServiceConstants.COURSE_WAIT_LIST_ACTIVE_STATE_KEY.equals(courseWaitListInfo.getStateKey())){
81 aoWapper.setHasWaitlist(true);
82 }
83
84
85 TermInfo term = null;
86 TermInfo subTerm=null;
87 aoWapper.setSubTermName("None");
88 TermInfo termTemp = getAcalService().getTerm(aoInfo.getTermId(), contextInfo);
89 List<TypeTypeRelationInfo> terms = getTypeService().getTypeTypeRelationsByRelatedTypeAndType(termTemp.getTypeKey(), TypeServiceConstants.TYPE_TYPE_RELATION_CONTAINS_TYPE_KEY, contextInfo);
90 if (terms == null || terms.isEmpty()) {
91 term = new TermInfo(termTemp);
92 } else {
93 subTerm = new TermInfo(termTemp);
94 term = getAcalService().getContainingTerms(aoInfo.getTermId(), contextInfo).get(0);
95 TypeInfo subTermType = getTypeService().getType(subTerm.getTypeKey(), contextInfo);
96 aoWapper.setSubTermName(subTermType.getName());
97 }
98 aoWapper.setTerm(term);
99 if (term != null) {
100 aoWapper.setTermName(term.getName());
101 }
102 aoWapper.setTermDisplayString(getTermDisplayString(aoInfo.getTermId(), term));
103
104 if (subTerm!=null) {
105 aoWapper.setTermStartEndDate(getTermStartEndDate(aoInfo.getTermId(), subTerm));
106 } else {
107 aoWapper.setTermStartEndDate(getTermStartEndDate(aoInfo.getTermId(), term));
108 }
109
110
111 aoWapper.setCourseOfferingCode(aoInfo.getCourseOfferingCode());
112 aoWapper.setCourseOfferingTitle(aoInfo.getCourseOfferingTitle());
113
114 String sCredits = courseOfferingInfo.getCreditCnt();
115 if (sCredits == null) {
116 sCredits = "0";
117 }
118 aoWapper.setCredits(sCredits);
119
120 aoWapper.setActivityCode(aoInfo.getActivityCode());
121 aoWapper.setAbbreviatedCourseType(getTypeService().getType(aoInfo.getTypeKey(), contextInfo).getName().toUpperCase().substring(0, 3));
122
123 boolean readOnlyView = Boolean.parseBoolean(dataObjectKeys.get("readOnlyView"));
124 aoWapper.setReadOnlyView(readOnlyView);
125
126
127 StateInfo state = getStateService().getState(aoWapper.getAoInfo().getStateKey(), contextInfo);
128 aoWapper.setStateName(state.getName());
129 TypeInfo typeInfo = getTypeService().getType(aoWapper.getAoInfo().getTypeKey(), contextInfo);
130 aoWapper.setTypeName(typeInfo.getName());
131
132
133 List<SeatPoolDefinitionInfo> seatPoolDefinitionInfoList = getCourseOfferingService().getSeatPoolDefinitionsForActivityOffering(aoInfo.getId(), contextInfo);
134
135
136 Collections.sort(seatPoolDefinitionInfoList, new Comparator<SeatPoolDefinitionInfo>() {
137 @Override
138 public int compare(SeatPoolDefinitionInfo sp1, SeatPoolDefinitionInfo sp2) {
139 return sp1.getProcessingPriority().compareTo(sp2.getProcessingPriority());
140 }
141 });
142
143 List<SeatPoolWrapper> seatPoolWrapperList = new ArrayList<SeatPoolWrapper>();
144
145 for (SeatPoolDefinitionInfo seatPoolDefinitionInfo : seatPoolDefinitionInfoList) {
146 SeatPoolWrapper spWrapper = new SeatPoolWrapper();
147
148 PopulationInfo pInfo = getPopulationService().getPopulation(seatPoolDefinitionInfo.getPopulationId(), contextInfo);
149 spWrapper.setSeatPoolPopulation(pInfo);
150 spWrapper.setSeatPool(seatPoolDefinitionInfo);
151 spWrapper.setId(seatPoolDefinitionInfo.getId());
152 seatPoolWrapperList.add(spWrapper);
153 }
154 aoWapper.setSeatpools(seatPoolWrapperList);
155
156 getScheduleHelper().loadSchedules(aoWapper,contextInfo);
157
158 return aoWapper;
159 } catch (Exception e) {
160 throw new RuntimeException(e);
161 }
162 }
163
164 private String getTermDisplayString(String termId, TermInfo term) {
165
166 StringBuilder stringBuilder = new StringBuilder();
167 Formatter formatter = new Formatter(stringBuilder, Locale.US);
168 String displayString = termId;
169 if (term != null) {
170 String startDate = DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.format(term.getStartDate());
171 String endDate = DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.format(term.getEndDate());
172 String termType = term.getName();
173 formatter.format("%s (%s to %s)", termType, startDate, endDate);
174 displayString = stringBuilder.toString();
175 }
176 return displayString;
177 }
178
179 protected ActivityOfferingScheduleHelperImpl getScheduleHelper(){
180
181 return new ActivityOfferingScheduleHelperImpl();
182 }
183
184 public CourseOfferingService getCourseOfferingService() {
185 return CourseOfferingResourceLoader.loadCourseOfferingService();
186 }
187
188 public CourseWaitListService getCourseWaitListService() {
189 return CourseOfferingResourceLoader.loadCourseWaitlistService();
190 }
191 public TypeService getTypeService(){
192 if (typeService == null){
193 typeService = CourseOfferingResourceLoader.loadTypeService();
194 }
195 return typeService;
196 }
197
198 public StateService getStateService(){
199 if (stateService == null){
200 stateService = CourseOfferingResourceLoader.loadStateService();
201 }
202 return stateService;
203 }
204
205 public AcademicCalendarService getAcalService() {
206 if(acalService == null) {
207 acalService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
208 }
209 return this.acalService;
210 }
211
212 private PopulationService getPopulationService() {
213 if(populationService == null) {
214 populationService = (PopulationService) GlobalResourceLoader.getService(new QName(PopulationServiceConstants.NAMESPACE, "PopulationService"));
215 }
216 return populationService;
217 }
218
219 private String getTermStartEndDate(String termId, TermInfo term) {
220
221 StringBuilder stringBuilder = new StringBuilder();
222 Formatter formatter = new Formatter(stringBuilder, Locale.US);
223 String displayString = termId;
224 if (term != null) {
225 String startDate = DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.format(term.getStartDate());
226 String endDate = DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.format(term.getEndDate());
227 formatter.format("%s - %s", startDate, endDate);
228 displayString = stringBuilder.toString();
229 }
230 return displayString;
231 }
232 }