View Javadoc
1   package org.kuali.ole.service.impl;
2   
3   
4   import org.kuali.ole.DataCarrierService;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.OLEParameterConstants;
7   import org.kuali.ole.OLESruItemHandler;
8   import org.kuali.ole.deliver.bo.FeeType;
9   import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
10  import org.kuali.ole.deliver.bo.OleLoanDocument;
11  import org.kuali.ole.deliver.bo.OlePatronDocument;
12  import org.kuali.ole.deliver.processor.LoanProcessor;
13  import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
14  import org.kuali.ole.describe.bo.OleLocation;
15  import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
16  import org.kuali.ole.pojo.OLESruItem;
17  import org.kuali.ole.service.OleCirculationPolicyService;
18  import org.kuali.ole.service.OleCirculationPolicyServiceImpl;
19  import org.kuali.ole.service.OleLoanDocumentWebService;
20  import org.kuali.ole.sys.context.SpringContext;
21  import org.kuali.ole.util.DocstoreUtil;
22  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
23  import org.kuali.rice.krad.service.BusinessObjectService;
24  import org.kuali.rice.krad.service.KRADServiceLocator;
25  import org.kuali.rice.krms.api.engine.EngineResults;
26  
27  import java.text.DateFormat;
28  import java.text.SimpleDateFormat;
29  import java.util.Date;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  /**
35   * Created with IntelliJ IDEA.
36   * User:
37   * Date: 7/10/13
38   * Time: 6:02 PM
39   * To change this template use File | Settings | File Templates.
40   */
41  
42  public class OleLoanDocumentWebServiceImpl implements OleLoanDocumentWebService {
43  
44      private BusinessObjectService businessObjectService;
45      private OLESruItemHandler oleSruItemHandler;
46      private DocstoreUtil docstoreUtil;
47      private OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService;
48      private LoanProcessor loanProcessor;
49      private OleCirculationPolicyService oleCirculationPolicyService;
50  
51      public BusinessObjectService getBusinessObjectService(){
52          if(businessObjectService==null){
53              businessObjectService = KRADServiceLocator.getBusinessObjectService();
54          }
55          return businessObjectService;
56      }
57  
58      public OLESruItemHandler getOleSruItemHandler(){
59          if(oleSruItemHandler==null){
60              oleSruItemHandler = new OLESruItemHandler();
61          }
62          return oleSruItemHandler;
63      }
64  
65      public OleDeliverRequestDocumentHelperServiceImpl getOleDeliverRequestDocumentHelperServiceImpl(){
66          if(oleDeliverRequestDocumentHelperService==null){
67              oleDeliverRequestDocumentHelperService = new OleDeliverRequestDocumentHelperServiceImpl();
68          }
69          return oleDeliverRequestDocumentHelperService;
70      }
71      public LoanProcessor getLoanProcessor(){
72          if(loanProcessor==null){
73              loanProcessor = new LoanProcessor();
74          }
75          return loanProcessor;
76      }
77  
78      public DocstoreUtil getDocstoreUtil() {
79          if(docstoreUtil == null){
80              docstoreUtil = new DocstoreUtil();
81          }
82          return docstoreUtil;
83      }
84  
85      public OleCirculationPolicyService getOleCirculationPolicyService() {
86          if (null == oleCirculationPolicyService) {
87              oleCirculationPolicyService = SpringContext.getBean(OleCirculationPolicyServiceImpl.class);
88          }
89          return oleCirculationPolicyService;
90      }
91  
92      public String getItemDueDate(String itemUuId) {
93          String dueDate = "";
94          try {
95              HashMap<String, String> loanMap = new HashMap<String, String>();
96              loanMap.put(OLEConstants.ITEM_UUID, itemUuId);
97              List<OleLoanDocument> oleLoanDocument = (List<OleLoanDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OleLoanDocument.class, loanMap);
98              if (oleLoanDocument != null && oleLoanDocument.size() > 0) {
99                  if (oleLoanDocument.get(0).getLoanDueDate() != null) {
100                     dueDate = oleLoanDocument.get(0).getLoanDueDate().toString();
101                 }
102             }
103         } catch (Exception e) {
104             e.printStackTrace();
105         }
106         return dueDate;
107     }
108 
109     public boolean getItemRenewable(String itemUuId, String itemType, String location) {
110         DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
111         boolean errorMessage = false;
112         try {
113             OleDeliverRequestBo oleDeliverRequestBo = getLoanProcessor().getPrioritizedRequest(itemUuId);
114             if (oleDeliverRequestBo != null) {
115                 errorMessage = false;
116                 return errorMessage;
117             } else {
118                 HashMap<String, String> loanMap = new HashMap<String, String>();
119                 loanMap.put(OLEConstants.ITEM_UUID, itemUuId);
120                 List<OleLoanDocument> oleLoanDocuments = (List<OleLoanDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OleLoanDocument.class, loanMap);
121                 loanMap.put(OLEConstants.ITEM_UUID, itemUuId);
122                 if (oleLoanDocuments != null && oleLoanDocuments.size() > 0) {
123                     HashMap<String, String> patronMap = new HashMap<String, String>();
124                     String patronId = oleLoanDocuments.get(0).getPatronId();
125                     String itemId = oleLoanDocuments.get(0).getItemId();
126                     patronMap.put(OLEConstants.OleDeliverRequest.PATRON_ID, patronId);
127                     List<OlePatronDocument> olePatronDocuments = (List<OlePatronDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class, patronMap);
128                     if (olePatronDocuments != null && olePatronDocuments.size() > 0) {
129                         Map<String, Object> detailMap = getOleDeliverRequestDocumentHelperServiceImpl().retrieveBIbItemHoldingData(oleLoanDocuments.get(0).getItemUuid());
130                         OleHoldings oleHoldings = (OleHoldings) detailMap.get(OLEConstants.HOLDING);
131                         org.kuali.ole.docstore.common.document.Item item = (org.kuali.ole.docstore.common.document.Item) detailMap.get(OLEConstants.DOCUMENT_ITEM);
132                         String itemLocation = null;
133                         if (item.getLocation() == null || (item.getLocation() != null && item.getLocation().trim().isEmpty())) {
134                             itemLocation = getDocstoreUtil().getLocation(oleHoldings.getLocation(), new StringBuffer(""));
135                         } else {
136                             itemLocation = item.getLocation();
137                         }
138                         Map<String, String> locationMap = getOleDeliverRequestDocumentHelperServiceImpl().getLocationMap(itemLocation);
139                         String borrowerType = olePatronDocuments.get(0).getOleBorrowerType().getBorrowerTypeCode();
140                         String numberOfClaimsReturned = String.valueOf(olePatronDocuments.get(0).getNumberOfClaimsReturned());
141                         String numberOfRenewals = oleLoanDocuments.get(0).getNumberOfRenewals();
142                         String digitRoutine = getLoanProcessor().getParameter(OLEParameterConstants.ITEM_DIGIT_ROUTINE);
143                         Integer overdueFineAmt = 0;
144                         Integer replacementFeeAmt = 0;
145                         Integer serviceFeeAmt = 0;
146                         DateFormat formatter = new SimpleDateFormat(OLEConstants.DDMMYYYYHHMMSS);
147                         Date loanDueDate = oleLoanDocuments.get(0).getLoanDueDate() != null ? new Date(oleLoanDocuments.get(0).getLoanDueDate().getTime()) : null;
148                         String dateToString = oleLoanDocuments.get(0).getLoanDueDate() != null ? formatter.format(oleLoanDocuments.get(0).getLoanDueDate()) : null;
149                         List<FeeType> feeTypeList = getOleCirculationPolicyService().getPatronBillPayment(oleLoanDocuments.get(0).getPatronId());
150                         if (feeTypeList != null & feeTypeList.size() > 0) {
151                             for (FeeType feeType : feeTypeList) {
152                                 Integer fineAmount = feeType.getFeeAmount().subtract(feeType.getPaidAmount()).intValue();
153                                 overdueFineAmt += feeType.getOleFeeType().getFeeTypeName().equalsIgnoreCase(OLEConstants.OVERDUE_FINE) ? fineAmount : 0;
154                                 replacementFeeAmt += feeType.getOleFeeType().getFeeTypeName().equalsIgnoreCase(OLEConstants.REPLACEMENT_FEE) ? fineAmount : 0;
155                                 serviceFeeAmt += feeType.getOleFeeType().getFeeTypeName().equalsIgnoreCase(OLEConstants.SERVICE_FEE) ? fineAmount : 0;
156                             }
157                         }
158                         dataCarrierService.addData(OLEConstants.GROUP_ID, OLEConstants.HUNDRED);
159                         dataCarrierService.removeData(patronId+itemId);
160                         HashMap keyLoanMap=new HashMap();
161                         keyLoanMap=oleCirculationPolicyService.getLoanedKeyMap(patronId,false);
162                         List<Integer> listOfOverDueDays = (List<Integer>)keyLoanMap.get(OLEConstants.LIST_OF_OVERDUE_DAYS);
163                         dataCarrierService.addData(OLEConstants.LIST_OVERDUE_DAYS, listOfOverDueDays);
164                         dataCarrierService.addData(OLEConstants.LIST_RECALLED_OVERDUE_DAYS, (List<Integer>) keyLoanMap.get(OLEConstants.LIST_RECALLED_OVERDUE_DAYS));
165                         HashMap<String, Object> termValues = new HashMap<String, Object>();
166                         termValues.put(OLEConstants.BORROWER_TYPE, borrowerType);
167                         termValues.put(OLEConstants.ITEM_TYPE, itemType);
168                         termValues.put(OLEConstants.ITEM_SHELVING, locationMap.get(OLEConstants.ITEM_SHELVING));
169                         termValues.put(OLEConstants.ITEM_COLLECTION, locationMap.get(OLEConstants.ITEM_COLLECTION));
170                         termValues.put(OLEConstants.ITEM_LIBRARY, locationMap.get(OLEConstants.ITEM_LIBRARY));
171                         termValues.put(OLEConstants.ITEM_CAMPUS, locationMap.get(OLEConstants.ITEM_CAMPUS));
172                         termValues.put(OLEConstants.ITEM_INSTITUTION, locationMap.get(OLEConstants.ITEM_INSTITUTION));
173                         termValues.put(OLEConstants.DIGIT_ROUTINE, digitRoutine);
174                         termValues.put(OLEConstants.ITEMS_DUE_DATE_STRING, dateToString);
175                         termValues.put(OLEConstants.NUM_CLAIMS_RETURNED, numberOfClaimsReturned);
176                         termValues.put(OLEConstants.OVERDUE_FINE_AMT, overdueFineAmt);
177                         termValues.put(OLEConstants.REPLACEMENT_FEE_AMT, replacementFeeAmt);
178                         termValues.put(OLEConstants.ALL_CHARGES, overdueFineAmt + replacementFeeAmt + serviceFeeAmt);
179                         termValues.put(OLEConstants.NUM_RENEWALS, numberOfRenewals);
180                         termValues.put(OLEConstants.ITEMS_DUE_DATE, loanDueDate);
181                         termValues.put(OLEConstants.PATRON_ID_POLICY, patronId);
182                         termValues.put(OLEConstants.ITEM_ID_POLICY, itemId);
183                         EngineResults engineResults = getLoanProcessor().getEngineResults(OLEConstants.RENEWAL_AGENDA_NM, termValues);
184                         dataCarrierService.removeData(patronId+itemId);
185                         List<String> errorMessages = (List<String>) engineResults.getAttribute(OLEConstants.ERROR_ACTION);
186                         HashMap<String, String> errorsAndPermission = (HashMap<String, String>) engineResults.getAttribute(OLEConstants.ERRORS_AND_PERMISSION);
187                         dataCarrierService.addData(OLEConstants.ERROR_ACTION, null);
188                         dataCarrierService.addData(OLEConstants.ERRORS_AND_PERMISSION, null);
189                         if ((errorMessages != null && errorMessages.size() > 0) || (errorsAndPermission != null && errorsAndPermission.size() > 0)) {
190                             errorMessage = false;
191                         }else{
192                             errorMessage=true;
193                         }
194                     }
195                 }
196             }
197         } catch (Exception e) {
198             e.printStackTrace();
199         }
200         return errorMessage;
201     }
202 
203     @Override
204     public String getItemInformation(String itemUUID, String itemType, String itemShelvingLocation ,String shelvingLocation, String localLocation) {
205         OLESruItem oleSruItem = new OLESruItem();
206        // oleSruItem.setItemDueDate(getItemDueDate(itemUUID));
207       //  oleSruItem.setRenewable(getItemRenewable(itemUUID,itemType,itemShelvingLocation));
208         if(shelvingLocation!=null){
209             oleSruItem.setShelvingLocation(getLocationName(shelvingLocation));
210         }
211         if(localLocation !=null){
212             oleSruItem.setLocalLocation(getLocationName(localLocation));
213         }
214         return getOleSruItemHandler().generateXmlFromObject(oleSruItem);  //To change body of implemented methods use File | Settings | File Templates.
215     }
216 
217     public String getLocationName(String locationCode) {
218         String locationName = "";
219         Map<String, String> locationMap = new HashMap<String, String>();
220         locationMap.put(OLEConstants.LOC_CD, locationCode);
221         List<OleLocation> oleLocationList = (List<OleLocation>) getBusinessObjectService().findMatching(OleLocation.class, locationMap);
222         if (oleLocationList.size() > 0) {
223             locationName = oleLocationList.get(0).getLocationName();
224         }
225         return locationName;
226     }
227 
228 
229 }