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