1 | |
package org.kuali.student.enrollment.class2.acal.keyvalue; |
2 | |
|
3 | |
import org.apache.commons.lang.StringUtils; |
4 | |
import org.kuali.rice.core.api.criteria.Predicate; |
5 | |
import org.kuali.rice.core.api.criteria.QueryByCriteria; |
6 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
7 | |
import org.kuali.rice.core.api.util.ConcreteKeyValue; |
8 | |
import org.kuali.rice.core.api.util.KeyValue; |
9 | |
import org.kuali.rice.krad.uif.control.UifKeyValuesFinderBase; |
10 | |
import org.kuali.rice.krad.uif.view.ViewModel; |
11 | |
import org.kuali.student.enrollment.acal.constants.AcademicCalendarServiceConstants; |
12 | |
import org.kuali.student.enrollment.acal.dto.HolidayCalendarInfo; |
13 | |
import org.kuali.student.enrollment.acal.service.AcademicCalendarService; |
14 | |
import org.kuali.student.enrollment.class2.acal.form.AcademicCalendarForm; |
15 | |
import org.kuali.student.enrollment.common.util.ContextBuilder; |
16 | |
import org.kuali.student.r2.common.constants.CommonServiceConstants; |
17 | |
import org.kuali.student.r2.common.dto.ContextInfo; |
18 | |
import org.kuali.student.r2.common.exceptions.*; |
19 | |
import org.kuali.student.r2.common.util.constants.AtpServiceConstants; |
20 | |
|
21 | |
import javax.xml.namespace.QName; |
22 | |
import java.io.Serializable; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.List; |
25 | |
import java.util.Date; |
26 | |
import java.text.SimpleDateFormat; |
27 | |
|
28 | |
import static org.kuali.rice.core.api.criteria.PredicateFactory.*; |
29 | |
import static org.kuali.rice.core.api.criteria.PredicateFactory.and; |
30 | |
|
31 | |
|
32 | 0 | public class HolidayWrapperListFinder extends UifKeyValuesFinderBase implements Serializable { |
33 | |
|
34 | |
private static final long serialVersionUID = 1L; |
35 | |
private transient AcademicCalendarService acalService; |
36 | |
|
37 | |
public final static String START_DATE = "startDate"; |
38 | |
public final static String END_DATE = "endDate"; |
39 | |
public final static String HCAL_STATE = "atpState"; |
40 | |
public final static String HCAL_TYPE = "atpType"; |
41 | |
|
42 | |
@Override |
43 | |
public List<KeyValue> getKeyValues(ViewModel model) { |
44 | |
|
45 | 0 | List<KeyValue> keyValues = new ArrayList<KeyValue>(); |
46 | 0 | AcademicCalendarForm acalForm = (AcademicCalendarForm)model; |
47 | 0 | Date startDate = acalForm.getAcademicCalendarInfo().getStartDate(); |
48 | 0 | Date endDate = acalForm.getAcademicCalendarInfo().getEndDate(); |
49 | 0 | SimpleDateFormat simpleDateformat = new SimpleDateFormat("yyyy"); |
50 | 0 | List<HolidayCalendarInfo> holidayCalendarInfoList = new ArrayList<HolidayCalendarInfo>(); |
51 | |
|
52 | |
|
53 | 0 | if (startDate == null && endDate == null ) { |
54 | 0 | startDate = new Date(); |
55 | |
} |
56 | |
|
57 | |
|
58 | 0 | if (startDate != null && endDate != null) { |
59 | 0 | QueryByCriteria qbc = buildQueryByCriteria(startDate, endDate); |
60 | |
try{ |
61 | 0 | holidayCalendarInfoList = getAcalService().searchForHolidayCalendars(qbc, getContextInfo()); |
62 | 0 | } catch (Exception e) { |
63 | 0 | throw new RuntimeException(e); |
64 | 0 | } |
65 | 0 | } |
66 | |
|
67 | |
|
68 | |
else { |
69 | |
Integer theStartYear; |
70 | 0 | if (startDate != null) |
71 | 0 | theStartYear = new Integer(simpleDateformat.format(startDate)); |
72 | |
else |
73 | 0 | theStartYear = new Integer(simpleDateformat.format(endDate)); |
74 | 0 | holidayCalendarInfoList = buildOfficialHolidayCalendarInfoList(theStartYear); |
75 | |
} |
76 | |
|
77 | 0 | for(HolidayCalendarInfo holidayCalendarInfo:holidayCalendarInfoList){ |
78 | 0 | ConcreteKeyValue keyValue = new ConcreteKeyValue(); |
79 | 0 | keyValue.setKey(holidayCalendarInfo.getId()); |
80 | 0 | keyValue.setValue(holidayCalendarInfo.getName()); |
81 | 0 | keyValues.add(keyValue); |
82 | 0 | } |
83 | 0 | return keyValues; |
84 | |
|
85 | |
|
86 | |
} |
87 | |
|
88 | |
|
89 | |
private List<HolidayCalendarInfo> buildOfficialHolidayCalendarInfoList(Integer theStartYear){ |
90 | 0 | List<HolidayCalendarInfo> hcList = new ArrayList<HolidayCalendarInfo>(); |
91 | 0 | List<HolidayCalendarInfo> hcOfficialList = new ArrayList<HolidayCalendarInfo>(); |
92 | |
try{ |
93 | 0 | hcList = getAcalService().getHolidayCalendarsByStartYear(theStartYear, new ContextInfo()); |
94 | 0 | for(HolidayCalendarInfo hc : hcList) { |
95 | 0 | if (StringUtils.equals(hc.getStateKey(), AtpServiceConstants.ATP_OFFICIAL_STATE_KEY)){ |
96 | 0 | hcOfficialList.add(hc); |
97 | |
} |
98 | |
} |
99 | 0 | }catch (InvalidParameterException ipe){ |
100 | 0 | throw new RuntimeException(ipe); |
101 | 0 | }catch (MissingParameterException mpe){ |
102 | 0 | throw new RuntimeException(mpe); |
103 | 0 | }catch (OperationFailedException ofe){ |
104 | 0 | throw new RuntimeException(ofe); |
105 | 0 | }catch (PermissionDeniedException pde){ |
106 | 0 | throw new RuntimeException(pde); |
107 | 0 | } |
108 | 0 | return hcOfficialList; |
109 | |
|
110 | |
} |
111 | |
|
112 | |
private QueryByCriteria buildQueryByCriteria(Date startDate, Date endDate){ |
113 | 0 | List<Predicate> predicates = new ArrayList<Predicate>(); |
114 | |
|
115 | 0 | Predicate startDatePredicate = and(greaterThanOrEqual(START_DATE, startDate), |
116 | |
lessThanOrEqual(START_DATE, endDate)); |
117 | |
|
118 | 0 | Predicate endDatePredicate = and(greaterThanOrEqual(END_DATE, startDate), |
119 | |
lessThanOrEqual(END_DATE, endDate)); |
120 | |
|
121 | 0 | predicates.add(or(startDatePredicate, endDatePredicate)); |
122 | 0 | predicates.add(equal(HCAL_STATE, AtpServiceConstants.ATP_OFFICIAL_STATE_KEY)); |
123 | 0 | predicates.add(equal(HCAL_TYPE, AtpServiceConstants.ATP_HOLIDAY_CALENDAR_TYPE_KEY)); |
124 | |
|
125 | 0 | QueryByCriteria.Builder qbcBuilder = QueryByCriteria.Builder.create(); |
126 | 0 | qbcBuilder.setPredicates(predicates.toArray(new Predicate[predicates.size()])); |
127 | 0 | QueryByCriteria qbc = qbcBuilder.build(); |
128 | |
|
129 | 0 | return qbc; |
130 | |
} |
131 | |
|
132 | |
public AcademicCalendarService getAcalService() { |
133 | 0 | if(acalService == null) { |
134 | 0 | acalService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(CommonServiceConstants.REF_OBJECT_URI_GLOBAL_PREFIX + "acal", "AcademicCalendarService")); |
135 | |
} |
136 | 0 | return this.acalService; |
137 | |
} |
138 | |
|
139 | |
public ContextInfo getContextInfo() { |
140 | 0 | return ContextBuilder.loadContextInfo(); |
141 | |
} |
142 | |
} |