View Javadoc
1   package org.kuali.ole.batch.ingest;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.batch.impl.AbstractBatchProcess;
6   import org.kuali.ole.select.bo.*;
7   import org.kuali.ole.service.OLESerialReceivingImportProcessor;
8   import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
9   import org.kuali.rice.krad.service.KRADServiceLocator;
10  import org.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
12  
13  import java.nio.file.FileSystems;
14  import java.util.ArrayList;
15  import java.util.HashMap;
16  import java.util.List;
17  import java.util.Map;
18  
19  /**
20   * Created with IntelliJ IDEA.
21   * User: rajeshbabuk
22   * Date: 2/6/14
23   * Time: 12:13 PM
24   * To change this template use File | Settings | File Templates.
25   */
26  public class OLEBatchProcessSerialRecordImport extends AbstractBatchProcess {
27      private static final Logger LOG = LoggerFactory.getLogger(OLEBatchProcessClaimReport.class);
28      private OLESerialReceivingImportProcessor oleSerialReceivingConverterService;
29      private String fileContent = null;
30      private String documentFileContent = null;
31      private String typeFileContent = null;
32      private String historyFileContent = null;
33      private String documentFileName =null;
34      private String historyFileName =null;
35      private String typeFileName =null;
36  
37      @Override
38      protected void prepareForRead() throws Exception {
39          oleSerialReceivingConverterService = new OLESerialReceivingImportProcessor();
40          if (processDef.getOutputFormat().equalsIgnoreCase("xml")) {
41              fileContent = getBatchProcessFileContent(job.getUploadFileName());
42          } else {
43              String[] fileNames = job.getUploadFileName().split(",");
44            //  if(fileNames.length == 3) {
45                  for(String fileName : fileNames) {
46                      if (fileName.endsWith(getParameter(OLEConstants.OLEBatchProcess.SERIAL_RECORD_NAME)+".csv")) {
47                          documentFileName = getBatchProcessFilePath(processDef.getBatchProcessType()) + job.getJobId() +FileSystems.getDefault().getSeparator()+ job.getJobId() + OLEConstants.OLEBatchProcess.PROFILE_JOB + "_" + fileName;
48                                  documentFileContent = getBatchProcessFileContent(fileName);
49                      } else if (fileName.endsWith(getParameter(OLEConstants.OLEBatchProcess.SERIAL_HISTORY_NAME)+".csv")) {
50                          historyFileName = getBatchProcessFilePath(processDef.getBatchProcessType()) +job.getJobId()+FileSystems.getDefault().getSeparator() +job.getJobId() + OLEConstants.OLEBatchProcess.PROFILE_JOB + "_" + fileName;
51                          historyFileContent = getBatchProcessFileContent(fileName);
52                      } else if (fileName.endsWith(getParameter(OLEConstants.OLEBatchProcess.SERIAL_TYPE_NAME)+".csv")) {
53                          typeFileName = getBatchProcessFilePath(processDef.getBatchProcessType()) + job.getJobId()+FileSystems.getDefault().getSeparator()+job.getJobId() + OLEConstants.OLEBatchProcess.PROFILE_JOB + "_" + fileName;
54                          typeFileContent = getBatchProcessFileContent(fileName);
55                      }
56                 // }
57              }/* else if (fileNames.length == 2) {
58                  if (fileNames[0].endsWith("recv_rec.csv")) {
59                      documentFileContent = getBatchProcessFileContent(fileNames[0]);
60                  } else if (fileNames[0].endsWith("recv_his_rec.csv")) {
61                      historyFileContent = getBatchProcessFileContent(fileNames[0]);
62                  } else if (fileNames[0].endsWith("recv_rec_type.csv")) {
63                      typeFileContent = getBatchProcessFileContent(fileNames[0]);
64                  }
65  
66                  if (fileNames[1].endsWith("recv_rec.csv")) {
67                      documentFileContent = getBatchProcessFileContent(fileNames[1]);
68                  } else if (fileNames[1].endsWith("recv_his_rec.csv")) {
69                      historyFileContent = getBatchProcessFileContent(fileNames[1]);
70                  } else if (fileNames[1].endsWith("recv_rec_type.csv")) {
71                      typeFileContent = getBatchProcessFileContent(fileNames[1]);
72                  }
73              } else {
74                  if (fileNames[0].endsWith("recv_rec.csv")) {
75                      documentFileContent = getBatchProcessFileContent(fileNames[0]);
76                  } else if (fileNames[0].endsWith("recv_his_rec.csv")) {
77                      historyFileContent = getBatchProcessFileContent(fileNames[0]);
78                  } else if (fileNames[0].endsWith("recv_rec_type.csv")) {
79                      typeFileContent = getBatchProcessFileContent(fileNames[0]);
80                  }
81              }*/
82          }
83          job.setNoOfSuccessRecords("0");
84          job.setNoOfFailureRecords("0");
85          job.setNoOfRecordsProcessed("0");
86          //To change body of implemented methods use File | Settings | File Templates.
87      }
88  
89      @Override
90      protected void prepareForWrite() throws Exception {
91          LOG.debug("Inside prepareForWrite for the file : "+job.getUploadFileName());
92          OLESerialReceivingRecordSummary oleSerialReceivingRecordSummary= null;
93          if(job.getUploadFileName().contains(".xml")){
94              oleSerialReceivingRecordSummary=oleSerialReceivingConverterService.createOLESerialReceivingDocumentFromXml(fileContent);
95              deleteBatchFile();
96              job.setTotalNoOfRecords(String.valueOf(oleSerialReceivingRecordSummary.getTotalRecordSize()));
97              job.setNoOfRecordsProcessed(String.valueOf(oleSerialReceivingRecordSummary.getSuccessRecordSize()));
98              job.setNoOfSuccessRecords(String.valueOf(oleSerialReceivingRecordSummary.getSuccessRecordSize()));
99              job.setNoOfFailureRecords(String.valueOf(oleSerialReceivingRecordSummary.getFailureRecordSize()));
100             job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
101             if (oleSerialReceivingRecordSummary.getFailureDocuments()!=null){
102                 OLESerialReceivingDocuments oleSerialReceivingDocuments=new OLESerialReceivingDocuments();
103                 oleSerialReceivingDocuments.setOleSerialReceivingDocuments(oleSerialReceivingRecordSummary.getFailureDocuments());
104                 String content=oleSerialReceivingConverterService.getSerialReceivingXMLContent(oleSerialReceivingDocuments);
105                 createBatchFailureFile(content);
106             }
107         } else {
108             oleSerialReceivingRecordSummary = oleSerialReceivingConverterService.createOLESerialReceivingDocumentFromCsv(documentFileName,typeFileName,historyFileName);
109             String[] fileNames = job.getUploadFileName().split(",");
110             for(String fileName : fileNames){
111                 deleteBatchFile(fileName);
112             }
113             int totalSize=oleSerialReceivingRecordSummary.getDocSuccessCount()+oleSerialReceivingRecordSummary.getDocFailureCount()+oleSerialReceivingRecordSummary.getHstrySucceesCount()+oleSerialReceivingRecordSummary.getHstryFailureCount()+oleSerialReceivingRecordSummary.getTypeSuccessCount()+oleSerialReceivingRecordSummary.getTypeFailureCount();
114             job.setTotalNoOfRecords(String.valueOf(totalSize));
115             job.setNoOfRecordsProcessed(String.valueOf(totalSize));
116             job.setNoOfSuccessRecords(String.valueOf(oleSerialReceivingRecordSummary.getDocSuccessCount()));
117             job.setNoOfFailureRecords(String.valueOf(oleSerialReceivingRecordSummary.getDocFailureCount()));
118             job.setHstrySucceesCount(String.valueOf(oleSerialReceivingRecordSummary.getHstrySucceesCount()));
119             job.setHstryFailureCount(String.valueOf(oleSerialReceivingRecordSummary.getHstryFailureCount()));
120             job.setTypeSuccessCount(String.valueOf(oleSerialReceivingRecordSummary.getTypeSuccessCount()));
121             job.setTypeFailureCount(String.valueOf(oleSerialReceivingRecordSummary.getTypeFailureCount()));
122             job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
123             if (oleSerialReceivingRecordSummary.getDocFailureList()!=null && oleSerialReceivingRecordSummary.getDocFailureList().size()>0){
124                 OLESerialReceivingFailureDocuments oleSerialReceivingFailureDocuments = createOLESerialReceivingFailureDocuments(oleSerialReceivingRecordSummary.getDocFailureList());
125 /*                OLESerialReceivingDocuments oleSerialReceivingDocuments=new OLESerialReceivingDocuments();
126                 oleSerialReceivingDocuments.setOleSerialReceivingDocuments(oleSerialReceivingRecordSummary.getDocFailureList());
127                 String content=oleSerialReceivingConverterService.getSerialReceivingXMLContent(oleSerialReceivingDocuments);*/
128                 String content = oleSerialReceivingConverterService.getSerialReceivingFailureDocumentsXmlContent(oleSerialReceivingFailureDocuments);
129                 createBatchFailureFile(content, StringUtils.substringAfter(documentFileName, getBatchProcessFilePath(processDef.getBatchProcessType()) + job.getJobId() + FileSystems.getDefault().getSeparator() + job.getJobId() + OLEConstants.OLEBatchProcess.PROFILE_JOB + "_").replace("csv","xml"));
130             }
131             if (oleSerialReceivingRecordSummary.getHstryFailureList()!=null && oleSerialReceivingRecordSummary.getHstryFailureList().size()>0){
132                 OLESerialReceivingFailureHistories oleSerialReceivingFailureHistories = createOleSerialReceivingFailureHistories(oleSerialReceivingRecordSummary.getHstryFailureList());
133 /*                OLESerialReceivingDocument oleSerialReceivingDocument=new OLESerialReceivingDocument();
134                 oleSerialReceivingDocument.setOleSerialReceivingHistoryList(oleSerialReceivingRecordSummary.getHstryFailureList());
135                 String content=oleSerialReceivingConverterService.getSerialReceivingDocumentXMLContent(oleSerialReceivingDocument);*/
136                 String content =  oleSerialReceivingConverterService.getSerialReceivingFailureHistoriesXmlContent(oleSerialReceivingFailureHistories);
137                 createBatchFailureFile(content,StringUtils.substringAfter(historyFileName, getBatchProcessFilePath(processDef.getBatchProcessType()) + job.getJobId() + FileSystems.getDefault().getSeparator() + job.getJobId() + OLEConstants.OLEBatchProcess.PROFILE_JOB + "_").replace("csv","xml"));
138             }
139             if (oleSerialReceivingRecordSummary.getTypeFailureList()!=null && oleSerialReceivingRecordSummary.getTypeFailureList().size()>0){
140                 OLESerialReceivingFailureTypes oleSerialReceivingFailureTypes = createOleSerialReceivingFailureTypes(oleSerialReceivingRecordSummary.getTypeFailureList());
141 /*                OLESerialReceivingDocument oleSerialReceivingDocument=new OLESerialReceivingDocument();
142                 oleSerialReceivingDocument.setOleSerialReceivingTypes(oleSerialReceivingRecordSummary.getTypeFailureList());
143                 String content=oleSerialReceivingConverterService.getSerialReceivingDocumentXMLContent(oleSerialReceivingDocument);*/
144                 String content =  oleSerialReceivingConverterService.getSerialReceivingFailureTypesXmlContent(oleSerialReceivingFailureTypes);
145                 createBatchFailureFile(content,StringUtils.substringAfter(typeFileName, getBatchProcessFilePath(processDef.getBatchProcessType()) + job.getJobId() + FileSystems.getDefault().getSeparator() + job.getJobId() + OLEConstants.OLEBatchProcess.PROFILE_JOB + "_").replace("csv","xml"));
146             }
147         }
148 
149         //To change body of implemented methods use File | Settings | File Templates.
150     }
151 
152     @Override
153     protected void getNextBatch() throws Exception {
154         //To change body of implemented methods use File | Settings | File Templates.
155     }
156 
157     @Override
158     protected void processBatch() throws Exception {
159         //To change body of implemented methods use File | Settings | File Templates.
160     }
161 
162     /**
163      * This method is used to get the file name from the system parameter
164      * @param name
165      * @return  parameter
166      */
167     private String getParameter(String name) {
168         LOG.info("Parameter Name : "+ name);
169         String parameter = "";
170         try {
171             Map<String, String> criteriaMap = new HashMap<String, String>();
172             criteriaMap.put("namespaceCode", OLEConstants.SYS_NMSPC);
173             criteriaMap.put("componentCode", OLEConstants.BATCH_CMPNT);
174             criteriaMap.put("name", name);
175             List<ParameterBo> parametersList = (List<ParameterBo>) KRADServiceLocator.getBusinessObjectService().findMatching(ParameterBo.class, criteriaMap);
176             for (ParameterBo parameterBo : parametersList) {
177                 parameter = parameterBo.getValue();
178             }
179         } catch (Exception e) {
180         e.printStackTrace();
181         }
182         LOG.info("Parameter Value : " + parameter);
183         return parameter;
184     }
185 
186     /**
187      * This method is used to create OLESerialReceivingFailureDocuments from list of OLESerialReceivingDocument
188      * @param oleSerialReceivingDocuments
189      * @return  OLESerialReceivingFailureDocuments
190      */
191     private OLESerialReceivingFailureDocuments createOLESerialReceivingFailureDocuments(List<OLESerialReceivingDocument> oleSerialReceivingDocuments){
192         LOG.info("Inside createOLESerialReceivingFailureDocuments for creating  OLESerialReceivingFailureDocuments for the " +oleSerialReceivingDocuments.size() + "of OLESerialReceivingDocument");
193         OLESerialReceivingFailureDocuments oleSerialReceivingFailureDocuments = new OLESerialReceivingFailureDocuments();
194         List<OLESerialReceivingFailureDocument> oleSerialReceivingFailureDocumentList = new ArrayList<OLESerialReceivingFailureDocument>();
195           OLESerialReceivingFailureDocument oleSerialReceivingFailureDocument ;
196         for(OLESerialReceivingDocument oleSerialReceivingDocument : oleSerialReceivingDocuments){
197             oleSerialReceivingFailureDocument = new OLESerialReceivingFailureDocument();
198             oleSerialReceivingFailureDocument.setOleSerialReceivingDocument(oleSerialReceivingDocument);
199             if(oleSerialReceivingDocument.isRecordAlreadyExist()){
200                 oleSerialReceivingFailureDocument.setErrorMessage(OLEConstants.SERIAL_DOC_ALRDY_EXIST);
201             }else if(!oleSerialReceivingDocument.isValidPo()){
202                 oleSerialReceivingFailureDocument.setErrorMessage(OLEConstants.INVLD_PO);
203             }else if(!oleSerialReceivingDocument.isValidBibAndInstance()){
204                 oleSerialReceivingFailureDocument.setErrorMessage(OLEConstants.INVLD_BIB_INS);
205             }else if(!oleSerialReceivingDocument.isAvailableSerialReceiptLocation()){
206                 oleSerialReceivingFailureDocument.setErrorMessage(OLEConstants.NO_SRL_RCPT_LOCN);
207             }else if(!oleSerialReceivingDocument.isValidSubscriptionStatus()){
208                 oleSerialReceivingFailureDocument.setErrorMessage(OLEConstants.INVLD_SUBS_STAT);
209             }else if(!oleSerialReceivingDocument.isValidRecordType()){
210                 oleSerialReceivingFailureDocument.setErrorMessage(OLEConstants.INVALID_RECORD_TYPE);
211             }else if(!oleSerialReceivingDocument.isValidChildRecordType()){
212                 oleSerialReceivingFailureDocument.setErrorMessage(OLEConstants.LINKED_TYP_FAILED);
213             }else if(!oleSerialReceivingDocument.isValidChildHistoryRecordType()){
214                 oleSerialReceivingFailureDocument.setErrorMessage(OLEConstants.LINKED_HSTRY_FAILED);
215             }
216             oleSerialReceivingFailureDocumentList.add(oleSerialReceivingFailureDocument);
217         }
218         oleSerialReceivingFailureDocuments.setOleSerialReceivingFailureDocuments(oleSerialReceivingFailureDocumentList);
219         if(oleSerialReceivingFailureDocumentList!=null && oleSerialReceivingFailureDocumentList.size()>0){
220             LOG.info(oleSerialReceivingFailureDocumentList.size()+"number of OLESerialReceivingFailureDocument created ");
221         } else{
222             LOG.info("No OLESerialReceivingFailureDocument created ");
223         }
224        return oleSerialReceivingFailureDocuments;
225     }
226 
227     /**
228      *   This method is used to create OLESerialReceivingFailureHistories from list of OLESerialReceivingHistory record
229      * @param oleSerialReceivingHistories
230      * @return OLESerialReceivingFailureHistories
231      */
232     private OLESerialReceivingFailureHistories createOleSerialReceivingFailureHistories(List<OLESerialReceivingHistory> oleSerialReceivingHistories){
233         LOG.info("Inside createOleSerialReceivingFailureHistories for creating  OLESerialReceivingFailureHistories for the " +oleSerialReceivingHistories.size() + "of OLESerialReceivingHistory");
234 
235         OLESerialReceivingFailureHistories oleSerialReceivingFailureHistories = new OLESerialReceivingFailureHistories();
236         List<OLESerialReceivingFailureHistory> oleSerialReceivingFailureHistoryList =  new ArrayList<OLESerialReceivingFailureHistory>();
237         OLESerialReceivingFailureHistory oleSerialReceivingFailureHistory ;
238         for(OLESerialReceivingHistory oleSerialReceivingHistory : oleSerialReceivingHistories){
239             oleSerialReceivingFailureHistory = new OLESerialReceivingFailureHistory();
240             oleSerialReceivingFailureHistory.setOleSerialReceivingHistory(oleSerialReceivingHistory);
241             if(!oleSerialReceivingHistory.isDocumentExist()){
242                 oleSerialReceivingFailureHistory.setErrorMessage(OLEConstants.LINK_DOC_MISS);
243             }else if(!oleSerialReceivingHistory.isValidRecordType()){
244                 oleSerialReceivingFailureHistory.setErrorMessage(OLEConstants.INVALID_RECORD_TYPE);
245             }/*else if(oleSerialReceivingHistory.isValidRecordType() && !oleSerialReceivingDocument.isValidChildRecordType()){
246                 oleSerialReceivingFailureHistory.setErrorMessage("One or more of the corresponding Serial Receiving Document's assosiated Serial Receiving Record Type Fails");
247             }else if(oleSerialReceivingHistory.isValidRecordType() && !oleSerialReceivingDocument.isValidChildHistoryRecordType()){
248                 oleSerialReceivingFailureHistory.setErrorMessage("One or more of the corresponding Serial Receiving Document's assosiated Serial Receiving Record History Fails");
249             }*/else{
250                 oleSerialReceivingFailureHistory.setErrorMessage(OLEConstants.LINK_DOC_FAILURE);
251             }
252             oleSerialReceivingFailureHistoryList.add(oleSerialReceivingFailureHistory);
253         }
254         oleSerialReceivingFailureHistories.setOleSerialReceivingFailureHistories(oleSerialReceivingFailureHistoryList);
255         if(oleSerialReceivingFailureHistoryList!=null && oleSerialReceivingFailureHistoryList.size()>0){
256             LOG.info(oleSerialReceivingFailureHistoryList.size()+"number of OLESerialReceivingFailureHistory created ");
257         } else{
258             LOG.info("No OLESerialReceivingFailureHistory created ");
259         }
260         return oleSerialReceivingFailureHistories;
261     }
262 
263     /**
264      * This method is used to create OLESerialReceivingFailureTypes from list of OLESerialReceivingType
265      * @param oleSerialReceivingTypes
266      * @return OLESerialReceivingFailureTypes
267      */
268     private OLESerialReceivingFailureTypes createOleSerialReceivingFailureTypes(List<OLESerialReceivingType> oleSerialReceivingTypes){
269         LOG.info("Inside createOleSerialReceivingFailureTypes for creating  OLESerialReceivingFailureTypes for the " +oleSerialReceivingTypes.size() + "of OLESerialReceivingType");
270         OLESerialReceivingFailureTypes oleSerialReceivingFailureTypes = new OLESerialReceivingFailureTypes();
271         List<OLESerialReceivingFailureType> oleSerialReceivingFailureTypeList =  new ArrayList<OLESerialReceivingFailureType>();
272         OLESerialReceivingFailureType oleSerialReceivingFailureType ;
273         for(OLESerialReceivingType oleSerialReceivingType : oleSerialReceivingTypes){
274             oleSerialReceivingFailureType = new OLESerialReceivingFailureType();
275             oleSerialReceivingFailureType.setOleSerialReceivingType(oleSerialReceivingType);
276             if(!oleSerialReceivingType.isDocumentExist()){
277                 oleSerialReceivingFailureType.setErrorMessage(OLEConstants.LINK_DOC_MISS);
278             }else if(!oleSerialReceivingType.isValidRecordType()){
279                  oleSerialReceivingFailureType.setErrorMessage(OLEConstants.INVALID_RECORD_TYPE);
280             }/*else if(oleSerialReceivingType.isValidRecordType() && !oleSerialReceivingDocument.isValidChildRecordType()){
281                 oleSerialReceivingFailureType.setErrorMessage("One or more of the corresponding Serial Receiving Document's assosiated Serial Receiving Record Type Fails");
282             }else if(oleSerialReceivingType.isValidRecordType() && !oleSerialReceivingDocument.isValidChildHistoryRecordType()){
283                 oleSerialReceivingFailureType.setErrorMessage("One or more of the corresponding Serial Receiving Document's assosiated Serial Receiving Record History Fails");
284             }*/else{
285                 oleSerialReceivingFailureType.setErrorMessage(OLEConstants.LINK_DOC_FAILURE);
286             }
287             oleSerialReceivingFailureTypeList.add(oleSerialReceivingFailureType);
288         }
289         oleSerialReceivingFailureTypes.setOleSerialReceivingFailureTypes(oleSerialReceivingFailureTypeList);
290         if(oleSerialReceivingFailureTypeList!=null && oleSerialReceivingFailureTypeList.size()>0){
291             LOG.info(oleSerialReceivingFailureTypeList.size()+"number of OLESerialReceivingFailureType created ");
292         } else{
293             LOG.info("No OLESerialReceivingFailureType created ");
294         }
295         return oleSerialReceivingFailureTypes;
296     }
297 }