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