View Javadoc

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   import org.kuali.student.r2.core.acal.dto.KeyDateInfo;
9   import org.kuali.student.r2.core.acal.dto.TermInfo;
10  import org.kuali.student.r2.core.acal.service.AcademicCalendarService;
11  import org.kuali.student.enrollment.class2.appointment.dto.AppointmentWindowWrapper;
12  import org.kuali.student.r2.common.dto.ContextInfo;
13  import org.kuali.student.r2.core.constants.AcademicCalendarServiceConstants;
14  import org.kuali.student.r2.common.util.date.DateFormatters;
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.class1.type.dto.TypeTypeRelationInfo;
19  import org.kuali.student.r2.core.class1.type.service.TypeService;
20  import org.kuali.student.r2.core.constants.AtpServiceConstants;
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  
26  import javax.xml.namespace.QName;
27  import java.util.ArrayList;
28  import java.util.Date;
29  import java.util.List;
30  import java.util.Map;
31  
32  
33  public class AppointmentWindowWrapperLookupableImpl extends LookupableImpl {
34      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AppointmentWindowWrapperLookupableImpl.class);
35      private static final long serialVersionUID = 1L;
36      private transient AppointmentService appointmentService;
37      private transient AcademicCalendarService academicCalendarService;
38      private transient TypeService typeService;
39      private transient PopulationService populationService;
40  
41  
42      public final static String TERM_TYPE_KEY = "termType";
43      public final static String TERM_YEAR_KEY = "termYear";
44  
45      @Override
46      protected List<?> getSearchResults(LookupForm lookupForm, Map<String, String> fieldValues, boolean unbounded) {
47  
48          List<AppointmentWindowWrapper> windowWrapperList;
49          String termTypeKey = fieldValues.get(TERM_TYPE_KEY);
50          String termYear = fieldValues.get(TERM_YEAR_KEY);
51          try {
52              List<KeyDateInfo> periods = _searchPeriods(termTypeKey, termYear);
53              if (periods == null || periods.isEmpty()) {
54                  return null;
55              }
56              windowWrapperList = _loadWindows(periods);
57          } catch (Exception e) {
58              LOG.warn("Error calling _loadWindows", e);
59              return null;
60          }
61  
62          return windowWrapperList;
63      }
64  
65      private List<KeyDateInfo> _searchPeriods(String termTypeKey, String termYear) throws Exception {
66  
67          //Parse the year to a date and the next year's date to compare against the startTerm
68          Date minBoundDate = DateFormatters.DEFULT_YEAR_FORMATTER.parse(termYear);
69          Date maxBoundDate = DateFormatters.DEFULT_YEAR_FORMATTER.parse(Integer.toString(Integer.parseInt(termYear) + 1));
70  
71          //Build up a term search criteria
72          QueryByCriteria.Builder qbcBuilder = QueryByCriteria.Builder.create();
73          qbcBuilder.setPredicates(PredicateFactory.and(
74                  PredicateFactory.equal("atpType", termTypeKey),
75                  PredicateFactory.greaterThanOrEqual("startDate", minBoundDate),
76                  PredicateFactory.lessThan("startDate", maxBoundDate)));
77  
78          QueryByCriteria criteria = qbcBuilder.build();
79  
80          //Perform Term Search with Service Call
81          AcademicCalendarService academicCalendarService = getAcalService();
82          List<TermInfo> terms = academicCalendarService.searchForTerms(criteria, null);
83  
84          //Check for exceptions
85          if (terms == null || terms.isEmpty()) {
86              return null; //Nothing found
87          }
88  
89          if (terms.size() > 1) {
90              LOG.error("Too many terms!");
91          }
92  
93          int firstTermInfo = 0;
94          TermInfo term = terms.get(firstTermInfo);
95  
96          //Get the milestones and filter out anything that is not registration period
97          List<KeyDateInfo> keyDates = academicCalendarService.getKeyDatesForTerm(term.getId(), null);
98          if (keyDates != null) {
99  
100             //Get the valid period types
101             List<TypeTypeRelationInfo> milestoneTypeRelations = getTypeService().getTypeTypeRelationsByOwnerAndType(AtpServiceConstants.MILESTONE_REGISTRATION_PERIOD_GROUP_TYPE_KEY, "kuali.type.type.relation.type.group", new ContextInfo());
102             List<String> validMilestoneTypes = new ArrayList<String>();
103             for (TypeTypeRelationInfo milestoneTypeRelation : milestoneTypeRelations) {
104                 validMilestoneTypes.add(milestoneTypeRelation.getRelatedTypeKey());
105             }
106 
107             //Add in only valid milestones that are registration periods
108             List<KeyDateInfo> periodMilestones = new ArrayList<KeyDateInfo>();
109             for (KeyDateInfo keyDate : keyDates) {
110                 if (validMilestoneTypes.contains(keyDate.getTypeKey())) {
111                     periodMilestones.add(keyDate);
112                 }
113             }
114             return periodMilestones;
115         }
116         return null;
117 
118     }
119 
120     private List<AppointmentWindowWrapper> _loadWindows(List<KeyDateInfo> periods) throws Exception {
121         List<AppointmentWindowWrapper> windowWrapperList = new ArrayList<AppointmentWindowWrapper>();
122         for (KeyDateInfo period : periods) {
123             List<AppointmentWindowInfo> windows = getAppointmentService().getAppointmentWindowsByPeriod(period.getId(), new ContextInfo());
124             if (windows != null) {
125                 for (AppointmentWindowInfo window : windows) {
126 
127                     //Look up the population
128                     PopulationInfo population = getPopulationService().getPopulation(window.getAssignedPopulationId(), new ContextInfo());
129 
130                     AppointmentWindowWrapper windowWrapper = new AppointmentWindowWrapper();
131 
132                     windowWrapper.setAppointmentWindowInfo(window);
133                     windowWrapper.setId(window.getId());
134                     windowWrapper.setWindowName(window.getName());
135                     windowWrapper.setPeriodKey(window.getPeriodMilestoneId());
136                     windowWrapper.setPeriodName(period.getName());
137 
138                     windowWrapper.setAssignedPopulationName(population.getName());
139                     windowWrapper.setWindowTypeKey(window.getTypeKey());
140 
141                     windowWrapper.setStartDate(window.getStartDate());
142                     windowWrapper.setStartTime(_parseTime(window.getStartDate()));
143                     windowWrapper.setStartTimeAmPm(_parseAmPm(window.getStartDate()));
144 
145                     windowWrapper.setEndDate(window.getEndDate());
146                     windowWrapper.setEndTime(_parseTime(window.getEndDate()));
147                     windowWrapper.setEndTimeAmPm(_parseAmPm(window.getEndDate()));
148 
149                     windowWrapperList.add(windowWrapper);
150                 }
151             }
152         }
153         return windowWrapperList;
154 
155     }
156 
157     private String _parseAmPm(Date date) {
158         if (date == null) {
159             return null;
160         }
161         return DateFormatters.AM_PM_TIME_FORMATTER.format(date);
162     }
163 
164     private String _parseTime(Date date) {
165         if (date == null) {
166             return null;
167         }
168         return DateFormatters.HOUR_MINUTE_TIME_FORMATTER.format(date);
169     }
170 
171     public AcademicCalendarService getAcalService() {
172         if (academicCalendarService == null) {
173             academicCalendarService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
174         }
175         return this.academicCalendarService;
176     }
177 
178 
179     public AppointmentService getAppointmentService() {
180         if (appointmentService == null) {
181             appointmentService = (AppointmentService) GlobalResourceLoader.getService(new QName(AppointmentServiceConstants.NAMESPACE, AppointmentServiceConstants.SERVICE_NAME_LOCAL_PART));
182         }
183         return appointmentService;
184     }
185 
186 
187     public TypeService getTypeService() {
188         if (typeService == null) {
189             typeService = (TypeService) GlobalResourceLoader.getService(new QName(TypeServiceConstants.NAMESPACE, TypeServiceConstants.SERVICE_NAME_LOCAL_PART));
190         }
191         return this.typeService;
192     }
193 
194     public PopulationService getPopulationService() {
195         if (populationService == null) {
196             populationService = (PopulationService) GlobalResourceLoader.getService(new QName(PopulationServiceConstants.NAMESPACE, "PopulationMockService")); // TODO: Fix with real service
197         }
198         return populationService;
199     }
200 
201 }