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.document;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.struts.upload.FormFile;
20  import org.kuali.ole.DataCarrierService;
21  import org.kuali.ole.batch.bo.OLEBatchProcessProfileBo;
22  import org.kuali.ole.module.purap.document.service.OlePurapService;
23  import org.kuali.ole.select.batch.service.RequisitionLoadTransactionsService;
24  import org.kuali.ole.select.batch.service.impl.RequisitionLoadTransactionsServiceImpl;
25  import org.kuali.ole.select.businessobject.BibInfoBean;
26  import org.kuali.ole.select.businessobject.OleLoadFailureRecords;
27  import org.kuali.ole.select.businessobject.OleLoadSumRecords;
28  import org.kuali.ole.select.service.BatchLoadService;
29  import org.kuali.ole.select.service.OleBatchIngestService;
30  import org.kuali.ole.select.service.impl.BatchLoadServiceImpl;
31  import org.kuali.ole.select.service.impl.OleBatchIngestServiceImpl;
32  import org.kuali.ole.sys.OLEConstants;
33  import org.kuali.ole.sys.businessobject.AcquisitionBatchUpload;
34  import org.kuali.ole.sys.context.SpringContext;
35  import org.kuali.ole.sys.document.FinancialSystemTransactionalDocumentBase;
36  import org.kuali.rice.core.api.datetime.DateTimeService;
37  import org.kuali.rice.core.api.mo.common.active.Inactivatable;
38  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
39  import org.kuali.rice.kew.api.WorkflowDocument;
40  import org.kuali.rice.kim.api.identity.Person;
41  import org.kuali.rice.kim.api.identity.PersonService;
42  import org.kuali.rice.krad.service.DocumentHeaderService;
43  import org.kuali.rice.krad.service.KRADServiceLocator;
44  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
45  import org.kuali.rice.krad.util.GlobalVariables;
46  
47  import java.io.InputStream;
48  import java.math.BigDecimal;
49  import java.text.SimpleDateFormat;
50  import java.util.*;
51  
52  public class AcquisitionBatchInputFileDocument extends FinancialSystemTransactionalDocumentBase implements Inactivatable {
53      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AcquisitionBatchInputFileDocument.class);
54      private static final String dateTimeFormat = "MMddHHmm";
55      private static final String dateFormat = "MMddyy";
56      protected boolean active;
57      protected BatchLoadService batchLoadService;
58      protected OlePurapService olePurapService;
59  
60      @Override
61      public boolean isActive() {
62          return active;
63      }
64  
65      public void setActive(boolean active) {
66          this.active = active;
67      }
68  
69      public boolean prepareForLoadSummary(InputStream fileContents, List<OleLoadFailureRecords> oleLoadFailureRecordsList, OleLoadSumRecords oleLoadSumRecords, FormFile uploadedFile, AcquisitionBatchUpload acquisitionBatchUpload) {
70  
71          LOG.debug("start --- prepareForLoadSummary method of AcquisitionBatchInputFileDocument");
72  
73          BatchLoadService batchLoadService = getBatchLoadService();
74          ;
75          List bibIsbnList = new ArrayList(0);
76          List bibVendorPoNumberList = new ArrayList(0);
77          List<BibInfoBean> reqFailureList = new ArrayList<BibInfoBean>(0);
78          List<BibInfoBean> titleFailureList = new ArrayList<BibInfoBean>(0);
79          List<BibInfoBean> isbnDupFailureList = new ArrayList<BibInfoBean>(0);
80          List<BibInfoBean> vendorPoNumberDupFailureList = new ArrayList<BibInfoBean>(0);
81          if (fileContents != null) {
82              try {
83  
84                  // get bibinfobean list values from xml
85                  InputStream fileContent = fileContents;
86                  OleBatchIngestService oleBatchIngestService = SpringContext.getBean(OleBatchIngestServiceImpl.class);
87                  String xml = oleBatchIngestService.transformRawDataToXml(fileContent);
88                  List<BibInfoBean> bibInfoBeanList = batchLoadService.getBibInfoBeanList(xml);
89                  if (bibInfoBeanList.size() <= 0) {
90                      return false;
91                  }
92                  batchLoadService.foundAllDuplicateRecords(bibInfoBeanList, reqFailureList, titleFailureList, isbnDupFailureList, vendorPoNumberDupFailureList, bibIsbnList);
93                  List docIsbnList = batchLoadService.getDocIsbnList(bibIsbnList);
94                  int dupRecords = batchLoadService.getNoOfDupIsbnRecords(docIsbnList, bibIsbnList);
95                  List<BibInfoBean> isbnFailureList = batchLoadService.getIsbnFailureRecordsList(docIsbnList, bibInfoBeanList);
96                  isbnFailureList.addAll(isbnDupFailureList);
97                  List vendorPoNumberList = batchLoadService.getVendorPoNumberList(bibInfoBeanList);
98                  dupRecords += vendorPoNumberList.size();
99                  List<BibInfoBean> vendorPoNumberFailureList = batchLoadService.getVendorPoNumberFailureRecordsList(vendorPoNumberList, bibInfoBeanList);
100                 int sucRecords = bibInfoBeanList.size();
101                 List reqList = new ArrayList(0);
102                 RequisitionLoadTransactionsService requisitionLoadTransactionsService = SpringContext.getBean(RequisitionLoadTransactionsServiceImpl.class);
103                 reqList = requisitionLoadTransactionsService.saveRequisitionDocument(bibInfoBeanList);
104                 if (reqFailureList.size() != 0) {
105                     reqFailureList.addAll(batchLoadService.getRequisitionFailureRecords(reqList, bibInfoBeanList));
106                 } else {
107                     reqFailureList = batchLoadService.getRequisitionFailureRecords(reqList, bibInfoBeanList);
108                 }
109                 batchLoadService.getDupRecordsCount(reqFailureList, titleFailureList, isbnDupFailureList, vendorPoNumberDupFailureList);
110                 List<BibInfoBean> bibFailureRecordsList = batchLoadService.getBibFailureRecordsList(isbnFailureList, vendorPoNumberFailureList, reqFailureList, titleFailureList, vendorPoNumberDupFailureList);
111                 String failureRawData = oleBatchIngestService.getRawXml(uploadedFile.getInputStream(), bibFailureRecordsList);
112                 String destinationPath = batchLoadService.getDestinationPath();
113 
114                 // saving the successful records
115                 batchLoadService.saveSuccessRecord(oleLoadSumRecords, dupRecords, bibInfoBeanList.size(), sucRecords, acquisitionBatchUpload.getBatchDescription(), new BigDecimal(acquisitionBatchUpload.getBatchLoadProfile()), uploadedFile.getFileName());
116 
117                 if (bibFailureRecordsList.size() > 0) {
118                     batchLoadService.createErrorMrkFile(failureRawData, oleLoadSumRecords.getAcqLoadSumId());
119                 }
120                 if (reqList.size() > 0) {
121                     batchLoadService.createReqIdTextFile(reqList, oleLoadSumRecords.getAcqLoadSumId());
122                 }
123                 batchLoadService.saveAllFailureRecords(isbnFailureList, vendorPoNumberFailureList, reqFailureList, titleFailureList, vendorPoNumberDupFailureList, oleLoadSumRecords.getAcqLoadSumId());
124                 // oleLoadFailureRecordsList.addAll(getLoadFailureRecordsList(oleLoadSumRecords.getAcqLoadSumId(),batchLoadService));
125 
126                 oleLoadSumRecords = getLoadSummaryAndFailureRecords(oleLoadSumRecords.getAcqLoadSumId(), oleLoadFailureRecordsList);
127                 olePurapService = getOlePurapService();
128                 String operatorInitials = olePurapService.getOperatorInitials();
129                 this.getDocumentHeader().setDocumentDescription(OLEConstants.MANUAL_INGEST_DOCUMENT_DESCRIPTION + (StringUtils.isEmpty(operatorInitials) ? "" : "_" + operatorInitials) + "_" + getCurrentDate());
130             } catch (Exception ex) {
131                 LOG.error("Exception while loading summary " + ex);
132                 throw new RuntimeException(ex);
133             }
134 
135         }
136 
137         LOG.debug("end --- prepareForLoadSummary method of AcquisitionBatchInputFileDocument");
138 
139         return true;
140     }
141 
142 
143     public OleLoadSumRecords getLoadSummaryAndFailureRecords(int acqLoadSumId, List<OleLoadFailureRecords> oleLoadFailureRecordsList) {
144         OleLoadSumRecords oleLoadSumRecords = null;
145         try {
146 
147             LOG.debug("start --- getLoadSummaryAndFailureRecords method of AcquisitionBatchInputFileDocument");
148 
149             BatchLoadService batchLoadService = getBatchLoadService();
150             ;
151             Map loadRecordsMap = new HashMap();
152             loadRecordsMap.put("acqLoadSumId", acqLoadSumId);
153             oleLoadSumRecords = batchLoadService.getOleLoadSumRecords(loadRecordsMap);
154             oleLoadFailureRecordsList.addAll(getLoadFailureRecordsList(acqLoadSumId, batchLoadService));
155 
156             LOG.debug("end --- getLoadSummaryAndFailureRecords method of AcquisitionBatchInputFileDocument");
157 
158 
159         } catch (Exception ex) {
160             LOG.error("Exception while loading summary & failure records" + ex);
161             throw new RuntimeException(ex);
162         }
163         return oleLoadSumRecords;
164     }
165 
166     public OleLoadSumRecords getLoadSummaryAndFailureRecordsByLoadSummaryId(int acqLoadSumId, List<OleLoadFailureRecords> oleLoadFailureRecordsList) {
167         OleLoadSumRecords oleLoadSumRecords = null;
168         try {
169             oleLoadSumRecords = getLoadSummaryAndFailureRecords(acqLoadSumId, oleLoadFailureRecordsList);
170             setDocumentHeader(SpringContext.getBean(DocumentHeaderService.class).getDocumentHeaderById(oleLoadSumRecords.getDocumentNumber()));
171             setDocumentNumber(oleLoadSumRecords.getDocumentNumber());
172             Person principalPerson = SpringContext.getBean(PersonService.class).getPerson(GlobalVariables.getUserSession().getPerson().getPrincipalId());
173             WorkflowDocument doc;
174             getDocumentHeader().setWorkflowDocument(KRADServiceLocatorWeb.getWorkflowDocumentService().loadWorkflowDocument(getDocumentNumber(), principalPerson));
175             int totalPOCount = oleLoadSumRecords.getPoToalCount(oleLoadSumRecords.getAcqLoadPoTotCount());
176             if (totalPOCount > 0) {
177                 oleLoadSumRecords.setAcqLoadPoTotCount(totalPOCount);
178             }
179         } catch (Exception ex) {
180             LOG.error("Exception while loading summary & failure records by load summary id ", ex);
181             throw new RuntimeException(ex);
182         }
183         return oleLoadSumRecords;
184     }
185 
186     public OleLoadSumRecords getLoadSummaryAndFailureRecordsByDocId(String docId, List<OleLoadFailureRecords> oleLoadFailureRecordsList) {
187         OleLoadSumRecords oleLoadSumRecords = null;
188         try {
189 
190             LOG.debug("start --- getLoadSummaryAndFailureRecords method of AcquisitionBatchInputFileDocument");
191             BatchLoadService batchLoadService = getBatchLoadService();
192             ;
193             /*Map loadRecordsMap = new HashMap();
194             loadRecordsMap.put("documentNumber", docId);
195             oleLoadSumRecords = batchLoadService.getOleLoadSumRecords(loadRecordsMap);*/
196             oleLoadSumRecords = getOleLoadSumRecords(docId, batchLoadService);
197             oleLoadFailureRecordsList.addAll(getLoadFailureRecordsList(oleLoadSumRecords.getAcqLoadSumId(), batchLoadService));
198             LOG.debug("end --- getLoadSummaryAndFailureRecords method of AcquisitionBatchInputFileDocument");
199 
200             int totalPOCount = oleLoadSumRecords.getPoToalCount(oleLoadSumRecords.getAcqLoadPoTotCount());
201             if (totalPOCount >= -1) {
202                 //DataCarrierService dataCarrierService = GlobalResourceLoader.getService(org.kuali.ole.OLEConstants.DATA_CARRIER_SERVICE);
203                 //OLEBatchProcessProfileBo oleBatchProcessProfileBo = (OLEBatchProcessProfileBo) dataCarrierService.getData(org.kuali.ole.OLEConstants.BATCH_PROFILE_BO);
204                 OLEBatchProcessProfileBo oleBatchProcessProfileBo = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(OLEBatchProcessProfileBo.class, oleLoadSumRecords.getBatchProcessProfileId());
205                 if(oleBatchProcessProfileBo != null && oleBatchProcessProfileBo.getRequisitionsforTitle().equalsIgnoreCase("One Requisition With All Titles"))  {
206                     oleLoadSumRecords.setAcqLoadTotCount(1);
207                     if(totalPOCount == 0){
208                         oleLoadSumRecords.setAcqLoadTotBibCount(0);
209                         oleLoadSumRecords.setAcqLoadFailCount(1);
210                         oleLoadSumRecords.setAcqLoadSuccCount(0);
211                     }
212                     else if(totalPOCount == 1){
213                         oleLoadSumRecords.setAcqLoadSuccCount(1);
214                         oleLoadSumRecords.setAcqLoadFailCount(0);
215                     }
216                 }
217                 oleLoadSumRecords.setAcqLoadPoTotCount(totalPOCount);
218             }
219         } catch (Exception ex) {
220             LOG.error("Exception while loading summary & failure records by docid " + ex);
221             throw new RuntimeException(ex);
222         }
223         return oleLoadSumRecords;
224     }
225 
226     public List<OleLoadFailureRecords> getLoadFailureRecordsList(int acqLoadSumId, BatchLoadService batchLoadService) {
227         Map loadRecordsMap = new HashMap();
228         loadRecordsMap.put("acqLoadSumId", acqLoadSumId);
229         return batchLoadService.getOleFailureRecordsList(loadRecordsMap);
230     }
231 
232     /**
233      * Returns current Date in MMddyy(100311) format to be
234      * appended in the document description
235      *
236      * @return current DateTime in String
237      */
238     public String getCurrentDate() {
239         LOG.debug("Inside getCurrentDate()");
240         //Modified as per review comments OLE-24
241         DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
242         Date date = dateTimeService.getCurrentDate();
243         SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
244         String currentDate = sdf.format(date);
245         LOG.debug("End of getCurrentDate()");
246         return currentDate;
247     }
248 
249     public boolean isFailureRecords() {
250         String docId = this.getDocumentNumber();
251         List<OleLoadFailureRecords> oleLoadFailureRecordsList = new ArrayList<OleLoadFailureRecords>(0);
252         BatchLoadService batchLoadService = getBatchLoadService();
253         ;
254         OleLoadSumRecords oleLoadSumRecords = getOleLoadSumRecords(docId, batchLoadService);
255         oleLoadFailureRecordsList = getLoadFailureRecordsList(oleLoadSumRecords.getAcqLoadSumId(), batchLoadService);
256         if (oleLoadFailureRecordsList.size() > 0) {
257             return true;
258         } else {
259             return false;
260         }
261     }
262 
263     public OleLoadSumRecords getOleLoadSumRecords(String docId, BatchLoadService batchLoadService) {
264 
265         Map loadRecordsMap = new HashMap();
266         loadRecordsMap.put("documentNumber", docId);
267         OleLoadSumRecords oleLoadSumRecords = batchLoadService.getOleLoadSumRecords(loadRecordsMap);
268         return oleLoadSumRecords;
269     }
270 
271 
272     public BatchLoadService getBatchLoadService() {
273         if (batchLoadService == null) {
274             batchLoadService = SpringContext.getBean(BatchLoadServiceImpl.class);
275         }
276         return batchLoadService;
277     }
278 
279     @Override
280     public boolean answerSplitNodeQuestion(String nodeName) throws UnsupportedOperationException {
281         if (nodeName.equals(OLEConstants.IS_FAILURE_RECORDS)) {
282             return isFailureRecords();
283         }
284         return super.answerSplitNodeQuestion(nodeName);
285     }
286 
287     public OlePurapService getOlePurapService() {
288         if (olePurapService == null) {
289             olePurapService = SpringContext.getBean(OlePurapService.class);
290         }
291         return olePurapService;
292     }
293 
294 }