View Javadoc
1   package org.kuali.ole.deliver.calendar.rule;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.deliver.calendar.bo.*;
5   import org.kuali.rice.kim.api.identity.IdentityService;
6   import org.kuali.rice.kim.service.UiDocumentService;
7   import org.kuali.rice.krad.maintenance.MaintenanceDocument;
8   import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
9   import org.kuali.rice.krad.util.GlobalVariables;
10  
11  import java.sql.Timestamp;
12  import java.util.ArrayList;
13  import java.util.Date;
14  import java.util.List;
15  
16  /**
17   * Created with IntelliJ IDEA.
18   * User: dileepp
19   * Date: 8/26/13
20   * Time: 4:17 PM
21   * To change this template use File | Settings | File Templates.
22   */
23  public class OleCalendarRule extends MaintenanceDocumentRuleBase {
24      protected UiDocumentService uiDocumentService;
25      protected IdentityService identityService;
26  
27      /**
28       * This method validates the patron object and returns boolean value
29       *
30       * @param document
31       * @return isValid
32       */
33  
34      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
35          LOG.debug(" Inside processCustomSaveDocumentBusinessRule");
36          boolean isValid = true;
37          OleCalendar oleCalendar = (OleCalendar) document.getNewMaintainableObject().getDataObject();
38          isValid &= validateBeginAndEndDate(oleCalendar);
39          isValid &= validateGeneralWeekDaysFromAndTo(oleCalendar.getOleCalendarWeekList());
40          isValid &= validateGeneralOpenAndCloseTime(oleCalendar.getOleCalendarWeekList());
41          isValid &= validateGeneralWeekDay(oleCalendar.getOleCalendarWeekList());
42          isValid &= validateExceptionDaysOpenAndCloseTime(oleCalendar.getOleCalendarExceptionDateList());
43          isValid &= validateExceptionDay(oleCalendar, isValid);
44          isValid &= validateExceptionDays(oleCalendar.getOleCalendarExceptionDateList());
45          isValid &= validateExceptionPeriodListOpenTime(oleCalendar.getOleCalendarExceptionPeriodList());
46          isValid &= validateGeneralPeriodDaysFromAndTo(oleCalendar);
47          isValid &= validateExceptionPeriodDay(oleCalendar, isValid);
48          isValid &= validateExceptionPeriodListDay(oleCalendar.getOleCalendarExceptionPeriodList(), isValid);
49         // isValid &= validateExceptionPeriodBeginEndDate(oleCalendar, isValid);
50  
51  
52          return isValid;
53      }
54      private boolean validateBeginAndEndDate(OleCalendar oleCalendar){
55          if(oleCalendar.getBeginDate()!=null && oleCalendar.getEndDate()!=null){
56              long begin=oleCalendar.getBeginDate().getTime();
57              long end =oleCalendar.getEndDate().getTime();
58              if(end<begin){
59                  GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_GENERAL, OLEConstants.CALENDAR_BEGIN_END_DATE);
60                  return false;
61              }
62          }
63          return true;
64      }
65      private boolean validateGeneralWeekDay(List<OleCalendarWeek> oleCalendarWeekList) {
66          List<Integer> weekDays = new ArrayList<Integer>();
67          for (int i = 0; i < oleCalendarWeekList.size(); i++) {
68              if(new Integer(oleCalendarWeekList.get(i).getStartDay()) <= new Integer(oleCalendarWeekList.get(i).getEndDay())){
69                  for (int x = new Integer(oleCalendarWeekList.get(i).getStartDay()); x <= new Integer(oleCalendarWeekList.get(i).getEndDay()); x++) {
70                      if (weekDays.size() > 0) {
71                          if (weekDays.contains(x)) {
72                              GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_GENERAL, OLEConstants.CALENDAR_WEEKDAYS_OVERLAP);
73                              return false;
74                          } else {
75                              weekDays.add(x);
76                          }
77                      } else {
78                          weekDays.add(x);
79                      }
80                  }
81              }
82              else{
83                  int end=6;
84                  int strtDay=new Integer(oleCalendarWeekList.get(i).getStartDay());
85                  for(int strt=strtDay;strt<=end;strt++ ){
86                      if (weekDays.size() > 0) {
87                          if (weekDays.contains(strt)) {
88                              GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_GENERAL, OLEConstants.CALENDAR_WEEKDAYS_OVERLAP);
89                              return false;
90                          } else {
91                              weekDays.add(strt);
92                          }
93                      } else {
94                          weekDays.add(strt);
95                      }
96                      if(end==strt){
97                          for(strt=0;strt<=new Integer(oleCalendarWeekList.get(i).getEndDay());strt++ ){
98                              if (weekDays.size() > 0) {
99                                  if (weekDays.contains(strt)) {
100                                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_GENERAL, OLEConstants.CALENDAR_WEEKDAYS_OVERLAP);
101                                     return false;
102                                 } else {
103                                     weekDays.add(strt);
104                                 }
105                             } else {
106                                 weekDays.add(strt);
107                             }
108                         }
109                     }
110 
111                 }
112         }
113 
114         }
115         return true;
116     }
117     private boolean validateGeneralWeekDaysFromAndTo(List<OleCalendarWeek> oleCalendarWeekList) {
118         boolean isValid = true;
119         for(OleCalendarWeek oleCalendarWeek:oleCalendarWeekList){
120             if(oleCalendarWeek.getStartDay().equals("9") || oleCalendarWeek.getEndDay().equals("9")){
121                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_GENERAL, OLEConstants.CALENDAR_GENERAL_ECH_DAY_WEEK);
122                 isValid &= false;
123             }
124         }
125         return isValid;
126     }
127     private boolean validateGeneralOpenAndCloseTime(List<OleCalendarWeek> oleCalendarWeekList) {
128         boolean isValid = true;
129         for (OleCalendarWeek oleCalendarWeek : oleCalendarWeekList) {
130             if (oleCalendarWeek.getOpenTime() == null || oleCalendarWeek.getOpenTime().equals("")) {
131                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_GENERAL, OLEConstants.CALENDAR_OPEN_TIME);
132                 isValid &= false;
133             }
134             if (oleCalendarWeek.getCloseTime() == null || oleCalendarWeek.getCloseTime().equals("")) {
135                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_GENERAL, OLEConstants.CALENDAR_CLOSE_TIME);
136                 isValid &= false;
137             }
138            /* if (oleCalendarWeek.getStartDay().equals(oleCalendarWeek.getEndDay())) {
139                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_GENERAL, OLEConstants.CALENDAR_WEEK_DAYS_SAME);
140                 isValid &= false;
141             }*/
142             if (isValid && oleCalendarWeek.isEachDayWeek()) {
143                 float closeTime = Float.parseFloat(oleCalendarWeek.getCloseTime().split(":")[0] + "." + oleCalendarWeek.getCloseTime().split(":")[1]);
144                 float openTime = Float.parseFloat(oleCalendarWeek.getOpenTime().split(":")[0] + "." + oleCalendarWeek.getOpenTime().split(":")[1]);
145                 if (openTime >= closeTime) {
146                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_GENERAL, OLEConstants.CALENDAR_OPEN_CLOSE_TIME_CHECK);
147                     isValid &= false;
148                 }
149             }
150 
151         }
152         if (oleCalendarWeekList.size() == 0) {
153             GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_GENERAL, OLEConstants.CALENDAR_GENERAL_LIST);
154             isValid &= false;
155         }
156         return isValid;
157     }
158 
159     private boolean validateExceptionDay(OleCalendar oleCalendar, boolean isValid) {
160         if (isValid) {
161             List<OleCalendarExceptionDate> oleCalendarExceptionDates = oleCalendar.getOleCalendarExceptionDateList();
162             long beginDay = oleCalendar.getBeginDate().getTime();
163             for (OleCalendarExceptionDate oleCalendarExceptionDate : oleCalendarExceptionDates) {
164                 long exceptionDay = Timestamp.valueOf(oleCalendarExceptionDate.getDate().toString() + " " + "00:00:00.01").getTime();
165                 if (!(exceptionDay > beginDay)) {
166                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_EXCEPTION, OLEConstants.CALENDAR_EXCEPTION_VALIDATE);
167                     return false;
168                 }
169             }
170         }
171         return true;
172     }
173 
174     private boolean validateExceptionDays(List<OleCalendarExceptionDate> oleCalendarExceptionDates) {
175 
176         List<Date> days = new ArrayList<>();
177         for (OleCalendarExceptionDate oleCalendarExceptionDate : oleCalendarExceptionDates) {
178             if (days.contains(oleCalendarExceptionDate.getDate())) {
179                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_EXCEPTION, OLEConstants.CALENDAR_EXCEPTION_DUPLICATE);
180                 return false;
181             } else {
182                 days.add(oleCalendarExceptionDate.getDate());
183             }
184         }
185         return true;
186     }
187 
188     private boolean validateExceptionDaysOpenAndCloseTime(List<OleCalendarExceptionDate> oleCalendarExceptionDates) {
189         boolean isValid = true;
190         for (OleCalendarExceptionDate oleCalendarExceptionDate : oleCalendarExceptionDates) {
191             if (oleCalendarExceptionDate.getDate() == null || oleCalendarExceptionDate.getDate().equals("")) {
192                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_EXCEPTION, OLEConstants.CALENDAR_EXCEPTION_DATE_EMPTY);
193                 isValid &= false;
194             }
195             if (oleCalendarExceptionDate.getExceptionType().equals("") || oleCalendarExceptionDate.getExceptionType().equalsIgnoreCase("Select")) {
196                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_EXCEPTION, OLEConstants.CALENDAR_EXCEPTION_TYPE_EMPTY);
197                 isValid &= false;
198             }
199             if (oleCalendarExceptionDate.getExceptionType().equalsIgnoreCase(OLEConstants.CALENDAR_EXCEPTION_TYPE) && isValid) {
200                 if (!oleCalendarExceptionDate.getOpenTime().equals("")) {
201                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_EXCEPTION, OLEConstants.CALENDAR_EXCEPTION_OPEN_TIME_EMPTY);
202                     isValid &= false;
203                 }
204                 if (!oleCalendarExceptionDate.getCloseTime().equals("")) {
205                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_EXCEPTION, OLEConstants.CALENDAR_EXCEPTION_CLOSE_TIME_EMPTY);
206                     isValid &= false;
207                 }
208             } else if (isValid) {
209                 if (oleCalendarExceptionDate.getOpenTime().equals("")) {
210                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_EXCEPTION, OLEConstants.CALENDAR_EXCEPTION_OPEN_TIME);
211                     isValid &= false;
212                 }
213                 if (oleCalendarExceptionDate.getCloseTime().equals("")) {
214                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_EXCEPTION, OLEConstants.CALENDAR_EXCEPTION_CLOSE_TIME);
215                     isValid &= false;
216                 }
217                 if (isValid) {
218                     float closeTime = Float.parseFloat(oleCalendarExceptionDate.getCloseTime().split(":")[0] + "." + oleCalendarExceptionDate.getCloseTime().split(":")[1]);
219                     float openTime = Float.parseFloat(oleCalendarExceptionDate.getOpenTime().split(":")[0] + "." + oleCalendarExceptionDate.getOpenTime().split(":")[1]);
220                     if (openTime >= closeTime) {
221                         GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_EXCEPTION, OLEConstants.CALENDAR_OPEN_CLOSE_TIME_CHECK);
222                         isValid &= false;
223                     }
224                 }
225             }
226 
227         }
228         return isValid;
229     }
230 
231     private boolean validateExceptionPeriodBeginEndDate(OleCalendar oleCalendar, boolean valid) {
232         boolean isValid = true;
233         long beginDay = oleCalendar.getBeginDate().getTime();
234         if (valid)
235             for (OleCalendarExceptionPeriod oleCalendarExceptionPeriod : oleCalendar.getOleCalendarExceptionPeriodList()) {
236                 long exceptionPeriodBeginDay = oleCalendarExceptionPeriod.getBeginDate().getTime();
237                 long exceptionPeriodEndDay = oleCalendarExceptionPeriod.getEndDate().getTime();
238                 if (exceptionPeriodBeginDay < beginDay) {
239                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_PERIOD, OLEConstants.CALENDAR_PERIOD_BEGIN);
240                     isValid &= false;
241                 }
242                 if (exceptionPeriodEndDay < beginDay) {
243                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_PERIOD, OLEConstants.CALENDAR_PERIOD_END);
244                     isValid &= false;
245                 }
246                 if (exceptionPeriodEndDay < exceptionPeriodBeginDay && isValid) {
247                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_PERIOD, OLEConstants.CALENDAR_PERIOD_BEGIN_END);
248                     isValid &= false;
249                 }
250 
251             }
252         return isValid;
253     }
254 
255     private boolean validateExceptionPeriodListDay(List<OleCalendarExceptionPeriod> oleCalendarExceptionPeriodList, boolean isValid) {
256         if (isValid) {
257             for (OleCalendarExceptionPeriod oleCalendarExceptionPeriod : oleCalendarExceptionPeriodList) {
258                 List<Integer> weekDays = new ArrayList<Integer>();
259                 for (int i = 0; i < oleCalendarExceptionPeriod.getOleCalendarExceptionPeriodWeekList().size(); i++) {
260                     if(new Integer(oleCalendarExceptionPeriod.getOleCalendarExceptionPeriodWeekList().get(i).getStartDay()) <= new Integer(oleCalendarExceptionPeriod.getOleCalendarExceptionPeriodWeekList().get(i).getEndDay())){
261                         for (int x = new Integer(oleCalendarExceptionPeriod.getOleCalendarExceptionPeriodWeekList().get(i).getStartDay()); x <= new Integer(oleCalendarExceptionPeriod.getOleCalendarExceptionPeriodWeekList().get(i).getEndDay()); x++) {
262                             if (weekDays.size() > 0) {
263                                 if (weekDays.contains(x)) {
264                                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_PERIOD, OLEConstants.CALENDAR_WEEKDAYS_OVERLAP);
265                                     return false;
266                                 } else {
267                                     weekDays.add(x);
268                                 }
269                             } else {
270                                 weekDays.add(x);
271                             }
272                         }
273                     }
274                     else{
275                         int end=6;
276                         int strtDay=new Integer(oleCalendarExceptionPeriod.getOleCalendarExceptionPeriodWeekList().get(i).getStartDay());
277                         for(int strt=strtDay;strt<=end;strt++ ){
278                             if (weekDays.size() > 0) {
279                                 if (weekDays.contains(strt)) {
280                                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_PERIOD, OLEConstants.CALENDAR_WEEKDAYS_OVERLAP);
281                                     return false;
282                                 } else {
283                                     weekDays.add(strt);
284                                 }
285                             } else {
286                                 weekDays.add(strt);
287                             }
288                             if(end==strt){
289                                 for(strt=0;strt<=new Integer(oleCalendarExceptionPeriod.getOleCalendarExceptionPeriodWeekList().get(i).getEndDay());strt++ ){
290                                     if (weekDays.size() > 0) {
291                                         if (weekDays.contains(strt)) {
292                                             GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_PERIOD, OLEConstants.CALENDAR_WEEKDAYS_OVERLAP);
293                                             return false;
294                                         } else {
295                                             weekDays.add(strt);
296                                         }
297                                     } else {
298                                         weekDays.add(strt);
299                                     }
300                                 }
301                             }
302 
303                         }
304                     }
305 
306                 }
307             }
308         }
309         return true;
310     }
311     private boolean validateGeneralPeriodDaysFromAndTo(OleCalendar oleCalendar) {
312         boolean isValid = true;
313         for (OleCalendarExceptionPeriod oleCalendarExceptionPeriod : oleCalendar.getOleCalendarExceptionPeriodList()) {
314             for(OleCalendarExceptionPeriodWeek oleCalendarExceptionPeriodWeek:oleCalendarExceptionPeriod.getOleCalendarExceptionPeriodWeekList()){
315                 if(oleCalendarExceptionPeriodWeek.getStartDay().equals("9") || oleCalendarExceptionPeriodWeek.getEndDay().equals("9")){
316                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_PERIOD, OLEConstants.CALENDAR_GENERAL_ECH_DAY_WEEK);
317                     isValid &= false;
318                 }
319             }
320         }
321         return isValid;
322     }
323     private boolean validateExceptionPeriodListOpenTime(List<OleCalendarExceptionPeriod> oleCalendarExceptionPeriodList) {
324         boolean isValid = true;
325         for (OleCalendarExceptionPeriod oleCalendarExceptionPeriod : oleCalendarExceptionPeriodList) {
326             if (oleCalendarExceptionPeriod.getBeginDate() == null || oleCalendarExceptionPeriod.getBeginDate().equals("")) {
327                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_PERIOD, OLEConstants.CALENDAR_PERIOD_BEGIN_DATE_EMPTY);
328                 isValid &= false;
329             }
330             if (oleCalendarExceptionPeriod.getEndDate() == null || oleCalendarExceptionPeriod.getEndDate().equals("")) {
331                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_PERIOD, OLEConstants.CALENDAR_PERIOD_END_DATE_EMPTY);
332                 isValid &= false;
333             }
334             if(oleCalendarExceptionPeriod.getExceptionPeriodType().equals("") ||  oleCalendarExceptionPeriod.getExceptionPeriodType()==null){
335                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_PERIOD, OLEConstants.CALENDAR_PERIOD_EXP_PRD_TYP);
336                 isValid &= false;
337             }
338             if (isValid)
339                 for (OleCalendarExceptionPeriodWeek oleCalendarExceptionPeriodWeek : oleCalendarExceptionPeriod.getOleCalendarExceptionPeriodWeekList()) {
340                     if (oleCalendarExceptionPeriodWeek.getOpenTime() == null || oleCalendarExceptionPeriodWeek.getOpenTime().equals("")) {
341                         GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_PERIOD, OLEConstants.CALENDAR_OPEN_TIME);
342                         isValid &= false;
343                     }
344                     if (oleCalendarExceptionPeriodWeek.getCloseTime() == null || oleCalendarExceptionPeriodWeek.getCloseTime().equals("")) {
345                         GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_PERIOD, OLEConstants.CALENDAR_CLOSE_TIME);
346                         isValid &= false;
347                     }
348                  /*   if (oleCalendarExceptionPeriodWeek.getStartDay().equals(oleCalendarExceptionPeriodWeek.getEndDay())) {
349                         GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_PERIOD, OLEConstants.CALENDAR_WEEK_DAYS_SAME);
350                         isValid &= false;
351                     }*/
352                     if (isValid && oleCalendarExceptionPeriodWeek.isEachDayOfExceptionWeek()) {
353                         float closeTime = Float.parseFloat(oleCalendarExceptionPeriodWeek.getCloseTime().split(":")[0] + "." + oleCalendarExceptionPeriodWeek.getCloseTime().split(":")[1]);
354                         float openTime = Float.parseFloat(oleCalendarExceptionPeriodWeek.getOpenTime().split(":")[0] + "." + oleCalendarExceptionPeriodWeek.getOpenTime().split(":")[1]);
355                         if (openTime >= closeTime) {
356                             GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_PERIOD, OLEConstants.CALENDAR_OPEN_CLOSE_TIME_CHECK);
357                             isValid &= false;
358                         }
359                     }
360 
361                 }
362             if (!oleCalendarExceptionPeriod.getExceptionPeriodType().equals("Holiday") && !oleCalendarExceptionPeriod.getExceptionPeriodType().equals("") && oleCalendarExceptionPeriod.getOleCalendarExceptionPeriodWeekList().size() == 0 ) {
363                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_PERIOD, OLEConstants.CALENDAR_PERIOD_LIST_EMPTY);
364                 isValid &= false;
365             }
366         }
367         return isValid;
368     }
369     private boolean validateExceptionPeriodDay(OleCalendar oleCalendar,Boolean valid) {
370         boolean isSuccess=false;
371         if(valid)
372         for (OleCalendarExceptionPeriod oleCalendarExceptionPeriod : oleCalendar.getOleCalendarExceptionPeriodList()) {
373             Timestamp beginDateTimestamp=oleCalendarExceptionPeriod.getBeginDate();
374             long beginDate=beginDateTimestamp.getTime();
375             Timestamp endDateTimestamp=oleCalendarExceptionPeriod.getEndDate();
376             long  endDate=endDateTimestamp.getTime();
377             if(oleCalendarExceptionPeriod.getCalendarExceptionPeriodId()!=null)
378             for (OleCalendarExceptionPeriod calendarExceptionPeriod : oleCalendar.getOleCalendarExceptionPeriodList()) {
379                 Timestamp calendarBeginDateTimestamp=calendarExceptionPeriod.getBeginDate();
380                 long calendarBeginDate=calendarBeginDateTimestamp.getTime();
381                 Timestamp calendarEndDateTimestamp=calendarExceptionPeriod.getEndDate();
382                 long  calendarEndDate=endDateTimestamp.getTime();
383                 if(calendarExceptionPeriod.getCalendarExceptionPeriodId()!=null && !oleCalendarExceptionPeriod.getCalendarExceptionPeriodId().equals(calendarExceptionPeriod.getCalendarExceptionPeriodId())){
384                     if(beginDate>=calendarBeginDate &&  beginDate <= calendarBeginDate ){
385                         isSuccess=true;
386                     }
387                     if(endDate >= calendarEndDate  &&  endDate <= calendarEndDate ){
388                         isSuccess=true;
389                     }
390                 }
391             }
392             if(isSuccess){
393                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.CALENDAR_PERIOD, OLEConstants.CALENDAR_PERIOD_RANGE_OVERLAP_ERROR);
394                 valid &= false;
395             }
396         }
397         return valid;
398     }
399 
400 }