View Javadoc
1   package org.kuali.ole.deliver.drools;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.OLEParameterConstants;
5   import org.kuali.ole.deliver.bo.OleFixedDateTimeSpan;
6   import org.kuali.ole.deliver.bo.OleFixedDueDate;
7   import org.kuali.ole.deliver.calendar.service.OleCalendarService;
8   import org.kuali.ole.deliver.calendar.service.impl.OleCalendarServiceImpl;
9   import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
10  import org.kuali.rice.coreservice.api.parameter.Parameter;
11  import org.kuali.rice.coreservice.api.parameter.ParameterKey;
12  import org.kuali.rice.krad.service.KRADServiceLocator;
13  
14  import java.sql.Timestamp;
15  import java.text.SimpleDateFormat;
16  import java.util.Date;
17  import java.util.HashMap;
18  import java.util.List;
19  import java.util.Map;
20  
21  /**
22   * Created by pvsubrah on 3/19/15.
23   */
24  public class LoanPeriodUtil {
25      public Timestamp calculateDueDate(String loanPeriod, String ruleName, String calendarGroupId){
26          Timestamp dueDate = null;
27  
28          if(loanPeriod!=null && loanPeriod.equalsIgnoreCase(OLEConstants.FIXED_DUE_DATE)){
29              java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
30              List<OleFixedDueDate> oleFixedDueDates =  getFixedDueDateBasedOnPolicySet(ruleName);
31              if(oleFixedDueDates!=null && oleFixedDueDates.size()>0){
32                  for(OleFixedDueDate oleFixedDueDate : oleFixedDueDates){
33                      List<OleFixedDateTimeSpan> oleFixedDateTimeSpans = oleFixedDueDate.getOleFixedDateTimeSpanList();
34                      for(OleFixedDateTimeSpan oleFixedDateTimeSpan : oleFixedDateTimeSpans){
35                          if(dateComparison(date,oleFixedDateTimeSpan.getFromDueDate(),oleFixedDateTimeSpan.getToDueDate())){
36                              String defaultCloseTime = getParameter(OLEParameterConstants.DEF_CLOSE_TIME);
37                              dueDate = Timestamp.valueOf(new SimpleDateFormat(OLEConstants.CHECK_IN_DATE_TIME_FORMAT).
38                                      format(oleFixedDateTimeSpan.getFixedDueDate()).concat(" ").concat(defaultCloseTime));
39                              break;
40                          }
41                      }
42                  }
43              }
44          } else{
45              //   loanPeriod = renewalCheck(loanPeriod);
46              OleCalendarService oleCalendarService = new OleCalendarServiceImpl();
47              String[] periodType=null;
48              if(loanPeriod != null && !loanPeriod.equalsIgnoreCase("")){
49                  periodType = loanPeriod.split("-");
50              }
51              String dayOrHours =periodType!=null && periodType.length > 1 ? periodType[1] : null;
52              if(dayOrHours != null && dayOrHours.equalsIgnoreCase("H")){
53                  dueDate = loanPeriod!=null ? oleCalendarService.calculateDueDateHrs(calendarGroupId,loanPeriod,new Timestamp(System.currentTimeMillis())) : null;
54              } else if(dayOrHours != null && dayOrHours.equalsIgnoreCase("D")){
55                  String sysFlag = getParameter(OLEConstants.CALENDER_FLAG);
56                  if(sysFlag.equalsIgnoreCase("true")){
57                      dueDate = loanPeriod!=null ? oleCalendarService.calculateDueDateHrs(calendarGroupId, loanPeriod, new Timestamp(System.currentTimeMillis())) : null;
58                  } else {
59                      dueDate = loanPeriod!=null ?oleCalendarService.calculateDueDate(calendarGroupId,loanPeriod,new Timestamp(System.currentTimeMillis())) : null;
60                  }
61              }
62  
63          }
64  
65          return dueDate;
66      }
67  
68      private List<OleFixedDueDate> getFixedDueDateBasedOnPolicySet(String ruleName){
69          Map<String,String> policySet = new HashMap<>();
70          policySet.put("circulationPolicySetId",ruleName);
71          List<OleFixedDueDate> oleFixedDueDates =  (List<OleFixedDueDate>) KRADServiceLocator.getBusinessObjectService().findMatching(OleFixedDueDate.class,policySet);
72          return oleFixedDueDates;
73      }
74  
75      private boolean dateComparison(Date date1,Date date2,Date date3){
76          if(date1!=null && date2!=null && date3!=null){
77              SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
78              String dateString1 = dateFormat.format(date1);
79              String dateString2 = dateFormat.format(date2);
80              String dateString3 = dateFormat.format(date3);
81              if((date1.after(date2) && date1.before(date3)) || dateString1.equals(dateString2) || dateString1.equals(dateString3)){
82                  return true;
83              }
84          }
85          return false;
86      }
87  
88      public String getParameter(String name) {
89          ParameterKey parameterKey = ParameterKey.create(OLEConstants.APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT,name);
90          Parameter parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
91          if(parameter==null){
92              parameterKey = ParameterKey.create(OLEConstants.APPL_ID_OLE, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT,name);
93              parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
94          }
95          return parameter!=null?parameter.getValue():null;
96      }
97  
98  }
99