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.enrollment.acal.constants.AcademicCalendarServiceConstants; |
8 | |
import org.kuali.student.enrollment.acal.dto.KeyDateInfo; |
9 | |
import org.kuali.student.enrollment.acal.service.AcademicCalendarService; |
10 | |
import org.kuali.student.enrollment.class2.appointment.dto.AppointmentWindowWrapper; |
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.common.util.constants.TypeServiceConstants; |
15 | |
import org.kuali.student.r2.core.appointment.constants.AppointmentServiceConstants; |
16 | |
import org.kuali.student.r2.core.appointment.dto.AppointmentWindowInfo; |
17 | |
import org.kuali.student.r2.core.appointment.service.AppointmentService; |
18 | |
import org.kuali.student.r2.core.population.dto.PopulationInfo; |
19 | |
import org.kuali.student.r2.core.population.service.PopulationService; |
20 | |
import org.kuali.student.r2.core.search.dto.SearchRequestInfo; |
21 | |
import org.kuali.student.r2.core.search.dto.SearchResultCellInfo; |
22 | |
import org.kuali.student.r2.core.search.dto.SearchResultInfo; |
23 | |
import org.kuali.student.r2.core.search.dto.SearchResultRowInfo; |
24 | |
import org.kuali.student.r2.core.search.service.SearchService; |
25 | |
import org.kuali.student.r2.core.type.dto.TypeInfo; |
26 | |
import org.kuali.student.r2.core.type.service.TypeService; |
27 | |
|
28 | |
import javax.xml.namespace.QName; |
29 | |
import java.text.SimpleDateFormat; |
30 | |
import java.util.*; |
31 | |
|
32 | |
|
33 | 0 | public class AppointmentWindowWrapperInquiryViewHelperServiceImpl extends InquirableImpl { |
34 | |
|
35 | |
public final static String WINDOW_WRAPPER_KEY = "id"; |
36 | |
private transient AppointmentService appointmentService; |
37 | |
|
38 | |
@Override |
39 | |
public AppointmentWindowWrapper retrieveDataObject(Map<String, String> parameters) { |
40 | |
|
41 | 0 | AppointmentWindowWrapper appointmentWindowWrapper = new AppointmentWindowWrapper(); |
42 | 0 | AppointmentService appointmentService = getAppointmentService(); |
43 | 0 | ContextInfo context = getContextInfo(); |
44 | |
try{ |
45 | |
|
46 | 0 | String windowId = parameters.get(WINDOW_WRAPPER_KEY); |
47 | 0 | if(windowId == null){ |
48 | 0 | System.out.println(">>>windowId is null"); |
49 | 0 | return null; |
50 | |
} |
51 | |
else { |
52 | 0 | System.out.println(">>>windowId ="+windowId); |
53 | |
} |
54 | |
|
55 | 0 | AppointmentWindowInfo appointmentWindowInfo = appointmentService.getAppointmentWindow(windowId, context); |
56 | 0 | appointmentWindowWrapper.setAppointmentWindowInfo(appointmentWindowInfo); |
57 | 0 | appointmentWindowWrapper.setId(appointmentWindowInfo.getId()); |
58 | 0 | KeyDateInfo period = getAcalService().getKeyDate(appointmentWindowInfo.getPeriodMilestoneId(),context); |
59 | 0 | appointmentWindowWrapper.setPeriodName(period.getName()); |
60 | 0 | PopulationInfo populationInfo = getPopulationService().getPopulation(appointmentWindowInfo.getAssignedPopulationId(),context); |
61 | 0 | appointmentWindowWrapper.setAssignedPopulationName(populationInfo.getName()); |
62 | 0 | TypeInfo type = getTypeService().getType(appointmentWindowInfo.getTypeKey(), context); |
63 | 0 | appointmentWindowWrapper.setWindowTypeName(type.getName()); |
64 | |
|
65 | |
|
66 | 0 | SearchRequestInfo searchRequest = new SearchRequestInfo("appt.search.appointmentCountForWindowId"); |
67 | 0 | searchRequest.addParam("windowId",windowId); |
68 | 0 | SearchResultInfo searchResult = getSearchService().search(searchRequest, null); |
69 | |
|
70 | |
|
71 | 0 | SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm aa"); |
72 | |
|
73 | 0 | Map<String, String> searchResultMap = convertToMap(searchResult).get(0); |
74 | 0 | Integer numberOfSlots = searchResultMap.get("numSlots")==null?null:Integer.parseInt(searchResultMap.get("numSlots")); |
75 | 0 | Integer numberOfStudents = searchResultMap.get("numAppts")==null?null:Integer.parseInt(searchResultMap.get("numAppts")); |
76 | 0 | double meanStudentsPerSlot = Math.ceil(numberOfStudents/(float)numberOfSlots); |
77 | 0 | String firstSlotPopulated = searchResultMap.get("firstSlot"); |
78 | 0 | String lastSlotPopulated = searchResultMap.get("lastSlot"); |
79 | 0 | Date windowCreate = searchResultMap.get("createTime")==null?null:formatter.parse(searchResultMap.get("createTime")); |
80 | |
|
81 | 0 | appointmentWindowWrapper.setNumberOfSlots(numberOfSlots); |
82 | 0 | appointmentWindowWrapper.setNumberOfStudents(numberOfStudents); |
83 | 0 | appointmentWindowWrapper.setMeanStudentsPerSlot(new Float(meanStudentsPerSlot)); |
84 | 0 | appointmentWindowWrapper.setFirstSlotPopulated(firstSlotPopulated); |
85 | 0 | appointmentWindowWrapper.setLastSlotPopulated(lastSlotPopulated); |
86 | 0 | appointmentWindowWrapper.setAssignmentsCreated(windowCreate); |
87 | |
|
88 | 0 | return appointmentWindowWrapper; |
89 | |
|
90 | 0 | }catch (Exception e){ |
91 | |
|
92 | |
} |
93 | |
|
94 | 0 | return null; |
95 | |
} |
96 | |
|
97 | |
private List<Map<String,String>> convertToMap(SearchResultInfo searchResult) { |
98 | 0 | List<Map<String,String>> list = new ArrayList<Map<String,String>>(); |
99 | 0 | for(SearchResultRowInfo row:searchResult.getRows()){ |
100 | 0 | Map<String,String> map = new HashMap<String,String>(); |
101 | 0 | for(SearchResultCellInfo cell:row.getCells()){ |
102 | 0 | map.put(cell.getKey(),cell.getValue()); |
103 | |
} |
104 | 0 | list.add(map); |
105 | 0 | } |
106 | 0 | return list; |
107 | |
} |
108 | |
|
109 | |
private String getFormattedDate(Date date) { |
110 | 0 | SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm aa"); |
111 | 0 | String formattedDate = formatter.format(date); |
112 | 0 | if (StringUtils.endsWithIgnoreCase(formattedDate, "12:00 am")){ |
113 | 0 | return StringUtils.removeEndIgnoreCase(formattedDate,"12:00 am"); |
114 | |
}else { |
115 | 0 | return formattedDate; |
116 | |
} |
117 | |
} |
118 | |
|
119 | |
public AcademicCalendarService getAcalService() { |
120 | 0 | return (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART)); |
121 | |
} |
122 | |
|
123 | |
public TypeService getTypeService() { |
124 | 0 | return (TypeService) GlobalResourceLoader.getService(new QName(TypeServiceConstants.NAMESPACE, TypeService.class.getSimpleName())); |
125 | |
} |
126 | |
|
127 | |
public AppointmentService getAppointmentService() { |
128 | 0 | return (AppointmentService) GlobalResourceLoader.getService(new QName(AppointmentServiceConstants.NAMESPACE, AppointmentServiceConstants.SERVICE_NAME_LOCAL_PART)); |
129 | |
} |
130 | |
|
131 | |
protected PopulationService getPopulationService() { |
132 | |
|
133 | 0 | return (PopulationService) GlobalResourceLoader.getService(new QName(CommonServiceConstants.REF_OBJECT_URI_GLOBAL_PREFIX+"population", "PopulationService")); |
134 | |
} |
135 | |
|
136 | |
protected SearchService getSearchService() { |
137 | 0 | return (SearchService) GlobalResourceLoader.getService(new QName(CommonServiceConstants.REF_OBJECT_URI_GLOBAL_PREFIX+"search", SearchService.class.getSimpleName())); |
138 | |
} |
139 | |
|
140 | |
public ContextInfo getContextInfo() { |
141 | 0 | ContextInfo contextInfo = new ContextInfo(); |
142 | 0 | contextInfo.setAuthenticatedPrincipalId(GlobalVariables.getUserSession().getPrincipalId()); |
143 | 0 | contextInfo.setPrincipalId(GlobalVariables.getUserSession().getPrincipalId()); |
144 | 0 | LocaleInfo localeInfo = new LocaleInfo(); |
145 | 0 | localeInfo.setLocaleLanguage(Locale.getDefault().getLanguage()); |
146 | 0 | localeInfo.setLocaleRegion(Locale.getDefault().getCountry()); |
147 | 0 | contextInfo.setLocale(localeInfo); |
148 | 0 | return contextInfo; |
149 | |
} |
150 | |
|
151 | |
|
152 | |
|
153 | |
} |
154 | |
|