1 | |
package org.kuali.student.enrollment.class2.appointment.service.impl; |
2 | |
|
3 | |
import org.kuali.rice.core.api.criteria.PredicateFactory; |
4 | |
import org.kuali.rice.core.api.criteria.QueryByCriteria; |
5 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
6 | |
import org.kuali.rice.krad.lookup.LookupableImpl; |
7 | |
import org.kuali.rice.krad.web.form.LookupForm; |
8 | |
|
9 | |
import org.kuali.student.enrollment.acal.constants.AcademicCalendarServiceConstants; |
10 | |
import org.kuali.student.enrollment.acal.dto.KeyDateInfo; |
11 | |
import org.kuali.student.enrollment.acal.dto.TermInfo; |
12 | |
import org.kuali.student.enrollment.acal.service.AcademicCalendarService; |
13 | |
import org.kuali.student.enrollment.class2.appointment.dto.AppointmentWindowWrapper; |
14 | |
import org.kuali.student.r2.common.dto.ContextInfo; |
15 | |
import org.kuali.student.r2.common.util.constants.PopulationServiceConstants; |
16 | |
import org.kuali.student.r2.common.util.constants.TypeServiceConstants; |
17 | |
import org.kuali.student.r2.core.appointment.constants.AppointmentServiceConstants; |
18 | |
import org.kuali.student.r2.core.appointment.dto.AppointmentWindowInfo; |
19 | |
import org.kuali.student.r2.core.appointment.service.AppointmentService; |
20 | |
import org.kuali.student.r2.core.population.dto.PopulationInfo; |
21 | |
import org.kuali.student.r2.core.population.service.PopulationService; |
22 | |
import org.kuali.student.r2.core.type.dto.TypeTypeRelationInfo; |
23 | |
import org.kuali.student.r2.core.type.service.TypeService; |
24 | |
|
25 | |
import javax.xml.namespace.QName; |
26 | |
import java.text.DateFormat; |
27 | |
import java.text.SimpleDateFormat; |
28 | |
import java.util.ArrayList; |
29 | |
import java.util.Date; |
30 | |
import java.util.List; |
31 | |
import java.util.Map; |
32 | |
|
33 | |
|
34 | 0 | public class AppointmentWindowWrapperLookupableImpl extends LookupableImpl { |
35 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AppointmentWindowWrapperLookupableImpl.class); |
36 | |
|
37 | |
private transient AppointmentService appointmentService; |
38 | |
private transient AcademicCalendarService academicCalendarService; |
39 | |
private transient TypeService typeService; |
40 | |
private transient PopulationService populationService; |
41 | |
|
42 | |
|
43 | |
public final static String TERM_TYPE_KEY = "termType"; |
44 | |
public final static String TERM_YEAR_KEY = "termYear"; |
45 | |
|
46 | |
@Override |
47 | |
protected List<?> getSearchResults(LookupForm lookupForm, Map<String, String> fieldValues, boolean unbounded) { |
48 | |
|
49 | 0 | List<AppointmentWindowWrapper> windowWrapperList = new ArrayList<AppointmentWindowWrapper>(); |
50 | 0 | String termTypeKey = fieldValues.get(TERM_TYPE_KEY); |
51 | 0 | String termYear = fieldValues.get(TERM_YEAR_KEY); |
52 | |
try{ |
53 | 0 | List<KeyDateInfo> periods = _searchPeriods(termTypeKey, termYear); |
54 | 0 | if(periods == null || periods.isEmpty()) |
55 | 0 | return null; |
56 | 0 | windowWrapperList = _loadWindows(periods); |
57 | 0 | }catch (Exception e){ |
58 | 0 | return null; |
59 | 0 | } |
60 | |
|
61 | 0 | return windowWrapperList; |
62 | |
} |
63 | |
|
64 | |
private List<KeyDateInfo> _searchPeriods (String termTypeKey, String termYear) throws Exception { |
65 | |
|
66 | |
|
67 | 0 | DateFormat df = new SimpleDateFormat("yyyy"); |
68 | 0 | Date minBoundDate = df.parse(termYear); |
69 | 0 | Date maxBoundDate = df.parse(Integer.toString(Integer.parseInt(termYear)+1)); |
70 | |
|
71 | |
|
72 | 0 | QueryByCriteria.Builder qbcBuilder = QueryByCriteria.Builder.create(); |
73 | 0 | qbcBuilder.setPredicates(PredicateFactory.and( |
74 | |
PredicateFactory.equal("atpType", termTypeKey), |
75 | |
PredicateFactory.greaterThanOrEqual("startDate", minBoundDate), |
76 | |
PredicateFactory.lessThan("startDate", maxBoundDate))); |
77 | |
|
78 | 0 | QueryByCriteria criteria = qbcBuilder.build(); |
79 | |
|
80 | |
|
81 | 0 | AcademicCalendarService academicCalendarService = getAcalService(); |
82 | 0 | List<TermInfo> terms = academicCalendarService.searchForTerms(criteria, null); |
83 | |
|
84 | |
|
85 | 0 | if(terms == null){ |
86 | 0 | return null; |
87 | |
} |
88 | 0 | if(terms.isEmpty()){ |
89 | 0 | return null; |
90 | |
} |
91 | 0 | if(terms.size()>1){ |
92 | 0 | LOG.error("Too many terms!"); |
93 | |
} |
94 | |
|
95 | 0 | TermInfo term = terms.get(0); |
96 | |
|
97 | |
|
98 | 0 | List<KeyDateInfo> keyDates = academicCalendarService.getKeyDatesForTerm(term.getId(), null); |
99 | 0 | if(keyDates != null){ |
100 | |
|
101 | |
|
102 | 0 | List<TypeTypeRelationInfo> milestoneTypeRelations = getTypeService().getTypeTypeRelationsByOwnerAndType("kuali.milestone.type.group.appt.regperiods","kuali.type.type.relation.type.group",new ContextInfo()); |
103 | 0 | List<String> validMilestoneTypes = new ArrayList<String>(); |
104 | 0 | for(TypeTypeRelationInfo milestoneTypeRelation:milestoneTypeRelations){ |
105 | 0 | validMilestoneTypes.add(milestoneTypeRelation.getRelatedTypeKey()); |
106 | |
} |
107 | |
|
108 | |
|
109 | 0 | List<KeyDateInfo> periodMilestones = new ArrayList<KeyDateInfo>(); |
110 | 0 | for(KeyDateInfo keyDate:keyDates){ |
111 | 0 | if(validMilestoneTypes.contains(keyDate.getTypeKey())){ |
112 | 0 | periodMilestones.add(keyDate); |
113 | |
} |
114 | |
} |
115 | 0 | return periodMilestones; |
116 | |
} |
117 | 0 | return null; |
118 | |
|
119 | |
} |
120 | |
|
121 | |
private List<AppointmentWindowWrapper> _loadWindows(List<KeyDateInfo> periods) throws Exception{ |
122 | 0 | List<AppointmentWindowWrapper> windowWrapperList = new ArrayList<AppointmentWindowWrapper>(); |
123 | 0 | for(KeyDateInfo period:periods){ |
124 | 0 | List<AppointmentWindowInfo> windows = getAppointmentService().getAppointmentWindowsByPeriod(period.getId(), new ContextInfo()); |
125 | 0 | if(windows!=null){ |
126 | 0 | for(AppointmentWindowInfo window:windows){ |
127 | |
|
128 | |
|
129 | 0 | PopulationInfo population = getPopulationService().getPopulation(window.getAssignedPopulationId(), new ContextInfo()); |
130 | |
|
131 | 0 | AppointmentWindowWrapper windowWrapper = new AppointmentWindowWrapper(); |
132 | |
|
133 | 0 | windowWrapper.setAppointmentWindowInfo(window); |
134 | 0 | windowWrapper.setId(window.getId()); |
135 | 0 | windowWrapper.setWindowName(window.getName()); |
136 | 0 | windowWrapper.setPeriodKey(window.getPeriodMilestoneId()); |
137 | 0 | windowWrapper.setPeriodName(period.getName()); |
138 | |
|
139 | 0 | windowWrapper.setAssignedPopulationName(population.getName()); |
140 | 0 | windowWrapper.setWindowTypeKey(window.getTypeKey()); |
141 | |
|
142 | 0 | windowWrapper.setStartDate(_parseDate(window.getStartDate())); |
143 | 0 | windowWrapper.setStartTime(_parseTime(window.getStartDate())); |
144 | 0 | windowWrapper.setStartTimeAmPm(_parseAmPm(window.getStartDate())); |
145 | |
|
146 | 0 | windowWrapper.setEndDate(_parseDate(window.getEndDate())); |
147 | 0 | windowWrapper.setEndTime(_parseTime(window.getEndDate())); |
148 | 0 | windowWrapper.setEndTimeAmPm(_parseAmPm(window.getEndDate())); |
149 | |
|
150 | 0 | windowWrapperList.add(windowWrapper); |
151 | 0 | } |
152 | |
} |
153 | 0 | } |
154 | 0 | return windowWrapperList; |
155 | |
|
156 | |
} |
157 | |
|
158 | |
private String _parseAmPm(Date date) { |
159 | 0 | if(date==null){ |
160 | 0 | return null; |
161 | |
} |
162 | 0 | DateFormat df = new SimpleDateFormat("a"); |
163 | 0 | return df.format(date); |
164 | |
} |
165 | |
|
166 | |
private String _parseTime(Date date) { |
167 | 0 | if(date==null){ |
168 | 0 | return null; |
169 | |
} |
170 | 0 | DateFormat df = new SimpleDateFormat("hh:mm"); |
171 | 0 | return df.format(date); |
172 | |
} |
173 | |
|
174 | |
private Date _parseDate(Date date) { |
175 | 0 | return date; |
176 | |
} |
177 | |
|
178 | |
public AcademicCalendarService getAcalService() { |
179 | 0 | if(academicCalendarService == null) { |
180 | 0 | academicCalendarService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART)); |
181 | |
} |
182 | 0 | return this.academicCalendarService; |
183 | |
} |
184 | |
|
185 | |
|
186 | |
public AppointmentService getAppointmentService() { |
187 | 0 | if(appointmentService == null) { |
188 | 0 | appointmentService = (AppointmentService) GlobalResourceLoader.getService(new QName(AppointmentServiceConstants.NAMESPACE, AppointmentServiceConstants.SERVICE_NAME_LOCAL_PART)); |
189 | |
} |
190 | 0 | return appointmentService; |
191 | |
} |
192 | |
|
193 | |
|
194 | |
public TypeService getTypeService() { |
195 | 0 | if(typeService == null) { |
196 | 0 | typeService = (TypeService) GlobalResourceLoader.getService(new QName(TypeServiceConstants.NAMESPACE, TypeServiceConstants.SERVICE_NAME_LOCAL_PART)); |
197 | |
} |
198 | 0 | return this.typeService; |
199 | |
} |
200 | |
|
201 | |
public PopulationService getPopulationService() { |
202 | 0 | if(populationService == null) { |
203 | 0 | populationService = (PopulationService) GlobalResourceLoader.getService(new QName(PopulationServiceConstants.NAMESPACE, PopulationService.class.getSimpleName())); |
204 | |
} |
205 | 0 | return populationService; |
206 | |
} |
207 | |
|
208 | |
} |