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