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
21
22
23
24
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
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 }
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 }
83 job.setNoOfSuccessRecords("0");
84 job.setNoOfFailureRecords("0");
85 job.setNoOfRecordsProcessed("0");
86
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
126
127
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
134
135
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
142
143
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
150 }
151
152 @Override
153 protected void getNextBatch() throws Exception {
154
155 }
156
157 @Override
158 protected void processBatch() throws Exception {
159
160 }
161
162
163
164
165
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
188
189
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
229
230
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 }
246
247
248
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
265
266
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 }
281
282
283
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 }