View Javadoc
1   package org.kuali.ole.deliver.service.impl;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.apache.commons.lang3.StringUtils;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.deliver.bo.*;
7   import org.kuali.ole.deliver.processor.LoanProcessor;
8   import org.kuali.ole.deliver.service.OLEDeliverItemSearchService;
9   import org.kuali.ole.deliver.service.OleLoanDocumentDaoOjb;
10  import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
11  import org.kuali.ole.docstore.common.document.BibTree;
12  import org.kuali.ole.docstore.common.document.HoldingsTree;
13  import org.kuali.ole.docstore.common.document.Item;
14  import org.kuali.ole.sys.context.SpringContext;
15  import org.kuali.rice.krad.service.KRADServiceLocator;
16  import org.kuali.rice.krad.util.GlobalVariables;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  /**
24   * Created by chenchulakshmig on 1/28/15.
25   */
26  public class OLEDeliverItemSearchServiceImpl implements OLEDeliverItemSearchService {
27  
28      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OLEDeliverItemSearchServiceImpl.class);
29  
30      private LoanProcessor loanProcessor;
31      private DocstoreClientLocator docstoreClientLocator;
32  
33      private LoanProcessor getLoanProcessor() {
34          if (loanProcessor == null) {
35              loanProcessor = SpringContext.getBean(LoanProcessor.class);
36          }
37          return loanProcessor;
38      }
39  
40      public DocstoreClientLocator getDocstoreClientLocator() {
41          if (docstoreClientLocator == null) {
42              docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
43          }
44          return docstoreClientLocator;
45      }
46  
47      public void setDeliverRequestInfo(Map itemIdMap, OLESingleItemResultDisplayRow singleItemResultDisplayRow) {
48          List<OleDeliverRequestBo> oleDeliverRequestBos = (List<OleDeliverRequestBo>) KRADServiceLocator.getBusinessObjectService().findMatching(OleDeliverRequestBo.class, itemIdMap);
49          if (CollectionUtils.isNotEmpty(oleDeliverRequestBos)) {
50              List<OLEDeliverRequestResultDisplayRow> deliverRequestResultDisplayRowList = new ArrayList<>();
51              for (OleDeliverRequestBo oleDeliverRequestBo : oleDeliverRequestBos) {
52                  OLEDeliverRequestResultDisplayRow deliverRequestResultDisplayRow = new OLEDeliverRequestResultDisplayRow();
53                  deliverRequestResultDisplayRow.setBorrowerQueuePosition(oleDeliverRequestBo.getBorrowerQueuePosition());
54                  deliverRequestResultDisplayRow.setRequestId(oleDeliverRequestBo.getRequestId());
55                  deliverRequestResultDisplayRow.setRequestTypeCode(oleDeliverRequestBo.getRequestTypeCode());
56                  deliverRequestResultDisplayRow.setBorrowerFirstName(oleDeliverRequestBo.getFirstName());
57                  deliverRequestResultDisplayRow.setBorrowerLastName(oleDeliverRequestBo.getLastName());
58                  deliverRequestResultDisplayRow.setBorrowerBarcode(oleDeliverRequestBo.getBorrowerBarcode());
59                  deliverRequestResultDisplayRow.setCreateDate(oleDeliverRequestBo.getCreateDate());
60                  deliverRequestResultDisplayRow.setExpiryDate(oleDeliverRequestBo.getRequestExpiryDate());
61                  deliverRequestResultDisplayRowList.add(deliverRequestResultDisplayRow);
62              }
63              singleItemResultDisplayRow.setOleDeliverRequestResultDisplayRowList(deliverRequestResultDisplayRowList);
64          }
65      }
66  
67      public void setOutstandingFineInfo(Map itemIdMap, OLESingleItemResultDisplayRow singleItemResultDisplayRow) {
68          List<FeeType> feeTypes = (List<FeeType>) KRADServiceLocator.getBusinessObjectService().findMatching(FeeType.class, itemIdMap);
69          if (CollectionUtils.isNotEmpty(feeTypes)) {
70              PatronBillHelperService patronBillHelperService = new PatronBillHelperService();
71              List<OLEFeeTypeResultDisplayRow> feeTypeResultDisplayRowList = new ArrayList<>();
72              for (FeeType feeType : feeTypes) {
73                  OLEFeeTypeResultDisplayRow feeTypeResultDisplayRow = new OLEFeeTypeResultDisplayRow();
74                  feeTypeResultDisplayRow.setFeeType(feeType.getFeeTypeName());
75                  feeTypeResultDisplayRow.setFeeAmount(feeType.getFeeAmount());
76                  feeTypeResultDisplayRow.setBorrowerId(feeType.getPatronBillPayment().getPatronId());
77                  OlePatronDocument olePatronDocument = patronBillHelperService.getPatronDetails(feeType.getPatronBillPayment().getPatronId());
78                  if (olePatronDocument != null) {
79                      feeTypeResultDisplayRow.setBorrowerBarcode(olePatronDocument.getBarcode());
80                      feeTypeResultDisplayRow.setBorrowerFirstName(olePatronDocument.getFirstName());
81                      feeTypeResultDisplayRow.setBorrowerLastName(olePatronDocument.getLastName());
82                  }
83                  feeTypeResultDisplayRowList.add(feeTypeResultDisplayRow);
84              }
85              singleItemResultDisplayRow.setOleFeeTypeResultDisplayRowList(feeTypeResultDisplayRowList);
86          }
87      }
88  
89      public void setNoteInfo(OLESingleItemResultDisplayRow singleItemResultDisplayRow) {
90          if (singleItemResultDisplayRow.getItemNoteType() != null) {
91              List<OLEItemNoteResultDisplayRow> itemNoteResultDisplayRowList = new ArrayList<>();
92              String[] itemNoteTypes = singleItemResultDisplayRow.getItemNoteType().split(",");
93              String[] itemNoteValues = singleItemResultDisplayRow.getItemNoteValue() != null ? singleItemResultDisplayRow.getItemNoteValue().split(",") : new String[itemNoteTypes.length];
94              for (int noteCount = 0; noteCount < itemNoteTypes.length; noteCount++) {
95                  OLEItemNoteResultDisplayRow itemNoteResultDisplayRow = new OLEItemNoteResultDisplayRow();
96                  itemNoteResultDisplayRow.setNoteType(itemNoteTypes[noteCount]);
97                  itemNoteResultDisplayRow.setNoteValue(itemNoteValues[noteCount]);
98                  itemNoteResultDisplayRowList.add(itemNoteResultDisplayRow);
99              }
100             singleItemResultDisplayRow.setOleItemNoteResultDisplayRowList(itemNoteResultDisplayRowList);
101         }
102     }
103 
104     public void setBorrowerInfo(OLESingleItemResultDisplayRow singleItemResultDisplayRow) {
105         if (StringUtils.isNotBlank(singleItemResultDisplayRow.getCurrentBorrowerId())) {
106             OlePatronDocument olePatronDocument = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(OlePatronDocument.class, singleItemResultDisplayRow.getCurrentBorrowerId());
107             if (olePatronDocument != null) {
108                 singleItemResultDisplayRow.setPatronURL(getLoanProcessor().patronNameURL(GlobalVariables.getUserSession().getPrincipalId(), singleItemResultDisplayRow.getCurrentBorrowerId()));
109                 singleItemResultDisplayRow.setPatronBarcode(olePatronDocument.getBarcode());
110                 if (StringUtils.isNotBlank(olePatronDocument.getBorrowerType())) {
111                     OleBorrowerType oleBorrowerType = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(OleBorrowerType.class, olePatronDocument.getBorrowerType());
112                     if (oleBorrowerType != null) {
113                         singleItemResultDisplayRow.setPatronType(oleBorrowerType.getBorrowerTypeName());
114                     }
115                 }
116                 if(olePatronDocument.getExpirationDate()!=null){
117                     singleItemResultDisplayRow.setPatronExpDate(olePatronDocument.getExpirationDate().toString());
118                 }
119                 if (olePatronDocument.getOlePatronEntityViewBo() != null) {
120                     singleItemResultDisplayRow.setPatronLastName(olePatronDocument.getOlePatronEntityViewBo().getLastName());
121                     singleItemResultDisplayRow.setPatronFirstName(olePatronDocument.getOlePatronEntityViewBo().getFirstName());
122                     singleItemResultDisplayRow.setPatronMiddleName(olePatronDocument.getOlePatronEntityViewBo().getMiddleName());
123                 }
124                 if (StringUtils.isBlank(singleItemResultDisplayRow.getDueDate())) {
125                     singleItemResultDisplayRow.setDueDate(OLEConstants.INDEFINITE);
126                 }
127             }
128             if (StringUtils.isNotBlank(singleItemResultDisplayRow.getProxyBorrowerId())) {
129                 singleItemResultDisplayRow.setProxyPatronURL(getLoanProcessor().patronNameURL(GlobalVariables.getUserSession().getPrincipalId(), singleItemResultDisplayRow.getProxyBorrowerId()));
130                 olePatronDocument = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(OlePatronDocument.class, singleItemResultDisplayRow.getProxyBorrowerId());
131                 singleItemResultDisplayRow.setProxyPatronBarcode(olePatronDocument.getBarcode());
132                 if (olePatronDocument.getOlePatronEntityViewBo() != null) {
133                     singleItemResultDisplayRow.setProxyPatronName(olePatronDocument.getOlePatronEntityViewBo().getName());
134                 }
135             }
136         }
137     }
138 
139     public void setAdditionalCopiesInfo(OLESingleItemResultDisplayRow singleItemResultDisplayRow) {
140         List<OLEHoldingsSearchResultDisplayRow> oleHoldingsSearchResultDisplayRowList = new ArrayList<>();
141         if (StringUtils.isNotBlank(singleItemResultDisplayRow.getBibIdentifier())) {
142             try {
143                 BibTree bibTree = getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(singleItemResultDisplayRow.getBibIdentifier());
144                 for (HoldingsTree holdingsTree : bibTree.getHoldingsTrees()) {
145                     OLEHoldingsSearchResultDisplayRow holdingsSearchResultDisplayRow = new OLEHoldingsSearchResultDisplayRow();
146                     holdingsSearchResultDisplayRow.setId(holdingsTree.getHoldings().getId());
147                     holdingsSearchResultDisplayRow.setBibIdentifier(bibTree.getBib().getId());
148                     holdingsSearchResultDisplayRow.setLocation(holdingsTree.getHoldings().getLocationName());
149                     if (holdingsTree.getHoldings().getContentObject() != null) {
150                         if (holdingsTree.getHoldings().getContentObject().getCallNumber() != null)
151                             holdingsSearchResultDisplayRow.setCallNumber(holdingsTree.getHoldings().getContentObject().getCallNumber().getNumber());
152                     }
153                     if (holdingsTree.getItems().size() > 0) {
154                         List<OLEItemSearchResultDisplayRow> itemSearchResultDisplayRowList = new ArrayList<>();
155                         for (Item item : holdingsTree.getItems()) {
156                             if (!item.getId().equals(singleItemResultDisplayRow.getId())) {
157                                 OLEItemSearchResultDisplayRow oleItemSearchResultDisplayRow = new OLEItemSearchResultDisplayRow();
158                                 oleItemSearchResultDisplayRow.setId(item.getId());
159                                 oleItemSearchResultDisplayRow.setHoldingsIdentifier(holdingsTree.getHoldings().getId());
160                                 oleItemSearchResultDisplayRow.setBibIdentifier(bibTree.getBib().getId());
161                                 oleItemSearchResultDisplayRow.setLocation(item.getLocation());
162                                 Object itemContent = item.getContentObject();
163                                 if (itemContent != null) {
164                                     org.kuali.ole.docstore.common.document.content.instance.Item itemPojo = (org.kuali.ole.docstore.common.document.content.instance.Item) itemContent;
165                                     oleItemSearchResultDisplayRow.setEnumeration(itemPojo.getEnumeration());
166                                     oleItemSearchResultDisplayRow.setChronology(itemPojo.getChronology());
167                                     if (itemPojo.getItemType() != null)
168                                         oleItemSearchResultDisplayRow.setItemType(itemPojo.getItemType().getCodeValue());
169                                     if (itemPojo.getItemStatus() != null)
170                                         oleItemSearchResultDisplayRow.setItemStatus(itemPojo.getItemStatus().getCodeValue());
171                                     if (itemPojo.getAccessInformation() != null)
172                                         oleItemSearchResultDisplayRow.setItemBarCode(itemPojo.getAccessInformation().getBarcode());
173                                     Map itemMap = new HashMap();
174                                     itemMap.put(OLEConstants.OleDeliverRequest.ITEM_ID, oleItemSearchResultDisplayRow.getItemBarCode());
175                                     List<OleDeliverRequestBo> deliverRequestBos = (List<OleDeliverRequestBo>) KRADServiceLocator.getBusinessObjectService().findMatching(OleDeliverRequestBo.class, itemMap);
176                                     if (CollectionUtils.isNotEmpty(deliverRequestBos)) {
177                                         oleItemSearchResultDisplayRow.setRequestExists(true);
178                                     }
179                                 }
180                                 itemSearchResultDisplayRowList.add(oleItemSearchResultDisplayRow);
181                             }
182                         }
183                         if (itemSearchResultDisplayRowList.size() > 0) {
184                             if (itemSearchResultDisplayRowList.size() == 1) {
185                                 holdingsSearchResultDisplayRow.setItemLocation(itemSearchResultDisplayRowList.get(0).getLocation());
186                             } else {
187                                 holdingsSearchResultDisplayRow.setItemLocation(OLEConstants.MULTIPLE);
188                             }
189                             holdingsSearchResultDisplayRow.setOleItemSearchResultDisplayRowList(itemSearchResultDisplayRowList);
190                             oleHoldingsSearchResultDisplayRowList.add(holdingsSearchResultDisplayRow);
191                         }
192                     }
193                 }
194             } catch (Exception e) {
195                 LOG.error("Exception " + e);
196             }
197         }
198         singleItemResultDisplayRow.setOleHoldingsSearchResultDisplayRowList(oleHoldingsSearchResultDisplayRowList);
199     }
200 
201     public void setRequestHistoryInfo(OLESingleItemResultDisplayRow singleItemResultDisplayRow) {
202         if (StringUtils.isNotBlank(singleItemResultDisplayRow.getItemBarCode())) {
203             List<OleDeliverRequestHistoryRecord> deliverRequestHistoryRecordList = (List<OleDeliverRequestHistoryRecord>) ((OleLoanDocumentDaoOjb) SpringContext.getBean("oleLoanDao")).getDeliverRequestHistoryRecords(singleItemResultDisplayRow.getItemBarCode());
204             if (deliverRequestHistoryRecordList != null && deliverRequestHistoryRecordList.size() > 0) {
205                 singleItemResultDisplayRow.setRequestTypeCode(deliverRequestHistoryRecordList.get(0).getDeliverRequestTypeCode());
206                 singleItemResultDisplayRow.setPickUpLocationCode(deliverRequestHistoryRecordList.get(0).getPickUpLocationCode());
207                 singleItemResultDisplayRow.setFinalStatus("Filed");
208                 singleItemResultDisplayRow.setFinalStatusDate(deliverRequestHistoryRecordList.get(0).getArchiveDate());
209             }
210         }
211     }
212 
213     public void setInTransitHistoryInfo(OLESingleItemResultDisplayRow singleItemResultDisplayRow) {
214         if (StringUtils.isNotBlank(singleItemResultDisplayRow.getId())) {
215             Map map = new HashMap();
216             map.put(OLEConstants.OVERLAY_ITEMUUID, singleItemResultDisplayRow.getId());
217             List<OLELoanIntransitRecordHistory> oleIntransitRecordHistories = (List<OLELoanIntransitRecordHistory>) KRADServiceLocator.getBusinessObjectService().findMatching(OLELoanIntransitRecordHistory.class, map);
218             if (CollectionUtils.isNotEmpty(oleIntransitRecordHistories)) {
219                 singleItemResultDisplayRow.setOleIntransitRecordHistories(oleIntransitRecordHistories);
220             }
221         }
222     }
223 }