View Javadoc
1   package org.kuali.ole.deliver.batch;
2   
3   
4   import org.apache.log4j.Logger;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.deliver.bo.OleCirculationDesk;
7   import org.kuali.ole.deliver.bo.OleRecentlyReturned;
8   import org.kuali.ole.deliver.processor.LoanProcessor;
9   import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
10  import org.kuali.ole.docstore.common.document.ItemOleml;
11  import org.kuali.ole.docstore.common.search.SearchResponse;
12  import org.kuali.ole.docstore.common.search.SearchResult;
13  import org.kuali.ole.docstore.common.search.SearchResultField;
14  import org.kuali.ole.docstore.common.document.content.instance.Item;
15  import org.kuali.ole.sys.context.SpringContext;
16  import org.kuali.rice.krad.service.BusinessObjectService;
17  import org.kuali.rice.krad.service.KRADServiceLocator;
18  import java.text.DateFormat;
19  import java.text.SimpleDateFormat;
20  import java.util.Date;
21  import java.util.HashMap;
22  import java.util.concurrent.TimeUnit;
23  
24  /**
25   * Created with IntelliJ IDEA.
26   * User: vivekb
27   * Date: 4/4/13
28   * Time: 5:44 PM
29   * To change this template use File | Settings | File Templates.
30   */
31  public class OleShelvingLagTime {
32  
33      private static final Logger LOG = Logger.getLogger(OleShelvingLagTime.class);
34  
35      private BusinessObjectService businessObjectService;
36  
37      private DocstoreClientLocator docstoreClientLocator;
38  
39      public DocstoreClientLocator getDocstoreClientLocator() {
40  
41          if (docstoreClientLocator == null) {
42              docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
43  
44          }
45          return docstoreClientLocator;
46      }
47  
48  
49      public void updateStatusIntoAvailableAfterReShelving() throws Exception {
50          businessObjectService = getBusinessObjectService();
51          LoanProcessor loanProcessor = new LoanProcessor();
52          StringBuffer query = new StringBuffer("");
53          query.append("(ItemStatus_display:RECENTLY-RETURNED)");
54          org.kuali.ole.docstore.common.document.Item item = new ItemOleml();
55          org.kuali.ole.docstore.common.search.SearchParams search_Params = new org.kuali.ole.docstore.common.search.SearchParams();
56          SearchResponse searchResponse = null;
57          search_Params.getSearchConditions().add(search_Params.buildSearchCondition("", search_Params.buildSearchField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), item.ITEM_STATUS, "RECENTLY-RETURNED"), ""));
58          search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), "id"));
59          searchResponse = getDocstoreClientLocator().getDocstoreClient().search(search_Params);
60          for (SearchResult searchResult : searchResponse.getSearchResults()) {
61              for (SearchResultField searchResultField : searchResult.getSearchResultFields()) {
62                  String fieldName = searchResultField.getFieldName();
63                  String fieldValue = searchResultField.getFieldValue() != null ? searchResultField.getFieldValue() : "";
64                  if (fieldName.equalsIgnoreCase("id") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase("item")) {
65                      String itemUUID = fieldValue;
66                      HashMap<String, String> map = new HashMap<String, String>();
67                      map.put("itemUuid", itemUUID);
68                      OleRecentlyReturned oleRecentlyReturned = businessObjectService.findByPrimaryKey(OleRecentlyReturned.class, map);
69                      if (oleRecentlyReturned != null) {
70                          map = new HashMap<String, String>();
71                          map.put("circulationDeskId", oleRecentlyReturned.getCirculationDeskId());
72                          OleCirculationDesk oleCirculationDesk = businessObjectService.findByPrimaryKey(OleCirculationDesk.class, map);
73                          if (oleCirculationDesk != null) {
74                              Integer shelvingLagTime = Integer.parseInt(oleCirculationDesk.getShelvingLagTime());
75                              if (LOG.isDebugEnabled()){
76                                  LOG.debug("shelvingLagTime" + shelvingLagTime);
77                              }
78                              String itemXml = loanProcessor.getItemXML(itemUUID);
79                              Item oleItem = loanProcessor.getItemPojo(itemXml);
80                              String dateString = oleItem.getItemStatusEffectiveDate();
81                              DateFormat formatter = new SimpleDateFormat(OLEConstants.DAT_FORMAT_EFFECTIVE_NOTICE);
82                              Date checkInDate = (Date) formatter.parse(dateString);
83                              String diff = getMinuteDiff(checkInDate, new Date());
84                              Integer diffTime = Integer.parseInt(diff);
85                              if (LOG.isDebugEnabled()){
86                                  LOG.debug("diffTime" + diffTime);
87                                  LOG.debug("shelvingLagTime.compareTo(diffTime)" + shelvingLagTime.compareTo(diffTime));
88                              }
89                              if (shelvingLagTime.compareTo(diffTime) <= 0) {
90                                  loanProcessor.updateItemStatus(oleItem, OLEConstants.NOT_CHECK_OUT_STATUS);
91                                  businessObjectService.delete(oleRecentlyReturned);
92                              }
93                          }
94                      }
95                  }
96  
97              }
98          }
99      }
100 
101 
102     private String getMinuteDiff(Date dateOne, Date dateTwo) {
103         String diff = "";
104         long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
105         diff = String.format("%d", TimeUnit.MILLISECONDS.toMinutes(timeDiff),
106                 -TimeUnit.MINUTES.toSeconds(timeDiff));
107         return diff;
108     }
109 
110     public BusinessObjectService getBusinessObjectService() {
111         if (businessObjectService == null) {
112             businessObjectService = KRADServiceLocator.getBusinessObjectService();
113         }
114         return businessObjectService;
115     }
116 }