001package org.kuali.ole.deliver.batch;
002
003
004import org.apache.commons.lang.StringUtils;
005import org.apache.log4j.Logger;
006import org.kuali.ole.OLEConstants;
007import org.kuali.ole.deliver.bo.OleCirculationDesk;
008import org.kuali.ole.deliver.bo.OleRecentlyReturned;
009import org.kuali.ole.deliver.processor.LoanProcessor;
010import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
011import org.kuali.ole.docstore.common.document.ItemOleml;
012import org.kuali.ole.docstore.common.search.SearchResponse;
013import org.kuali.ole.docstore.common.search.SearchResult;
014import org.kuali.ole.docstore.common.search.SearchResultField;
015import org.kuali.ole.docstore.common.document.content.instance.Item;
016import org.kuali.ole.sys.context.SpringContext;
017import org.kuali.rice.core.api.config.property.ConfigContext;
018import org.kuali.rice.krad.service.BusinessObjectService;
019import org.kuali.rice.krad.service.KRADServiceLocator;
020import java.text.DateFormat;
021import java.text.SimpleDateFormat;
022import java.util.Date;
023import java.util.HashMap;
024import java.util.concurrent.TimeUnit;
025
026/**
027 * Created with IntelliJ IDEA.
028 * User: vivekb
029 * Date: 4/4/13
030 * Time: 5:44 PM
031 * To change this template use File | Settings | File Templates.
032 */
033public class OleShelvingLagTime {
034
035    private static final Logger LOG = Logger.getLogger(OleShelvingLagTime.class);
036    private static final String solrMaxPageSize = ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.OleCirculationDesk.SOLR_MAX_PAGE_SIZE);
037
038    private BusinessObjectService businessObjectService;
039
040    private DocstoreClientLocator docstoreClientLocator;
041
042    public DocstoreClientLocator getDocstoreClientLocator() {
043
044        if (docstoreClientLocator == null) {
045            docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
046
047        }
048        return docstoreClientLocator;
049    }
050
051
052    public void updateStatusIntoAvailableAfterReShelving() throws Exception {
053        businessObjectService = getBusinessObjectService();
054        LoanProcessor loanProcessor = new LoanProcessor();
055        StringBuffer query = new StringBuffer("");
056        query.append("(ItemStatus_display:RECENTLY-RETURNED)");
057        org.kuali.ole.docstore.common.document.Item item = new ItemOleml();
058        org.kuali.ole.docstore.common.search.SearchParams search_Params = new org.kuali.ole.docstore.common.search.SearchParams();
059        SearchResponse searchResponse = null;
060        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"), ""));
061        search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), "id"));
062        if (StringUtils.isNotBlank(solrMaxPageSize)) {
063            search_Params.setPageSize(Integer.parseInt(solrMaxPageSize));
064        }
065        searchResponse = getDocstoreClientLocator().getDocstoreClient().search(search_Params);
066        for (SearchResult searchResult : searchResponse.getSearchResults()) {
067            for (SearchResultField searchResultField : searchResult.getSearchResultFields()) {
068                String fieldName = searchResultField.getFieldName();
069                String fieldValue = searchResultField.getFieldValue() != null ? searchResultField.getFieldValue() : "";
070                if (fieldName.equalsIgnoreCase("id") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase("item")) {
071                    String itemUUID = fieldValue;
072                    HashMap<String, String> map = new HashMap<String, String>();
073                    map.put("itemUuid", itemUUID);
074                    OleRecentlyReturned oleRecentlyReturned = businessObjectService.findByPrimaryKey(OleRecentlyReturned.class, map);
075                    if (oleRecentlyReturned != null) {
076                        map = new HashMap<String, String>();
077                        map.put("circulationDeskId", oleRecentlyReturned.getCirculationDeskId());
078                        OleCirculationDesk oleCirculationDesk = businessObjectService.findByPrimaryKey(OleCirculationDesk.class, map);
079                        if (oleCirculationDesk != null) {
080                            Integer shelvingLagTime = Integer.parseInt(oleCirculationDesk.getShelvingLagTime());
081                            if (LOG.isDebugEnabled()){
082                                LOG.debug("shelvingLagTime" + shelvingLagTime);
083                            }
084                            String itemXml = loanProcessor.getItemXML(itemUUID);
085                            Item oleItem = loanProcessor.getItemPojo(itemXml);
086                            String dateString = oleItem.getItemStatusEffectiveDate();
087                            DateFormat formatter = new SimpleDateFormat(OLEConstants.DAT_FORMAT_EFFECTIVE_NOTICE);
088                            Date checkInDate = (Date) formatter.parse(dateString);
089                            String diff = getMinuteDiff(checkInDate, new Date());
090                            Integer diffTime = Integer.parseInt(diff);
091                            if (LOG.isDebugEnabled()){
092                                LOG.debug("diffTime" + diffTime);
093                                LOG.debug("shelvingLagTime.compareTo(diffTime)" + shelvingLagTime.compareTo(diffTime));
094                            }
095                            if (shelvingLagTime.compareTo(diffTime) <= 0) {
096                                loanProcessor.updateItemStatus(oleItem, OLEConstants.NOT_CHECK_OUT_STATUS);
097                                businessObjectService.delete(oleRecentlyReturned);
098                            }
099                        }
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}