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