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