View Javadoc
1   package org.kuali.ole.deliver.calendar.service.impl;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.deliver.calendar.bo.*;
6   import org.kuali.ole.deliver.calendar.controller.OleCalendarController;
7   import org.kuali.ole.deliver.calendar.service.DateUtil;
8   import org.kuali.ole.deliver.calendar.service.OleCalendarService;
9   import org.kuali.ole.deliver.processor.LoanProcessor;
10  import org.kuali.rice.krad.service.BusinessObjectService;
11  import org.kuali.rice.krad.service.KRADServiceLocator;
12  import org.kuali.rice.krad.util.GlobalVariables;
13  import org.kuali.rice.krad.util.KRADConstants;
14  
15  import java.sql.Timestamp;
16  import java.util.*;
17  import java.util.concurrent.TimeUnit;
18  
19  /**
20   * Created with IntelliJ IDEA.
21   * User: arjuns
22   * Date: 7/27/13
23   * Time: 5:39 PM
24   * To change this template use File | Settings | File Templates.
25   */
26  
27  
28  public class OleCalendarServiceImpl implements OleCalendarService {
29  
30      private static final Logger LOG = Logger.getLogger(OleCalendarController.class);
31  
32      private BusinessObjectService businessObjectService;
33  
34      private int totalWorkingDays = 0;
35      private int totalDays = 0;
36  
37      public OleCalendarGroup getCalendarGroup(String deskId) {
38          HashMap calendarGroup = new HashMap();
39          calendarGroup.put(OLEConstants.CALENDER_ID, deskId);
40          List<OleCalendarGroup> oleCalendarGroupList = (List<OleCalendarGroup>) getBusinessObjectService().findMatching(OleCalendarGroup.class, calendarGroup);
41          if (oleCalendarGroupList != null && oleCalendarGroupList.size() > 0) {
42              return oleCalendarGroupList.get(0);
43          }
44          return null;
45      }
46  
47  
48      @Override
49      public OleCalendar getActiveCalendar(Timestamp date, String groupId) {
50          HashMap cg = new HashMap();
51          cg.put(OLEConstants.CALENDER_ID, groupId);
52          List<OleCalendar> oleCalendarList = (List<OleCalendar>) getBusinessObjectService().findMatching(OleCalendar.class, cg);
53          for (OleCalendar calendar : oleCalendarList) {
54              if (calendar.getBeginDate() != null && date != null && calendar.getEndDate() != null && isCalendarExists(calendar.getBeginDate(), calendar.getEndDate(), date.getYear(), date.getMonth()) && (date.getTime() >= calendar.getBeginDate().getTime() && date.getTime() <= calendar.getEndDate().getTime())) {
55                  calendar=continuousWeek(calendar);
56                  return calendar;
57              } else if (calendar.getBeginDate() != null && calendar.getEndDate() == null) {
58                  calendar=continuousWeek(calendar);
59                  return calendar;
60              }
61          }
62          return null;
63      }
64  
65      private OleCalendar continuousWeek(OleCalendar oleCalendar){     //each day of a week
66          int week=-1;
67          List<OleCalendarWeek> oleCalendarWeekList=new ArrayList<OleCalendarWeek>();
68          for(OleCalendarWeek oleCalendarWeek:oleCalendar.getOleCalendarWeekList()){
69              if(!oleCalendarWeek.isEachDayWeek() && !oleCalendarWeek.getEndDay().equals(oleCalendarWeek.getStartDay())){
70                  if(Integer.parseInt(oleCalendarWeek.getStartDay())<Integer.parseInt(oleCalendarWeek.getEndDay())){
71                      for(int strt=new Integer(oleCalendarWeek.getStartDay());strt<=new Integer(oleCalendarWeek.getEndDay());strt++){
72                          OleCalendarWeek oleCalendarWeek1=new OleCalendarWeek();
73                          oleCalendarWeek1.setCalendarId(oleCalendarWeek.getCalendarId());
74                          oleCalendarWeek1.setStartDay(String.valueOf(strt));
75                          oleCalendarWeek1.setEndDay(String.valueOf(strt));
76                          oleCalendarWeek1.setOpenTimeSession(oleCalendarWeek.getOpenTimeSession());
77                          oleCalendarWeek1.setCloseTimeSession(oleCalendarWeek.getCloseTimeSession());
78                          oleCalendarWeek1.setOpenTime("00:00");
79                          oleCalendarWeek1.setCloseTime("23:59");
80                          if(strt==new Integer(oleCalendarWeek.getStartDay())){
81                              oleCalendarWeek1.setOpenTime(oleCalendarWeek.getOpenTime());
82                          }
83                          else if(strt==new Integer(oleCalendarWeek.getEndDay())){
84                              oleCalendarWeek1.setCloseTime(oleCalendarWeek.getCloseTime());
85                          }
86                          oleCalendarWeekList.add(week+1,oleCalendarWeek1);
87                      }
88                  }
89                  else{
90                      for(int strt=new Integer(oleCalendarWeek.getStartDay());strt<7;strt++){
91                          OleCalendarWeek oleCalendarWeek1=new OleCalendarWeek();
92                          oleCalendarWeek1.setCalendarId(oleCalendarWeek.getCalendarId());
93                          oleCalendarWeek1.setStartDay(String.valueOf(strt));
94                          oleCalendarWeek1.setEndDay(String.valueOf(strt));
95                          oleCalendarWeek1.setOpenTimeSession(oleCalendarWeek.getOpenTimeSession());
96                          oleCalendarWeek1.setCloseTimeSession(oleCalendarWeek.getCloseTimeSession());
97                          oleCalendarWeek1.setOpenTime("00:00");
98                          oleCalendarWeek1.setCloseTime("23:59");
99                          if(strt==new Integer(oleCalendarWeek.getStartDay())){
100                             oleCalendarWeek1.setOpenTime(oleCalendarWeek.getOpenTime());
101                         }
102                         else if(strt==new Integer(oleCalendarWeek.getEndDay())){
103                             oleCalendarWeek1.setCloseTime(oleCalendarWeek.getCloseTime());
104                         }
105                         oleCalendarWeekList.add(week+1,oleCalendarWeek1);
106                         if(strt>=6){
107                             for(int end=0;end<=new Integer(oleCalendarWeek.getEndDay());end++){
108                                 oleCalendarWeek1=new OleCalendarWeek();
109                                 oleCalendarWeek1.setCalendarId(oleCalendarWeek.getCalendarId());
110                                 oleCalendarWeek1.setStartDay(String.valueOf(end));
111                                 oleCalendarWeek1.setEndDay(String.valueOf(end));
112                                 oleCalendarWeek1.setOpenTimeSession(oleCalendarWeek.getOpenTimeSession());
113                                 oleCalendarWeek1.setCloseTimeSession(oleCalendarWeek.getCloseTimeSession());
114                                 oleCalendarWeek1.setOpenTime("00:00");
115                                 oleCalendarWeek1.setCloseTime("23:59");
116                                 if(end==new Integer(oleCalendarWeek.getStartDay())){
117                                     oleCalendarWeek1.setOpenTime(oleCalendarWeek.getOpenTime());
118                                 }
119                                 else if(end==new Integer(oleCalendarWeek.getEndDay())){
120                                     oleCalendarWeek1.setCloseTime(oleCalendarWeek.getCloseTime());
121                                 }
122                                 oleCalendarWeekList.add(week+1,oleCalendarWeek1);
123                             }
124                         }
125                     }
126 
127                 }
128             }
129             else{
130                 oleCalendarWeekList.add(oleCalendarWeek);
131             }
132         }
133         oleCalendar.setOleCalendarWeekList(oleCalendarWeekList);
134         for(OleCalendarExceptionPeriod oleCalendarExceptionPeriod:oleCalendar.getOleCalendarExceptionPeriodList()){
135             int period=-1;
136             List<OleCalendarExceptionPeriodWeek> oleCalendarExceptionPeriodWeekList = new ArrayList<OleCalendarExceptionPeriodWeek>();
137             for(OleCalendarExceptionPeriodWeek oleCalendarExceptionPeriodWeek:oleCalendarExceptionPeriod.getOleCalendarExceptionPeriodWeekList()){
138                 if(!oleCalendarExceptionPeriodWeek.isEachDayOfExceptionWeek() && !oleCalendarExceptionPeriodWeek.getEndDay().equals(oleCalendarExceptionPeriodWeek.getStartDay())){
139                     if(Integer.parseInt(oleCalendarExceptionPeriodWeek.getStartDay())<Integer.parseInt(oleCalendarExceptionPeriodWeek.getEndDay())){
140                         for(int strt=new Integer(oleCalendarExceptionPeriodWeek.getStartDay());strt<=new Integer(oleCalendarExceptionPeriodWeek.getEndDay());strt++){
141                             OleCalendarExceptionPeriodWeek oleCalendarExceptionPeriodWeek1=new OleCalendarExceptionPeriodWeek();
142                             oleCalendarExceptionPeriodWeek1.setStartDay(String.valueOf(strt));
143                             oleCalendarExceptionPeriodWeek1.setEndDay(String.valueOf(strt));
144                             oleCalendarExceptionPeriodWeek1.setOpenTimeSession("AM");
145                             oleCalendarExceptionPeriodWeek1.setCloseTimeSession("PM");
146                             oleCalendarExceptionPeriodWeek1.setOpenTime("00:00");
147                             oleCalendarExceptionPeriodWeek1.setCloseTime("23:59");
148                             oleCalendarExceptionPeriodWeek1.setEachDayOfExceptionWeek(true);
149                             if(strt==new Integer(oleCalendarExceptionPeriodWeek.getStartDay())){
150                                 oleCalendarExceptionPeriodWeek1.setOpenTime(oleCalendarExceptionPeriodWeek.getOpenTime());
151                             }
152                             else if(strt==new Integer(oleCalendarExceptionPeriodWeek.getEndDay())){
153                                 oleCalendarExceptionPeriodWeek1.setCloseTime(oleCalendarExceptionPeriodWeek.getCloseTime());
154                             }
155                             oleCalendarExceptionPeriodWeekList.add(period+1,oleCalendarExceptionPeriodWeek1);
156                         }
157                     }
158                     else{
159                         for(int strt=new Integer(oleCalendarExceptionPeriodWeek.getStartDay());strt<7;strt++){
160                             OleCalendarExceptionPeriodWeek oleCalendarExceptionPeriodWeek1=new OleCalendarExceptionPeriodWeek();
161                             oleCalendarExceptionPeriodWeek1.setStartDay(String.valueOf(strt));
162                             oleCalendarExceptionPeriodWeek1.setEndDay(String.valueOf(strt));
163                             oleCalendarExceptionPeriodWeek1.setOpenTimeSession("AM");
164                             oleCalendarExceptionPeriodWeek1.setCloseTimeSession("PM");
165                             oleCalendarExceptionPeriodWeek1.setOpenTime("00:00");
166                             oleCalendarExceptionPeriodWeek1.setCloseTime("23:59");
167                             oleCalendarExceptionPeriodWeek1.setEachDayOfExceptionWeek(true);
168                             if(strt==new Integer(oleCalendarExceptionPeriodWeek.getStartDay())){
169                                 oleCalendarExceptionPeriodWeek1.setOpenTime(oleCalendarExceptionPeriodWeek.getOpenTime());
170                             }
171                             else if(strt==new Integer(oleCalendarExceptionPeriodWeek.getEndDay())){
172                                 oleCalendarExceptionPeriodWeek1.setCloseTime(oleCalendarExceptionPeriodWeek.getCloseTime());
173                             }
174                             oleCalendarExceptionPeriodWeekList.add(period+1,oleCalendarExceptionPeriodWeek1);
175                             if(strt>=6){
176                                 for(int end=0;end<=new Integer(oleCalendarExceptionPeriodWeek.getEndDay());end++){
177                                     oleCalendarExceptionPeriodWeek1=new OleCalendarExceptionPeriodWeek();
178                                     oleCalendarExceptionPeriodWeek1.setStartDay(String.valueOf(strt));
179                                     oleCalendarExceptionPeriodWeek1.setEndDay(String.valueOf(strt));
180                                     oleCalendarExceptionPeriodWeek1.setOpenTimeSession("AM");
181                                     oleCalendarExceptionPeriodWeek1.setCloseTimeSession("PM");
182                                     oleCalendarExceptionPeriodWeek1.setOpenTime("00:00");
183                                     oleCalendarExceptionPeriodWeek1.setCloseTime("23:59");
184                                     oleCalendarExceptionPeriodWeek1.setEachDayOfExceptionWeek(true);
185                                     if(strt==new Integer(oleCalendarExceptionPeriodWeek.getStartDay())){
186                                         oleCalendarExceptionPeriodWeek1.setOpenTime(oleCalendarExceptionPeriodWeek.getOpenTime());
187                                     }
188                                     else if(strt==new Integer(oleCalendarExceptionPeriodWeek.getEndDay())){
189                                         oleCalendarExceptionPeriodWeek1.setCloseTime(oleCalendarExceptionPeriodWeek.getCloseTime());
190                                     }
191                                     oleCalendarExceptionPeriodWeekList.add(period+1,oleCalendarExceptionPeriodWeek1);
192                                 }
193                             }
194                         }
195 
196                     }
197                 }
198                 else{
199                     oleCalendarExceptionPeriodWeekList.add(oleCalendarExceptionPeriodWeek);
200                 }
201             }
202             oleCalendarExceptionPeriod.setOleCalendarExceptionPeriodWeekList(oleCalendarExceptionPeriodWeekList);
203         }
204     return oleCalendar;
205     }
206 
207     private boolean isCalendarExists(Timestamp fromDate, Timestamp toDate, Integer dateYear, Integer dateMonth) {
208         Integer fromYear = fromDate.getYear();
209         Integer fromMonth = fromDate.getMonth();
210         Integer fromHour = fromDate.getHours();
211         Integer fromMin = fromDate.getMinutes();
212         Integer fromSecond = fromDate.getSeconds();
213         Integer toYear = toDate.getYear();
214         Integer toMonth = toDate.getMonth();
215         Integer toHour = toDate.getHours();
216         Integer toMin = toDate.getMinutes();
217         Integer toSecond = toDate.getSeconds();
218         if (dateYear >= fromYear && dateYear <= toYear) {
219             if (dateMonth >= fromMonth && dateMonth <= toMonth) {
220                 return true;
221             }
222         }
223         return false;
224     }
225 
226 
227     private boolean isHoliday(String deskId, Timestamp currentDate) {
228         OleCalendarGroup ocg = getCalendarGroup(deskId);
229         if(ocg == null)
230             return false;
231         OleCalendar oc = getActiveCalendar(currentDate, ocg.getCalendarGroupId());
232         boolean holidayExceptionType = false;
233 
234         List<OleCalendarExceptionDate> oleCalendarExceptionDates = oc!=null && oc.getOleCalendarExceptionDateList()!=null ? oc.getOleCalendarExceptionDateList() : new ArrayList<OleCalendarExceptionDate>();
235         for (OleCalendarExceptionDate exceptionDate : oleCalendarExceptionDates) {
236             if (exceptionDate.getExceptionType().equals(OLEConstants.CALENDAR_EXCEPTION_TYPE) && currentDate != null && currentDate.toString().split(" ")[0].equals(exceptionDate.getDate().toString().split(" ")[0])) {
237 
238                 holidayExceptionType = true;
239                 break;
240 
241             }
242         }
243         if (!holidayExceptionType) {
244             HashMap<String, String> timeMap = new HashMap<String, String>();
245             timeMap = getOpenAndCloseTime(deskId, currentDate);
246             if (timeMap.size() != 2) {
247                 holidayExceptionType = true;
248             }
249         }
250         return holidayExceptionType;
251     }
252 
253 
254     //Due date calculation - days input
255     public Timestamp calculateDueDate(String deskId, String timePeriod, Timestamp currentDate) {
256 
257         Integer count = Integer.parseInt(timePeriod != null ? timePeriod.split("-")[0] : "0");
258         while (count > 0) {
259             boolean temp = false;
260             if (isHoliday(deskId, currentDate)) {
261                 temp = true;
262             }
263             if (temp) {
264                 currentDate = DateUtil.addDays(currentDate, 1);
265             } else {
266                 count = count - 1;
267                 currentDate = DateUtil.addDays(currentDate, 1);
268             }
269         }
270         if (isHoliday(deskId, currentDate)) {
271             currentDate = nextWorkingDay(deskId, currentDate);
272 
273         }
274         HashMap<String, String> timeMap = getOpenAndCloseTime(deskId, currentDate);
275         float openTime = 0;
276         float closeTime = 0;
277         float currentTime = 0;
278 
279         if (timeMap.size() > 0) {
280             String[] openTimes = timeMap.get(OLEConstants.CALENDAR_GET_OPEN_TIME) != null ? timeMap.get(OLEConstants.CALENDAR_GET_OPEN_TIME).split(":") : new String[0];
281             String openTimeHrs = openTimes.length > 0 ? openTimes[0] : null;
282             String openTimeMin = openTimes.length > 1 ? openTimes[1] : null;
283             if (openTimeHrs != null && openTimeMin != null) {
284                 openTime = Float.parseFloat(openTimeHrs + "." + openTimeMin);
285             }
286             String[] closeTimes = timeMap.get(OLEConstants.CALENDAR_GET_CLOSE_TIME) != null ? timeMap.get(OLEConstants.CALENDAR_GET_CLOSE_TIME).split(":") : new String[0];
287             String closeTimeHrs = closeTimes.length > 0 ? closeTimes[0] : null;
288             String closeTimeMin = closeTimes.length > 1 ? closeTimes[1] : null;
289             if (closeTimeHrs != null && closeTimeMin != null) {
290                 closeTime = Float.parseFloat(closeTimeHrs + "." + closeTimeMin);
291             }
292             String[] currentTimeFromDate = currentDate.toString().split(" ");
293             String[] currentTimes = currentTimeFromDate.length > 1 && currentTimeFromDate[1] != null ? currentTimeFromDate[1].split(":") : new String[0];
294             String currentTimeHrs = currentTimes.length > 0 ? currentTimes[0] : null;
295             String currentTimeMin = currentTimes.length > 1 ? currentTimes[1] : null;
296             if (currentTimeHrs != null && currentTimeMin != null) {
297                 currentTime = Float.parseFloat(currentTimeHrs + "." + currentTimeMin);
298             }
299             if (currentTime < openTime) {
300                 currentDate = Timestamp.valueOf(new StringBuilder().append(currentDate.toString().split(" ")[0]).append(" ").append(timeMap.get(OLEConstants.CALENDAR_GET_OPEN_TIME)).append(":00").toString());
301             }
302             else if (currentTime > closeTime) {
303                 boolean holidayExceptionType = true;
304                 Timestamp temp;
305 
306                 int count1 = 1;
307                 while (holidayExceptionType) {
308 
309                     temp = DateUtil.addDays(currentDate, count1);
310                     timeMap = getOpenAndCloseTime(deskId, temp);
311                     holidayExceptionType = isHoliday(deskId, temp);
312                     if (holidayExceptionType == false) {
313                         timeMap = getOpenAndCloseTime(deskId, temp);
314                         currentDate = Timestamp.valueOf(new StringBuilder().append(temp.toString().split(" ")[0]).append(" ").append(timeMap.get(OLEConstants.CALENDAR_GET_OPEN_TIME)).append(":00").toString());
315                     }
316                     count1++;
317                 }//while ends
318             }else {
319                 currentDate = Timestamp.valueOf(new StringBuilder().append(currentDate.toString().split(" ")[0]).append(" ").append(timeMap.get(OLEConstants.CALENDAR_GET_CLOSE_TIME)).append(":00").toString());
320             }
321 
322         }
323         return currentDate;
324 
325     }//end calculateDueDate method..
326 
327     public Timestamp nextWorkingDay(String deskId, Timestamp currentDate) {
328         boolean holidayExceptionType = true;
329         Timestamp temp;
330         int count = 1;
331         while (holidayExceptionType) {
332 
333             temp = DateUtil.addDays(currentDate, count);
334             holidayExceptionType = isHoliday(deskId, temp);
335             if (holidayExceptionType == false) {
336                 return temp;
337             }
338 
339             count++;
340 
341         }//while ends
342         return null;
343     }
344 
345     //Due date calculation - hours input
346     public Timestamp calculateDueDateHrs(String deskId, String timePeriod, Timestamp currentDate) {
347         boolean isDay = false;
348         String[] timePeriods = timePeriod.split("-");
349         String timePeriodType = timePeriods.length > 1 ? timePeriods[1] : "";
350         Timestamp destinationTimestamp = null;
351         Timestamp destinationTemp = null;
352         LoanProcessor loanProcessor = new LoanProcessor();
353         String sysFlag = loanProcessor.getParameter(OLEConstants.CALENDER_FLAG);
354         if (timePeriodType != null && timePeriodType.equalsIgnoreCase("H")) {
355             destinationTemp = DateUtil.addHours(currentDate, new Integer(timePeriods.length > 0 ? timePeriods[0] : "0"));
356         }
357         if (timePeriodType != null && timePeriodType.equalsIgnoreCase("D")) {
358             isDay =  true;
359             destinationTemp = DateUtil.addDays(currentDate, Integer.parseInt(timePeriods.length > 0 ? timePeriods[0] : "0"));
360         }
361         if (sysFlag.equalsIgnoreCase("true")) {
362             HashMap<String, String> timeMap = new HashMap<String, String>();
363             if (isHoliday(deskId, destinationTemp)) { //holiday
364                 destinationTemp = nextWorkingDay(deskId, destinationTemp);
365                 timeMap = getOpenAndCloseTime(deskId, destinationTemp);
366                 destinationTimestamp = Timestamp.valueOf(new StringBuilder().append(destinationTemp.toString().split(" ")[0]).append(" ").append(timeMap.get(OLEConstants.CALENDAR_GET_OPEN_TIME)).append(":00").toString());
367             } else {
368                 timeMap = getOpenAndCloseTime(deskId, destinationTemp);
369                 long openTime = Timestamp.valueOf(new StringBuilder().append(destinationTemp.toString().split(" ")[0]).append(" ").append(timeMap.get(OLEConstants.CALENDAR_GET_OPEN_TIME)).append(":00").toString()).getTime();
370                 long closeTime = Timestamp.valueOf(new StringBuilder().append(destinationTemp.toString().split(" ")[0]).append(" ").append(timeMap.get(OLEConstants.CALENDAR_GET_CLOSE_TIME)).append(":00").toString()).getTime();
371                 long currentTime = destinationTemp.getTime();
372                 if (currentTime < openTime) {
373                     destinationTimestamp = Timestamp.valueOf(new StringBuilder().append(destinationTemp.toString().split(" ")[0]).append(" ").append(timeMap.get(OLEConstants.CALENDAR_GET_OPEN_TIME)).append(":00").toString());
374                 } else if (currentTime >= openTime && currentTime <= closeTime) {
375                     if(isDay){
376                         destinationTimestamp = Timestamp.valueOf(new StringBuilder().append(destinationTemp.toString().split(" ")[0]).append(" ").append(timeMap.get(OLEConstants.CALENDAR_GET_CLOSE_TIME)).append(":00").toString());
377                     }else{
378                         destinationTimestamp = destinationTemp;
379                     }
380 
381                 } else {
382                     destinationTemp = nextWorkingDay(deskId, destinationTemp);
383                     timeMap = getOpenAndCloseTime(deskId, destinationTemp);
384                     destinationTimestamp = Timestamp.valueOf(new StringBuilder().append(destinationTemp.toString().split(" ")[0]).append(" ").append(timeMap.get(OLEConstants.CALENDAR_GET_OPEN_TIME)).append(":00").toString());
385                 }
386 
387             }
388         } else {
389             Long currentTimeMillis = currentDate.getTime();
390             Long loanHoursMillis = (Long.parseLong(timePeriod.split("-")[0])) * 60 * 60 * 1000;
391             Long currentDayStartTime;
392             Long currentDayEndTime;
393             Long workingMillisLeft;
394             Long tempLoanHoursMillis = loanHoursMillis;
395             if (!isHoliday(deskId, currentDate)) {
396                 HashMap<String, String> startEndTimeMap = getOpenAndCloseTime(deskId, currentDate);
397                 // currentDayStartTime = Timestamp.valueOf(new StringBuilder().append(currentDate.toString().split(" ")[0]).append(" ").append(startEndTimeMap.get(OLEConstants.CALENDAR_GET_OPEN_TIME)).append(":00").toString()).getTime();
398                 currentDayEndTime = Timestamp.valueOf(new StringBuilder().append(currentDate.toString().split(" ")[0]).append(" ").append(startEndTimeMap.get(OLEConstants.CALENDAR_GET_CLOSE_TIME)).append(":00").toString()).getTime();
399                 workingMillisLeft = currentDayEndTime - currentTimeMillis;
400                 loanHoursMillis = loanHoursMillis - workingMillisLeft;
401                 Timestamp nextDay = currentDate;
402                 if (loanHoursMillis <= 0) {
403                     nextDay = new Timestamp(currentTimeMillis + tempLoanHoursMillis);
404                 }
405                 //while ends
406                 while (loanHoursMillis > 0) {
407                     boolean holidayExceptionType = true;
408                     //   int count = 1;
409                     while (holidayExceptionType) {
410                         nextDay = DateUtil.addDays(nextDay, 1);
411                         holidayExceptionType = isHoliday(deskId, nextDay);
412                         //      count++;
413                     }
414                     startEndTimeMap = getOpenAndCloseTime(deskId, nextDay);
415                     currentDayStartTime = Timestamp.valueOf(new StringBuilder().append(nextDay.toString().split(" ")[0]).append(" ").append(startEndTimeMap.get(OLEConstants.CALENDAR_GET_OPEN_TIME)).append(":00").toString()).getTime();
416                     currentDayEndTime = Timestamp.valueOf(new StringBuilder().append(nextDay.toString().split(" ")[0]).append(" ").append(startEndTimeMap.get(OLEConstants.CALENDAR_GET_CLOSE_TIME)).append(":00").toString()).getTime();
417                     workingMillisLeft = currentDayEndTime - currentDayStartTime;
418                     tempLoanHoursMillis = loanHoursMillis;
419                     loanHoursMillis = loanHoursMillis - workingMillisLeft;
420                     if (loanHoursMillis <= 0) {
421                         nextDay = new Timestamp(currentDayStartTime + tempLoanHoursMillis);
422                     }
423                 }
424                 if (nextDay != null) {
425                     destinationTimestamp = nextDay;
426                 }
427             }
428         }
429         return destinationTimestamp;
430     }
431 
432 
433     public Float calculateFine(String deskId, Timestamp dueDate, Timestamp currentDate, String fineAmount) {
434 
435         String difference = String.valueOf(currentDate.getTime() - dueDate.getTime());
436         Long diffInt = new Long(difference);
437         Float fineRate = 0.0f;
438 
439         if (diffInt > 0) {
440 
441             if (fineAmount != null && fineAmount.contains("/")) {
442                 String[] fineCal = fineAmount.split("/");
443                 String fineAmt = fineCal[0];
444                 String fineMode = fineCal[1];
445                 LoanProcessor loanProcessor = new LoanProcessor();
446                 String sysFlag = loanProcessor.getParameter(OLEConstants.FINE_FLAG);
447                 if (fineMode != null && fineMode.equalsIgnoreCase("H")) {
448                     Integer numberOfHours = 0;
449                     if (sysFlag.equalsIgnoreCase("true")) {
450                         numberOfHours = getHoursDiff(dueDate, currentDate);
451                     } else {
452                         numberOfHours = workingHours(deskId, dueDate, currentDate);
453                     }
454                     fineRate = numberOfHours * Float.parseFloat(fineAmt);
455                 } else if (fineMode != null && fineMode.equalsIgnoreCase("D")) {
456                     workingDays(deskId, dueDate, currentDate);
457                     if (sysFlag.equalsIgnoreCase("true")) {
458                         fineRate = totalDays * Float.parseFloat(fineAmt);
459                     } else {
460                         fineRate = totalWorkingDays * Float.parseFloat(fineAmt);
461                     }
462                 }
463             }
464         }
465         return fineRate;
466 
467     }
468 
469     public Integer getHoursDiff(java.util.Date dateOne, java.util.Date dateTwo) {
470         if (dateOne != null && dateTwo != null && dateOne.compareTo(dateTwo) <= 0) {
471             String diff = "";
472             long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
473             diff = String.format("%d", TimeUnit.MILLISECONDS.toHours(timeDiff),
474                     -TimeUnit.HOURS.toMinutes(timeDiff));
475             return new Integer(diff);
476         }
477         return 0;
478     }
479 
480     public void workingDays(String deskId, Timestamp dueDate, Timestamp currentDate) {
481         totalWorkingDays = 0;
482         totalDays = 0;
483         while (dueDate.getTime() < currentDate.getTime()) {
484             if (currentDate.toString().split(" ")[0].equalsIgnoreCase(dueDate.toString().split(" ")[0])) {
485 
486                 break;
487             } else if (!currentDate.toString().split(" ")[0].equalsIgnoreCase(dueDate.toString().split(" ")[0])) {
488                 boolean isHoliday = isHoliday(deskId, dueDate);
489                 if (!isHoliday) {
490                     totalWorkingDays++;
491 
492                 }
493                 totalDays++;
494                 dueDate = DateUtil.addDays(dueDate, 1);
495             }
496         }
497     }
498 
499     public Integer workingHours(String deskId, Timestamp dueDateTime, Timestamp currentDate) {
500         float totalHours = 0;
501         float openTime;
502         float closeTime;
503         int openTimeHrs;
504         int closeTimeHrs;
505         int totalHrs = 0;
506         Timestamp dueDate=dueDateTime;
507         HashMap<String, String> timeMap = new HashMap<String, String>();
508         while (dueDate.getTime() <= currentDate.getTime() || currentDate.toString().split(" ")[0].equalsIgnoreCase(dueDate.toString().split(" ")[0])) {
509             if (currentDate.toString().split(" ")[0].equalsIgnoreCase(dueDate.toString().split(" ")[0])) {
510 
511                 timeMap = getOpenAndCloseTime(deskId, currentDate);
512                 openTime = Float.parseFloat(timeMap.get(OLEConstants.CALENDAR_GET_OPEN_TIME).split(":")[0] + "." + timeMap.get(OLEConstants.CALENDAR_GET_OPEN_TIME).split(":")[1]);
513                 openTimeHrs=Integer.parseInt(timeMap.get(OLEConstants.CALENDAR_GET_OPEN_TIME).split(":")[0]);
514 
515                 if(currentDate.toString().split(" ")[0].equalsIgnoreCase(dueDateTime.toString().split(" ")[0])){
516                     openTime =Float.parseFloat(dueDateTime.toString().split(" ")[1].split(":")[0]+"."+dueDateTime.toString().split(" ")[1].split(":")[1]);
517                     openTimeHrs=Integer.parseInt(dueDateTime.toString().split(" ")[1].split(":")[0]);
518                 }
519                 float openTimeTemp=Float.parseFloat(currentDate.toString().split(" ")[1].split(":")[0]+"."+currentDate.toString().split(" ")[1].split(":")[1]);
520                 int openTimeHrsTemp=Integer.parseInt(currentDate.toString().split(" ")[1].split(":")[0]);
521 
522                 totalHours = totalHours + (openTimeTemp-openTime);
523                 totalHrs = totalHrs + (openTimeHrsTemp-openTimeHrs);
524                 break;
525             } else if (!currentDate.toString().split(" ")[0].equalsIgnoreCase(dueDate.toString().split(" ")[0])) {
526                 boolean isHoliday = isHoliday(deskId, dueDate);
527                 if (!isHoliday) {
528                     timeMap = getOpenAndCloseTime(deskId, dueDate);
529                     openTime = Float.parseFloat(timeMap.get(OLEConstants.CALENDAR_GET_OPEN_TIME).split(":")[0] + "." + timeMap.get(OLEConstants.CALENDAR_GET_OPEN_TIME).split(":")[1]);
530                     closeTime = Float.parseFloat(timeMap.get(OLEConstants.CALENDAR_GET_CLOSE_TIME).split(":")[0] + "." + timeMap.get(OLEConstants.CALENDAR_GET_CLOSE_TIME).split(":")[1]);
531                     closeTimeHrs=Integer.parseInt(timeMap.get(OLEConstants.CALENDAR_GET_CLOSE_TIME).split(":")[0]);
532                     openTimeHrs=Integer.parseInt(timeMap.get(OLEConstants.CALENDAR_GET_OPEN_TIME).split(":")[0]);
533                     if(dueDate.equals(dueDateTime)){
534                         Float currentTime=Float.parseFloat(dueDateTime.toString().split(" ")[1].split(":")[0]+"."+dueDateTime.toString().split(" ")[1].split(":")[1]);
535                         if(currentTime>openTime){
536                             openTime=currentTime;
537                         }
538                         int currentTimeHrs=Integer.parseInt(dueDateTime.toString().split(" ")[1].split(":")[0]);
539                         if(currentTimeHrs>openTimeHrs){
540                             openTimeHrs=currentTimeHrs;
541                         }
542                     }
543                     totalHours = totalHours + (closeTime - openTime);
544                     totalHrs = totalHrs + (closeTimeHrs - openTimeHrs );
545                 }
546                 dueDate = DateUtil.addDays(dueDate, 1);
547             }
548         }
549 
550 
551         return totalHrs;
552 
553     }
554 
555 
556     //Open and Close Time of a Date
557     public HashMap getOpenAndCloseTime(String deskId, Timestamp currentDate) {
558         HashMap<String, String> timeMap = new HashMap<String, String>();
559         OleCalendarGroup ocg = getCalendarGroup(deskId);
560         OleCalendar calendar = getActiveCalendar(currentDate, ocg.getCalendarGroupId());
561 
562 
563         List<OleCalendarWeek> oleCalendarWeekList = calendar.getOleCalendarWeekList();
564         List<OleCalendarExceptionDate> oleCalendarExceptionDates = calendar.getOleCalendarExceptionDateList();
565         List<OleCalendarExceptionPeriod> oleCalendarExceptionPeriodList = calendar.getOleCalendarExceptionPeriodList();
566 
567         boolean temp = true;
568         if (timeMap.size() == 0) {
569             for (OleCalendarExceptionDate exceptionDate : oleCalendarExceptionDates) {
570                 if (currentDate.toString().split(" ")[0].equals(exceptionDate.getDate().toString().split(" ")[0])) {
571                     timeMap.put(OLEConstants.CALENDAR_GET_OPEN_TIME, exceptionDate.getOpenTime());
572                     timeMap.put(OLEConstants.CALENDAR_GET_CLOSE_TIME, convertTimeInto24HrFormat(exceptionDate.getCloseTime()));
573                     temp = false;
574                     break;
575                 }
576             }
577         }
578         boolean exceptionPeriodFlag=true;
579         if (timeMap.size() == 0) {
580             for (OleCalendarExceptionPeriod exceptionPeriod : oleCalendarExceptionPeriodList) {
581                 if (currentDate.getTime() >= exceptionPeriod.getBeginDate().getTime() && currentDate.getTime() <= exceptionPeriod.getEndDate().getTime()) {
582                     exceptionPeriodFlag=false;
583                     List<OleCalendarExceptionPeriodWeek> oleCalendarExceptionPeriodWeekList = exceptionPeriod.getOleCalendarExceptionPeriodWeekList();
584                     for (OleCalendarExceptionPeriodWeek exceptionPeriodWeek : oleCalendarExceptionPeriodWeekList) {
585                         int startDay = new Integer(exceptionPeriodWeek.getStartDay());
586                         int endDay = new Integer(exceptionPeriodWeek.getEndDay());
587                         //if(!exceptionPeriodWeek.isEachDayOfExceptionWeek()){
588                             if (startDay > endDay) {
589                                 if ((currentDate.getDay() >= startDay && currentDate.getDay() <= 6) || (currentDate.getDay() >= 0 && currentDate.getDay() <= endDay)) {
590                                     timeMap.put(OLEConstants.CALENDAR_GET_OPEN_TIME, exceptionPeriodWeek.getOpenTime());
591                                     timeMap.put(OLEConstants.CALENDAR_GET_CLOSE_TIME, convertTimeInto24HrFormat(exceptionPeriodWeek.getCloseTime()));
592                                     temp = false;
593                                     break;
594                                 }
595                             } else {
596                                 if ((currentDate.getDay() >= startDay && currentDate.getDay() <= endDay)) {
597                                     timeMap.put(OLEConstants.CALENDAR_GET_OPEN_TIME, exceptionPeriodWeek.getOpenTime());
598                                     timeMap.put(OLEConstants.CALENDAR_GET_CLOSE_TIME, convertTimeInto24HrFormat(exceptionPeriodWeek.getCloseTime()));
599                                     temp = false;
600                                     break;
601                                 }
602                             }  //end of else
603                     }
604                 }
605 
606 
607             }
608         }
609         if (timeMap.size() == 0 && exceptionPeriodFlag) {
610             for (OleCalendarWeek week : oleCalendarWeekList) {
611                 int startDay = new Integer(week.getStartDay());
612                 int endDay = new Integer(week.getEndDay());
613                 if (startDay > endDay) {
614                     if ((currentDate.getDay() >= startDay && currentDate.getDay() <= 6) || (currentDate.getDay() >= 0 && currentDate.getDay() <= endDay)) {
615                         timeMap.put(OLEConstants.CALENDAR_GET_OPEN_TIME, week.getOpenTime());
616                         timeMap.put(OLEConstants.CALENDAR_GET_CLOSE_TIME, convertTimeInto24HrFormat(week.getCloseTime()));
617                         temp = false;
618                         break;
619                     }
620                 } else {
621                     if ((currentDate.getDay() >= startDay && currentDate.getDay() <= endDay)) {
622                         timeMap.put(OLEConstants.CALENDAR_GET_OPEN_TIME, week.getOpenTime());
623                         timeMap.put(OLEConstants.CALENDAR_GET_CLOSE_TIME, convertTimeInto24HrFormat(week.getCloseTime()));
624                         temp = false;
625                         break;
626                     }
627                 }  //end of else
628             }
629         }
630 
631 
632         return timeMap;
633     }
634     //Open and Close Time of a Date
635 
636     private String convertTimeInto24HrFormat(String time){
637         StringBuffer newTime = new StringBuffer();
638         String[] times = time.split(":");
639         if(times!=null && times.length>1 && Integer.parseInt(times[0])==0){
640                 newTime.append("24:"+times[1]);
641         } else{
642                newTime.append(time);
643         }
644         return newTime.toString();
645     }
646     public void generalInfoValidation(OleCalendar oleCalendar, boolean isNew) {
647         List<OleCalendarWeek> oleCalendarWeekList = oleCalendar.getOleCalendarWeekList();
648 
649         //millitary format conversion
650 
651         DateUtil dateUtil = new DateUtil();
652 
653         if (oleCalendar.getCalendarGroupId().equals("")) {
654             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, "temp.group.name");//"oleCalendarGroupId"
655         }
656 
657 
658         if (oleCalendar.getOleCalendarWeekList().size() > 0) {
659 
660             for (OleCalendarWeek oleCalendarWeek : oleCalendarWeekList) {
661                 if (oleCalendarWeek.getOpenTime().equals("")) {
662                     GlobalVariables.getMessageMap().putErrorForSectionId("GeneralInfo", "temp.week.open.time");
663                 }
664                 if (oleCalendarWeek.getCloseTime().equals("")) {
665                     GlobalVariables.getMessageMap().putErrorForSectionId("GeneralInfo", "temp.week.close.time");
666                 }
667             }
668 
669             boolean repeat = false;
670 
671             List<Integer> dest = new ArrayList<Integer>();
672             for (int i = 0; i < oleCalendarWeekList.size(); i++) {
673                 for (int x = new Integer(oleCalendarWeekList.get(i).getStartDay()); x <= new Integer(oleCalendarWeekList.get(i).getEndDay()); x++) {
674                     if (dest.size() > 0) {
675                         if (dest.contains(x)) {
676                             repeat = true;
677                             break;
678                         } else {
679                             dest.add(x);
680                         }
681                     } else {
682                         dest.add(x);
683                     }
684 
685                 }
686 
687 
688             }
689             if (repeat) {
690                 GlobalVariables.getMessageMap().putErrorForSectionId("GeneralInfo", "temp.weak.days.overlap");
691 
692             }
693 
694 //            To set exception period end date (concat with 23:59:59)
695             String excptEndDateSplit = null;
696             for (OleCalendarExceptionPeriod oleCalendarExceptionPeriod : oleCalendar.getOleCalendarExceptionPeriodList()) {
697                 if (oleCalendarExceptionPeriod.getEndDate()!=null && oleCalendarExceptionPeriod.getEndDate().toString().contains(" ")) {
698                     excptEndDateSplit = oleCalendarExceptionPeriod.getEndDate().toString().split(" ")[0];
699                     oleCalendarExceptionPeriod.setEndDate(Timestamp.valueOf(excptEndDateSplit + " " + "23:59:59"));
700                 }
701             }
702 //            To set exception period end date (concat with 23:59:59)
703 
704 
705             String openTimePeriodWeek = "";
706             String closeTimePeriodWeek = "";
707             String openTimeConvExceptionDate = "";
708             String closeTimeConvExceptionDate = "";
709 
710             for (OleCalendarWeek oleCalendarWeek : oleCalendar.getOleCalendarWeekList()) {
711                 if (oleCalendarWeek.getOpenTime().equals("") || oleCalendarWeek.getCloseTime().equals("")) {
712                     break;
713                 }
714                 String openTimeWeek = oleCalendarWeek.getOpenTime() + oleCalendarWeek.getOpenTimeSession();
715                 String closeTimeWeek = oleCalendarWeek.getCloseTime() + oleCalendarWeek.getCloseTimeSession();
716                 String openTime = "";
717                 String closeTime = "";
718 
719                 try {
720                     openTime = dateUtil.convertTo24HoursFormat(openTimeWeek);
721                     closeTime = dateUtil.convertTo24HoursFormat(closeTimeWeek);
722                 } catch (java.text.ParseException e) {
723                     LOG.error("Exception while converting to 12Hours format", e);
724                 }
725 
726                 oleCalendarWeek.setOpenTime(openTime);
727                 oleCalendarWeek.setCloseTime(closeTime);
728             }
729 
730             for (OleCalendarExceptionPeriod oleCalendarExceptionPeriod : oleCalendar.getOleCalendarExceptionPeriodList()) {
731                 for (OleCalendarExceptionPeriodWeek oleCalendarExceptionPeriodWeek : oleCalendarExceptionPeriod.getOleCalendarExceptionPeriodWeekList()) {
732                     if(!oleCalendarExceptionPeriodWeek.getOpenTime().equals("")&& oleCalendarExceptionPeriodWeek.getOpenTime()!=null && !oleCalendarExceptionPeriodWeek.getCloseTime().equals("") && oleCalendarExceptionPeriodWeek.getCloseTime()!=null){
733                         String openTimeExceptionPeriodWeek = oleCalendarExceptionPeriodWeek.getOpenTime() + oleCalendarExceptionPeriodWeek.getOpenTimeSession();
734                         String closeTimeExceptionPeriodWeek = oleCalendarExceptionPeriodWeek.getCloseTime() + oleCalendarExceptionPeriodWeek.getCloseTimeSession();
735                         try {
736                             openTimePeriodWeek = dateUtil.convertTo24HoursFormat(openTimeExceptionPeriodWeek);
737                             closeTimePeriodWeek = dateUtil.convertTo24HoursFormat(closeTimeExceptionPeriodWeek);
738                         } catch (java.text.ParseException e) {
739                             LOG.error("Exception while converting to 12Hours format", e);
740                         }
741                         oleCalendarExceptionPeriodWeek.setOpenTime(openTimePeriodWeek);
742                         oleCalendarExceptionPeriodWeek.setCloseTime(closeTimePeriodWeek);
743                     }
744 
745                 }
746             }
747 
748             for (OleCalendarExceptionDate oleCalendarExceptionDate : oleCalendar.getOleCalendarExceptionDateList()) {
749 
750                 if ((!oleCalendarExceptionDate.getOpenTime().equals("")) && (!oleCalendarExceptionDate.getCloseTime().equals(""))) {
751                     String openTimeExceptionDate = oleCalendarExceptionDate.getOpenTime() + oleCalendarExceptionDate.getOpenTimeSession();
752                     String closeTimeExceptionDate = oleCalendarExceptionDate.getCloseTime() + oleCalendarExceptionDate.getCloseTimeSession();
753                     try {
754                         openTimeConvExceptionDate = dateUtil.convertTo24HoursFormat(openTimeExceptionDate);
755                         closeTimeConvExceptionDate = dateUtil.convertTo24HoursFormat(closeTimeExceptionDate);
756                     } catch (java.text.ParseException e) {
757                         LOG.error("Exception while converting to 12Hours format", e);
758                     }
759                     if (!oleCalendarExceptionDate.getExceptionType().equals("Holiday")) {  //changed
760                         oleCalendarExceptionDate.setOpenTime(openTimeConvExceptionDate);
761                         oleCalendarExceptionDate.setCloseTime(closeTimeConvExceptionDate);
762 
763                     }
764                 }
765 
766             }
767 
768             if (isNew) {
769                 String groupId1 = oleCalendar.getCalendarGroupId();
770                 Map map1 = new HashMap();
771                 map1.put("calendarGroupId", groupId1);
772                 Collection<OleCalendar> calendars1 = getBusinessObjectService().findMatching(OleCalendar.class, map1);
773                 Integer latestCalChrSeqNo = 0;
774                 Integer maxChrSeqNo = 0;
775                 for (OleCalendar calendar1 : calendars1) {
776                     if (calendar1.getChronologicalSequence() != null) {
777                         latestCalChrSeqNo = Integer.parseInt(calendar1.getChronologicalSequence());
778                         maxChrSeqNo = Math.max(maxChrSeqNo, latestCalChrSeqNo);
779                     }
780                 }
781 
782                 map1.put("chronologicalSequence", maxChrSeqNo.toString());
783                 List<OleCalendar> calendars2 = (List<OleCalendar>) getBusinessObjectService().findMatching(OleCalendar.class, map1);
784                 OleCalendar lastCalendar = calendars2 != null && calendars2.size() > 0 ? calendars2.get(0) : null;
785                 if (oleCalendar != null && oleCalendar.getBeginDate() != null && lastCalendar != null && lastCalendar.getBeginDate() != null &&
786                         oleCalendar.getBeginDate().compareTo(lastCalendar.getBeginDate()) <= 0) {
787                     GlobalVariables.getMessageMap().putErrorForSectionId("GeneralInfo", "temp.strt.end.date.new.cal");
788                 }
789             }
790         }
791         /*chronologicalSequence strt*/
792         Map<String, String> criteria = new HashMap<String, String>();
793         int seq = 0;
794 
795         criteria.put("calendarGroupId", oleCalendar.getCalendarGroupId());
796         List<OleCalendar> oleCalendarGroups = (List<OleCalendar>) getBusinessObjectService().findMatching(OleCalendar.class, criteria);
797         if (oleCalendarGroups.size() > 0) {
798             //int high=0;
799             for (int i = 0; i < oleCalendarGroups.size(); i++) {
800                 if (seq == 0) {
801                     seq = new Integer(oleCalendarGroups.get(i).getChronologicalSequence());
802                 } else {
803                     if (seq < new Integer(oleCalendarGroups.get(i).getChronologicalSequence())) {
804                         seq = new Integer(oleCalendarGroups.get(i).getChronologicalSequence());
805                     }
806                 }
807             }
808             if(oleCalendar.getChronologicalSequence()== null || oleCalendar.getChronologicalSequence().equals("")){
809                 oleCalendar.setChronologicalSequence(new StringBuilder().append(seq + 1).toString());
810             }
811 
812         } else {
813 
814             oleCalendar.setChronologicalSequence(new StringBuilder().append(seq + 1).toString());
815         }
816         /*chronologicalSequence end*/
817     }
818 
819     public BusinessObjectService getBusinessObjectService() {
820         if (this.businessObjectService == null) {
821             this.businessObjectService = KRADServiceLocator.getBusinessObjectService();
822         }
823         return this.businessObjectService;
824     }
825 
826     public void assignEndDate(OleCalendar oleCalendar) {
827 
828         if (GlobalVariables.getMessageMap().getErrorMessages().isEmpty()) {
829 
830             //*-*-*-Setting End-Date of a calendar while adding a new Calendar*-*-*-
831             Date endDate = DateUtil.addDays(oleCalendar.getBeginDate(), -1);
832             String groupId = oleCalendar.getCalendarGroupId();
833             Map map = new HashMap();
834             map.put("calendarGroupId", groupId);
835             Collection<OleCalendar> calendars = getBusinessObjectService().findMatching(OleCalendar.class, map);
836             Integer latestCalChrSeqNo = 0;
837             Integer maxChrSeqNo = 0;
838 
839             for (OleCalendar calendar1 : calendars) {
840                 if (calendar1.getChronologicalSequence() != null) {
841                     latestCalChrSeqNo = Integer.parseInt(calendar1.getChronologicalSequence());
842                     maxChrSeqNo = Math.max(maxChrSeqNo, latestCalChrSeqNo);
843                 }
844             }
845 
846             for (OleCalendar calendar : calendars) {
847                 if (oleCalendar.getCalendarId() != null && oleCalendar.getCalendarId().equals(calendar.getCalendarId())) {
848                     break;
849                 }
850                 if ((oleCalendar.getCalendarId() == null || !oleCalendar.getCalendarId().equals(calendar.getCalendarId()))
851                         && calendar.getCalendarGroupId().equalsIgnoreCase(groupId) && (maxChrSeqNo==0 || maxChrSeqNo==Integer.parseInt(calendar.getChronologicalSequence()))) {
852                     String endDateSplit = null;
853                     if (endDate != null) {
854                         if (endDate.toString().contains(" ")) {
855 
856                             endDateSplit = endDate.toString().split(" ")[0];
857                         }
858 
859 
860                     }
861                     calendar.setEndDate(Timestamp.valueOf(endDateSplit +" "+ "23:59:59"));
862                     getBusinessObjectService().save(calendar);
863                 }
864             }
865         }
866     }
867 
868     public void deleteCalendarCollections(OleCalendar oleCalendar){
869         //general
870         List<OleCalendarWeek> oleCalendarWeekList = oleCalendar.getOleCalendarWeekDeleteList();
871         for(OleCalendarWeek oleCalendarWeek:oleCalendarWeekList){
872             Map<String,String> map=new HashMap<String,String>();
873             map.put("calendarId",oleCalendarWeek.getCalendarId());
874             KRADServiceLocator.getBusinessObjectService().deleteMatching(OleCalendarWeek.class,map);
875         }
876         //exception day
877         List<OleCalendarExceptionDate> oleCalendarExceptionDateDeleteList = oleCalendar.getOleCalendarExceptionDateDeleteList();
878         for(OleCalendarExceptionDate oleCalendarExceptionDateList:oleCalendarExceptionDateDeleteList){
879             Map<String,String> map=new HashMap<String,String>();
880             map.put("calendarExceptionDateId",oleCalendarExceptionDateList.getCalendarExceptionDateId());
881             KRADServiceLocator.getBusinessObjectService().deleteMatching(OleCalendarExceptionDate.class,map);
882         }
883         //exception period week list
884         List<OleCalendarExceptionPeriodWeek> oleCalendarExceptionPeriodDeleteWeekList = oleCalendar.getOleCalendarExceptionPeriodDeleteWeekList();
885         for(OleCalendarExceptionPeriodWeek oleCalendarExceptionPeriodWeek:oleCalendarExceptionPeriodDeleteWeekList){
886             Map<String,String> map=new HashMap<String,String>();
887             map.put("calendarWeekId",oleCalendarExceptionPeriodWeek.getCalendarWeekId());
888             KRADServiceLocator.getBusinessObjectService().deleteMatching(OleCalendarExceptionPeriodWeek.class,map);
889         }
890         //exception period list
891         List<OleCalendarExceptionPeriod> oleCalendarExceptionPeriodDeleteList = oleCalendar.getOleCalendarExceptionPeriodDeleteList();
892         for(OleCalendarExceptionPeriod oleCalendarExceptionPeriod:oleCalendarExceptionPeriodDeleteList){
893             Map<String,String> map=new HashMap<String,String>();
894             map.put("calendarExceptionPeriodId",oleCalendarExceptionPeriod.getCalendarExceptionPeriodId());
895             KRADServiceLocator.getBusinessObjectService().deleteMatching(OleCalendarExceptionPeriod.class,map);
896         }
897     }
898 
899     public void convert12HrsFormat(OleCalendar oldCalendar) {
900         DateUtil dateUtil = new DateUtil();
901         for (OleCalendarWeek oldCalendarWeek : oldCalendar.getOleCalendarWeekList()) {
902 
903             if (oldCalendarWeek.getOpenTime().equals("") || oldCalendarWeek.getCloseTime().equals("")) {
904                 break;
905             }
906             String convertedOpenTime = "";
907             String convertCloseTime = "";
908             String oleOpenTime = oldCalendarWeek.getOpenTime();
909             String oleCloseTime = oldCalendarWeek.getCloseTime();
910             try {
911                 convertedOpenTime = dateUtil.convertTo12HoursFormat(oleOpenTime);
912                 convertCloseTime = dateUtil.convertTo12HoursFormat(oleCloseTime);
913             } catch (java.text.ParseException e) {
914                 LOG.error("Exception while converting to 12Hours format", e);
915             }
916 
917             if (convertedOpenTime != null) {
918                 oldCalendarWeek.setOpenTime(convertedOpenTime.substring(0, convertedOpenTime.length() - 2));
919                 //oldCalendarWeek.setOpenTimeSession(convertedOpenTime.substring(convertedOpenTime.length() - 2, convertedOpenTime.length()));
920             }
921             if (convertCloseTime != null) {
922                 // oldCalendarWeek.setCloseTimeSession(convertCloseTime.substring(convertCloseTime.length() - 2, convertCloseTime.length()));
923                 oldCalendarWeek.setCloseTime(convertCloseTime.substring(0, convertCloseTime.length() - 2));
924             }
925         }
926 
927 
928         for (OleCalendarExceptionPeriod oleCalendarExceptionPeriod : oldCalendar.getOleCalendarExceptionPeriodList()) {
929             //changed
930             for (OleCalendarExceptionPeriodWeek oleCalendarExceptionPeriodWeek : oleCalendarExceptionPeriod.getOleCalendarExceptionPeriodWeekList()) {
931                 if(!oleCalendarExceptionPeriodWeek.getOpenTime().equals("")&& oleCalendarExceptionPeriodWeek.getOpenTime()!=null && !oleCalendarExceptionPeriodWeek.getCloseTime().equals("") && oleCalendarExceptionPeriodWeek.getCloseTime()!=null){
932                     String convOpenTimeExcptPrdWk = "";
933                     String convCloseTimeExcptPrdWk = "";
934                     String oleOpenTimeExcptPrdWk = oleCalendarExceptionPeriodWeek.getOpenTime();
935                     String oleCloseTimeExcptPrdWk = oleCalendarExceptionPeriodWeek.getCloseTime();
936                     try {
937                         convOpenTimeExcptPrdWk = dateUtil.convertTo12HoursFormat(oleOpenTimeExcptPrdWk);
938                         convCloseTimeExcptPrdWk = dateUtil.convertTo12HoursFormat(oleCloseTimeExcptPrdWk);
939                     } catch (java.text.ParseException e) {
940                         LOG.error("Exception while converting to 12Hours format", e);
941                     }
942 
943                     if (convOpenTimeExcptPrdWk != null) {
944                         oleCalendarExceptionPeriodWeek.setOpenTime(convOpenTimeExcptPrdWk.substring(0, convOpenTimeExcptPrdWk.length() - 2));
945                         //oleCalendarExceptionPeriodWeek.setOpenTimeSession(convOpenTimeExcptPrdWk.substring(convOpenTimeExcptPrdWk.length() - 2, convOpenTimeExcptPrdWk.length()));
946                     }
947                     if (convCloseTimeExcptPrdWk != null) {
948                         // oleCalendarExceptionPeriodWeek.setCloseTimeSession(convCloseTimeExcptPrdWk.substring(convCloseTimeExcptPrdWk.length() - 2, convCloseTimeExcptPrdWk.length()));
949                         oleCalendarExceptionPeriodWeek.setCloseTime(convCloseTimeExcptPrdWk.substring(0, convCloseTimeExcptPrdWk.length() - 2));
950                     }
951                 }
952 
953             }
954         }
955 
956 
957         for (OleCalendarExceptionDate oleCalendarExceptionDate : oldCalendar.getOleCalendarExceptionDateList()) {
958             String convertedOpenTimeExcptDate = "";
959             String convertedCloseTimeExcptDate = "";
960             String oleOpenTimeExcptDate = oleCalendarExceptionDate.getOpenTime();
961             String oleCloseTimeExcptDate = oleCalendarExceptionDate.getCloseTime();
962             if ((!oleCalendarExceptionDate.getOpenTime().equals("")) && (!oleCalendarExceptionDate.getCloseTime().equals(""))) { //changed
963                 try {
964                     convertedOpenTimeExcptDate = dateUtil.convertTo12HoursFormat(oleOpenTimeExcptDate);
965                     convertedCloseTimeExcptDate = dateUtil.convertTo12HoursFormat(oleCloseTimeExcptDate);
966                 } catch (java.text.ParseException e) {
967                     LOG.error("Exception while converting to 12Hours format", e);  //To change body of catch statement use File | Settings | File Templates.
968                 }
969 
970                 if (convertedOpenTimeExcptDate != null) {
971                     oleCalendarExceptionDate.setOpenTime(convertedOpenTimeExcptDate.substring(0, convertedOpenTimeExcptDate.length() - 2));
972                     //  oleCalendarExceptionDate.setOpenTimeSession(convertedOpenTimeExcptDate.substring(convertedOpenTimeExcptDate.length() - 2, convertedOpenTimeExcptDate.length()));
973                 }
974                 if (convertedCloseTimeExcptDate != null) {
975                     //  oleCalendarExceptionDate.setCloseTimeSession(convertedCloseTimeExcptDate.substring(convertedCloseTimeExcptDate.length() - 2, convertedCloseTimeExcptDate.length()));
976                     oleCalendarExceptionDate.setCloseTime(convertedCloseTimeExcptDate.substring(0, convertedCloseTimeExcptDate.length() - 2));
977                 }
978             }
979         }
980     }
981 
982     public void validateCalendarDocument(OleCalendar oleCalendar){
983         oleCalendar.setEndDateNoMessage(null);
984         oleCalendar.setEndDateYesMessage(null);
985         oleCalendar.setEndDateYesFlag(false);
986         oleCalendar.setEndDateNoFlag(false);
987         oleCalendar.setMessage("");
988         OleCalendar calendar = null;
989         String groupId = oleCalendar.getCalendarGroupId();
990         Map map = new HashMap();
991         map.put("calendarGroupId", groupId);
992         Collection<OleCalendar> calendars = getBusinessObjectService().findMatching(OleCalendar.class, map);
993         int cseq = 0;
994         for (OleCalendar oleCalendar1 : calendars) {
995             if (cseq < Integer.parseInt(oleCalendar1.getChronologicalSequence())) {
996                 cseq = Integer.parseInt(oleCalendar1.getChronologicalSequence());
997             }
998         }
999         if (oleCalendar.getCalendarId() != null && !oleCalendar.getCalendarId().equals("") && oleCalendar.getChronologicalSequence() != null && !oleCalendar.getChronologicalSequence().equals("")) { //for edit
1000             Map calMap = new HashMap();
1001             calMap.put("calendarId", oleCalendar.getCalendarId());
1002             calendar = getBusinessObjectService().findByPrimaryKey(OleCalendar.class, calMap);
1003             String beginDate = null;
1004             String beginDateDataBase = null;
1005             String endDate = null;
1006             String endDateDataBase = null;
1007             if (cseq != Integer.parseInt(oleCalendar.getChronologicalSequence())) {
1008                 if (oleCalendar.getEndDate() != null && oleCalendar.getEndDate().toString().contains(" ")) {
1009                     endDate = oleCalendar.getEndDate().toString().split(" ")[0];
1010                 }
1011                 if (oleCalendar.getBeginDate() != null && oleCalendar.getBeginDate().toString().contains(" ")) {
1012                     beginDate = oleCalendar.getBeginDate().toString().split(" ")[0];
1013                 }
1014                 if (calendar.getEndDate() != null && calendar.getEndDate().toString().contains(" ")) {
1015                     endDateDataBase = calendar.getEndDate().toString().split(" ")[0];
1016                 }
1017                 if (calendar.getBeginDate() != null && calendar.getBeginDate().toString().contains(" ")) {
1018                     beginDateDataBase = calendar.getBeginDate().toString().split(" ")[0];
1019                 }
1020                 if (beginDate != null && beginDateDataBase != null && !beginDate.equals(beginDateDataBase)) {
1021                     oleCalendar.setMessage("This Calendar End-Date cannot be edited");
1022                 }
1023                 if (endDate != null && endDateDataBase != null && !endDate.equals(endDateDataBase)) {
1024                     oleCalendar.setMessage("This Calendar End-Date cannot be edited");
1025                 }
1026             }
1027             if (oleCalendar.getMessage().equals("This Calendar End-Date cannot be edited")) {
1028 
1029                 oleCalendar.setBeginDate(calendar.getBeginDate());
1030                 oleCalendar.setEndDate(calendar.getEndDate());
1031 
1032             }
1033         }
1034 
1035         if (oleCalendar.getChronologicalSequence() != null && !oleCalendar.getChronologicalSequence().equals("") && oleCalendar.getEndDate()!=null) {
1036             if (cseq == 0 || cseq == Integer.parseInt(oleCalendar.getChronologicalSequence())) {
1037                 oleCalendar.setEndDateYesMessage("By entering an End Date this calendar will become inactive and there is no future calendar.");
1038                 oleCalendar.setEndDateYesFlag(true);
1039             }
1040         }
1041         if (oleCalendar.getChronologicalSequence().equals("")&& oleCalendar.getEndDate()!=null) {
1042             oleCalendar.setEndDateYesMessage("By entering an End Date this calendar will become inactive and there is no future calendar.");
1043             oleCalendar.setEndDateYesFlag(true);
1044         }
1045 
1046         if (calendars.size() >= 1 && (oleCalendar.getCalendarId() == null || oleCalendar.getCalendarId().equals(""))) {
1047             oleCalendar.setEndDateNoMessage("The Previous calendars End-Date will be changed to ensure no gaps are created.");
1048             oleCalendar.setEndDateNoFlag(true);
1049         }
1050     }
1051 
1052     public void deleteCalendarDocument(OleCalendar oleCalendar){
1053         OleCalendar prevCalendar = null;
1054         OleCalendar nextCalendar = null;
1055         try {
1056             if (oleCalendar != null && oleCalendar.getCalendarId() != null) {
1057                 Map map = new HashMap<>();
1058                 map.put("calendarGroupId", oleCalendar.getCalendarGroupId());
1059                 List<OleCalendar> calendars = (List<OleCalendar>) getBusinessObjectService().findMatchingOrderBy(OleCalendar.class, map, "CL_SEQ", true);
1060                 for (int i = 0; i < calendars.size(); i++) {
1061                     OleCalendar calendar = calendars.get(i);
1062                     if (calendar.getChronologicalSequence().equalsIgnoreCase(oleCalendar.getChronologicalSequence())) {
1063 
1064                         prevCalendar = (i - 1) >= 0 ? calendars.get(i - 1) : null;
1065                         nextCalendar = (i + 1) < calendars.size() ? calendars.get(i + 1) : null;
1066                         if (nextCalendar == null && prevCalendar != null) {
1067                             prevCalendar.setEndDate(null);
1068                             getBusinessObjectService().save(prevCalendar);
1069                         } else if (nextCalendar != null && prevCalendar != null) {
1070                             Timestamp prevCalNewEndDate = DateUtil.addDays(nextCalendar.getBeginDate(),-1);
1071                             prevCalendar.setEndDate(prevCalNewEndDate);
1072                             getBusinessObjectService().save(prevCalendar);
1073                         }
1074 
1075                     }
1076                 }
1077                 getBusinessObjectService().delete(oleCalendar);
1078             } else {
1079                 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_MESSAGES, OLEConstants.OlePatron.ERROR_PATRON_NOT_FOUND);
1080             }
1081         } catch (Exception ex) {
1082             LOG.error(ex.getMessage(), ex);
1083             throw new RuntimeException();
1084         }
1085     }
1086 }