1 package org.kuali.student.enrollment.class2.appointment.service.impl;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
5 import org.kuali.rice.krad.inquiry.InquirableImpl;
6 import org.kuali.rice.krad.util.GlobalVariables;
7 import org.kuali.student.r2.core.acal.dto.KeyDateInfo;
8 import org.kuali.student.r2.core.acal.service.AcademicCalendarService;
9 import org.kuali.student.enrollment.class2.appointment.dto.AppointmentWindowWrapper;
10 import org.kuali.student.r2.common.class1.search.ApptWindowCountsSearchImpl;
11 import org.kuali.student.r2.common.constants.CommonServiceConstants;
12 import org.kuali.student.r2.common.dto.ContextInfo;
13 import org.kuali.student.r2.common.dto.LocaleInfo;
14 import org.kuali.student.r2.core.constants.AcademicCalendarServiceConstants;
15 import org.kuali.student.r2.common.util.date.DateFormatters;
16 import org.kuali.student.r2.core.appointment.constants.AppointmentServiceConstants;
17 import org.kuali.student.r2.core.appointment.dto.AppointmentWindowInfo;
18 import org.kuali.student.r2.core.appointment.service.AppointmentService;
19 import org.kuali.student.r2.core.class1.type.dto.TypeInfo;
20 import org.kuali.student.r2.core.class1.type.service.TypeService;
21 import org.kuali.student.r2.core.constants.PopulationServiceConstants;
22 import org.kuali.student.r2.core.constants.TypeServiceConstants;
23 import org.kuali.student.r2.core.population.dto.PopulationInfo;
24 import org.kuali.student.r2.core.population.service.PopulationService;
25 import org.kuali.student.r2.core.search.dto.SearchRequestInfo;
26 import org.kuali.student.r2.core.search.dto.SearchResultInfo;
27 import org.kuali.student.r2.core.search.service.SearchService;
28 import org.kuali.student.r2.core.search.util.SearchResultHelper;
29
30 import javax.xml.namespace.QName;
31 import java.util.Date;
32 import java.util.Locale;
33 import java.util.Map;
34
35
36 public class AppointmentWindowWrapperInquiryViewHelperServiceImpl extends InquirableImpl {
37 private static final long serialVersionUID = 1L;
38 public final static String WINDOW_WRAPPER_KEY = "id";
39 private transient AppointmentService appointmentService;
40 private transient AcademicCalendarService academicCalendarService;
41 private transient TypeService typeService;
42 private transient PopulationService populationService;
43 private transient SearchService searchService;
44
45 @Override
46 public AppointmentWindowWrapper retrieveDataObject(Map<String, String> parameters) {
47
48 AppointmentWindowWrapper appointmentWindowWrapper = null;
49 AppointmentService appointmentService = getAppointmentService();
50 ContextInfo context = getContextInfo();
51 try{
52
53 String windowId = parameters.get(WINDOW_WRAPPER_KEY);
54 if(windowId == null){
55 System.out.println(">>>windowId is null");
56 return null;
57 }
58 else {
59 System.out.println(">>>windowId ="+windowId);
60 }
61 appointmentWindowWrapper = new AppointmentWindowWrapper();
62
63 AppointmentWindowInfo appointmentWindowInfo = appointmentService.getAppointmentWindow(windowId, context);
64 appointmentWindowWrapper.setAppointmentWindowInfo(appointmentWindowInfo);
65 appointmentWindowWrapper.setId(appointmentWindowInfo.getId());
66 KeyDateInfo period = getAcalService().getKeyDate(appointmentWindowInfo.getPeriodMilestoneId(),context);
67 appointmentWindowWrapper.setPeriodName(period.getName());
68 PopulationInfo populationInfo = getPopulationService().getPopulation(appointmentWindowInfo.getAssignedPopulationId(),context);
69 appointmentWindowWrapper.setAssignedPopulationName(populationInfo.getName());
70 TypeInfo type = getTypeService().getType(appointmentWindowInfo.getTypeKey(), context);
71 appointmentWindowWrapper.setWindowTypeName(type.getName());
72
73
74 SearchRequestInfo searchRequest = new SearchRequestInfo(ApptWindowCountsSearchImpl.SEARCH_TYPE.getKey());
75 searchRequest.addParam(ApptWindowCountsSearchImpl.APPT_WINDOW_ID.getKey(),windowId);
76 SearchResultInfo searchResult = getSearchService().search(searchRequest, null);
77
78 SearchResultHelper resultHelper = new SearchResultHelper(searchResult);
79 if (resultHelper.getSearchResult().getTotalResults() != null && resultHelper.getSearchResult().getTotalResults() > 0){
80 Integer numberOfSlots = resultHelper.getAsInteger(0, ApptWindowCountsSearchImpl.NUM_SLOTS);
81 Integer numberOfStudents = resultHelper.getAsInteger(0, ApptWindowCountsSearchImpl.NUM_APPTS);
82 double meanStudentsPerSlot = Math.ceil(numberOfStudents/(float)numberOfSlots);
83 String firstSlotPopulated = resultHelper.get(0, ApptWindowCountsSearchImpl.FIRST_SLOT);
84 String lastSlotPopulated = resultHelper.get(0, ApptWindowCountsSearchImpl.LAST_SLOT);
85 Date windowCreate = resultHelper.getAsDate(0, ApptWindowCountsSearchImpl.CREATE_TIME);
86
87
88 appointmentWindowWrapper.setNumberOfSlots(numberOfSlots);
89 appointmentWindowWrapper.setNumberOfStudents(numberOfStudents);
90 appointmentWindowWrapper.setMeanStudentsPerSlot(new Float(meanStudentsPerSlot));
91 appointmentWindowWrapper.setFirstSlotPopulated(firstSlotPopulated);
92 appointmentWindowWrapper.setLastSlotPopulated(lastSlotPopulated);
93 appointmentWindowWrapper.setAssignmentsCreated(windowCreate);
94 }
95
96 }catch (Exception e){
97 throw new RuntimeException("Unable to retireve Apppointment Window from Inquiry", e);
98 }
99
100 return appointmentWindowWrapper;
101 }
102
103
104 private String getFormattedDate(Date date) {
105
106 String formattedDate = DateFormatters.MONTH_DAY_YEAR_TIME_DATE_FORMATTER.format(date);
107 if (StringUtils.endsWithIgnoreCase(formattedDate, "12:00 am")){
108 return StringUtils.removeEndIgnoreCase(formattedDate,"12:00 am");
109 }else {
110 return formattedDate;
111 }
112 }
113
114 public AcademicCalendarService getAcalService() {
115 if(academicCalendarService == null) {
116 academicCalendarService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
117 }
118 return this.academicCalendarService;
119 }
120
121
122 public AppointmentService getAppointmentService() {
123 if(appointmentService == null) {
124 appointmentService = (AppointmentService) GlobalResourceLoader.getService(new QName(AppointmentServiceConstants.NAMESPACE, AppointmentServiceConstants.SERVICE_NAME_LOCAL_PART));
125 }
126 return appointmentService;
127 }
128
129
130 public TypeService getTypeService() {
131 if(typeService == null) {
132 typeService = (TypeService) GlobalResourceLoader.getService(new QName(TypeServiceConstants.NAMESPACE, TypeServiceConstants.SERVICE_NAME_LOCAL_PART));
133 }
134 return this.typeService;
135 }
136
137 public PopulationService getPopulationService() {
138 if(populationService == null) {
139 populationService = (PopulationService) GlobalResourceLoader.getService(new QName(PopulationServiceConstants.NAMESPACE, "PopulationMockService"));
140 }
141 return populationService;
142 }
143
144 protected SearchService getSearchService() {
145 if(searchService == null) {
146 searchService = (SearchService) GlobalResourceLoader.getService(new QName(CommonServiceConstants.REF_OBJECT_URI_GLOBAL_PREFIX + "search", SearchService.class.getSimpleName()));
147 }
148 return searchService;
149 }
150
151 public ContextInfo getContextInfo() {
152 ContextInfo contextInfo = new ContextInfo();
153 contextInfo.setAuthenticatedPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
154 contextInfo.setPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
155 LocaleInfo localeInfo = new LocaleInfo();
156 localeInfo.setLocaleLanguage(Locale.getDefault().getLanguage());
157 localeInfo.setLocaleRegion(Locale.getDefault().getCountry());
158 contextInfo.setLocale(localeInfo);
159 return contextInfo;
160 }
161
162
163
164 }
165