1 package org.kuali.ole.batch.impl;
2
3 import org.apache.commons.collections.CollectionUtils;
4 import org.apache.commons.lang.StringUtils;
5 import org.kuali.ole.DataCarrierService;
6 import org.kuali.ole.OLEConstants;
7 import org.kuali.ole.batch.bo.*;
8 import org.kuali.ole.batch.helper.OLEBatchProcessDataHelper;
9 import org.kuali.ole.batch.service.BatchProcessBibImportService;
10 import org.kuali.ole.batch.util.BatchBibImportUtil;
11 import org.kuali.ole.converter.MarcXMLConverter;
12 import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
13 import org.kuali.ole.docstore.common.document.*;
14 import org.kuali.ole.docstore.common.document.content.bib.marc.*;
15 import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
16
17 import org.kuali.ole.docstore.common.exception.DocstoreIndexException;
18 import org.kuali.ole.docstore.common.search.SearchParams;
19 import org.kuali.ole.docstore.common.search.SearchResponse;
20 import org.kuali.ole.docstore.common.search.SearchResult;
21 import org.kuali.ole.docstore.common.search.SearchResultField;
22 import org.kuali.ole.docstore.common.util.BatchBibTreeDBUtil;
23 import org.kuali.ole.docstore.model.enums.DocType;
24 import org.kuali.ole.service.OLEEResourceHelperService;
25 import org.kuali.ole.sys.context.SpringContext;
26 import org.kuali.rice.krad.service.BusinessObjectService;
27 import org.kuali.rice.krad.service.KRADServiceLocator;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import java.sql.SQLException;
32 import java.text.SimpleDateFormat;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.Date;
36 import java.util.List;
37
38
39
40
41
42
43
44
45 public class BatchProcessBibImportServiceImpl implements BatchProcessBibImportService {
46
47 private static final Logger LOG = LoggerFactory.getLogger(BatchProcessBibImportServiceImpl.class);
48 private OLEBatchProcessDataHelper oleBatchProcessDataHelper;
49 private String bibMatchRecordRegex = "[0-9]+";
50 private BusinessObjectService businessObjectService;
51 private DocstoreClientLocator docstoreClientLocator;
52 private OLEEResourceHelperService oleeResourceHelperService;
53
54
55 public DocstoreClientLocator getDocstoreClientLocator() {
56 if (docstoreClientLocator == null) {
57 docstoreClientLocator = (DocstoreClientLocator)SpringContext.getService("docstoreClientLocator");
58 }
59 return docstoreClientLocator;
60 }
61
62 public OLEEResourceHelperService getOleeResourceHelperService() {
63 if(oleeResourceHelperService == null) {
64 oleeResourceHelperService = new OLEEResourceHelperService();
65 }
66 return oleeResourceHelperService;
67 }
68
69
70
71
72
73
74
75
76
77 @Override
78 public Bib performProcessBib(BibMarcRecord bibRecord, OLEBatchProcessProfileBo oleBatchProcessProfileBo, String staffOnly) throws Exception {
79 Bib requestBib = null;
80 deleteFields(bibRecord, oleBatchProcessProfileBo);
81 renameFields(bibRecord, oleBatchProcessProfileBo);
82 setDefaultOrConstants(bibRecord, oleBatchProcessProfileBo);
83 String uuid = getUuid(bibRecord);
84 requestBib = buildBibRequest(bibRecord, uuid);
85 setBibStatus(requestBib, oleBatchProcessProfileBo, uuid);
86 if (StringUtils.isEmpty(uuid)) {
87 requestBib.setStaffOnly(oleBatchProcessProfileBo.isBibStaffOnly());
88 } else {
89 if (OLEConstants.OLEBatchProcess.CHANGE.equals(oleBatchProcessProfileBo.getOverlayNoChangeOrSet())) {
90 requestBib.setStaffOnly(oleBatchProcessProfileBo.isOverlayBibStaffOnly());
91 }else if (oleBatchProcessProfileBo.getOverlayNoChangeOrSet().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DONOT_CHANGE)) {
92 if (staffOnly != null) {
93 requestBib.setStaffOnly(Boolean.valueOf(staffOnly));
94 }
95 }
96 }
97 return requestBib;
98 }
99
100
101
102
103
104
105
106
107
108 @Override
109 public Bib findMatchingBibRecord(BibMarcRecord bibRecord, OLEBatchProcessProfileBo oleBatchProcessProfileBo, List<BibMarcRecord> failureRecordsList) throws Exception {
110
111 Bib bibDocument = null;
112 List<OLEBatchProcessProfileMatchPoint> bibMatchingRecordList = BatchBibImportUtil.buildMatchPointListByDataType(oleBatchProcessProfileBo.getOleBatchProcessProfileMatchPointList(), DocType.BIB.getCode());
113 for (OLEBatchProcessProfileMatchPoint oleBatchProcessProfileBibMatchPoint : bibMatchingRecordList) {
114 String profileBibMatchRecord = oleBatchProcessProfileBibMatchPoint.getMatchPoint();
115 List<String> profileBibMatchRecordValues = getMatchingrecordValue(bibRecord, profileBibMatchRecord);
116 String matchRecord = getMatchRecord(profileBibMatchRecord);
117 List<String> matchPointUuids = new ArrayList<String>();
118 for (String profileBibMatchRecordValue : profileBibMatchRecordValues) {
119 if (matchRecord != null && profileBibMatchRecordValue != null && StringUtils.isNotEmpty(profileBibMatchRecordValue)) {
120 SearchParams searchParams = new SearchParams();
121 searchParams.getSearchConditions().add(searchParams.buildSearchCondition("phrase", searchParams.buildSearchField(DocType.BIB.getCode(), matchRecord, profileBibMatchRecordValue), "AND"));
122 searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("bibliographic", "bibIdentifier"));
123 try {
124 SearchResponse searchResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
125 List<SearchResult> searchResults = searchResponse.getSearchResults();
126 if (searchResults.size() > 0) {
127 for (SearchResult searchResult : searchResults) {
128 for (SearchResultField searchResultfield : searchResult.getSearchResultFields()) {
129 if (searchResultfield.getFieldName().equalsIgnoreCase("bibIdentifier") && searchResultfield.getFieldValue() != null && !searchResultfield.getFieldValue().isEmpty()) {
130 matchPointUuids.add(searchResultfield.getFieldValue());
131 }
132 }
133 }
134 }
135 } catch (Exception ex) {
136 throw new RuntimeException(ex);
137 }
138 if (matchPointUuids.size() > 0) {
139 break;
140 }
141 }
142 }
143 if (matchPointUuids.size() == 1) {
144 for (String uuid : matchPointUuids) {
145 Bib bib = docstoreClientLocator.getDocstoreClient().retrieveBib(uuid);
146 if (bib != null) {
147 bibDocument = bib;
148 }
149 }
150 } else if (matchPointUuids.size() > 1) {
151 failureRecordsList.add(bibRecord);
152 break;
153 }
154 }
155 return bibDocument;
156 }
157
158
159
160
161
162
163
164 @Override
165 public String preProcessMarc(String marcFileContent) throws Exception {
166 String marcXMLContent = null;
167 MarcXMLConverter marcXMLConverter = new MarcXMLConverter();
168 marcXMLContent = marcXMLConverter.convert(marcFileContent);
169
170 String modifiedXMLContent =
171 marcXMLContent.
172 replace("collection xmlns=\"http://www.loc.gov/MARC21/slim\" xmlns=\"http://www.loc.gov/MARC21/slim",
173 "collection xmlns=\"http://www.loc.gov/MARC21/slim");
174 return modifiedXMLContent;
175 }
176
177
178
179
180
181
182
183 public void deleteFields(BibMarcRecord bibRecord, OLEBatchProcessProfileBo oleBatchProcessProfileBo) {
184 List<OLEBatchProcessProfileDeleteField> deleteFields = oleBatchProcessProfileBo.getOleBatchProcessProfileDeleteFieldsList();
185 for (OLEBatchProcessProfileDeleteField oleBatchProcessProfileDeleteField : deleteFields) {
186 if (oleBatchProcessProfileDeleteField.getFirstIndicator() == null) {
187 oleBatchProcessProfileDeleteField.setFirstIndicator("");
188 }
189 if (oleBatchProcessProfileDeleteField.getFirstIndicator() != null) {
190 if (oleBatchProcessProfileDeleteField.getFirstIndicator().contains("#")) {
191 oleBatchProcessProfileDeleteField.setFirstIndicator("");
192 }
193 }
194 if (oleBatchProcessProfileDeleteField.getSecondIndicator() == null) {
195 oleBatchProcessProfileDeleteField.setSecondIndicator("");
196 }
197 if (oleBatchProcessProfileDeleteField.getSecondIndicator() != null) {
198 if (oleBatchProcessProfileDeleteField.getSecondIndicator().contains("#")) {
199 oleBatchProcessProfileDeleteField.setSecondIndicator("");
200 }
201 }
202 if (StringUtils.isEmpty(oleBatchProcessProfileDeleteField.getSubField()) || (oleBatchProcessProfileDeleteField.getSubField() == "" || oleBatchProcessProfileDeleteField.getSubField() == null)) {
203 if (!isProtectedDataField(oleBatchProcessProfileBo, oleBatchProcessProfileDeleteField)) {
204 if (StringUtils.isNotEmpty(oleBatchProcessProfileDeleteField.getTag())) {
205 getOleBatchProcessDataHelper().deleteMarcFields(bibRecord, oleBatchProcessProfileDeleteField);
206 }
207 }
208 }
209 if (StringUtils.isNotEmpty(oleBatchProcessProfileDeleteField.getSubField()) || oleBatchProcessProfileDeleteField.getSubField() != "" || oleBatchProcessProfileDeleteField.getSubField() != null) {
210 if (StringUtils.isNotEmpty(oleBatchProcessProfileDeleteField.getSubField())) {
211 getOleBatchProcessDataHelper().deleteMarcSubFields(bibRecord, oleBatchProcessProfileDeleteField);
212 }
213 }
214 }
215 }
216
217 private boolean isProtectedSubField(OLEBatchProcessProfileBo oleBatchProcessProfileBo, OLEBatchProcessProfileDeleteField deleteField) {
218 List<OLEBatchGloballyProtectedField> batchGloballyProtectedFieldList = oleBatchProcessProfileBo.getOleBatchGloballyProtectedFieldList();
219 List<OLEBatchProcessProfileProtectedField> batchProcessProfileProtectedFieldList = oleBatchProcessProfileBo.getOleBatchProcessProfileProtectedFieldList();
220 for (OLEBatchGloballyProtectedField oleBatchGloballyProtectedField : batchGloballyProtectedFieldList) {
221 if (oleBatchGloballyProtectedField.isIgnoreValue()) {
222 if (oleBatchGloballyProtectedField.getFirstIndicator() == null) {
223 oleBatchGloballyProtectedField.setFirstIndicator("");
224 }
225 if (oleBatchGloballyProtectedField.getSecondIndicator() == null) {
226 oleBatchGloballyProtectedField.setSecondIndicator("");
227 }
228 if (StringUtils.isNotEmpty(oleBatchGloballyProtectedField.getTag()) && StringUtils.isNotEmpty(deleteField.getTag()) && oleBatchGloballyProtectedField.getTag().equalsIgnoreCase(deleteField.getTag())) {
229 if (oleBatchGloballyProtectedField.getFirstIndicator().equalsIgnoreCase(deleteField.getFirstIndicator())) {
230 if (oleBatchGloballyProtectedField.getSecondIndicator().equalsIgnoreCase(deleteField.getSecondIndicator())) {
231 if (StringUtils.isNotEmpty(oleBatchGloballyProtectedField.getSubField()) && StringUtils.isNotEmpty(deleteField.getSubField()) && oleBatchGloballyProtectedField.getSubField().equalsIgnoreCase(deleteField.getSubField())) {
232 return true;
233 } else if (StringUtils.isEmpty(oleBatchGloballyProtectedField.getSubField()) || oleBatchGloballyProtectedField.getSubField() == "" || oleBatchGloballyProtectedField.getSubField() == null) {
234 return true;
235 }
236 }
237 }
238 }
239 }
240 }
241 for (OLEBatchProcessProfileProtectedField oleBatchProcessProfileProtectedField : batchProcessProfileProtectedFieldList) {
242 if (oleBatchProcessProfileProtectedField.getFirstIndicator() == null) {
243 oleBatchProcessProfileProtectedField.setFirstIndicator("");
244 }
245 if (oleBatchProcessProfileProtectedField.getSecondIndicator() == null) {
246 oleBatchProcessProfileProtectedField.setSecondIndicator("");
247 }
248 if (StringUtils.isNotEmpty(oleBatchProcessProfileProtectedField.getTag()) && StringUtils.isNotEmpty(deleteField.getTag()) && oleBatchProcessProfileProtectedField.getTag().equalsIgnoreCase(deleteField.getTag())) {
249 if (oleBatchProcessProfileProtectedField.getFirstIndicator().equalsIgnoreCase(deleteField.getFirstIndicator())) {
250 if (oleBatchProcessProfileProtectedField.getSecondIndicator().equalsIgnoreCase(deleteField.getSecondIndicator())) {
251 if (StringUtils.isNotEmpty(oleBatchProcessProfileProtectedField.getSubField()) && StringUtils.isNotEmpty(deleteField.getSubField()) && oleBatchProcessProfileProtectedField.getSubField().equalsIgnoreCase(deleteField.getSubField())) {
252 return true;
253 } else if (StringUtils.isEmpty(oleBatchProcessProfileProtectedField.getSubField()) || oleBatchProcessProfileProtectedField.getSubField() == "" || oleBatchProcessProfileProtectedField.getSubField() == null) {
254 return true;
255 }
256 }
257 }
258 }
259 }
260 return false;
261 }
262
263 private boolean isProtectedDataField(OLEBatchProcessProfileBo oleBatchProcessProfileBo, OLEBatchProcessProfileDeleteField deleteField) {
264 List<OLEBatchGloballyProtectedField> batchGloballyProtectedFieldList = oleBatchProcessProfileBo.getOleBatchGloballyProtectedFieldList();
265 List<OLEBatchProcessProfileProtectedField> batchProcessProfileProtectedFieldList = oleBatchProcessProfileBo.getOleBatchProcessProfileProtectedFieldList();
266 boolean isExist = false;
267 for (OLEBatchGloballyProtectedField oleBatchGloballyProtectedField : batchGloballyProtectedFieldList) {
268 if (StringUtils.isNotEmpty(oleBatchGloballyProtectedField.getTag()) && StringUtils.isNotEmpty(deleteField.getTag()) && oleBatchGloballyProtectedField.getTag().equalsIgnoreCase(deleteField.getTag())) {
269 isExist = true;
270 }
271 }
272 if (isExist) {
273 for (OLEBatchGloballyProtectedField oleBatchGloballyProtectedField : batchGloballyProtectedFieldList) {
274 if (oleBatchGloballyProtectedField.isIgnoreValue()) {
275 if (StringUtils.isNotEmpty(oleBatchGloballyProtectedField.getTag()) && StringUtils.isNotEmpty(deleteField.getTag()) && oleBatchGloballyProtectedField.getTag().equalsIgnoreCase(deleteField.getTag())) {
276 return false;
277 }
278 }
279 }
280 } else {
281 return false;
282 }
283 for (OLEBatchProcessProfileProtectedField oleBatchProcessProfileProtectedField : batchProcessProfileProtectedFieldList) {
284 if (StringUtils.isNotEmpty(oleBatchProcessProfileProtectedField.getTag()) && StringUtils.isNotEmpty(deleteField.getTag()) && oleBatchProcessProfileProtectedField.getTag().equalsIgnoreCase(deleteField.getTag())) {
285 return false;
286 }
287 }
288 return true;
289 }
290
291
292
293
294
295
296
297
298
299 private boolean isProtectedField(OLEBatchProcessProfileBo oleBatchProcessProfileBo, String deleteField, String subFieldContains) {
300 List<OLEBatchGloballyProtectedField> batchGloballyProtectedFieldList = oleBatchProcessProfileBo.getOleBatchGloballyProtectedFieldList();
301 List<OLEBatchProcessProfileProtectedField> batchProcessProfileProtectedFieldList = oleBatchProcessProfileBo.getOleBatchProcessProfileProtectedFieldList();
302 String batchGlblyPrctdFld = "";
303 String prflPrctFld = "";
304 String deleteFldWithValue = deleteField + subFieldContains;
305 for (OLEBatchGloballyProtectedField oleBatchGloballyProtectedField : batchGloballyProtectedFieldList) {
306 if (oleBatchGloballyProtectedField.isIgnoreValue()) {
307 batchGlblyPrctdFld = getBatchDataFldFullString(oleBatchGloballyProtectedField.getTag(), oleBatchGloballyProtectedField.getFirstIndicator(), oleBatchGloballyProtectedField.getSecondIndicator(), oleBatchGloballyProtectedField.getSubField());
308 if (deleteField != null && batchGlblyPrctdFld.equalsIgnoreCase(deleteField)) {
309 return true;
310 } else if (deleteField != null && batchGlblyPrctdFld.contains(deleteField)) {
311 return true;
312 }
313 }
314 }
315 for (OLEBatchProcessProfileProtectedField oleBatchProcessProfileProtectedField : batchProcessProfileProtectedFieldList) {
316 prflPrctFld = getBatchDataFldFullValueString(oleBatchProcessProfileProtectedField.getTag(), oleBatchProcessProfileProtectedField.getFirstIndicator(), oleBatchProcessProfileProtectedField.getSecondIndicator(), oleBatchProcessProfileProtectedField.getSubField(), oleBatchProcessProfileProtectedField.getSubFieldContains());
317 if (deleteFldWithValue != null && prflPrctFld.equalsIgnoreCase(deleteFldWithValue)) {
318 return true;
319 } else if (deleteFldWithValue != null && prflPrctFld.contains(deleteFldWithValue)) {
320 return true;
321 }
322 }
323 return false;
324 }
325
326
327
328
329
330
331
332
333
334
335 private String getBatchDataFldFullString(String tag, String ind1, String ind2, String subField) {
336 String fullRecord = null;
337 if (tag != null) {
338 if (ind1 == null || ind1.equalsIgnoreCase(" ") || StringUtils.isEmpty(ind1)) ind1 = "#";
339 if (ind2 == null || ind2.equalsIgnoreCase(" ") || StringUtils.isEmpty(ind2)) ind2 = "#";
340 if (!subField.contains("$")) {
341 fullRecord = tag + " " + ind1 + ind2 + " $" + subField;
342 } else {
343 fullRecord = tag + " " + ind1 + ind2 + " " + subField;
344 }
345 }
346 return fullRecord;
347 }
348
349
350
351
352
353
354
355
356
357
358
359 private String getBatchDataFldFullString(String tag, String ind1, String ind2, String subField, String profileBibMatchRecord) {
360 String fullRecord = null;
361 if (StringUtils.isNotEmpty(profileBibMatchRecord) && profileBibMatchRecord.contains(tag)) {
362 String[] profileBibInds = profileBibMatchRecord.split(" ");
363 if (profileBibInds.length == 3 && profileBibInds[1].contains("#")) {
364 String inds = profileBibInds[1];
365 if (inds.equals("##")) {
366 ind1 = "";
367 ind2 = "";
368 } else if (inds.startsWith("#")) {
369 ind1 = "";
370 } else if (inds.endsWith("#")) {
371 ind2 = "";
372 }
373
374 } else if (profileBibInds.length == 2 && profileBibInds[1].contains("$")) {
375 fullRecord = tag + " $" + subField;
376 return fullRecord;
377 }
378 }
379
380
381 if (tag != null) {
382 if (ind1 == null || ind1.equalsIgnoreCase(" ") || StringUtils.isEmpty(ind1)) ind1 = "#";
383 if (ind2 == null || ind2.equalsIgnoreCase(" ") || StringUtils.isEmpty(ind2)) ind2 = "#";
384 fullRecord = tag + " " + ind1 + ind2 + " $" + subField;
385 }
386 return fullRecord;
387 }
388
389
390
391
392
393
394
395
396
397
398
399 private String getBatchDataFldFullValueString(String tag, String ind1, String ind2, String subField, String subFieldValue) {
400 String fullRecord = null;
401 if (tag != null) {
402 if (ind1 == null || ind1.equalsIgnoreCase(" ") || StringUtils.isEmpty(ind1)) ind1 = "#";
403 if (ind2 == null || ind2.equalsIgnoreCase(" ") || StringUtils.isEmpty(ind2)) ind2 = "#";
404 if (subField != null && !subField.contains("$")) {
405 fullRecord = tag + " " + ind1 + ind2 + " $" + subField + subFieldValue;
406 } else {
407 subField="";
408 subFieldValue="";
409 fullRecord = tag + " " + ind1 + ind2 + " " + subField + subFieldValue;
410 }
411
412 }
413 return fullRecord;
414 }
415
416
417
418
419
420
421
422 public void renameFields(BibMarcRecord targetRecord, OLEBatchProcessProfileBo oleBatchProcessProfileBo) {
423 List<OLEBatchProcessProfileRenameField> renameList = oleBatchProcessProfileBo.getOleBatchProcessProfileRenameFieldsList();
424 for (OLEBatchProcessProfileRenameField renameField : renameList) {
425 if (renameField.getOriginalFirstIndicator() == null) {
426 renameField.setOriginalFirstIndicator("");
427 }
428 if (renameField.getOriginalFirstIndicator() != null) {
429 if (renameField.getOriginalFirstIndicator().contains("#")) {
430 renameField.setOriginalFirstIndicator(" ");
431 }
432 }
433 if (renameField.getOriginalSecondIndicator() == null) {
434 renameField.setOriginalSecondIndicator("");
435 }
436 if (renameField.getOriginalSecondIndicator() != null) {
437 if (renameField.getOriginalSecondIndicator().contains("#")) {
438 renameField.setOriginalSecondIndicator(" ");
439 }
440 }
441 OLEBatchProcessProfileDeleteField oleBatchProcessProfileDeleteField = new OLEBatchProcessProfileDeleteField();
442 oleBatchProcessProfileDeleteField.setTag(renameField.getOriginalTag());
443 oleBatchProcessProfileDeleteField.setFirstIndicator(renameField.getOriginalFirstIndicator());
444 oleBatchProcessProfileDeleteField.setSecondIndicator(renameField.getOriginalSecondIndicator());
445 oleBatchProcessProfileDeleteField.setSubField(renameField.getOriginalSubField());
446 if (StringUtils.isNotEmpty(oleBatchProcessProfileDeleteField.getSubField()) || oleBatchProcessProfileDeleteField.getSubField() != "" || oleBatchProcessProfileDeleteField.getSubField() != null) {
447 if (StringUtils.isNotEmpty(oleBatchProcessProfileDeleteField.getSubField())) {
448 getOleBatchProcessDataHelper().addMarcFields(targetRecord, renameField);
449 getOleBatchProcessDataHelper().deleteMarcSubFields(targetRecord, oleBatchProcessProfileDeleteField);
450 }
451 }
452 if (StringUtils.isEmpty(oleBatchProcessProfileDeleteField.getSubField()) || (oleBatchProcessProfileDeleteField.getSubField() == "" || oleBatchProcessProfileDeleteField.getSubField() == null)) {
453 if (StringUtils.isNotEmpty(oleBatchProcessProfileDeleteField.getTag())) {
454 getOleBatchProcessDataHelper().renameMarcFields(targetRecord, renameField);
455 }
456 }
457 }
458 }
459
460
461
462
463
464
465
466 private DataField addControlField003To035a(String valueOf003) {
467 DataField dataField = new DataField();
468 dataField.setTag(OLEConstants.OLEBatchProcess.DATA_FIELD_035);
469 SubField subField = new SubField();
470 subField.setCode("a");
471 subField.setValue(valueOf003);
472 List<SubField> subFields = new ArrayList<>();
473 subFields.add(subField);
474 dataField.setSubFields(subFields);
475 return dataField;
476 }
477
478
479
480
481
482
483
484
485 private ControlField getControlField(List<ControlField> controlFields, String tag) {
486 for (ControlField controlField : controlFields) {
487 if (tag.equalsIgnoreCase(controlField.getTag())) {
488 return controlField;
489 }
490 }
491 return null;
492 }
493
494
495
496
497
498
499
500
501 public Bib buildBibRequest(BibMarcRecord bibRecord, String uuid) {
502 BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
503 String bibXML = bibMarcRecordProcessor.generateXML(bibRecord);
504 return buildBibRequest(bibXML, uuid);
505 }
506
507 public Bib buildBibRequest(String bibXML, String uuid) {
508 Bib requestBib = new Bib();
509 if (StringUtils.isNotEmpty(uuid)) {
510 requestBib.setId(uuid);
511 }
512 requestBib.setContent(bibXML);
513 return requestBib;
514 }
515
516
517
518
519
520
521
522
523 public void setBibStatus(Bib requestBib, OLEBatchProcessProfileBo oleBatchProcessProfileBo, String uuid) {
524 if (null != requestBib) {
525 if (uuid == null) {
526 requestBib.setStatus(oleBatchProcessProfileBo.getNewBibStaus());
527 } else if (uuid != null && OLEConstants.OLEBatchProcess.CHANGE.equals(oleBatchProcessProfileBo.getNoChangeOrSet())) {
528 requestBib.setStatus(oleBatchProcessProfileBo.getExistedBibStatus());
529 }
530 }
531 }
532
533
534
535
536
537
538
539 public String getUuid(BibMarcRecord bibRecord) {
540 List<ControlField> controlFields = bibRecord.getControlFields();
541 String uuid = null;
542 for (ControlField controlField : controlFields) {
543 if (OLEConstants.OLEBatchProcess.CONTROL_FIELD_001.equals(controlField.getTag())) {
544 uuid = "wbm-" + controlField.getValue();
545 break;
546 }
547 }
548 return uuid;
549 }
550
551
552
553
554
555
556
557 public void setDefaultOrConstants(BibMarcRecord targetRecord, OLEBatchProcessProfileBo oleBatchProcessProfileBo) {
558 String fullDataField = null;
559 List<OLEBatchProcessProfileConstantsBo> oleBatchProcessProfileConstantsBoList = oleBatchProcessProfileBo.getOleBatchProcessProfileConstantsList();
560 for (OLEBatchProcessProfileConstantsBo oleBatchProcessProfileConstantsBo : oleBatchProcessProfileConstantsBoList) {
561 if (!"Bibmarc".equalsIgnoreCase(oleBatchProcessProfileConstantsBo.getDataType()))
562 continue;
563
564 boolean isDataFieldExist = false;
565 for (DataField dataField : targetRecord.getDataFields()) {
566 if (dataField.getTag().equalsIgnoreCase(oleBatchProcessProfileConstantsBo.getAttributeName().substring(0, 3))) {
567 for (SubField subField : dataField.getSubFields()) {
568 fullDataField = getBatchDataFldFullString(dataField.getTag(), dataField.getInd1(), dataField.getInd2(), subField.getCode());
569 buildDataFiledfullForComparision(oleBatchProcessProfileConstantsBo);
570 if (fullDataField.equalsIgnoreCase(oleBatchProcessProfileConstantsBo.getAttributeName())) {
571 if (oleBatchProcessProfileConstantsBo.getDefaultValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.PROFILE_CONSTANT_DEFAULT)) {
572 isDataFieldExist = true;
573 if (StringUtils.isEmpty(subField.getValue())) {
574 subField.setValue(oleBatchProcessProfileConstantsBo.getAttributeValue());
575 }
576 }
577 }
578 }
579 }
580 }
581 if (!isDataFieldExist) {
582 DataField dataField = BatchBibImportUtil.buildDataField(oleBatchProcessProfileConstantsBo.getAttributeName(), oleBatchProcessProfileConstantsBo.getAttributeValue());
583 targetRecord.getDataFields().add(dataField);
584 }
585 }
586
587 }
588
589 private void buildDataFiledfullForComparision(OLEBatchProcessProfileConstantsBo oleBatchProcessProfileConstantsBo) {
590 if (oleBatchProcessProfileConstantsBo.getAttributeName().length() < 7) {
591 String[] attributeList = oleBatchProcessProfileConstantsBo.getAttributeName().split(" ");
592 if (attributeList.length == 2) {
593 oleBatchProcessProfileConstantsBo.setAttributeName(getBatchDataFldFullString(attributeList[0], "#", "#", attributeList[1]));
594 }
595 }
596 }
597
598
599
600
601
602
603
604 private String getMatchRecord(String profileBibMatchRecord) {
605 String matchRecord = null;
606 if (OLEConstants.OLEBatchProcess.CONTROL_FIELD_001.equals(profileBibMatchRecord)) {
607 matchRecord = profileBibMatchRecord;
608 } else {
609
610 String[] matchRecordSplit = profileBibMatchRecord.split(" ");
611 String fullSubField = matchRecordSplit[matchRecordSplit.length - 1];
612 matchRecord = matchRecordSplit[0] + fullSubField.substring(fullSubField.length() - 1);
613 }
614 return OLEConstants.PREFIX_FOR_DATA_FIELD + matchRecord;
615 }
616
617
618
619
620
621
622
623
624 private List<String> getMatchingrecordValue(BibMarcRecord bibRecord, String profileBibMatchRecord) {
625 List<String> profileBibMatchRecordValue = new ArrayList<>();
626 if (OLEConstants.OLEBatchProcess.CONTROL_FIELD_001.equals(profileBibMatchRecord)) {
627 List<ControlField> controlFields = bibRecord.getControlFields();
628 for (ControlField controlField : controlFields) {
629 if (profileBibMatchRecord.equals(controlField.getTag())) {
630 profileBibMatchRecordValue.add(controlField.getValue());
631 controlFields.remove(controlField);
632 break;
633 }
634 }
635 } else {
636
637 List<DataField> dataFieldsList = bibRecord.getDataFields();
638 String dataFieldString = null;
639 for (DataField dataField : dataFieldsList) {
640 for (SubField subField : dataField.getSubFields()) {
641 if (StringUtils.isNotEmpty(profileBibMatchRecord) && profileBibMatchRecord.contains(dataField.getTag())) {
642 dataFieldString = getBatchDataFldFullString(dataField.getTag(), dataField.getInd1(), dataField.getInd2(), subField.getCode(), profileBibMatchRecord);
643 if (dataFieldString.equalsIgnoreCase(profileBibMatchRecord)) {
644 profileBibMatchRecordValue.add(subField.getValue());
645 }
646 }
647 }
648 }
649 }
650 return profileBibMatchRecordValue;
651 }
652
653
654
655
656
657
658
659
660 @Override
661 public void process001(BibMarcRecord bibMarcRecord, OLEBatchProcessProfileBo oleBatchProcessProfileBo) {
662 ControlField controlField001 = getMatchedControlField(bibMarcRecord, OLEConstants.OLEBatchProcess.CONTROL_FIELD_001);
663 ControlField controlField003 = getMatchedControlField(bibMarcRecord, OLEConstants.OLEBatchProcess.CONTROL_FIELD_003);
664
665 if (controlField001 != null) {
666 String controlField001Value = controlField001.getValue();
667 if (OLEConstants.OLEBatchProcess.DELETE_001.equalsIgnoreCase(oleBatchProcessProfileBo.getDontChange001())) {
668 bibMarcRecord.getControlFields().remove(controlField001);
669 } else if (OLEConstants.OLEBatchProcess.CHANGE_TAG_035.equalsIgnoreCase(oleBatchProcessProfileBo.getDontChange001())) {
670 boolean removePrefixIndicator = oleBatchProcessProfileBo.getRemoveValueFrom001();
671 if (oleBatchProcessProfileBo.getValueToRemove() != null && !oleBatchProcessProfileBo.getValueToRemove().isEmpty()) {
672 String[] valuesToRemove = oleBatchProcessProfileBo.getValueToRemove().split(",");
673 for (String valueToRemove : valuesToRemove) {
674 controlField001Value = controlField001Value.replaceAll(valueToRemove.trim(), "");
675 }
676 }
677 String prefix = "";
678 if (OLEConstants.OLEBatchProcess.PREPEND_001_TO_035.equalsIgnoreCase(oleBatchProcessProfileBo.getPrepend003To035())) {
679 if (controlField003 != null && controlField003.getValue() != null && controlField003.getValue().length() > 0) {
680 prefix = "(" + controlField003.getValue() + ")";
681 }
682 } else if (OLEConstants.OLEBatchProcess.PREPEND_VALUE_TO_035.equalsIgnoreCase(oleBatchProcessProfileBo.getPrepend003To035())) {
683 prefix = StringUtils.isEmpty(oleBatchProcessProfileBo.getValueToPrepend()) ? "" : oleBatchProcessProfileBo.getValueToPrepend();
684 }
685 String dataField035aValue = prefix + controlField001Value;
686 DataField dataField035a = addControlField003To035a(dataField035aValue);
687 bibMarcRecord.getDataFields().add(dataField035a);
688 bibMarcRecord.getControlFields().remove(controlField001);
689 }
690 }
691 }
692
693
694
695
696
697
698
699
700 public ControlField getMatchedControlField(BibMarcRecord bibMarcRecord, String tag) {
701
702 for (ControlField controlField : bibMarcRecord.getControlFields()) {
703 if (controlField.getTag().equalsIgnoreCase(tag)) {
704 return controlField;
705 }
706 }
707 return null;
708 }
709
710
711
712
713
714
715
716
717
718 @Override
719 public BibMarcRecord overlayFields(BibMarcRecord inComingRecord, BibMarcRecord matchedRecord, OLEBatchProcessProfileBo processProfile) {
720
721 List<String> protectedFields = getProtectedFieldsTags(processProfile);
722 List<DataField> dataFields = matchedRecord.getDataFields();
723 List<DataField> dataFieldList = new ArrayList<>();
724
725
726 for (DataField dataField : dataFields) {
727 if (protectedFields.contains(dataField.getTag())) {
728 dataFieldList.add(dataField);
729 }
730 }
731
732
733 dataFieldList.addAll(inComingRecord.getDataFields());
734 Collections.sort(dataFieldList);
735 matchedRecord.setDataFields(dataFieldList);
736 List<ControlField> controlFields = inComingRecord.getControlFields();
737 ControlField controlField001 = getMatchedControlField(inComingRecord, OLEConstants.OLEBatchProcess.CONTROL_FIELD_001);
738 if (controlField001 != null) {
739 controlFields.remove(controlField001);
740 }
741 controlFields.add(getControlField(matchedRecord.getControlFields(), OLEConstants.OLEBatchProcess.CONTROL_FIELD_001));
742 matchedRecord.setControlFields(controlFields);
743 matchedRecord.setLeader(inComingRecord.getLeader());
744 return matchedRecord;
745
746 }
747
748
749 @Override
750 public List<BibMarcRecord> saveBatch(List<BibMarcRecord> bibMarcRecords, OLEBatchBibImportDataObjects oleBatchBibImportDataObjects, OLEBatchBibImportStatistics oleBatchbibImportStatistics) {
751 Date dNow = new Date();
752 docstoreClientLocator = getDocstoreClientLocator();
753 try {
754 oleBatchBibImportDataObjects.setBibTreesObj(docstoreClientLocator.getDocstoreClient().processBibTrees(oleBatchBibImportDataObjects.getBibTrees()));
755 } catch (DocstoreIndexException docexp) {
756 SimpleDateFormat formatter = new SimpleDateFormat("yy-mm-dd hh:mm:ss");
757 String updateDate = formatter.format(dNow);
758 BatchBibTreeDBUtil bibTreeDBUtil = new BatchBibTreeDBUtil();
759 try {
760 bibTreeDBUtil.init(0, 0, updateDate);
761 } catch (SQLException e1) {
762 LOG.error("Batch Process", e1);
763
764 }
765 } catch (Exception e) {
766 LOG.error("Batch Process", e);
767 oleBatchbibImportStatistics.getErrorBuilder().append(OLEConstants.OLEBatchProcess.PROCESS_FAILURE).append(System.lineSeparator());
768 }
769
770 for (int i = 0; i < oleBatchBibImportDataObjects.getBibTrees().getBibTrees().size(); i++) {
771 BibTree bibTree = oleBatchBibImportDataObjects.getBibTrees().getBibTrees().get(i);
772 if (null != bibTree) {
773 if (null != bibTree.getBib() && DocstoreDocument.ResultType.FAILURE.equals(bibTree.getBib().getResult())) {
774 setErrorMessage(bibMarcRecords, oleBatchbibImportStatistics, i, bibTree.getBib().getMessage());
775 continue;
776 }
777 if (CollectionUtils.isNotEmpty(bibTree.getHoldingsTrees())) {
778 for (HoldingsTree holdingsTree : bibTree.getHoldingsTrees()) {
779 if (null != holdingsTree.getHoldings()) {
780 if (holdingsTree.getHoldings().getHoldingsType() != null && holdingsTree.getHoldings().getHoldingsType().equalsIgnoreCase("electronic") && holdingsTree.getHoldings().getOperation().equals(DocstoreDocument.OperationType.CREATE)
781 && DocstoreDocument.ResultType.SUCCESS.equals(holdingsTree.getHoldings().getResult())) {
782 getOleeResourceHelperService().updateEHoldingsInEResource(holdingsTree.getHoldings());
783 }
784 setErrorMessage(bibMarcRecords, oleBatchbibImportStatistics, i, holdingsTree.getHoldings().getMessage());
785 if(holdingsTree.getHoldings().getOperation().equals(DocstoreDocument.OperationType.DELETE) && DocstoreDocument.ResultType.SUCCESS.equals(holdingsTree.getHoldings().getResult())){
786 oleBatchbibImportStatistics.getTotalRecordsDeleted().add(holdingsTree.getHoldings());
787 getOleeResourceHelperService().deleteEHoldingsInEResource(holdingsTree.getHoldings());
788 }
789
790 }
791
792
793 if (CollectionUtils.isNotEmpty(holdingsTree.getItems())) {
794 for (Item item : holdingsTree.getItems()) {
795 if (null != item) {
796 if (DocstoreDocument.ResultType.FAILURE.equals(item.getResult())) {
797 setErrorMessage(bibMarcRecords, oleBatchbibImportStatistics, i, item.getMessage());
798 }
799 }
800 }
801 }
802 }
803 }
804 }
805 }
806 return oleBatchbibImportStatistics.getMismatchRecordList();
807 }
808
809 private void setErrorMessage(List<BibMarcRecord> bibMarcRecords, OLEBatchBibImportStatistics oleBatchbibImportStatistics, int recordNumber, String failureMessage) {
810 if (StringUtils.isNotEmpty(failureMessage)) {
811 BibMarcRecord bibMarcRecord = bibMarcRecords.get(recordNumber);
812 oleBatchbibImportStatistics.getMismatchRecordList().add(bibMarcRecord);
813 oleBatchbibImportStatistics.getErrorBuilder().append("Record #" + ++recordNumber).append(" : For Title - " + BatchBibImportUtil.getTitle(bibMarcRecord) +" - ");
814 oleBatchbibImportStatistics.getErrorBuilder().append(failureMessage).append(System.lineSeparator());
815 }
816 }
817
818
819 @Override
820 public List<OrderBibMarcRecord> saveOderBatch(List<OrderBibMarcRecord> orderBibMarcRecords, OLEBatchBibImportDataObjects oleBatchBibImportDataObjects, OLEBatchBibImportStatistics bibImportStatistics) {
821 docstoreClientLocator = getDocstoreClientLocator();
822 List<BibMarcRecord> bibMarcRecords = new ArrayList<>();
823 for (OrderBibMarcRecord orderBibMarcRecord : orderBibMarcRecords) {
824 bibMarcRecords.add(orderBibMarcRecord.getBibMarcRecord());
825 }
826 saveBatch(bibMarcRecords, oleBatchBibImportDataObjects, bibImportStatistics);
827 return oleBatchBibImportDataObjects.getResponseOrderRecord(orderBibMarcRecords);
828 }
829
830
831
832
833
834
835
836
837
838 private List<String> getProtectedFieldsTags(OLEBatchProcessProfileBo processProfile) {
839 List<String> protectedFields = new ArrayList<>();
840
841 for (OLEBatchGloballyProtectedField oleBatchGloballyProtectedField : processProfile.getOleBatchGloballyProtectedFieldList()) {
842 protectedFields.add(oleBatchGloballyProtectedField.getTag());
843 }
844 for (OLEBatchProcessProfileProtectedField oleBatchProcessProfileProtectedField : processProfile.getOleBatchProcessProfileProtectedFieldList()) {
845 protectedFields.add(oleBatchProcessProfileProtectedField.getTag());
846 }
847 return protectedFields;
848 }
849
850 public OLEBatchProcessDataHelper getOleBatchProcessDataHelper() {
851 if (oleBatchProcessDataHelper == null) oleBatchProcessDataHelper = OLEBatchProcessDataHelper.getInstance();
852 return oleBatchProcessDataHelper;
853 }
854
855 public BusinessObjectService getBusinessObjectService() {
856 if (null == businessObjectService) businessObjectService = KRADServiceLocator.getBusinessObjectService();
857 return businessObjectService;
858 }
859
860
861 }