View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.select.service.impl;
17  
18  import org.apache.commons.lang.StringEscapeUtils;
19  import org.kuali.ole.DataCarrierService;
20  import org.kuali.ole.OleOrderRecords;
21  import org.kuali.ole.batch.bo.OLEBatchProcessProfileBo;
22  import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
23  import org.kuali.ole.module.purap.PurapConstants;
24  import org.kuali.ole.module.purap.PurapPropertyConstants;
25  import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
26  import org.kuali.ole.module.purap.businessobject.PurchaseOrderType;
27  import org.kuali.ole.module.purap.businessobject.RequisitionAccount;
28  import org.kuali.ole.module.purap.businessobject.RequisitionItem;
29  import org.kuali.ole.module.purap.document.RequisitionDocument;
30  import org.kuali.ole.module.purap.document.service.OlePurapService;
31  import org.kuali.ole.module.purap.document.service.RequisitionService;
32  import org.kuali.ole.pojo.OleOrderRecord;
33  import org.kuali.ole.pojo.OleTxRecord;
34  import org.kuali.ole.select.OleSelectConstant;
35  import org.kuali.ole.select.OleSelectNotificationConstant;
36  import org.kuali.ole.select.batch.service.RequisitionCreateDocumentService;
37  import org.kuali.ole.select.batch.service.impl.RequisitionCreateDocumentServiceImpl;
38  import org.kuali.ole.select.bo.OLELinkPurapDonor;
39  import org.kuali.ole.select.businessobject.*;
40  import org.kuali.ole.select.document.OleRequisitionDocument;
41  import org.kuali.ole.select.document.service.OleRequestSourceService;
42  import org.kuali.ole.select.document.service.OleRequestorService;
43  import org.kuali.ole.select.document.service.OleSelectDocumentService;
44  import org.kuali.ole.select.service.BibInfoService;
45  import org.kuali.ole.select.service.OleReqPOCreateDocumentService;
46  import org.kuali.ole.sys.OLEConstants;
47  import org.kuali.ole.sys.OLEConstants.FinancialDocumentTypeCodes;
48  import org.kuali.ole.sys.businessobject.Building;
49  import org.kuali.ole.sys.context.SpringContext;
50  import org.kuali.ole.vnd.VendorConstants;
51  import org.kuali.ole.vnd.VendorPropertyConstants;
52  import org.kuali.ole.vnd.businessobject.VendorAddress;
53  import org.kuali.ole.vnd.businessobject.VendorContract;
54  import org.kuali.ole.vnd.businessobject.VendorDetail;
55  import org.kuali.ole.vnd.document.service.VendorService;
56  import org.kuali.rice.core.api.config.property.ConfigurationService;
57  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
58  import org.kuali.rice.core.api.util.type.KualiDecimal;
59  import org.kuali.rice.kew.api.exception.WorkflowException;
60  import org.kuali.rice.krad.UserSession;
61  import org.kuali.rice.krad.service.BusinessObjectService;
62  import org.kuali.rice.krad.service.DocumentService;
63  import org.kuali.rice.krad.util.GlobalVariables;
64  import org.kuali.rice.krad.util.ObjectUtils;
65  import org.springmodules.orm.ojb.OjbOperationException;
66  
67  import javax.persistence.OptimisticLockException;
68  import java.math.BigDecimal;
69  import java.util.*;
70  
71  public class OleReqPOCreateDocumentServiceImpl extends RequisitionCreateDocumentServiceImpl implements OleReqPOCreateDocumentService {
72      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleReqPOCreateDocumentServiceImpl.class);
73  
74      protected RequisitionCreateDocumentService requisitionCreateDocumentService;
75      protected OleRequestorService oleRequestorService;
76      protected VendorService vendorService;
77      protected OleRequestSourceService oleRequestSourceService;
78      protected OlePurapService olePurapService;
79      protected BusinessObjectService businessObjectService;
80      protected DocumentService documentService;
81      protected BibInfoService bibInfoService;
82      protected ConfigurationService kualiConfigurationService;
83      protected List reqList;
84      private DocstoreClientLocator docstoreClientLocator;
85      private OLEBatchProcessProfileBo oleBatchProcessProfileBo;
86      private OleRequisitionDocument requisitionDocument;
87      private OlePatronDocumentList olePatronDocumentList;
88      private OleSelectDocumentService oleSelectDocumentService;
89  
90      public OleSelectDocumentService getOleSelectDocumentService() {
91          if (oleSelectDocumentService == null) {
92              oleSelectDocumentService = SpringContext.getBean(OleSelectDocumentService.class);
93          }
94          return oleSelectDocumentService;
95      }
96  
97      public OlePatronDocumentList getOlePatronDocumentList() {
98          if (olePatronDocumentList == null) {
99              olePatronDocumentList = SpringContext.getBean(OlePatronDocumentList.class);
100         }
101         return olePatronDocumentList;
102     }
103 
104     public DocstoreClientLocator getDocstoreClientLocator() {
105 
106         if (docstoreClientLocator == null) {
107             docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
108 
109         }
110         return docstoreClientLocator;
111     }
112 
113     /**
114      * Set the values for the Requisition Document and save.
115      *
116      * @param oleOrderRecords OleOrderRecords
117      */
118     public void saveRequisitionDocument(OleOrderRecords oleOrderRecords) throws Exception {
119         String docNumber = "";
120         String isValidBFN = "";
121         vendorService = getVendorService();
122         oleRequestorService = getOleRequestorService();
123         requisitionCreateDocumentService = getRequisitionCreateDocumentService();
124         oleRequestSourceService = getOleRequestSourceService();
125         olePurapService = getOlePurapService();
126         setUserFromStaffUpload(oleOrderRecords);
127         DataCarrierService dataCarrierService = GlobalResourceLoader.getService(org.kuali.ole.OLEConstants.DATA_CARRIER_SERVICE);
128         oleBatchProcessProfileBo = (OLEBatchProcessProfileBo) dataCarrierService.getData(org.kuali.ole.OLEConstants.BATCH_PROFILE_BO);
129         String requisitionsForTitle = null;
130         if(oleBatchProcessProfileBo != null){
131             requisitionsForTitle = oleBatchProcessProfileBo.getRequisitionsforTitle();
132         }
133         if (oleBatchProcessProfileBo != null && (requisitionsForTitle.equalsIgnoreCase("None") || requisitionsForTitle.equalsIgnoreCase("One Requisition Per Title"))) {
134             reqList = new ArrayList(0);
135             HashMap<String, String> dataMap = new HashMap<String, String>();
136             List<OleOrderRecord> oleOrderRecordList = oleOrderRecords.getRecords();
137             for (OleOrderRecord oleOrderRecord : oleOrderRecordList) {
138                 oleOrderRecord.getMessageMap().put("isApoRule", false);
139                 String isValidRecord = ((oleOrderRecord.getMessageMap().get("isValidRecord"))).toString();
140                 if ("true".equalsIgnoreCase(isValidRecord)) {
141                     isValidBFN = ((oleOrderRecord.getMessageMap().get("isValidBFN"))).toString();
142                     if (isValidBFN.equalsIgnoreCase("true")) {
143                         String[] isbnDupFlag = (((List) oleOrderRecord.getMessageMap().get("rulesEvaluated")).get(0)).toString().split(":");
144                         if ("false".equalsIgnoreCase(isbnDupFlag[1].trim()) && oleOrderRecord.getOleTxRecord() != null && oleOrderRecord.getOleBibRecord() != null) {
145                             requisitionDocument = null;
146                             try {
147                                 requisitionDocument = createRequisitionDocument();
148                                 /*requisitionDocument.setLicensingRequirementCode("NLR");*/
149                                 requisitionDocument.setRequisitionSourceCode(oleOrderRecord.getOleTxRecord().getRequisitionSource());
150                                 if (oleOrderRecordList != null && oleOrderRecordList.size() > 0) {
151 
152                                     if (oleOrderRecord.getOleTxRecord().getOrderType() != null) {
153                                         Map purchaseOrderTypeMap = new HashMap();
154                                         purchaseOrderTypeMap.put("purchaseOrderType", oleOrderRecord.getOleTxRecord().getOrderType());
155                                         org.kuali.rice.krad.service.BusinessObjectService
156                                                 businessObjectService = SpringContext.getBean(org.kuali.rice.krad.service.BusinessObjectService.class);
157                                         List<PurchaseOrderType> purchaseOrderTypeDocumentList = (List) businessObjectService.findMatching(PurchaseOrderType.class, purchaseOrderTypeMap);
158                                         if (purchaseOrderTypeDocumentList != null && purchaseOrderTypeDocumentList.size() > 0) {
159                                             requisitionDocument.setPurchaseOrderTypeId(purchaseOrderTypeDocumentList.get(0).getPurchaseOrderTypeId());
160                                         }
161                                     }
162                                     else {
163                                         requisitionDocument.setPurchaseOrderTypeId(OLEConstants.DEFAULT_ORDER_TYPE_VALUE);
164                                     }
165                                     setDocumentValues(requisitionDocument, oleOrderRecord);
166                                 }
167                                 requisitionDocument.setItems(generateItemList(oleOrderRecord));                                // Checking the APO rules
168                                 RequisitionService requisitionService = SpringContext.getBean(RequisitionService.class);
169                               //  requisitionDocument.getDocumentHeader().getWorkflowDocument();
170                                 boolean apoRuleFlag = requisitionService.isAutomaticPurchaseOrderAllowed(requisitionDocument);
171                                 if (!apoRuleFlag) {
172                                     oleOrderRecord.getMessageMap().put("isApoRule", true);
173                                 }
174                                 if(LOG.isInfoEnabled()){
175                                     LOG.info("before calling saveRequisitionDocuments");
176                                 }
177                                 docNumber = requisitionCreateDocumentService.saveRequisitionDocuments(requisitionDocument);
178                                 reqList.add(requisitionDocument.getPurapDocumentIdentifier());
179                             }
180                             catch(OjbOperationException opl) {
181                                 LOG.error("OjbOperationException while performing saveRequisitionDocument"+opl);
182                                opl.printStackTrace();
183                             }
184                             catch(OptimisticLockException ole){
185                                LOG.error("OjbOperationException while performing saveRequisitionDocument"+ole);
186                                ole.printStackTrace();
187                             }
188                             catch (Exception ex) {
189                                 LOG.error("####Rollback####" + ex);
190                                 ex.printStackTrace();
191                                 Iterator<OleRequisitionItem> iterator = requisitionDocument.getItems().iterator();
192                                 while (iterator.hasNext()) {
193                                     OleRequisitionItem item = iterator.next();
194                                     //OleDocstoreHelperService oleDocstoreHelperService = SpringContext.getBean(OleDocstoreHelperService.class);
195                                     getDocstoreClientLocator().getDocstoreClient().deleteBib(item.getBibUUID());
196 
197                                 }
198                                 LOG.error("####Rollback####" + ex);
199                                 throw ex;
200                             }
201                         }
202                     }
203                 }
204             }
205         }
206         else if(oleBatchProcessProfileBo != null && requisitionsForTitle.equalsIgnoreCase("One Requisition With All Titles")){
207             reqList = new ArrayList(0);
208             HashMap<String, String> dataMap = new HashMap<String, String>();
209             List<OleOrderRecord> oleOrderRecordList = oleOrderRecords.getRecords();
210             requisitionDocument = null;
211             List<OleOrderRecord> validRecords = new ArrayList<OleOrderRecord>();
212             for (OleOrderRecord oleOrderRecord : oleOrderRecordList) {
213                 oleOrderRecord.getMessageMap().put("isApoRule", false);
214                 String isValidRecord = ((oleOrderRecord.getMessageMap().get("isValidRecord"))).toString();
215                 if ("true".equalsIgnoreCase(isValidRecord)) {
216                     isValidBFN = ((oleOrderRecord.getMessageMap().get("isValidBFN"))).toString();
217                     if (isValidBFN.equalsIgnoreCase("true")) {
218                         String[] isbnDupFlag = (((List) oleOrderRecord.getMessageMap().get("rulesEvaluated")).get(0)).toString().split(":");
219                         if ("false".equalsIgnoreCase(isbnDupFlag[1].trim()) && oleOrderRecord.getOleTxRecord() != null && oleOrderRecord.getOleBibRecord() != null) {
220                             validRecords.add(oleOrderRecord);
221                         }
222                     }
223                 }
224             }
225             try {
226                 if(oleOrderRecordList.size() == validRecords.size()){
227                     requisitionDocument = createRequisitionDocument();
228                     requisitionDocument.setRequisitionSourceCode(oleOrderRecordList.get(0).getOleTxRecord().getRequisitionSource());
229                     if (oleOrderRecordList != null && oleOrderRecordList.size() > 0) {
230                         if (oleOrderRecordList.get(0).getOleTxRecord().getOrderType() != null) {
231                             Map purchaseOrderTypeMap = new HashMap();
232                             purchaseOrderTypeMap.put("purchaseOrderType", oleOrderRecordList.get(0).getOleTxRecord().getOrderType());
233                             org.kuali.rice.krad.service.BusinessObjectService
234                                     businessObjectService = SpringContext.getBean(org.kuali.rice.krad.service.BusinessObjectService.class);
235                             List<PurchaseOrderType> purchaseOrderTypeDocumentList = (List) businessObjectService.findMatching(PurchaseOrderType.class, purchaseOrderTypeMap);
236                             if (purchaseOrderTypeDocumentList != null && purchaseOrderTypeDocumentList.size() > 0) {
237                                 requisitionDocument.setPurchaseOrderTypeId(purchaseOrderTypeDocumentList.get(0).getPurchaseOrderTypeId());
238                             }
239                         }
240                         else {
241                             requisitionDocument.setPurchaseOrderTypeId(OLEConstants.DEFAULT_ORDER_TYPE_VALUE);
242                         }
243                         setDocumentValues(requisitionDocument, oleOrderRecordList.get(0));
244                     }
245                     requisitionDocument.setItems(generateMultipleItemsForOneRequisition(oleOrderRecordList));
246                     /*requisitionDocument.setLicensingRequirementCode("NLR");*/
247                     // Checking the APO rules
248                     RequisitionService requisitionService = SpringContext.getBean(RequisitionService.class);
249                     requisitionDocument.getDocumentHeader().getWorkflowDocument();
250                     //boolean apoRuleFlag = requisitionService.isAutomaticPurchaseOrderAllowed(requisitionDocument);
251                     docNumber = requisitionCreateDocumentService.saveRequisitionDocuments(requisitionDocument);
252                     reqList.add(requisitionDocument.getPurapDocumentIdentifier());
253                 }
254                 else {
255                     if(validRecords != null && validRecords.size() > 0){
256                         List<String> uuidList = new ArrayList<>();
257                         for(int recCount = 0;recCount<validRecords.size();recCount++) {
258                             uuidList.add(validRecords.get(recCount).getOleBibRecord().getBibUUID());
259                         }
260                         getDocstoreClientLocator().getDocstoreClient().deleteBibs(uuidList);
261                     }
262                 }
263             }
264             catch(Exception ex){
265                 LOG.error("####Rollback####" + ex);
266                 ex.printStackTrace();
267             }
268         }
269     }
270 
271 
272     /**
273      * To create the Requisition document object
274      *
275      * @return OleRequisitionDocument
276      */
277     protected OleRequisitionDocument createRequisitionDocument() throws WorkflowException {
278         String user;
279         if (GlobalVariables.getUserSession() == null) {
280             kualiConfigurationService = SpringContext.getBean(ConfigurationService.class);
281             user = kualiConfigurationService.getPropertyValueAsString(OleSelectNotificationConstant.ACCOUNT_DOCUMENT_INTIATOR);
282             if(LOG.isInfoEnabled()){
283                 LOG.info("createRequisitionDocument - user from session"+user);
284             }
285             GlobalVariables.setUserSession(new UserSession(user));
286         }
287 
288         return (OleRequisitionDocument) SpringContext.getBean(DocumentService.class).getNewDocument(FinancialDocumentTypeCodes.REQUISITION);
289     }
290 
291     private void setUserFromStaffUpload(OleOrderRecords oleOrderRecords) {
292         List<OleOrderRecord> oleOrderRecordList = oleOrderRecords.getRecords();
293         String user = null;
294         if (oleOrderRecordList.size() > 0) {
295             user = (String) oleOrderRecordList.get(0).getMessageMap().get(OLEConstants.USER_ID);
296             if (user == null) {
297                 user = getConfigurationService().getPropertyValueAsString(
298                         OleSelectNotificationConstant.ACCOUNT_DOCUMENT_INTIATOR);
299             }
300             GlobalVariables.setUserSession(new UserSession(user));
301         }
302     }
303 
304     /**
305      * To create the requisition document
306      *
307      * @param requisitionDocument OleRequisitionDocument
308      * @param oleOrderRecord      OleOrderRecord
309      * @return RequisitionDocument
310      */
311     protected RequisitionDocument setDocumentValues(OleRequisitionDocument requisitionDocument, OleOrderRecord oleOrderRecord) throws Exception {
312         // ******************Document Overview Section******************
313         requisitionDocument.getDocumentHeader().setDocumentDescription(getDocumentDescription(requisitionDocument, oleOrderRecord));
314         requisitionDocument.setStatusCode(PurapConstants.RequisitionStatuses.APPDOC_IN_PROCESS);
315         /**
316          * Commented vendorPoNumber based on JIRA-2842
317          */
318         //requisitionDocument.setVendorPoNumber(oleOrderRecord.getOleTxRecord().getVendorNumber());
319         requisitionDocument.setVendorPoNumber(oleOrderRecord.getOleTxRecord().getVendorItemIdentifier());
320         // ******************Financial Document Detail Section******************
321         // ******************Requisition Detail Section******************
322         requisitionDocument.setChartOfAccountsCode(oleOrderRecord.getOleTxRecord().getChartCode());
323         requisitionDocument.setOrganizationCode(oleOrderRecord.getOleTxRecord().getOrgCode());
324         requisitionDocument.setDocumentFundingSourceCode(oleOrderRecord.getOleTxRecord().getFundingSource());
325         requisitionDocument.setUseTaxIndicator(true);//oleOrderRecord.getOleTxRecord().getUseTaxIndicator()
326         // ******************Delivery Section******************
327         setDeliveryDetails(requisitionDocument, oleOrderRecord);
328         requisitionDocument.setDeliveryCampusCode(oleOrderRecord.getOleTxRecord().getDeliveryCampusCode());
329         // ******************Vendor Section******************
330         setVendorDetails(requisitionDocument, oleOrderRecord);
331         // ******************Items Section******************
332         // ******************Capital Assets Section******************
333         // ******************Payment INfo Section******************
334         // ******************Additional Institutional Info Section******************
335         requisitionDocument.setPurchaseOrderTransmissionMethodCode(oleOrderRecord.getOleTxRecord().getMethodOfPOTransmission());//FAX
336         requisitionDocument.setPurchaseOrderCostSourceCode(oleOrderRecord.getOleTxRecord().getCostSource());//CON
337         requisitionDocument.setRequestorPersonName(getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.REQUESTOR_PERSON_NAME));
338         requisitionDocument.setRequestorPersonPhoneNumber(getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.REQUESTOR_PERSON_PHONE_NUMBER));
339         requisitionDocument.setRequestorPersonEmailAddress(getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.REQUESTOR_PERSON_EMAIL_ADDRESS));
340         // ******************Account Summary Section******************
341         // ******************View Related Documents Section******************
342         // ******************View Payment History Section******************
343         // ******************Notes and Attachments Section******************
344         // ******************Ad Hoc Recipients Section******************
345         // ******************Route Log Section ******************
346         // ******************Common Document Properties..******************
347         requisitionDocument.setOrganizationAutomaticPurchaseOrderLimit(new KualiDecimal(getConfigurationService().getPropertyValueAsString(VendorPropertyConstants.VENDOR_CONTRACT_DEFAULT_APO_LIMIT)));
348         requisitionDocument.setPurchaseOrderAutomaticIndicator(Boolean.parseBoolean(getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.PURCHASE_ORDER_AUTOMATIC_INDICATIOR)));
349         requisitionDocument.setReceivingDocumentRequiredIndicator(oleOrderRecord.getOleTxRecord().isReceivingRequired());
350         requisitionDocument.setPaymentRequestPositiveApprovalIndicator(oleOrderRecord.getOleTxRecord().isPayReqPositiveApprovalReq());
351         requisitionDocument.setRequisitionSourceCode(oleOrderRecord.getOleTxRecord().getRequisitionSource());
352         return requisitionDocument;
353 
354     }
355 
356     /**
357      * To set the delivery details for the Requisition Document
358      *
359      * @param requisitionDocument OleRequisitionDocument
360      * @param oleOrderRecord      OleOrderRecord
361      * @return requisitionDocument OleRequisitionDocument
362      */
363     private void setDeliveryDetails(OleRequisitionDocument requisitionDocument, OleOrderRecord oleOrderRecord) {
364         if (LOG.isDebugEnabled())
365             LOG.debug("bibInfoBean.getDeliveryBuildingCode----------->" + oleOrderRecord.getOleTxRecord().getBuildingCode());
366 
367         if (oleOrderRecord.getOleTxRecord().getDeliveryCampusCode() != null && oleOrderRecord.getOleTxRecord().getBuildingCode() != null) {
368             Building building = vendorService.getBuildingDetails(oleOrderRecord.getOleTxRecord().getDeliveryCampusCode(), oleOrderRecord.getOleTxRecord().getBuildingCode());
369             if (building != null) {
370                 requisitionDocument.setDeliveryBuildingCode(building.getBuildingCode());
371                 requisitionDocument.setDeliveryCampusCode(building.getCampusCode());
372                 requisitionDocument.setDeliveryBuildingLine1Address(building.getBuildingStreetAddress());
373                 requisitionDocument.setDeliveryBuildingName(building.getBuildingName());
374                 requisitionDocument.setDeliveryCityName(building.getBuildingAddressCityName());
375                 requisitionDocument.setDeliveryStateCode(building.getBuildingAddressStateCode());
376                 requisitionDocument.setDeliveryPostalCode(building.getBuildingAddressZipCode());
377                 requisitionDocument.setDeliveryCountryCode(building.getBuildingAddressCountryCode());
378                 requisitionDocument.setDeliveryBuildingRoomNumber(getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.DELIVERY_DEFAULT_ROOM_NUMBER));
379                 requisitionDocument.setDeliveryToName(getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.DELIVERY_TO_NAME));
380 
381 
382                 requisitionDocument.setBillingCountryCode(building.getBuildingCode());
383                 requisitionDocument.setBillingLine1Address(building.getBuildingStreetAddress());
384                 requisitionDocument.setBillingName(building.getBuildingName());
385                 requisitionDocument.setBillingCityName(building.getBuildingAddressCityName());
386                 requisitionDocument.setBillingStateCode(building.getBuildingAddressStateCode());
387                 requisitionDocument.setBillingPostalCode(building.getBuildingAddressZipCode());
388                 requisitionDocument.setBillingCountryCode(building.getBuildingAddressCountryCode());
389                 requisitionDocument.setBillingPhoneNumber(getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.BILLING_PHONE_NUMBER));
390 
391             }
392         }
393     }
394 
395     /**
396      * To set the vendor details for the Requisition Document
397      *
398      * @param requisitionDocument OleRequisitionDocument
399      * @return requisitionDocument OleRequisitionDocument
400      */
401     private void setVendorDetails(OleRequisitionDocument requisitionDocument, OleOrderRecord oleOrderRecord) {
402         if (oleOrderRecord.getOleTxRecord().getVendorNumber() != null) {
403             VendorDetail vendorDetail = vendorService.getVendorDetail(oleOrderRecord.getOleTxRecord().getVendorNumber());
404 
405             requisitionDocument.setVendorCustomerNumber(oleOrderRecord.getOleTxRecord().getVendorInfoCustomer());
406             requisitionDocument.setVendorNumber(oleOrderRecord.getOleTxRecord().getVendorNumber());
407             requisitionDocument.setVendorNumber(vendorDetail.getVendorNumber());
408             requisitionDocument.setVendorName(vendorDetail.getVendorName());
409             requisitionDocument.setVendorHeaderGeneratedIdentifier(vendorDetail.getVendorHeaderGeneratedIdentifier());
410             requisitionDocument.setVendorDetailAssignedIdentifier(vendorDetail.getVendorDetailAssignedIdentifier());
411             requisitionDocument.setVendorDetail(vendorDetail);
412             String deliveryCampus = oleOrderRecord.getOleTxRecord().getDeliveryCampusCode();
413             Integer headerId = null;
414             Integer detailId = null;
415             int dashInd = vendorDetail.getVendorNumber().indexOf('-');
416             // make sure there's at least one char before and after '-'
417             if (dashInd > 0 && dashInd < vendorDetail.getVendorNumber().length() - 1) {
418                 headerId = new Integer(vendorDetail.getVendorNumber().substring(0, dashInd));
419                 detailId = new Integer(vendorDetail.getVendorNumber().substring(dashInd + 1));
420             }
421             VendorAddress vendorAddress = vendorService.getVendorDefaultAddress(headerId, detailId, VendorConstants.AddressTypes.PURCHASE_ORDER, deliveryCampus);
422             setVendorAddress(vendorAddress, requisitionDocument);
423 
424             List<VendorContract> vendorContracts = vendorDetail.getVendorContracts();
425             for (Iterator<VendorContract> vendorContract = vendorContracts.iterator(); vendorContract.hasNext(); ) {
426                 requisitionDocument.setVendorContractGeneratedIdentifier((vendorContract.next()).getVendorContractGeneratedIdentifier());
427             }
428         }
429 
430     }
431 
432     /**
433      * To generate single Item list for the Requisition Document
434      *
435      * @param oleOrderRecord OleOrderRecord
436      * @return ArrayList
437      */
438     private List<RequisitionItem> generateItemList(OleOrderRecord oleOrderRecord) throws Exception {
439         List<RequisitionItem> items = new ArrayList<RequisitionItem>();
440         int itemLineNumber = 1;
441         items.add(createRequisitionItem(oleOrderRecord, itemLineNumber));
442         return items;
443     }
444     /*private List<Note> generateBoNotesList(BibInfoBean bibInfoBean) {
445         List<Note> notes = new ArrayList<Note>();
446         notes.add(createNotes(bibInfoBean));
447         return notes;
448     }*/
449 
450     /**
451      * To generate multiple Item list for the Requisition Document
452      *
453      * @param oleOrderRecordList List<OleOrderRecord>
454      * @return ArrayList
455      */
456     private List<RequisitionItem> generateMultipleItemsForOneRequisition(List<OleOrderRecord> oleOrderRecordList) throws Exception {
457         List<RequisitionItem> items = new ArrayList<RequisitionItem>();
458         int itemLineNumber = 0;
459         // set items to document
460         for (OleOrderRecord oleOrderRecord : oleOrderRecordList) {
461             itemLineNumber++;
462             items.add(createRequisitionItem(oleOrderRecord, itemLineNumber));
463         }
464         return items;
465     }
466 
467     /**
468      * To create the requisition item for the Requisition Document.
469      *
470      * @param oleOrderRecord OleOrderRecord
471      * @return RequisitionItem
472      */
473     @SuppressWarnings("deprecation")
474     protected RequisitionItem createRequisitionItem(OleOrderRecord oleOrderRecord, int itemLineNumber) throws Exception {
475         OleRequisitionItem item = new OleRequisitionItem();
476         item.setOleOrderRecord(oleOrderRecord);
477         //item.setBibInfoBean(bibInfoBean);
478         item.setItemLineNumber(itemLineNumber);
479         item.setItemUnitOfMeasureCode(getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.UOM));
480         item.setItemQuantity(new KualiDecimal(oleOrderRecord.getOleTxRecord().getQuantity()));
481         //item.setItemDescription((String) ((List) oleOrderRecord.getOleBibRecord().getBibAssociatedFieldsValueMap().get("ISBN_search")).get(0));
482         setItemDescription(oleOrderRecord,item);
483         item.setItemUnitPrice(new BigDecimal(oleOrderRecord.getOleTxRecord().getListPrice()));
484         item.setItemTypeCode(oleOrderRecord.getOleTxRecord().getItemType());
485         item.setItemListPrice(new KualiDecimal(oleOrderRecord.getOleTxRecord().getListPrice()));
486         item.setItemLocation(oleOrderRecord.getOleTxRecord().getDefaultLocation());
487         if (oleOrderRecord.getOleTxRecord().getOleDonors()!=null && oleOrderRecord.getOleTxRecord().getOleDonors().size()>0){
488             List<OLELinkPurapDonor> oleLinkPurapDonorList=new ArrayList<>();
489             for (String donor:oleOrderRecord.getOleTxRecord().getOleDonors()){
490                 OLELinkPurapDonor oleLinkPurapDonor=new OLELinkPurapDonor();
491                 oleLinkPurapDonor.setDonorCode(donor);
492                 oleLinkPurapDonorList.add(oleLinkPurapDonor);
493             }
494             item.setOleDonors(oleLinkPurapDonorList);
495         }
496         if (ObjectUtils.isNotNull(oleOrderRecord.getOleBibRecord().getBibUUID())) {
497             item.setItemTitleId(oleOrderRecord.getOleBibRecord().getBibUUID());
498         }
499 
500         //if (item.getItemType() == null) {
501         org.kuali.ole.module.purap.businessobject.ItemType itemType = businessObjectService.findBySinglePrimaryKey(org.kuali.ole.module.purap.businessobject.ItemType.class, "ITEM");
502         item.setItemType(itemType);
503         // }
504 
505         RequisitionAccount requisitionAccount = new RequisitionAccount();
506         /**
507          * Below code commented based on JIRA-2617.
508          */
509         //requisitionAccount.setChartOfAccountsCode(oleOrderRecord.getOleTxRecord().getChartCode());
510         requisitionAccount.setChartOfAccountsCode(oleOrderRecord.getOleTxRecord().getItemChartCode());
511         requisitionAccount.setAccountNumber(oleOrderRecord.getOleTxRecord().getAccountNumber());
512         requisitionAccount.setFinancialObjectCode(oleOrderRecord.getOleTxRecord().getObjectCode());
513         requisitionAccount.setDebitCreditCode(OLEConstants.GL_DEBIT_CODE);
514         if (oleOrderRecord.getOleTxRecord().getListPrice() != null) {
515             requisitionAccount.setAmount(new KualiDecimal(oleOrderRecord.getOleTxRecord().getListPrice()));
516         }
517         if (oleOrderRecord.getOleTxRecord().getPercent() != null) {
518             requisitionAccount.setAccountLinePercent(new BigDecimal(oleOrderRecord.getOleTxRecord().getPercent()));
519         }
520         if (oleOrderRecord.getOleTxRecord().getAccountNumber() != null) {
521             List<PurApAccountingLine> sourceAccountingLines = item.getSourceAccountingLines();
522             if (sourceAccountingLines.size() == 0) {
523                 sourceAccountingLines = new ArrayList<PurApAccountingLine>(0);
524             }
525             sourceAccountingLines.add((PurApAccountingLine) requisitionAccount);
526             item.setSourceAccountingLines(sourceAccountingLines);
527         }
528 
529         /*OleRequestor oleRequestor = checkRequestorExist(oleOrderRecord);
530         if(oleRequestor == null) {
531             oleRequestor =  new OleRequestor();
532             oleRequestor = saveRequestor(oleOrderRecord, oleRequestor);
533         }
534 
535         item.setOleRequestor(oleRequestor);
536 
537         item.setRequestorId(oleRequestor.getRequestorId());
538          //item.setRequestSourceTypeId(oleRequestSourceService.getRequestSourceTypeId(bibInfoBean.getRequestSource()));
539         item.setRequestorFirstName(oleRequestor.getRequestorFirstName());
540 
541         item.setRequestorLastName(oleRequestor.getRequestorLastName());*/
542         //item.setRequestSourceUrl(oleOrderRecord.getOleTxRecord().getRequestSourceUrl());
543 
544         //getOleRequestorService().saveRequestor(oleRequestor);
545 
546         String requestorType = null;
547 
548         if (requestorType == null || "".equals(requestorType)) {
549 
550             requestorType = OleSelectConstant.REQUESTOR_TYPE_STAFF;
551         }
552 
553         int requestorTypeId = getRequestorTypeId(requestorType);
554         item.setRequestorTypeId(requestorTypeId);
555         if (oleOrderRecord.getOleTxRecord().getRequisitionSource().equals(OleSelectConstant.REQUISITON_SRC_TYPE_WEBFORM)) {//&& !oleOrderRecord.getOleTxRecord().getRequestersNotes().trim().equals("")) {
556             OleRequisitionNotes note = new OleRequisitionNotes();
557             Map notes = new HashMap();
558             String noteType = OleSelectConstant.REQUESTOR_NOTES_PRE_ORDER_SERVICE;
559             notes.put("noteType", noteType);
560             List<OleNoteType> noteTypeList = (List) businessObjectService.findMatching(org.kuali.ole.select.businessobject.OleNoteType.class, notes);
561             note.setNoteTypeId(noteTypeList.get(0).getNoteTypeId());
562             //note.setNote(oleOrderRecord.getOleTxRecord().getRequestersNotes());
563             item.getNotes().add(note);
564         }
565         setItemDescription(oleOrderRecord, item);
566         populateValuesFromProfileAttributesAndDataMapping(item);
567         return item;
568     }
569 
570     private void populateValuesFromProfileAttributesAndDataMapping(OleRequisitionItem singleItem){
571         DataCarrierService dataCarrierService = GlobalResourceLoader.getService(org.kuali.ole.OLEConstants.DATA_CARRIER_SERVICE);
572         OleTxRecord oleTxRecord = (OleTxRecord)dataCarrierService.getData(org.kuali.ole.OLEConstants.OLE_TX_RECORD);
573         if(oleTxRecord != null){
574             if(oleTxRecord.getRequestorName() != null){
575                 String fullName = oleTxRecord.getRequestorName();
576                 String[] requestorNames = fullName.split(", ");
577                 if(requestorNames.length == 2){
578                     String lastName = requestorNames[0];
579                     String firstName = requestorNames[1];
580                     Map<String,String> requestorNameMap = new HashMap<>();
581                     requestorNameMap.put(OLEConstants.OlePersonRequestorLookupable.FIRST_NAME,firstName);
582                     requestorNameMap.put(OLEConstants.OlePersonRequestorLookupable.LAST_NAME,lastName);
583                     List<OLERequestorPatronDocument> olePatronDocumentList = new ArrayList<OLERequestorPatronDocument>();
584                     olePatronDocumentList = getOleSelectDocumentService().getPatronDocumentListFromWebService();
585                     HashMap<String, List<OLERequestorPatronDocument>> patronListMap = new HashMap<String, List<OLERequestorPatronDocument>>();
586                     patronListMap.put(requisitionDocument.getDocumentNumber(), olePatronDocumentList);
587                     getOlePatronDocumentList().setPatronListMap(patronListMap);
588                     if(olePatronDocumentList != null && olePatronDocumentList.size()>0){
589                         for(int recCount = 0;recCount < olePatronDocumentList.size();recCount++) {
590                             if(olePatronDocumentList.get(recCount).getFirstName().equalsIgnoreCase(firstName) && olePatronDocumentList.get(recCount).getLastName().equalsIgnoreCase(lastName)){
591                                 String patronId = olePatronDocumentList.get(recCount).getOlePatronId();
592                                 singleItem.setRequestorId(patronId);
593                                 singleItem.setRequestorFirstName(fullName);
594                                 break;
595                             }
596                         }
597                     }
598                 }
599             }
600             singleItem.setItemDiscount(new KualiDecimal(oleTxRecord.getDiscount()));
601             singleItem.setItemDiscountType(oleTxRecord.getDiscountType());
602             if(singleItem.getItemDiscount() != null && singleItem.getItemDiscountType() == null){
603                 singleItem.setItemDiscountType(OLEConstants.PERCENTAGE);
604             }
605             singleItem.setItemUnitPrice(getOlePurapService().calculateDiscount(singleItem));
606             /*if(requisitionDocument.getVendorDetail().getVendorHeader().getVendorForeignIndicator()) {
607                 singleItem.setItemForeignDiscountType("#");
608                 if(!singleItem.getItemListPrice().equals(new KualiDecimal(0.0))){
609                     singleItem.setItemForeignListPrice(singleItem.getItemListPrice());
610                 }
611                 if(!singleItem.getItemDiscount().equals(new KualiDecimal(0.0))){
612                     singleItem.setItemForeignDiscount(singleItem.getItemDiscount());
613                 }
614                 getOlePurapService().calculateForeignCurrency(singleItem);
615                 KualiDecimal itemTotalPrice = singleItem.getItemForeignUnitCost();
616                 requisitionDocument.setTotalDollarAmount(requisitionDocument.getTotalDollarAmount().add(itemTotalPrice));
617                 singleItem.setItemListPrice(new KualiDecimal(0.0));
618                 singleItem.setItemUnitPrice(new BigDecimal(0.0));
619                 singleItem.setItemDiscount(new KualiDecimal(0.0));
620                 singleItem.setItemDiscountType(null);
621             }*/
622             singleItem.setItemStatus(oleTxRecord.getItemStatus());
623 
624         }
625     }
626 
627     private void setItemDescription(OleOrderRecord oleOrderRecord, OleRequisitionItem item) throws Exception {
628 
629         String title = oleOrderRecord.getOleBibRecord().getBib().getTitle() != null ? oleOrderRecord.getOleBibRecord().getBib().getTitle()+ "," : "";
630         String author = oleOrderRecord.getOleBibRecord().getBib().getAuthor() != null ? oleOrderRecord.getOleBibRecord().getBib().getAuthor()+ "," : "";
631         String publisher = oleOrderRecord.getOleBibRecord().getBib().getPublisher() != null ? oleOrderRecord.getOleBibRecord().getBib().getPublisher()+ "," : "";
632         String isbn = oleOrderRecord.getOleBibRecord().getBib().getIsbn() != null ? oleOrderRecord.getOleBibRecord().getBib().getIsbn() + ",": "";
633         /*Map<String, ?> bibAssociatedFieldValuesMap = oleOrderRecord.getOleBibRecord().getBibAssociatedFieldsValueMap();
634         StringEscapeUtils stringEscapeUtils = new StringEscapeUtils();
635         List titleList = (List) bibAssociatedFieldValuesMap.get("Title_search");
636         String title = titleList != null && !titleList.isEmpty() ? (String) (titleList).get(0) + "," : "";
637         title = stringEscapeUtils.unescapeXml(title);
638         List authorList = (List) bibAssociatedFieldValuesMap.get("Author_search");
639         String author = (authorList != null && !authorList.isEmpty() ? (String) (authorList).get(0) + "," : "");
640         author = stringEscapeUtils.unescapeXml(author);
641         List publisherList = (List) bibAssociatedFieldValuesMap.get("Publisher_search");
642         String publisher = (publisherList != null && !publisherList.isEmpty() ? (String) (publisherList).get(0) + "," : "");
643         publisher = stringEscapeUtils.unescapeXml(publisher);
644         List isbnList = (List) bibAssociatedFieldValuesMap.get("020a");
645         String isbn = (isbnList != null && !isbnList.isEmpty() ? (String) (isbnList).get(0) + "," : "");*/
646         String description = title + author
647                 + publisher + isbn;
648         item.setItemDescription(description.substring(0, (description.lastIndexOf(","))));
649         item.setItemTitle(oleOrderRecord.getOleBibRecord().getBib().getTitle());
650         item.setItemAuthor(oleOrderRecord.getOleBibRecord().getBib().getAuthor());
651         item.setBibUUID(oleOrderRecord.getOleBibRecord().getBibUUID());
652 
653     }
654 
655     public int getRequestorTypeId(String requestorType) {
656         int requestorTypeId;
657         Map requestorTypeMap = new HashMap();
658         requestorTypeMap.put("requestorType", requestorType);
659         BusinessObjectService businessObjectService = SpringContext.getBean(org.kuali.rice.krad.service.BusinessObjectService.class);
660         List<OleRequestorType> requestorTypeIdList = (List) businessObjectService.findMatching(OleRequestorType.class, requestorTypeMap);
661         Iterator itr = requestorTypeIdList.iterator();
662         requestorTypeId = requestorTypeIdList.iterator().next().getRequestorTypeId();
663         return requestorTypeId;
664     }
665 
666     /**
667      * This method will check whether Requestor exist ,if exist returns existing record if not save the requester.
668      *
669      * @param oleOrderRecord
670      * @return OleRequestor
671      */
672     protected OleRequestor checkRequestorExist(OleOrderRecord oleOrderRecord) {
673 
674         String requestorFirstName = getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.FIRST_NAME);
675         String requestorLastName = getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.LAST_NAME);
676         String firstName;
677         String lastName;
678         Map requestorName = new HashMap();
679         requestorName.put("requestorFirstName", requestorFirstName);
680         requestorName.put("requestorLastName", requestorLastName);
681         List<OleRequestor> requestorList = (List) businessObjectService.findMatching(OleRequestor.class, requestorName);
682         if (requestorList.size() < 1) {
683             return null;
684         } else {
685             for (int i = 0; i < requestorList.size(); i++) {
686                 firstName = requestorList.get(i).getRequestorFirstName().toString();
687                 lastName = requestorList.get(i).getRequestorLastName().toString();
688                 if (requestorFirstName.equalsIgnoreCase(firstName) && requestorLastName.equalsIgnoreCase(lastName)) {
689                     return requestorList.get(i);
690                 }
691             }
692             return null;
693         }
694     }
695 
696     /**
697      * This method will set the values on OleRequestor and save.
698      *
699      * @return OleRequestor
700      */
701     protected OleRequestor saveRequestor(OleOrderRecord oleOrderRecord, OleRequestor oleRequestor) {
702         oleRequestor.setRequestorFirstName(getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.FIRST_NAME));
703         oleRequestor.setRequestorLastName(getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.LAST_NAME));
704         oleRequestor.setRequestorAddress1(getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.ADDRESS1));
705         oleRequestor.setRequestorAddress2(getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.ADDRESS2));
706         oleRequestor.setRequestorCityName(getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.CITY));
707         oleRequestor.setRequestorStateCode(getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.STATE_CODE));
708         oleRequestor.setRequestorPostalCode(getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.POSTAL_CODE));
709         oleRequestor.setRequestorCountryCode(getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.COUNTRY_CODE));
710         oleRequestor.setRequestorPhoneNumber(getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.PHONE_NUMBER));
711         oleRequestor.setRequestorEmail(getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.EMAIL));
712         oleRequestor.setRequestorSms(getConfigurationService().getPropertyValueAsString(PurapPropertyConstants.SMS));
713         oleRequestor.setRequestorTypeId(Integer.toString(getRequestorTypeId(OleSelectConstant.REQUESTOR_TYPE_BATCHINGEST)));
714         getOleRequestorService().saveRequestor(oleRequestor);
715         return oleRequestor;
716     }
717 
718 
719     /**
720      * Set the Vendor address of the given ID.
721      *
722      * @param vendorAddress       VendorAddress
723      * @param requisitionDocument RequisitionDocument
724      */
725     protected void setVendorAddress(VendorAddress vendorAddress, RequisitionDocument requisitionDocument) {
726 
727         if (vendorAddress != null) {
728             requisitionDocument.setVendorAddressGeneratedIdentifier(vendorAddress.getVendorAddressGeneratedIdentifier());
729             requisitionDocument.setVendorAddressInternationalProvinceName(vendorAddress.getVendorAddressInternationalProvinceName());
730             requisitionDocument.setVendorLine1Address(vendorAddress.getVendorLine1Address());
731             requisitionDocument.setVendorLine2Address(vendorAddress.getVendorLine2Address());
732             requisitionDocument.setVendorCityName(vendorAddress.getVendorCityName());
733             requisitionDocument.setVendorStateCode(vendorAddress.getVendorStateCode());
734             requisitionDocument.setVendorPostalCode(vendorAddress.getVendorZipCode());
735             requisitionDocument.setVendorCountryCode(vendorAddress.getVendorCountryCode());
736         }
737 
738     }
739 
740     public String getDocumentDescription(OleRequisitionDocument requisitionDocument, OleOrderRecord oleOrderRecord) {
741 
742         String description = "";
743         description = ((requisitionDocument.getVendorName() != null && !requisitionDocument.getVendorName().isEmpty()) ? requisitionDocument.getVendorName() : OLEConstants.DOCUMENT_DESCRIPTION) + "_" +
744                 ((requisitionDocument.getOrderType() != null) ? requisitionDocument.getOrderType().getDescription() : OLEConstants.ORDER_TYPE) + "_" +
745                 ((olePurapService.getOperatorInitials() != null && !olePurapService.getOperatorInitials().isEmpty()) ? olePurapService.getOperatorInitials() : "") + "_" +
746                 ((requisitionDocument.getCurrentDateTime() != null) ? requisitionDocument.getCurrentDateTime().toString() : "") + "_" +
747                 ((oleOrderRecord.getOleTxRecord().getVendorItemIdentifier() != null && !oleOrderRecord.getOleTxRecord().getVendorItemIdentifier().isEmpty()) ? oleOrderRecord.getOleTxRecord().getVendorItemIdentifier() + "_" : "");
748         if (!description.equals("") && description != null)
749             description = description.substring(0, description.lastIndexOf("_"));
750         return description;
751     }
752 
753     public RequisitionCreateDocumentService getRequisitionCreateDocumentService() {
754         return requisitionCreateDocumentService;
755     }
756 
757     public void setRequisitionCreateDocumentService(RequisitionCreateDocumentService requisitionCreateDocumentService) {
758         this.requisitionCreateDocumentService = requisitionCreateDocumentService;
759     }
760 
761     public OleRequestorService getOleRequestorService() {
762         return oleRequestorService;
763     }
764 
765     public void setOleRequestorService(OleRequestorService oleRequestorService) {
766         this.oleRequestorService = oleRequestorService;
767     }
768 
769     public VendorService getVendorService() {
770         return vendorService;
771     }
772 
773     public void setVendorService(VendorService vendorService) {
774         this.vendorService = vendorService;
775     }
776 
777     public OleRequestSourceService getOleRequestSourceService() {
778         return oleRequestSourceService;
779     }
780 
781     public void setOleRequestSourceService(OleRequestSourceService oleRequestSourceService) {
782         this.oleRequestSourceService = oleRequestSourceService;
783     }
784 
785     public OlePurapService getOlePurapService() {
786         return olePurapService;
787     }
788 
789     public void setOlePurapService(OlePurapService olePurapService) {
790         this.olePurapService = olePurapService;
791     }
792 
793     public BibInfoService getBibInfoService() {
794         return bibInfoService;
795     }
796 
797     public void setBibInfoService(BibInfoService bibInfoService) {
798         this.bibInfoService = bibInfoService;
799     }
800 
801     public BusinessObjectService getBusinessObjectService() {
802         return businessObjectService;
803     }
804 
805     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
806         this.businessObjectService = businessObjectService;
807     }
808 
809     public List getReqList() {
810         return reqList;
811     }
812 
813     public ConfigurationService getConfigurationService() {
814         return kualiConfigurationService;
815     }
816 
817     public void setConfigurationService(ConfigurationService kualiConfigurationService) {
818         this.kualiConfigurationService = kualiConfigurationService;
819     }
820 
821 }