View Javadoc
1   package org.kuali.ole.deliver.service;
2   
3   import org.apache.commons.lang3.StringUtils;
4   import org.junit.Before;
5   import org.junit.Test;
6   import org.kuali.ole.OLEConstants;
7   import org.kuali.ole.deliver.util.BulkUpdateDataObject;
8   import org.kuali.rice.core.api.config.property.Config;
9   import org.mockito.Mock;
10  import org.mockito.Mockito;
11  import org.mockito.MockitoAnnotations;
12  
13  import java.sql.Timestamp;
14  import java.text.SimpleDateFormat;
15  import java.util.*;
16  import java.util.regex.Matcher;
17  import java.util.regex.Pattern;
18  
19  /**
20   * Created by sheiksalahudeenm on 8/11/15.
21   */
22  public class OleLoanDocumentPlatformAwareDaoTest {
23  
24      @Mock
25      private Config mockConfig;
26  
27      @Before
28      public void setUp() throws Exception {
29          MockitoAnnotations.initMocks(this);
30      }
31  
32      @Test
33      public void getSQLsForBatch() throws Exception {
34          Mockito.when(mockConfig.getProperty("db.vendor")).thenReturn("mysql");
35          OleLoanDocumentPlatformAwareDao oleLoanDocumentPlatformAwareDao  = new OleLoanDocumentPlatformAwareDao();
36          oleLoanDocumentPlatformAwareDao.setCurrentContextConfig(mockConfig);
37          List<BulkUpdateDataObject> bulkUpdateDataObjects = new ArrayList<>();
38          BulkUpdateDataObject bulkUpdateDataObject = new BulkUpdateDataObject();
39          HashMap setClauseMap = new HashMap();
40          setClauseMap.put("CURR_DUE_DT_TIME",getTimeStamp(new SimpleDateFormat("MM/dd/yyyy").parse("06/20/2015"), "11:36:00"));
41          bulkUpdateDataObject.setSetClauseMap(setClauseMap);
42          HashMap whereClauseMap = new HashMap();
43          whereClauseMap.put("ITEM_UUID", "wio-1292");
44          bulkUpdateDataObject.setWhereClauseMap(whereClauseMap);
45          bulkUpdateDataObjects.add(bulkUpdateDataObject);
46  
47          BulkUpdateDataObject bulkUpdateDataObject1 = new BulkUpdateDataObject();
48          HashMap setClauseMap1 = new HashMap();
49          setClauseMap1.put("CURR_DUE_DT_TIME",getTimeStamp(new SimpleDateFormat("MM/dd/yyyy").parse("03/21/2015"), "10:36:00"));
50          bulkUpdateDataObject1.setSetClauseMap(setClauseMap1);
51          HashMap whereClauseMap1 = new HashMap();
52          whereClauseMap1.put("ITEM_UUID", "wio-457");
53          bulkUpdateDataObject1.setWhereClauseMap(whereClauseMap1);
54          bulkUpdateDataObjects.add(bulkUpdateDataObject1);
55  
56          List<String> updateQueries = oleLoanDocumentPlatformAwareDao.getUpdateQueriesForDate(bulkUpdateDataObjects);
57          for (Iterator<String> iterator = updateQueries.iterator(); iterator.hasNext(); ) {
58              String sql = iterator.next();
59              System.out.println(sql);
60          }
61      }
62  
63      private Timestamp getTimeStamp(Date loanDueDateToAlter, String loanDueDateTimeToAlter) throws Exception {
64          boolean timeFlag = false;
65          Timestamp timestamp;
66          Pattern pattern;
67          Matcher matcher;
68          SimpleDateFormat fmt = new SimpleDateFormat(OLEConstants.OlePatron.PATRON_MAINTENANCE_DATE_FORMAT);
69  
70          if (StringUtils.isNotBlank(loanDueDateTimeToAlter)) {
71              String[] str = loanDueDateTimeToAlter.split(":");
72              pattern = Pattern.compile(OLEConstants.TIME_24_HR_PATTERN);
73              matcher = pattern.matcher(loanDueDateTimeToAlter);
74              timeFlag = matcher.matches();
75              if (timeFlag) {
76                  if (str != null && str.length <= 2) {
77                      loanDueDateTimeToAlter = loanDueDateTimeToAlter + OLEConstants.CHECK_IN_TIME_MS;
78                  }
79                  timestamp = Timestamp.valueOf(new SimpleDateFormat(OLEConstants.CHECK_IN_DATE_TIME_FORMAT).format(loanDueDateToAlter).concat(" ").concat(loanDueDateTimeToAlter));
80              } else {
81                  throw new Exception();
82              }
83          } else if (fmt.format(loanDueDateToAlter).compareTo(fmt.format(new Date())) == 0) {
84              timestamp = new Timestamp(new Date().getTime());
85          } else {
86              timestamp = Timestamp.valueOf(new SimpleDateFormat(OLEConstants.CHECK_IN_DATE_TIME_FORMAT).format(loanDueDateToAlter).concat(" ").concat(new SimpleDateFormat("HH:mm:ss").format(new Date())));
87          }
88          return timestamp;
89      }
90  }