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