1 package org.kuali.ole.batch.helper;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.apache.log4j.Logger;
5 import org.kuali.ole.OLEConstants;
6 import org.kuali.ole.batch.bo.*;
7 import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
8 import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.ControlField;
9 import org.kuali.ole.docstore.common.document.content.bib.marc.DataField;
10 import org.kuali.ole.docstore.common.document.content.bib.marc.SubField;
11 import org.kuali.ole.pojo.bib.BibliographicRecord;
12 import org.kuali.rice.core.api.config.property.ConfigContext;
13 import org.marc4j.MarcStreamWriter;
14 import org.marc4j.MarcWriter;
15 import org.marc4j.MarcXmlReader;
16 import org.marc4j.marc.Record;
17
18 import java.io.*;
19 import java.nio.file.FileSystems;
20 import java.util.*;
21
22
23
24
25
26
27
28
29 public class OLEBatchProcessDataHelper {
30
31 private String statingDirectory = ConfigContext.getCurrentContextConfig().getProperty("staging.directory");
32 private static final Logger LOG = Logger.getLogger(OLEBatchProcessDataHelper.class);
33
34 private static volatile OLEBatchProcessDataHelper exportDataHelper;
35
36
37 private static final String applicationUrl = ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.OLEBatchProcess.BATCH_EXPORT_PATH_APP_URL);
38 private static final String homeDirectory = ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.USER_HOME_DIRECTORY);
39
40 private OLEBatchProcessDataHelper() {
41 }
42
43 public static synchronized OLEBatchProcessDataHelper getInstance() {
44 if (exportDataHelper == null) {
45 exportDataHelper = new OLEBatchProcessDataHelper();
46 }
47 return exportDataHelper;
48 }
49
50
51
52
53
54
55
56 public void deleteFieldsSubfields(OLEBatchProcessProfileBo oleBatchProcessProfileBo, BibMarcRecord record) {
57 List<OLEBatchProcessProfileDeleteField> deleteFieldList = oleBatchProcessProfileBo.getOleBatchProcessProfileDeleteFieldsList();
58 for (OLEBatchProcessProfileDeleteField deleteField : deleteFieldList) {
59 if (StringUtils.isNotEmpty(deleteField.getSubField())) {
60 deleteMarcSubFields(record, deleteField);
61 } else if (StringUtils.isNotEmpty(deleteField.getTag())) {
62 deleteMarcFields(record, deleteField);
63 }
64 }
65 }
66
67
68
69
70
71
72
73 public void deleteMarcFields(BibMarcRecord record, OLEBatchProcessProfileDeleteField deleteField) {
74 List<DataField> dataFieldList = record.getDataFields();
75 List<Integer> indices = findDataFieldIndex(dataFieldList, deleteField.getTag(), deleteField.getFirstIndicator(), deleteField.getSecondIndicator());
76 for (int i = indices.size() - 1; i >= 0; i--) {
77 dataFieldList.remove(indices.get(i).intValue());
78 }
79 }
80
81 public void addMarcFields(BibMarcRecord record, OLEBatchProcessProfileRenameField renameField) {
82 List<DataField> dataFieldList = record.getDataFields();
83 DataField dataField = new DataField();
84 dataField.setTag(renameField.getRenamedTag());
85 dataField.setInd1(renameField.getRenamedFirstIndicator());
86 dataField.setInd2(renameField.getRenamedSecondIndicator());
87 SubField subField = new SubField();
88 if (renameField.getRenamedSubField().contains("$")) {
89 renameField.setRenamedSubField(renameField.getRenamedSubField().substring(1, renameField.getRenamedSubField().length()));
90 }
91 subField.setCode(renameField.getRenamedSubField());
92 subField.setValue(renameField.getRenamedSubFieldContains());
93 ListIterator<DataField> iterator = dataFieldList.listIterator();
94 if (renameField.getOriginalSubField().contains("$")) {
95 renameField.setOriginalSubField(renameField.getOriginalSubField().substring(1, renameField.getOriginalSubField().length()));
96 }
97 while (iterator.hasNext()) {
98 DataField dField = iterator.next();
99 if (dField.getTag().equals(renameField.getOriginalTag())) {
100 Iterator<SubField> subFieldIterator = dField.getSubFields().iterator();
101 while (subFieldIterator.hasNext()) {
102 SubField sField = subFieldIterator.next();
103 if (sField.getCode().equals(renameField.getOriginalSubField())) {
104 if (StringUtils.isEmpty(renameField.getRenamedSubFieldContains())) {
105 subField.setValue(sField.getValue());
106 }
107 dataField.addSubField(subField);
108 }
109 }
110 }
111 }
112 dataFieldList.add(dataField);
113 }
114
115
116
117
118
119
120
121
122 public void deleteMarcSubFields(BibMarcRecord record, OLEBatchProcessProfileDeleteField deleteField) {
123 List<DataField> dataFieldList = record.getDataFields();
124 boolean hasEmptySubFields = false;
125 for (DataField dataField : dataFieldList) {
126 if (!dataField.getTag().equals(deleteField.getTag())) continue;
127
128 List<Integer> indices = findSubFieldIndex(dataField.getSubFields(), deleteField.getSubField(), deleteField.getSubFieldContains());
129 for (int i = indices.size() - 1; i >= 0; i--) {
130 dataField.getSubFields().remove(indices.get(i).intValue());
131 }
132 if(dataField.getSubFields().isEmpty()){
133 hasEmptySubFields=true;
134 }
135 }
136 if(hasEmptySubFields)
137 deleteEmptyFields(dataFieldList);
138 }
139
140
141
142
143
144 private void deleteEmptyFields(List<DataField>dataFieldList){
145 Iterator<DataField> itr = dataFieldList.iterator();
146 while(itr.hasNext()){
147 DataField dataField = itr.next();
148 if(dataField.getSubFields().isEmpty()){
149 itr.remove();
150 }
151 }
152
153 }
154
155
156
157
158
159
160
161 public void renameMarcFieldsSubFields(OLEBatchProcessProfileBo oleBatchProcessProfileBo, BibMarcRecord record) {
162 List<OLEBatchProcessProfileRenameField> renameList = oleBatchProcessProfileBo.getOleBatchProcessProfileRenameFieldsList();
163 for (OLEBatchProcessProfileRenameField renameField : renameList) {
164 if (StringUtils.isNotEmpty(renameField.getOriginalSubField()) && StringUtils.isNotEmpty(renameField.getRenamedSubField())
165 && !renameField.getOriginalSubField().equals(renameField.getRenamedSubField())) {
166 renameMarcSubFields(record, renameField);
167 }
168 if (StringUtils.isNotEmpty(renameField.getOriginalTag()) && StringUtils.isNotEmpty(renameField.getRenamedTag())
169 && !renameField.getOriginalTag().equals(renameField.getRenamedSubField())) {
170 renameMarcFields(record, renameField);
171 }
172 }
173 }
174
175
176
177
178
179
180
181 public void renameMarcFields(BibMarcRecord record, OLEBatchProcessProfileRenameField renameField) {
182 List<DataField> dataFieldList = record.getDataFields();
183 List<Integer> indices = findDataFieldIndex(dataFieldList, renameField.getOriginalTag(), renameField.getOriginalFirstIndicator(), renameField.getOriginalSecondIndicator());
184 for (int i = 0; i < indices.size(); i++) {
185 DataField dataField = dataFieldList.get(indices.get(i));
186 if (StringUtils.isNotEmpty(renameField.getRenamedFirstIndicator()))
187 dataField.setInd1(renameField.getRenamedFirstIndicator());
188 if (StringUtils.isNotEmpty(renameField.getRenamedSecondIndicator()))
189 dataField.setInd2(renameField.getRenamedSecondIndicator());
190 if (StringUtils.isNotEmpty(renameField.getRenamedTag())) dataField.setTag(renameField.getRenamedTag());
191 }
192
193
194 }
195
196
197
198
199
200
201
202 public void renameMarcSubFields(BibMarcRecord record, OLEBatchProcessProfileRenameField renameField) {
203 List<DataField> dataFieldList = record.getDataFields();
204 for (DataField dataField : dataFieldList) {
205 if (!dataField.getTag().equals(renameField.getOriginalTag())) continue;
206 List<Integer> indices = findSubFieldIndex(dataField.getSubFields(), renameField.getOriginalSubField(), renameField.getOriginalSubFieldContains());
207 for (int i = 0; i < indices.size(); i++) {
208 SubField subField = dataField.getSubFields().get(indices.get(i));
209 if (StringUtils.isNotEmpty(renameField.getRenamedSubField()))
210 subField.setCode(renameField.getRenamedSubField());
211 if (StringUtils.isNotEmpty(renameField.getRenamedSubFieldContains()))
212 subField.setValue(renameField.getRenamedSubFieldContains());
213 }
214 }
215 }
216
217
218
219
220
221
222
223
224
225
226
227 public List<Integer> findDataFieldIndex(List<DataField> dataFieldList, String tag, String ind1, String ind2) {
228 if (StringUtils.isEmpty(tag)) return Collections.emptyList();
229 List<Integer> indices = new ArrayList<Integer>();
230 for (int i = 0; i < dataFieldList.size(); i++) {
231 if (!tag.equals(dataFieldList.get(i).getTag())) continue;
232 if (StringUtils.isNotEmpty(ind1) ? !ind1.equals(dataFieldList.get(i).getInd1()) : false) continue;
233 if (StringUtils.isNotEmpty(ind2) ? !ind2.equals(dataFieldList.get(i).getInd2()) : false) continue;
234 indices.add(i);
235 }
236 return indices;
237 }
238
239
240
241
242
243
244
245
246
247
248 public List<Integer> findSubFieldIndex(List<SubField> subFieldList, String subField, String content) {
249 if (StringUtils.isEmpty(subField)) return Collections.emptyList();
250 List<Integer> indices = new ArrayList<Integer>();
251 for (int i = 0; i < subFieldList.size(); i++) {
252 if(subField.contains("$")){
253 subField = subField.substring(1);
254 }
255 if (StringUtils.isNotEmpty(subField) ? !subField.equals(subFieldList.get(i).getCode()) : true) continue;
256 if (StringUtils.isNotEmpty(content) ? !subFieldList.get(i).getValue().contains(content) : false) continue;
257 indices.add(i);
258 }
259 return indices;
260 }
261
262
263
264
265
266
267
268 public String getBatchProcessFilePath(String batchProceesType, String jobId) {
269 String batchProcessLocation = getBatchProcessFilePath(batchProceesType);
270 if (batchProcessLocation != null) {
271 batchProcessLocation = batchProcessLocation + jobId + FileSystems.getDefault().getSeparator();
272 File file = new File(batchProcessLocation);
273 if (!file.isDirectory()) {
274 file.mkdir();
275 }
276 }
277 return batchProcessLocation;
278 }
279
280
281
282
283
284
285
286 public String getBatchProcessFilePath(String batchProceesType) {
287
288 String batchProcessLocation = null;
289 if (batchProceesType.equals(OLEConstants.OLEBatchProcess.BATCH_DELETE)) {
290 batchProcessLocation = ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.OLEBatchProcess.BATCH_DELETE_DIR_PATH) + "/";
291 } else if (batchProceesType.equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_BIB_IMPORT)) {
292 batchProcessLocation = ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.OLEBatchProcess.BATCH_BIB_IMPORT_DIR_PATH) + "/";
293 } else if (batchProceesType.equals(OLEConstants.OLEBatchProcess.BATCH_INVOICE)) {
294 batchProcessLocation = ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.OLEBatchProcess.BATCH_INVOICE_DIR_PATH) + "/";
295 } else if (batchProceesType.equals(OLEConstants.OLEBatchProcess.ORDER_RECORD_IMPORT)) {
296 batchProcessLocation = ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.OLEBatchProcess.BATCH_ORDER_RECORD_IMPORT_DIR_PATH) + "/";
297 } else if (batchProceesType.equals(OLEConstants.OLEBatchProcess.PATRON_IMPORT)) {
298 batchProcessLocation = ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.OLEBatchProcess.BATCH_PATRON_IMPORT_DIR_PATH) + "/";
299 } else if (batchProceesType.equals(OLEConstants.OLEBatchProcess.LOCATION_IMPORT)) {
300 batchProcessLocation = ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.OLEBatchProcess.BATCH_LOCATION_IMPORT_DIR_PATH) + "/";
301 } else if (batchProceesType.equals(OLEConstants.OLEBatchProcess.BATCH_EXPORT)) {
302 batchProcessLocation = ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.OLEBatchProcess.BATCH_EXPORT_DIR_PATH) + "/";
303 }else if (batchProceesType.equals(OLEConstants.OLEBatchProcess.SERIAL_RECORD_IMPORT)) {
304 batchProcessLocation = ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.OLEBatchProcess.SERIAL_IMPORT_DIR_PATH) + "/";
305 }
306
307
308 if (batchProcessLocation != null) {
309 File file = new File(batchProcessLocation);
310 if (!file.isDirectory()) {
311 file.mkdir();
312 }
313 }
314 return batchProcessLocation;
315 }
316
317
318
319
320
321
322
323
324
325 public void createBatchFailureFile(String failureRecordData, String batchProcessType, String batchFileName, String jobId) throws Exception {
326 String fileLocation = getBatchProcessFilePath(batchProcessType , jobId);
327
328
329
330
331
332 BufferedWriter out = new BufferedWriter(new FileWriter(fileLocation + batchFileName));
333 out.write(failureRecordData);
334 out.close();
335
336 }
337
338
339
340
341
342
343
344
345
346 public void createBatchSuccessFile(String successRecordData, String batchProcessType, String batchFileName, String jobId) throws Exception {
347 String fileLocation = fileLocation = getBatchProcessFilePath(batchProcessType, jobId);
348
349
350
351 BufferedWriter out = new BufferedWriter(new FileWriter(fileLocation + batchFileName));
352 out.write(successRecordData);
353 out.close();
354
355 }
356
357
358
359
360
361
362
363
364
365 public void createBatchDeleteFailureReportFile(String failureReportData, String batchProcessType, String batchFileName, String jobId) throws Exception {
366 String fileLocation = getBatchProcessFilePath(batchProcessType, jobId);
367
368
369
370 BufferedWriter out = new BufferedWriter(new FileWriter(fileLocation + batchFileName));
371 out.write(failureReportData);
372 out.close();
373
374 }
375
376
377
378
379
380 public void deleteBatchFailureFile(String batchProcessType, String batchFileName) throws Exception {
381
382 File file = new File(getBatchProcessFilePath(batchProcessType) + batchFileName);
383 if (file.exists()) {
384 file.delete();
385 }
386
387
388 }
389
390 public void createFile(String[] content, String batchProcessType, String batchFileName, String jobId) throws Exception {
391 String fileLocation = getBatchProcessFilePath(batchProcessType) + jobId + "/";
392 File file = new File(fileLocation);
393 if (!file.isDirectory()) {
394 file.mkdir();
395 }
396 BufferedWriter out = new BufferedWriter(new FileWriter(fileLocation + batchFileName));
397 for (String id : content) {
398 out.write(id);
399 out.newLine();
400 }
401 out.close();
402 }
403
404
405
406
407 public void deleteBatchFile(String batchProcessType, String batchFileName, String jobId) throws Exception {
408
409 String fileLocation = getBatchProcessFilePath(batchProcessType, jobId);
410
411
412
413
414
415 File file = new File(fileLocation + batchFileName);
416 if (file.exists()) {
417 file.delete();
418 }
419 }
420
421
422
423
424 public String getBatchProcessFileContent(String batchProcessType, String batchFileName, String jobId) throws Exception {
425
426 String fileLocation = getBatchProcessFilePath(batchProcessType, jobId);
427
428
429
430
431
432 File file = new File(fileLocation + batchFileName);
433 if (file.exists()) {
434 BufferedReader br = new BufferedReader(new FileReader(file));
435 StringBuilder sb = new StringBuilder();
436 String line = br.readLine();
437 while (line != null) {
438 sb.append(line);
439 line = br.readLine();
440 if (line != null) {
441 sb.append("\n");
442 }
443 }
444 return sb.toString();
445 }
446 return null;
447 }
448
449
450
451
452 public void createBatchProcessFile(String batchProcessType, String batchFileName, String fileContent, String jobId) throws Exception {
453 String fileLocation = getBatchProcessFilePath(batchProcessType, jobId);
454
455
456
457
458
459
460 if (fileLocation != null) {
461 BufferedWriter out = new BufferedWriter(new FileWriter(fileLocation + batchFileName));
462 out.write(fileContent);
463 out.close();
464 }
465 }
466
467
468
469
470
471 public void createBatchProcessFile(String batchProcessType, String mrcFileName, String ediFileName, String mrcfileContent, String ediFileContent , String jobId) throws Exception {
472 String fileLocation = getBatchProcessFilePath(batchProcessType , jobId);
473 if (fileLocation != null) {
474 BufferedWriter mrcOut = new BufferedWriter(new FileWriter(fileLocation + mrcFileName));
475 mrcOut.write(mrcfileContent);
476 mrcOut.close();
477 BufferedWriter ediOut = new BufferedWriter(new FileWriter(fileLocation + ediFileName));
478 ediOut.write(ediFileContent);
479 ediOut.close();
480 }
481 }
482
483 public void createBatchProcessFile(String batchProcessType, String documentFileName, String typeFileName, String historyFileName, String documentFileContent, String typeFileContent, String historyFileContent , String jobId) throws Exception {
484 String fileLocation = getBatchProcessFilePath(batchProcessType ,jobId);
485 if (fileLocation != null) {
486 if (documentFileName != null) {
487 BufferedWriter documentOut = new BufferedWriter(new FileWriter(fileLocation + documentFileName));
488 documentOut.write(documentFileContent);
489 documentOut.close();
490 }
491 if (typeFileName != null) {
492 BufferedWriter typeOut = new BufferedWriter(new FileWriter(fileLocation + typeFileName));
493 typeOut.write(typeFileContent);
494 typeOut.close();
495 }
496 if (historyFileName != null) {
497 BufferedWriter historyOut = new BufferedWriter(new FileWriter(fileLocation + historyFileName));
498 historyOut.write(historyFileContent);
499 historyOut.close();
500 }
501 }
502 }
503
504
505
506
507
508
509
510
511
512
513
514
515 public void createBatchBibImportFailureFile(String failureRecordData, String batchProcessType, String batchFileName, String jobId) throws Exception {
516 String fileLocation = getBatchProcessFilePath(batchProcessType,jobId);
517 String filePath = fileLocation + FileSystems.getDefault().getSeparator() + batchFileName;
518 createMarcRecord(failureRecordData, filePath);
519 }
520
521 public void createMarcRecord(String marcRecordContent, String filePath) throws Exception {
522 File fileToWrite = new File(filePath);
523 FileOutputStream fileOutputStream = new FileOutputStream(fileToWrite);
524
525 InputStream input = new ByteArrayInputStream(marcRecordContent.getBytes());
526 if (!fileToWrite.exists()) {
527 fileToWrite.getParentFile().mkdirs();
528 fileToWrite.createNewFile();
529 }
530 try {
531 MarcXmlReader marcXmlReader = new MarcXmlReader(input);
532 MarcWriter writer = new MarcStreamWriter(fileOutputStream);
533
534 while (marcXmlReader.hasNext()) {
535 Record record = marcXmlReader.next();
536 writer.write(record);
537 }
538 writer.close();
539 } catch (Exception e) {
540 e.printStackTrace();
541 throw new RuntimeException(e);
542 }
543 }
544
545 public String getExportPathUrl(OLEBatchProcessJobDetailsBo job) {
546
547
548 return applicationUrl + OLEConstants.OLEBatchProcess.BATCH_CHANNEL_STRING + applicationUrl +
549 OLEConstants.OLEBatchProcess.DIRECTORY_LIST_LOCATION + job.getUploadFileName().replace(statingDirectory,"");
550 }
551
552 public String getBibPathUrl(OLEBatchProcessJobDetailsBo job) {
553 return applicationUrl + OLEConstants.OLEBatchProcess.BATCH_CHANNEL_STRING + applicationUrl
554 + OLEConstants.OLEBatchProcess.DIRECTORY_LIST_LOCATION + getBatchProcessFilePath(job.getBatchProcessType(), job.getJobId()).replace(statingDirectory, "");
555 }
556
557 public String getDeletePathUrl(OLEBatchProcessJobDetailsBo job) {
558 return applicationUrl + OLEConstants.OLEBatchProcess.BATCH_CHANNEL_STRING + applicationUrl
559 + OLEConstants.OLEBatchProcess.DIRECTORY_LIST_LOCATION + getBatchProcessFilePath(job.getBatchProcessType(), job.getJobId()).replace(statingDirectory, "");
560 }
561
562 public String getSerialCSVPathUrl(OLEBatchProcessJobDetailsBo job) {
563 return applicationUrl + OLEConstants.OLEBatchProcess.BATCH_CHANNEL_STRING + applicationUrl
564 + OLEConstants.OLEBatchProcess.DIRECTORY_LIST_LOCATION + getBatchProcessFilePath(job.getBatchProcessType(), job.getJobId()).replace(statingDirectory, "");
565 }
566 }