View Javadoc
1   /*
2    * Copyright 2007 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.fp.batch;
17  
18  import java.io.File;
19  import java.util.ArrayList;
20  import java.util.Date;
21  import java.util.List;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.ole.fp.batch.service.ProcurementCardLoadTransactionsService;
25  import org.kuali.ole.sys.batch.AbstractStep;
26  import org.kuali.ole.sys.batch.BatchInputFileType;
27  import org.kuali.ole.sys.batch.service.BatchInputFileService;
28  import org.kuali.ole.sys.batch.service.WrappingBatchService;
29  import org.kuali.ole.sys.service.ReportWriterService;
30  
31  /**
32   * This step will call a service method to load the kuali pcard xml file into the transaction table. Validates the data before the
33   * load. Functions performed by this step: 1) Lookup path and filename from APC for the pcard input file 2) Load the pcard xml file
34   * 3) Parse each transaction and validate against the data dictionary 4) Clean fp_prcrmnt_card_trn_mt from the previous run 5) Load
35   * new transactions into fp_prcrmnt_card_trn_mt 6) Rename input file using the current date (backup) RESTART: All functions performed
36   * withing a single transaction. Step can be restarted as needed.
37   */
38  public class ProcurementCardLoadStep extends AbstractStep {
39      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProcurementCardLoadStep.class);
40  
41      private ProcurementCardLoadTransactionsService procurementCardLoadTransactionsService;
42      private BatchInputFileService batchInputFileService;
43      private BatchInputFileType procurementCardInputFileType;
44      private ReportWriterService reportWriterService;
45  
46      /**
47       * custom get requiredDirectoryNames step- assign the passed in procurementInputFileType to the batchInputFileType
48       * 
49       * @see org.kuali.ole.sys.batch.AbstractStep#prepareStepDirectory()
50       */
51      @Override
52      public List<String> getRequiredDirectoryNames() {
53          setBatchInputFileType(procurementCardInputFileType);
54          return super.getRequiredDirectoryNames();
55      }
56      
57      /**
58       * Controls the procurement card process.
59       */
60      public boolean execute(String jobName, Date jobRunDate) {
61          procurementCardLoadTransactionsService.cleanTransactionsTable();
62  
63          List<String> fileNamesToLoad = batchInputFileService.listInputFileNamesWithDoneFile(procurementCardInputFileType);
64          ((WrappingBatchService) reportWriterService).initialize();
65  
66          boolean processSuccess = true;
67          List<String> processedFiles = new ArrayList();
68          for (String inputFileName : fileNamesToLoad) {
69              processSuccess = procurementCardLoadTransactionsService.loadProcurementCardFile(inputFileName, reportWriterService);
70              if (processSuccess) {
71                  processedFiles.add(inputFileName);
72              }
73          }
74          ((WrappingBatchService) reportWriterService).destroy();
75  
76          removeDoneFiles(fileNamesToLoad);
77  
78          return processSuccess;
79      }
80  
81      /**
82       * Clears out associated .done files for the processed data files.
83       */
84      private void removeDoneFiles(List<String> dataFileNames) {
85          for (String dataFileName : dataFileNames) {
86              File doneFile = new File(StringUtils.substringBeforeLast(dataFileName, ".") + ".done");
87              if (doneFile.exists()) {
88                  doneFile.delete();
89              }
90          }
91      }
92  
93      /**
94       * Sets the batchInputFileService attribute value.
95       */
96      public void setBatchInputFileService(BatchInputFileService batchInputFileService) {
97          this.batchInputFileService = batchInputFileService;
98      }
99  
100     /**
101      * Sets the procurementCardInputFileType attribute value.
102      */
103     public void setProcurementCardInputFileType(BatchInputFileType procurementCardInputFileType) {
104         this.procurementCardInputFileType = procurementCardInputFileType;
105     }
106 
107     /**
108      * Sets the procurementCardLoadTransactionsService attribute value.
109      */
110     public void setProcurementCardLoadTransactionsService(ProcurementCardLoadTransactionsService procurementCardLoadTransactionsService) {
111         this.procurementCardLoadTransactionsService = procurementCardLoadTransactionsService;
112     }
113 
114     /**
115      * 
116      * @return
117      */
118     public ReportWriterService getReportWriterService() {
119         return reportWriterService;
120     }
121 
122     /**
123      * 
124      * @param reportWriterService
125      */
126     public void setReportWriterService(ReportWriterService reportWriterService) {
127         this.reportWriterService = reportWriterService;
128     }
129 }