View Javadoc
1   package org.kuali.ole.batch.helper;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.batch.bo.*;
6   import org.kuali.ole.batch.impl.BatchProcessBibImportServiceImpl;
7   import org.kuali.ole.batch.util.BatchBibImportUtil;
8   import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
9   import org.kuali.ole.docstore.common.document.*;
10  import org.kuali.ole.docstore.common.document.content.bib.marc.*;
11  import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
12  import org.kuali.ole.docstore.common.exception.DocstoreException;
13  import org.kuali.ole.docstore.common.exception.DocstoreSearchException;
14  import org.kuali.ole.docstore.common.search.SearchCondition;
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.model.enums.DocType;
19  import org.kuali.ole.sys.context.SpringContext;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  
23  import java.util.*;
24  
25  /**
26   * Created with IntelliJ IDEA.
27   * User: jayabharathreddy
28   * Date: 7/1/14
29   * Time: 11:55 AM
30   * To change this template use File | Settings | File Templates.
31   */
32  public class BatchBibImportHelper {
33  
34      private static final Logger LOG = LoggerFactory.getLogger(BatchBibImportHelper.class);
35      private BatchProcessBibImportServiceImpl batchProcessBibImportService = new BatchProcessBibImportServiceImpl();
36      private OLEBatchBibImportStatistics bibImportStatistics = null;
37      private List<DataField> holdingsDataFields = new ArrayList<>();
38      private List<DataField> eHoldingsDataFields = new ArrayList<>();
39      private List<DataField> itemDataFields = new ArrayList<>();
40      private List<OLEBatchProcessProfileDataMappingOptionsBo> dataMapping = new ArrayList<>();
41      private DocstoreClientLocator docstoreClientLocator;
42      private List<OLEBatchProcessProfileConstantsBo> constantsMapping = new ArrayList<>();
43      String holdingsDataFieldNumber = null;
44      String itemDataFieldNumber = null;
45      private String userName;
46  
47      /**
48       * @return
49       */
50      public DocstoreClientLocator getDocstoreClientLocator() {
51          if (null == docstoreClientLocator) {
52              docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
53          }
54          return docstoreClientLocator;
55      }
56  
57      /**
58       * @param bibMarcRecordList
59       * @param profile
60       * @param batchBibImportStatistics
61       * @param user
62       */
63      public OLEBatchBibImportDataObjects processBatch(List<BibMarcRecord> bibMarcRecordList, OLEBatchProcessProfileBo profile, OLEBatchBibImportStatistics batchBibImportStatistics, String user) {
64  
65          OLEBatchBibImportDataObjects oleBatchBibImportDataObjects = new OLEBatchBibImportDataObjects();
66          bibImportStatistics = batchBibImportStatistics;
67          userName = user;
68  
69          MatchingProfile matchingProfile = profile.getMatchingProfileObj();
70  
71          for (BibMarcRecord bibRecord : bibMarcRecordList) {
72              processBibMarcRecord(profile, oleBatchBibImportDataObjects, matchingProfile, bibRecord);
73  
74          }
75  
76          return oleBatchBibImportDataObjects;
77      }
78  
79      private void processBibMarcRecord(OLEBatchProcessProfileBo profile, OLEBatchBibImportDataObjects oleBatchBibImportDataObjects, MatchingProfile matchingProfile, BibMarcRecord bibRecord) {
80  
81          BibTree bibTree = new BibTree();
82  
83          String leader = bibRecord.getLeader();
84          char unicode = leader.charAt(9);
85          if (unicode == 'a') {
86  
87              if (BatchBibImportUtil.has245aDataField(bibRecord)) {
88  
89                  dataMapping = processPriority(bibRecord, profile);
90                  constantsMapping = profile.getOleBatchProcessProfileConstantsList();
91                  // Getting Holdings and item Data Fields  for every Bib Record
92                  getHoldingsItemDataFields(bibRecord, dataMapping, holdingsDataFields, itemDataFields, eHoldingsDataFields);
93  
94  
95                  if (!matchingProfile.isMatchBibs()) {
96                      // Do not perform match
97                      bibTree = buildBibTree(bibRecord, profile);
98                      setOperationAddToBibTreeList(DocstoreDocument.OperationType.CREATE, DocstoreDocument.OperationType.CREATE, DocstoreDocument.OperationType.CREATE, bibTree);
99                  } else {
100                     try {
101                         // Finding Matching for Bib` from Bib match point
102                         Bib matchedBib = findMatchingBib(profile, bibRecord);
103                         if (matchedBib == null) {
104                             // No match  Found
105                             if (matchingProfile.isBibNotMatched_addBib()) {
106                                 // Add Bib Tree
107                                 bibTree = buildBibTree(bibRecord, profile);
108                                 setOperationAddToBibTreeList(DocstoreDocument.OperationType.CREATE, DocstoreDocument.OperationType.CREATE, DocstoreDocument.OperationType.CREATE, bibTree);
109                             } else if (matchingProfile.isBibNotMatched_discardBib()) {
110                                 // Discard bib if no match found. New bib is set to bib tree with no operation and proceed.
111                                 Bib bib = new Bib();
112                                 bib.setMessage(OLEConstants.OLEBatchProcess.NO_MATCH_DISCARD_BIB);
113                                 bibTree.setBib(bib);
114                             }
115                         } else {
116                             // Match found
117                             if (matchingProfile.isBibMatched_addBib()) {
118                                 // Add Bib
119                                 bibTree = buildBibTree(bibRecord, profile);
120                                 setOperationAddToBibTreeList(DocstoreDocument.OperationType.CREATE, DocstoreDocument.OperationType.CREATE, DocstoreDocument.OperationType.CREATE, bibTree);
121                             } else {
122                                 // Update Bib
123                                 if (matchingProfile.isBibMatched_updateBib()) {
124                                     boolean isBibOverlay = true;
125                                     // Overlay bib when status is not equal to profile status
126 
127                                     for (OLEBatchProcessProfileBibStatus oleBatchProcessProfileBibStatus : profile.getOleBatchProcessProfileBibStatusList()) {
128                                         if (oleBatchProcessProfileBibStatus.getBatchProcessBibStatus() != null && oleBatchProcessProfileBibStatus.getBatchProcessBibStatus().equals(matchedBib.getStatus())) {
129                                             isBibOverlay = false;
130                                             break;
131                                         }
132                                     }
133                                     if (isBibOverlay) {
134                                         overlayBib(bibRecord, matchedBib, profile);
135                                     }
136                                     bibTree.setBib(matchedBib);
137                                     processHoldingsAndItemsForBib(bibRecord, profile, bibTree, oleBatchBibImportDataObjects);
138                                     setOperationAddToBibTreeList(DocstoreDocument.OperationType.UPDATE, null, null, bibTree);
139                                 }
140 
141                                 // Discarding Bib if match found and processing  holdings and items
142                                 if (matchingProfile.isBibMatched_discardBib()) {
143                                     bibTree.setBib(matchedBib);
144                                     processHoldingsAndItemsForBib(bibRecord, profile, bibTree, oleBatchBibImportDataObjects);
145 
146                                 }
147                             }
148                         }
149                     } catch (DocstoreException e) {
150                         Bib bib = new Bib();
151                         bib.setMessage(e.getErrorMessage());
152                         bib.setResult(DocstoreDocument.ResultType.FAILURE);
153                         bibTree.setBib(bib);
154                         LOG.info(e.getErrorMessage());
155                     }
156 
157                 }
158             } else {
159                 Bib bib = new Bib();
160                 bib.setMessage("Bib Record with id " + bibRecord.getRecordId() + " does not have a 245a data field");
161                 bib.setResult(DocstoreDocument.ResultType.FAILURE);
162                 bibTree.setBib(bib);
163             }
164         } else {
165             Bib bib = new Bib();
166             bib.setMessage("Invalid Leader Field - " + leader);
167             bib.setResult(DocstoreDocument.ResultType.FAILURE);
168             bibTree.setBib(bib);
169             bibImportStatistics.getInvalidLeaderField().add("Invalid Leader Field - " + leader);
170         }
171 
172         oleBatchBibImportDataObjects.getBibTrees().getBibTrees().add(bibTree);
173 
174     }
175 
176     private void setOperationAddToBibTreeList(DocstoreDocument.OperationType bibOperation, DocstoreDocument.OperationType holdingOperation, DocstoreDocument.OperationType itemOperation, BibTree bibTree) {
177         bibTree.getBib().setOperation(bibOperation);
178         setHoldingsTreeOperations(bibTree.getHoldingsTrees(), holdingOperation, itemOperation);
179     }
180 
181 
182     private void setHoldingsTreeOperations(List<HoldingsTree> holdingsTrees, DocstoreDocument.OperationType holdingOperation, DocstoreDocument.OperationType itemOperation) {
183         for (HoldingsTree holdingsTree : holdingsTrees) {
184             Holdings holdings = holdingsTree.getHoldings();
185             if (holdingOperation != null && holdings != null) {
186                 holdings.setOperation(holdingOperation);
187                 setItemOperation(itemOperation, holdingsTree);
188             }
189 
190         }
191     }
192 
193 
194     private void setItemOperation(DocstoreDocument.OperationType itemOperation, HoldingsTree holdingsTree) {
195         if (holdingsTree.getHoldings().getHoldingsType().equalsIgnoreCase(PHoldings.PRINT)) {
196             for (Item item : holdingsTree.getItems()) {
197                 if (item != null && itemOperation != null) {
198                     item.setOperation(itemOperation);
199                 }
200             }
201         }
202     }
203 
204 
205     /**
206      * Process Holdings and Items for Bib
207      *
208      * @param bibRecord
209      * @param profile
210      * @param bibTree
211      */
212     private void processHoldingsAndItemsForBib(BibMarcRecord bibRecord, OLEBatchProcessProfileBo profile, BibTree bibTree, OLEBatchBibImportDataObjects oleBatchBibImportDataObjects) {
213 
214         // Getting hHoldings Tree from bibTree
215         List<HoldingsTree> holdingsTrees = bibTree.getHoldingsTrees();
216 
217         MatchingProfile matchingProfile = profile.getMatchingProfileObj();
218 
219         if (!matchingProfile.isMatchHoldings()) {
220             // Do not perform matching
221             if (matchingProfile.isNoMatchHoldings_deleteAddHoldingsItems()) {
222                 //Getting holdings Tree to  delete from bib
223                 HoldingsTrees holdingsTreesDelete = BatchBibImportUtil.getHoldingsTrees(bibTree.getBib().getId());
224                 // Setting  Holding Trees With Operation Delete
225                 for (HoldingsTree holdingsTree : holdingsTreesDelete.getHoldingsTrees()) {
226                     Holdings holdings = holdingsTree.getHoldings();
227                     holdings.setOperation(DocstoreDocument.OperationType.DELETE);
228                     holdingsTrees.add(holdingsTree);
229                 }
230                 // Building  Holdings  Trees  with Create Operation
231                 List<HoldingsTree> holdingsTreesCreate = buildHoldingsTreesForBib(bibRecord, profile, true);
232                 setHoldingsTreeOperations(holdingsTreesCreate, DocstoreDocument.OperationType.CREATE, DocstoreDocument.OperationType.CREATE);
233                 holdingsTrees.addAll(holdingsTreesCreate);
234             } else if (matchingProfile.isNoMatchHoldings_retainAddHoldingsItems()) {
235                 // Building  Holdings  Trees  with Create Operation
236                 List<HoldingsTree> holdingsTreesCreate = buildHoldingsTreesForBib(bibRecord, profile, true);
237                 setHoldingsTreeOperations(holdingsTreesCreate, DocstoreDocument.OperationType.CREATE, DocstoreDocument.OperationType.CREATE);
238                 holdingsTrees.addAll(holdingsTreesCreate);
239             }
240         } else {
241             // Building  Match Point List for PHoldings
242             List<OLEBatchProcessProfileMatchPoint> pHoldingsMatchPointList = BatchBibImportUtil.buildMatchPointListByDataType(profile.getOleBatchProcessProfileMatchPointList(), DocType.HOLDINGS.getCode());
243             String docType = DocType.HOLDINGS.getCode();
244 
245             // When input file contains only one Holdings
246             if (holdingsDataFields.size() == 1) {
247                 Holdings matchedHoldings = getMatchedHoldings(bibTree.getBib(), holdingsDataFields, docType);
248                 if (matchedHoldings != null) {
249                     // Matched Holdings found
250                     processMatchedPHoldings(bibRecord, profile, oleBatchBibImportDataObjects, holdingsTrees, matchingProfile, docType, matchedHoldings, holdingsDataFields.get(0));
251                 }
252             } else {
253                 // It there is match points and we Check both Map[ping and Constants
254                 if (pHoldingsMatchPointList != null && pHoldingsMatchPointList.size() > 0) {
255                     // Process pHoldings   to find matching record
256                     processMatchedPHoldings(bibRecord, profile, bibTree.getBib(), oleBatchBibImportDataObjects, holdingsTrees, matchingProfile, pHoldingsMatchPointList, docType);
257                 }
258 
259             }
260 
261 
262             // Building  Match Point List for EHoldings
263             List<OLEBatchProcessProfileMatchPoint> eHoldingsMatchPointList = BatchBibImportUtil.buildMatchPointListByDataType(profile.getOleBatchProcessProfileMatchPointList(), DocType.EHOLDINGS.getCode());
264 
265             // When input file contains only one EHoldings
266             if (eHoldingsDataFields.size() == 1) {
267                 docType = DocType.EHOLDINGS.getCode();
268                 Holdings matchedHoldings = getMatchedHoldings(bibTree.getBib(), eHoldingsDataFields, docType);
269                 if (matchedHoldings == null) {
270                     matchedHoldings = findMatchingForPHoldingsAndEholdings(bibTree.getBib().getId(), null, eHoldingsMatchPointList, docType);
271                     processMatchedEHoldings(bibRecord, profile, oleBatchBibImportDataObjects, holdingsTrees, matchingProfile, matchedHoldings, null);
272                 }
273             } else {
274                 if (eHoldingsMatchPointList != null && eHoldingsMatchPointList.size() > 0) {
275                     docType = DocType.EHOLDINGS.getCode();
276                     // Process EHoldings   to find matching record
277                     processMatchedEHoldings(bibRecord, profile, bibTree.getBib(), oleBatchBibImportDataObjects, holdingsTrees, matchingProfile, eHoldingsMatchPointList, docType);
278                 }
279             }
280 
281 
282         }
283 
284     }
285 
286 
287     /**
288      * @param bibRecord
289      * @param profile
290      * @param bib
291      * @param oleBatchBibImportDataObjects
292      * @param holdingsTrees
293      * @param matchingProfile
294      * @param holdingsMatchPointList
295      * @param docType
296      */
297     private void processMatchedPHoldings(BibMarcRecord bibRecord, OLEBatchProcessProfileBo profile, Bib bib, OLEBatchBibImportDataObjects oleBatchBibImportDataObjects, List<HoldingsTree> holdingsTrees, MatchingProfile matchingProfile, List<OLEBatchProcessProfileMatchPoint> holdingsMatchPointList, String docType) {
298         Holdings matchedHoldings = null;
299         for (DataField holdingsDataField : holdingsDataFields) {
300             try {
301                 if (matchedHoldings == null) {
302                     matchedHoldings = findMatchingForPHoldingsAndEholdings(bib.getId(), holdingsDataField, holdingsMatchPointList, docType);
303                 }
304                 processMatchedPHoldings(bibRecord, profile, oleBatchBibImportDataObjects, holdingsTrees, matchingProfile, docType, matchedHoldings, holdingsDataField);
305             } catch (DocstoreException e) {
306                 bibImportStatistics.getMoreThanOneHoldingsMatched().add(bibRecord);
307                 LOG.info(e.getErrorMessage());
308                 continue;
309             }
310         }
311 
312         if (holdingsDataFields.size() == 0) {
313             try {
314                 if (matchedHoldings == null) {
315                     matchedHoldings = findMatchingForPHoldingsAndEholdings(bib.getId(), null, holdingsMatchPointList, docType);
316                 }
317                 processMatchedPHoldings(bibRecord, profile, oleBatchBibImportDataObjects, holdingsTrees, matchingProfile, docType, matchedHoldings, null);
318             } catch (DocstoreException e) {
319                 bibImportStatistics.getMoreThanOneHoldingsMatched().add(bibRecord);
320                 LOG.info(e.getErrorMessage());
321             }
322         }
323     }
324 
325     private void processMatchedPHoldings(BibMarcRecord bibRecord, OLEBatchProcessProfileBo profile, OLEBatchBibImportDataObjects oleBatchBibImportDataObjects, List<HoldingsTree> holdingsTrees, MatchingProfile matchingProfile, String docType, Holdings matchedHoldings, DataField holdingsDataField) {
326 
327         List<HoldingsTree> holdingsTreesList = new ArrayList<>();
328         if (matchedHoldings == null) {
329 
330             if (matchingProfile.isHoldingsNotMatched_addHoldings()) {
331                 // Add Holdings
332                 if (matchingProfile.isHoldingsNotMatched_addItems()) {
333                     //add Items with holdings
334                     holdingsTreesList.addAll(buildPHoldingsTreesForBib(profile, true, holdingsDataField));
335                 } else {
336                     holdingsTreesList.addAll(buildPHoldingsTreesForBib(profile, false, holdingsDataField));
337                 }
338                 // Setting Operation type as create
339                 setHoldingsTreeOperations(holdingsTreesList, DocstoreDocument.OperationType.CREATE, DocstoreDocument.OperationType.CREATE);
340                 holdingsTrees.addAll(holdingsTreesList);
341             }
342 
343         } else {
344             HoldingsTree holdingsTree = new HoldingsTree();
345             if (matchingProfile.isHoldingsMatched_addHoldings()) {
346                 // Add Holdings
347 
348                 if (matchingProfile.isHoldingsMatched_addItems()) {
349                     //add Items with holdings
350                     holdingsTreesList.addAll(buildPHoldingsTreesForBib(profile, true, holdingsDataField));
351                 } else {
352                     holdingsTreesList.addAll(buildPHoldingsTreesForBib(profile, false, holdingsDataField));
353                 }
354                 // Setting Operation type as create
355                 setHoldingsTreeOperations(holdingsTreesList, DocstoreDocument.OperationType.CREATE, DocstoreDocument.OperationType.CREATE);
356                 holdingsTrees.addAll(holdingsTreesList);
357 
358             } else if (matchingProfile.isHoldingsMatched_updateHoldings()) {
359                 //Update Holdings
360                 overlayHoldings(holdingsDataField, matchedHoldings, profile);
361                 // Setting Operation
362                 matchedHoldings.setOperation(DocstoreDocument.OperationType.UPDATE);
363 
364                 holdingsTree.setHoldings(matchedHoldings);
365                 holdingsTrees.add(holdingsTree);
366 
367                 // Processing Items
368                 if (docType.equals(DocType.HOLDINGS.getCode())) {
369                     processItem(bibRecord, matchedHoldings.getId(), oleBatchBibImportDataObjects, profile, matchingProfile, matchedHoldings, holdingsTree);
370                 }
371                 // Discard Holdings and process Items
372             } else if (matchingProfile.isHoldingsMatched_discardHoldings()) {
373                 if (docType.equals(DocType.HOLDINGS.getCode())) {
374                     holdingsTree.setHoldings(matchedHoldings);
375                     holdingsTrees.add(holdingsTree);
376                     processItem(bibRecord, matchedHoldings.getId(), oleBatchBibImportDataObjects, profile, matchingProfile, matchedHoldings, holdingsTree);
377                 }
378             }
379 
380 
381         }
382 
383     }
384 
385 
386     private List<HoldingsTree> processMatchedEHoldings(BibMarcRecord bibRecord, OLEBatchProcessProfileBo profile, OLEBatchBibImportDataObjects oleBatchBibImportDataObjects, List<HoldingsTree> holdingsTrees, MatchingProfile matchingProfile, Holdings matchedHoldings, DataField eHoldingsDataField) {
387         if (matchedHoldings == null) {
388             if (matchingProfile.isHoldingsNotMatched_addHoldings()) {
389                 // Add Holdings
390                 holdingsTrees.addAll(buildEHoldingsTreesForBib(bibRecord, profile, false));
391                 // Setting Operation type as create
392                 setHoldingsTreeOperations(holdingsTrees, DocstoreDocument.OperationType.CREATE, DocstoreDocument.OperationType.CREATE);
393             }
394         } else {
395             if (matchingProfile.isHoldingsMatched_addHoldings()) {
396                 // Add Holdings
397                 holdingsTrees.addAll(buildEHoldingsTreesForBib(bibRecord, profile, false));
398                 // Setting Operation type as create
399                 setHoldingsTreeOperations(holdingsTrees, DocstoreDocument.OperationType.CREATE, DocstoreDocument.OperationType.CREATE);
400             } else if (matchingProfile.isHoldingsMatched_updateHoldings()) {
401                 //Update Holdings
402                 overlayHoldings(eHoldingsDataField, matchedHoldings, profile);
403                 // Setting Operation
404                 matchedHoldings.setOperation(DocstoreDocument.OperationType.UPDATE);
405                 HoldingsTree holdingsTree = new HoldingsTree();
406                 holdingsTree.setHoldings(matchedHoldings);
407                 holdingsTrees.add(holdingsTree);
408             }
409         }
410         return holdingsTrees;
411     }
412 
413 
414     /**
415      * @param bibRecord
416      * @param profile
417      * @param bib
418      * @param oleBatchBibImportDataObjects
419      * @param holdingsTrees
420      * @param matchingProfile
421      * @param holdingsMatchPointList
422      * @param docType
423      */
424     private void processMatchedEHoldings(BibMarcRecord bibRecord, OLEBatchProcessProfileBo profile, Bib bib, OLEBatchBibImportDataObjects oleBatchBibImportDataObjects, List<HoldingsTree> holdingsTrees, MatchingProfile matchingProfile, List<OLEBatchProcessProfileMatchPoint> holdingsMatchPointList, String docType) {
425         Holdings matchedHoldings = null;
426         try {
427             if (eHoldingsDataFields != null && eHoldingsDataFields.size() > 0) {
428                 if (matchedHoldings == null) {
429                     matchedHoldings = findMatchingForPHoldingsAndEholdings(bib.getId(), eHoldingsDataFields.get(0), holdingsMatchPointList, docType);
430                 }
431                 processMatchedEHoldings(bibRecord, profile, oleBatchBibImportDataObjects, holdingsTrees, matchingProfile, matchedHoldings, eHoldingsDataFields.get(0));
432             }
433         } catch (DocstoreException e) {
434             bibImportStatistics.getMoreThanOneHoldingsMatched().add(bibRecord);
435             LOG.info(e.getErrorMessage());
436         }
437     }
438 
439     /**
440      * @param bib
441      * @param holdingsDataFields
442      * @param docType
443      * @return
444      */
445     private Holdings getMatchedHoldings(Bib bib, List<DataField> holdingsDataFields, String docType) {
446         Holdings matchedHoldings = null;
447 
448 
449         boolean itemConstant = false;
450         boolean holdingsConstant = false;
451         boolean eHoldingsConstant = false;
452         // Checks in constants and defaults and if there is no data mapping making flag as true.
453         for (OLEBatchProcessProfileConstantsBo oLEBatchProcessProfileConstantsBo : constantsMapping) {
454             String docTypeConstant = oLEBatchProcessProfileConstantsBo.getDataType();
455             if (docTypeConstant.equalsIgnoreCase(DocType.ITEM.getCode())) {
456                 if (itemDataFields.size() == 0) {
457                     itemConstant = true;
458                 }
459             }
460 
461             if (docTypeConstant.equalsIgnoreCase(DocType.HOLDINGS.getCode())) {
462                 if (holdingsDataFields.size() == 0) {
463                     holdingsConstant = true;
464                 }
465             }
466 
467             if (docTypeConstant.equalsIgnoreCase(DocType.EHOLDINGS.getCode())) {
468                 if (holdingsDataFields.size() == 0) {
469                     eHoldingsConstant = true;
470                 }
471             }
472         }
473 
474         // If incoming file has single holdings
475         if (holdingsDataFields.size() == 1 || holdingsConstant || eHoldingsConstant) {
476             HoldingsTrees holdingsTrees = null;
477 
478             if (docType.equalsIgnoreCase(DocType.EHOLDINGS.getCode())) {
479                 holdingsTrees = BatchBibImportUtil.getHoldingsTrees(bib.getId());
480             } else {
481 
482                 if (itemDataFields.size() == 1 || itemConstant) {
483                     holdingsTrees = BatchBibImportUtil.getHoldingsTrees(bib.getId());
484                 }
485             }
486             if (holdingsTrees != null && holdingsTrees.getHoldingsTrees().size() == 1) {
487                 try {
488                     List<String> itemIds = BatchBibImportUtil.getItemIds(holdingsTrees.getHoldingsTrees().get(0).getHoldings().getId());
489                     if (itemIds != null && itemIds.size() == 1) {
490                         matchedHoldings = getDocstoreClientLocator().getDocstoreClient().retrieveHoldings(holdingsTrees.getHoldingsTrees().get(0).getHoldings().getId());
491                     }
492 
493                 } catch (Exception e) {
494                     LOG.error("Error :", e);
495                 }
496             }
497         }
498         return matchedHoldings;
499     }
500 
501 
502     /**
503      * @param bibRecord
504      * @param profile
505      * @param withItem
506      * @return
507      */
508     private List<HoldingsTree> buildHoldingsTreesForBib(BibMarcRecord bibRecord, OLEBatchProcessProfileBo profile, boolean withItem) {
509         List<HoldingsTree> holdingsTreeList = new ArrayList<>();
510         holdingsTreeList.addAll(buildPHoldingsTreesForBib(profile, withItem, null));
511         holdingsTreeList.addAll(buildEHoldingsTreesForBib(bibRecord, profile, withItem));
512         return holdingsTreeList;
513 
514     }
515 
516 
517     /**
518      * @param holdingsTreeList
519      * @param OleBatchProcessProfileDataMappingOptionsBoList
520      */
521     private void applyDataMappingForPHoldings(List<HoldingsTree> holdingsTreeList, List<OLEBatchProcessProfileDataMappingOptionsBo> OleBatchProcessProfileDataMappingOptionsBoList) {
522         String docType = null;
523         String docField = null;
524         String dataField = null;
525 
526         String bibFieldValue = null;
527         for (OLEBatchProcessProfileDataMappingOptionsBo oleBatchProcessProfileDataMappingOptionsBo : OleBatchProcessProfileDataMappingOptionsBoList) {
528             docType = oleBatchProcessProfileDataMappingOptionsBo.getDataTypeDestinationField();
529             docField = oleBatchProcessProfileDataMappingOptionsBo.getDestinationField();
530             dataField = oleBatchProcessProfileDataMappingOptionsBo.getSourceField().substring(0, 3);
531 
532             bibFieldValue = oleBatchProcessProfileDataMappingOptionsBo.getSourceFieldValue();
533             if (docType.equalsIgnoreCase(DocType.HOLDINGS.getCode())) {
534                 for (HoldingsTree holdingsTree : holdingsTreeList) {
535                     PHoldings pHoldings = (PHoldings) BatchBibImportUtil.getHoldings(holdingsTree, DocType.HOLDINGS.getCode());
536 
537                     if (dataField.equalsIgnoreCase(holdingsDataFieldNumber))
538                         continue;
539 
540                     if (pHoldings != null) {
541                         pHoldings.setField(docField, bibFieldValue);
542                     }
543                 }
544             } else if (docType.equalsIgnoreCase(DocType.ITEM.getCode()))
545                 for (HoldingsTree holdingsTree : holdingsTreeList) {
546                     List<Item> items = BatchBibImportUtil.getItem(holdingsTreeList);
547 
548                     if (dataField.equalsIgnoreCase(itemDataFieldNumber))
549                         continue;
550                     for (Item item : items) {
551                         if (item != null) {
552                             item.setField(docField, bibFieldValue);
553                         }
554                     }
555 
556                 }
557         }
558     }
559 
560 
561     /**
562      * @param profile
563      * @param withItem
564      * @return
565      */
566     private List<HoldingsTree> buildPHoldingsTreesForBib(OLEBatchProcessProfileBo profile, boolean withItem, DataField dataField) {
567         List<HoldingsTree> holdingsTreeList = new ArrayList<>();
568 
569         if ((profile.getDataToImport().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DATA_TO_IMPORT_BIB_INSTANCE))
570                 || (profile.getDataToImport().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DATA_TO_IMPORT_BIB_INSTANCE_EINSTANCE))) {
571             if (dataField == null) {
572                 buildPHoldings(holdingsTreeList, profile, withItem);
573             } else {
574                 buildPHoldings(holdingsTreeList, profile, withItem, dataField);
575             }
576 
577         }
578         applyDataMappingForPHoldings(holdingsTreeList, dataMapping);
579         applyDefaultsAndConstants(profile, holdingsTreeList);
580         return holdingsTreeList;
581 
582     }
583 
584     /**
585      * @param bibRecord
586      * @param profile
587      * @param withItem
588      * @return
589      */
590     private List<HoldingsTree> buildEHoldingsTreesForBib(BibMarcRecord bibRecord, OLEBatchProcessProfileBo profile, boolean withItem) {
591         List<HoldingsTree> holdingsTreeList = new ArrayList<>();
592         if ((profile.getDataToImport().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DATA_TO_IMPORT_BIB_EINSTANCE))
593                 || (profile.getDataToImport().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DATA_TO_IMPORT_BIB_INSTANCE_EINSTANCE))) {
594             buildEHoldings(bibRecord, dataMapping, holdingsTreeList, profile);
595             applyDataMapping(holdingsTreeList, dataMapping);
596             applyDefaultsAndConstants(profile, holdingsTreeList);
597         }
598         return holdingsTreeList;
599     }
600 
601     /**
602      * @param bibRecord
603      * @param holdingId
604      * @param profile
605      * @param matchingProfile
606      * @param matchedHoldings
607      * @param holdingsTree
608      */
609     private void processItem(BibMarcRecord bibRecord, String holdingId, OLEBatchBibImportDataObjects oleBatchBibImportDataObjects, OLEBatchProcessProfileBo profile, MatchingProfile matchingProfile, Holdings matchedHoldings, HoldingsTree holdingsTree) {
610 
611         if (!matchingProfile.isMatchItems()) {
612             List<Item> items = new ArrayList<>();
613 
614 
615             if (matchingProfile.isNoMatchItem_deleteAddItems()) {
616                 List<String> itemIds = BatchBibImportUtil.getItemIds(holdingId);
617 
618                 for (String itemId : itemIds) {
619                     Item item = new Item();
620                     item.setOperation(DocstoreDocument.OperationType.DELETE);
621                     item.setId((itemId));
622                     items.add(item);
623                 }
624 
625                 items.addAll(buildItems(profile, matchedHoldings));
626 
627             } else if (matchingProfile.isNoMatchItem_retainAddItems()) {
628                 items.addAll(buildItems(profile, matchedHoldings));
629             }
630 
631             holdingsTree.getItems().addAll(items);
632         } else {
633             Item matchedItem = null;
634             if (itemDataFields.size() == 0) {
635                 // If no data mapping, check in constants
636                 matchedItem = findMatchingItem(profile, holdingId, bibRecord, null);
637                 performMatchedItem(profile, matchingProfile, matchedHoldings, matchedItem, null, holdingsTree);
638             } else {
639                 for (DataField dataField : itemDataFields) {
640                     try {
641                         matchedItem = findMatchingItem(profile, holdingId, bibRecord, dataField);
642                         performMatchedItem(profile, matchingProfile, matchedHoldings, matchedItem, dataField, holdingsTree);
643                     } catch (DocstoreException e) {
644                         LOG.info(e.getErrorMessage());
645                     }
646                 }
647 
648             }
649 
650         }
651     }
652 
653     private void performMatchedItem(OLEBatchProcessProfileBo profile, MatchingProfile matchingProfile, Holdings matchedHoldings, Item matchedItem, DataField dataField, HoldingsTree holdingsTree) {
654         List<Item> items = new ArrayList<>();
655         if (matchedItem == null) {
656             if (matchingProfile.isItemNotMatched_addItem()) {
657                 items.addAll(buildItems(profile, matchedHoldings));
658             }
659         } else {
660             if (matchingProfile.isItemMatched_addItem()) {
661                 items.addAll(buildItems(profile, matchedHoldings));
662             } else if (matchingProfile.isItemMatched_updateItem()) {
663                 overlayItem(matchedItem, profile, dataField);
664                 matchedItem.setOperation(DocstoreDocument.OperationType.UPDATE);
665                 items.add(matchedItem);
666             }
667         }
668         holdingsTree.getItems().addAll(items);
669     }
670 
671     /**
672      * Build  Bib Tree by Using Bib record and Mapping from profile
673      *
674      * @param bibRecord
675      * @param profile
676      * @return
677      */
678     private BibTree buildBibTree(BibMarcRecord bibRecord, OLEBatchProcessProfileBo profile) {
679         BibTree bibTree = new BibTree();
680         if ((profile.getDataToImport().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DATA_TO_IMPORT_BIB_INSTANCE))
681                 || (profile.getDataToImport().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DATA_TO_IMPORT_BIB_EINSTANCE))
682                 || (profile.getDataToImport().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DATA_TO_IMPORT_BIB_INSTANCE_EINSTANCE))) {
683             // Build bib tree with  Holdings and Items
684             List<HoldingsTree> holdingsTrees = buildHoldingsTreesForBib(bibRecord, profile, true);
685             bibTree.getHoldingsTrees().addAll(holdingsTrees);
686         }
687 
688         Bib bib = buildBib(bibRecord, profile);
689         bibTree.setBib(bib);
690         return bibTree;
691     }
692 
693     /**
694      * Processing New Bib
695      *
696      * @param bibRecord
697      * @param profile
698      * @return
699      */
700     private Bib buildBib(BibMarcRecord bibRecord, OLEBatchProcessProfileBo profile) {
701         Bib requestBib = null;
702         batchProcessBibImportService.deleteFields(bibRecord, profile);
703         batchProcessBibImportService.renameFields(bibRecord, profile);
704         batchProcessBibImportService.setDefaultOrConstants(bibRecord, profile);
705         batchProcessBibImportService.process001(bibRecord, profile);
706 
707         String uuid = batchProcessBibImportService.getUuid(bibRecord);
708         requestBib = batchProcessBibImportService.buildBibRequest(bibRecord, uuid);
709         batchProcessBibImportService.setBibStatus(requestBib, profile, uuid);
710         if (StringUtils.isEmpty(uuid)) {
711             requestBib.setStaffOnly(profile.isBibStaffOnly());
712         }
713         requestBib.setCreatedBy(userName);
714         return requestBib;
715     }
716 
717     /**
718      * @param holdingsTreeList
719      * @param profile
720      * @param withItem
721      */
722     private void buildPHoldings(List<HoldingsTree> holdingsTreeList, OLEBatchProcessProfileBo profile, boolean withItem) {
723 
724         for (DataField dataField : holdingsDataFields) {
725             buildHoldingsFromDataField(holdingsTreeList, profile, dataField);
726         }
727 
728         if (holdingsDataFields.size() == 0) {
729             buildHoldingsFromDataField(holdingsTreeList, profile, null);
730         }
731         //Build Items for Holdings
732         if (withItem) {
733             buildItemForHoldings(dataMapping, holdingsTreeList, profile);
734         }
735     }
736 
737 
738     /**
739      * @param holdingsTreeList
740      * @param profile
741      * @param withItem
742      * @param dataField
743      */
744     private void buildPHoldings(List<HoldingsTree> holdingsTreeList, OLEBatchProcessProfileBo profile, boolean withItem, DataField dataField) {
745 
746         buildHoldingsFromDataField(holdingsTreeList, profile, dataField);
747         //Build Items for Holdings
748         if (withItem) {
749             buildItemForHoldings(dataMapping, holdingsTreeList, profile);
750         }
751     }
752 
753     /**
754      * @param holdings
755      * @param profile
756      * @param dataField
757      */
758 
759     private void buildHoldingsFromDataField(Holdings holdings, OLEBatchProcessProfileBo profile, DataField dataField) {
760         String docField;
761         String bibField;
762 
763         if (holdings != null && dataField != null) {
764             holdings.setStaffOnly(profile.isInstanceStaffOnly());
765             for (OLEBatchProcessProfileDataMappingOptionsBo oleBatchProcessProfileDataMappingOptionsBo : dataMapping) {
766                 docField = oleBatchProcessProfileDataMappingOptionsBo.getDestinationField();
767                 bibField = oleBatchProcessProfileDataMappingOptionsBo.getSourceField();
768                 String dataFieldValue = BatchBibImportUtil.getSubFieldValue(bibField, dataField);
769                 if (StringUtils.isNotEmpty(dataFieldValue)) {
770                     holdings.setField(docField, dataFieldValue);
771                 }
772             }
773         }
774 
775     }
776 
777     /**
778      * @param holdingsTreeList
779      * @param profile
780      * @param dataField
781      */
782 
783     private void buildHoldingsFromDataField(List<HoldingsTree> holdingsTreeList, OLEBatchProcessProfileBo profile, DataField dataField) {
784         HoldingsTree holdingTree = BatchBibImportUtil.buildHoldingsTree(DocType.HOLDINGS.getCode());
785         Holdings holdings = holdingTree.getHoldings();
786         buildHoldingsFromDataField(holdings, profile, dataField);
787         holdingsTreeList.add(holdingTree);
788     }
789 
790     /**
791      * @param bibRecord
792      * @param oleBatchProcessProfileBo
793      */
794     public List<OLEBatchProcessProfileDataMappingOptionsBo> processPriority(BibMarcRecord bibRecord, OLEBatchProcessProfileBo oleBatchProcessProfileBo) {
795         String docField = null;
796         String bibField = null;
797         String bibFieldValue = null;
798         List<OLEBatchProcessProfileDataMappingOptionsBo> oleBatchProcessProfileMappingOptionsBos = new ArrayList<>();
799         HashMap<String, OLEBatchProcessProfileDataMappingOptionsBo> oleBatchProcessProfileMappingOptionsBoMap = new HashMap<>();
800         List<OLEBatchProcessProfileMappingOptionsBo> oleBatchProcessProfileMappingOptionsList = oleBatchProcessProfileBo.getOleBatchProcessProfileMappingOptionsList();
801         for (OLEBatchProcessProfileMappingOptionsBo oleBatchProcessProfileMappingOptionsBo : oleBatchProcessProfileMappingOptionsList) {
802             List<OLEBatchProcessProfileDataMappingOptionsBo> oleBatchProcessProfileDataMappingOptionsBoList = oleBatchProcessProfileMappingOptionsBo.getOleBatchProcessProfileDataMappingOptionsBoList();
803             Collections.sort(oleBatchProcessProfileDataMappingOptionsBoList);
804             for (OLEBatchProcessProfileDataMappingOptionsBo oleBatchProcessProfileDataMappingOptionsBo : oleBatchProcessProfileDataMappingOptionsBoList) {
805                 docField = oleBatchProcessProfileDataMappingOptionsBo.getDestinationField();
806                 bibField = oleBatchProcessProfileDataMappingOptionsBo.getSourceField();
807                 String key = oleBatchProcessProfileDataMappingOptionsBo.getDataTypeDestinationField() + docField;
808                 if (!oleBatchProcessProfileMappingOptionsBoMap.containsKey(key)) {
809                     bibFieldValue = BatchBibImportUtil.getBibDataFieldValue(bibRecord, bibField);
810                     if (!StringUtils.isEmpty(bibFieldValue)) {
811                         oleBatchProcessProfileDataMappingOptionsBo.setSourceFieldValue(bibFieldValue);
812                         oleBatchProcessProfileMappingOptionsBoMap.put(key, oleBatchProcessProfileDataMappingOptionsBo);
813                     }
814                 }
815             }
816         }
817         for (Map.Entry<String, OLEBatchProcessProfileDataMappingOptionsBo> entry : oleBatchProcessProfileMappingOptionsBoMap.entrySet()) {
818             oleBatchProcessProfileMappingOptionsBos.add(entry.getValue());
819         }
820         return oleBatchProcessProfileMappingOptionsBos;
821     }
822 
823     /**
824      * Getting  Holdings and Item Data fields
825      *
826      * @param bibRecord
827      * @param oleBatchProcessProfileDataMappingOptionsBos
828      * @param holdingsDataFields
829      * @param itemDataFields
830      * @param eHoldingsDataFields
831      */
832     private void getHoldingsItemDataFields(BibMarcRecord bibRecord, List<OLEBatchProcessProfileDataMappingOptionsBo> oleBatchProcessProfileDataMappingOptionsBos, List<DataField> holdingsDataFields, List<DataField> itemDataFields, List<DataField> eHoldingsDataFields) {
833         holdingsDataFieldNumber = null;
834         itemDataFieldNumber = null;
835         String eHoldingsDataField = null;
836         holdingsDataFields.clear();
837         itemDataFields.clear();
838         eHoldingsDataFields.clear();
839 
840         Map<String, String> itemHoldingsMapping = new TreeMap<>();
841         for (OLEBatchProcessProfileDataMappingOptionsBo oleBatchProcessProfileDataMappingOptionsBo : oleBatchProcessProfileDataMappingOptionsBos) {
842             if (holdingsDataFieldNumber == null && oleBatchProcessProfileDataMappingOptionsBo.getDataTypeDestinationField().equalsIgnoreCase(DocType.HOLDINGS.getCode())) {
843                 holdingsDataFieldNumber = oleBatchProcessProfileDataMappingOptionsBo.getSourceField().substring(0, 3);
844             }
845 
846             if (itemDataFieldNumber == null && oleBatchProcessProfileDataMappingOptionsBo.getDataTypeDestinationField().equalsIgnoreCase(DocType.ITEM.getCode())) {
847                 itemDataFieldNumber = oleBatchProcessProfileDataMappingOptionsBo.getSourceField().substring(0, 3);
848             }
849 
850             if (eHoldingsDataField == null && oleBatchProcessProfileDataMappingOptionsBo.getDataTypeDestinationField().equalsIgnoreCase(DocType.EHOLDINGS.getCode())
851                     && oleBatchProcessProfileDataMappingOptionsBo.getDestinationField().equalsIgnoreCase(EHoldings.DESTINATION_FIELD_LINK_URL)) {
852                 eHoldingsDataField = oleBatchProcessProfileDataMappingOptionsBo.getSourceField().substring(0, 3);
853             }
854 
855             if (oleBatchProcessProfileDataMappingOptionsBo.getDataTypeDestinationField().equalsIgnoreCase(DocType.ITEM.getCode())) {
856                 itemHoldingsMapping.put(oleBatchProcessProfileDataMappingOptionsBo.getDestinationField(), oleBatchProcessProfileDataMappingOptionsBo.getDestinationField());
857             }
858         }
859         for (DataField dataField : bibRecord.getDataFields()) {
860 
861             if (dataField.getTag().equalsIgnoreCase(holdingsDataFieldNumber)) {
862                 holdingsDataFields.add(dataField);
863             }
864 
865             if (dataField.getTag().equalsIgnoreCase(itemDataFieldNumber)) {
866                 itemDataFields.add(dataField);
867             }
868 
869 
870             if (dataField.getTag().equalsIgnoreCase(eHoldingsDataField)) {
871                 eHoldingsDataFields.add(dataField);
872             }
873         }
874 
875 
876         // This is to check  if Item does not contain Holding mapping details .To create Single Holdings and Item.
877         if (itemHoldingsMapping.size() > 0 && !BatchBibImportUtil.isItemHoldingMapping(itemHoldingsMapping)) {
878             if (holdingsDataFields.size() > 0) {
879                 DataField dataField = holdingsDataFields.get(0);
880                 holdingsDataFields.clear();
881                 holdingsDataFields.add(dataField);
882             }
883 
884             if (itemDataFields.size() > 0) {
885                 DataField dataField = itemDataFields.get(0);
886                 itemDataFields.clear();
887                 itemDataFields.add(dataField);
888             }
889 
890         }
891     }
892 
893     /**
894      * Build Items for Holdings
895      *
896      * @param oleBatchProcessProfileDataMappingOptionsBos
897      * @param holdingsTreeList
898      * @param profile
899      */
900     private void buildItemForHoldings(List<OLEBatchProcessProfileDataMappingOptionsBo> oleBatchProcessProfileDataMappingOptionsBos, List<HoldingsTree> holdingsTreeList, OLEBatchProcessProfileBo profile) {
901         boolean isItemDefaultorConstants = false;
902 
903         if (itemDataFields.size() == 0) {
904             String docType = null;
905             List<OLEBatchProcessProfileConstantsBo> constantsMapping = profile.getOleBatchProcessProfileConstantsList();
906             for (OLEBatchProcessProfileConstantsBo oLEBatchProcessProfileConstantsBo : constantsMapping) {
907                 docType = oLEBatchProcessProfileConstantsBo.getDataType();
908 
909                 if (!docType.equalsIgnoreCase((DocType.ITEM.getCode())))
910                     continue;
911 
912                 if (docType.equalsIgnoreCase(DocType.ITEM.getCode())) {
913                     isItemDefaultorConstants = true;
914                     break;
915                 }
916             }
917         }
918 
919         for (HoldingsTree holdingsTree : holdingsTreeList) {
920             //Comparing Holdings Data Value and Item Data value. If Matched Create Item
921             String docField = null;
922             String bibField = null;
923             String dataFieldValue = null;
924             Map<String, String> itemDocFields = new TreeMap<>();
925 
926             Holdings holdings = holdingsTree.getHoldings();
927             BatchBibImportUtil.buildLocationLevels(holdings);
928 
929             for (DataField dataField : itemDataFields) {
930                 Item item = new Item();
931                 item.setField(Item.DESTINATION_FIELD_ITEM_ITEM_BARCODE, "");
932                 dataFieldValue = null;
933                 for (OLEBatchProcessProfileDataMappingOptionsBo oleBatchProcessProfileDataMappingOptionsBo : oleBatchProcessProfileDataMappingOptionsBos) {
934                     if (oleBatchProcessProfileDataMappingOptionsBo.getDataTypeDestinationField().equalsIgnoreCase(DocType.ITEM.getCode())) {
935                         docField = oleBatchProcessProfileDataMappingOptionsBo.getDestinationField();
936                         bibField = oleBatchProcessProfileDataMappingOptionsBo.getSourceField();
937                         dataFieldValue = BatchBibImportUtil.getSubFieldValue(bibField, dataField);
938                         if (StringUtils.isNotEmpty(dataFieldValue)) {
939                             item.setField(docField, dataFieldValue);
940                         }
941                         itemDocFields.put(docField, docField);
942                     }
943                 }
944                 if (item.getItemHoldingsDataMappingValue(itemDocFields).equalsIgnoreCase(holdings.getHoldingsDataMappingValue(itemDocFields))) {
945                     setItem(profile, holdingsTree, item);
946                 }
947             }
948             if (isItemDefaultorConstants) {
949                 Item item = new Item();
950                 item.setField(Item.DESTINATION_FIELD_ITEM_ITEM_BARCODE, "");
951                 setItem(profile, holdingsTree, item);
952             }
953         }
954 
955     }
956 
957 
958     /**
959      * @param profile
960      * @param holdingsTree
961      * @param item
962      */
963     private void setItem(OLEBatchProcessProfileBo profile, HoldingsTree holdingsTree, Item item) {
964         item.setStaffOnly(profile.isItemStaffOnly());
965         holdingsTree.getItems().add(item);
966         BatchBibImportUtil.buildLocationLevels(item);
967         applyDefaultConstantsandSerilazeContent(item, profile);
968     }
969 
970     /**
971      * @param holdingsTreeList
972      * @param oleBatchProcessProfileConstantsBoList
973      * @param defaultOrConstant
974      */
975     private void applyDefaultsAndConstants(List<HoldingsTree> holdingsTreeList, List<OLEBatchProcessProfileConstantsBo> oleBatchProcessProfileConstantsBoList, String defaultOrConstant) {
976         for (OLEBatchProcessProfileConstantsBo oLEBatchProcessProfileConstantsBo : oleBatchProcessProfileConstantsBoList) {
977             String defaultValue = oLEBatchProcessProfileConstantsBo.getDefaultValue();
978 
979             if (!defaultValue.equalsIgnoreCase(defaultOrConstant))
980                 continue;
981 
982             String docType = oLEBatchProcessProfileConstantsBo.getDataType();
983             String docField = oLEBatchProcessProfileConstantsBo.getAttributeName();
984             String fieldValue = oLEBatchProcessProfileConstantsBo.getAttributeValue();
985 
986 
987             if (docType.equalsIgnoreCase(DocType.HOLDINGS.getCode())) {
988                 for (HoldingsTree holdingsTree : holdingsTreeList) {
989                     PHoldings pHoldings = (PHoldings) BatchBibImportUtil.getHoldings(holdingsTree, DocType.HOLDINGS.getCode());
990                     if (pHoldings != null) {
991                         if (defaultValue.equalsIgnoreCase(OLEConstants.OLEBatchProcess.CONSTANT)) {
992                             pHoldings.setField(docField, fieldValue);
993                         } else {
994                             pHoldings.setDefaultField(docField, fieldValue);
995                         }
996 
997                     }
998                 }
999 
1000             } else if (docType.equalsIgnoreCase(DocType.EHOLDINGS.getCode())) {
1001                 for (HoldingsTree holdingsTree : holdingsTreeList) {
1002                     EHoldings eHoldings = (EHoldings) BatchBibImportUtil.getHoldings(holdingsTree, DocType.EHOLDINGS.getCode());
1003 
1004                     if (EHoldings.DESTINATION_FIELD_LINK_URL.equalsIgnoreCase(docField) && defaultOrConstant.equalsIgnoreCase(OLEConstants.OLEBatchProcess.DEFAULT))
1005                         continue;
1006 
1007                     if (EHoldings.DESTINATION_FIELD_LINK_TEXT.equalsIgnoreCase(docField) && defaultOrConstant.equalsIgnoreCase(OLEConstants.OLEBatchProcess.DEFAULT))
1008                         continue;
1009 
1010 
1011                     if (eHoldings != null) {
1012                         if (defaultValue.equalsIgnoreCase(OLEConstants.OLEBatchProcess.CONSTANT)) {
1013                             eHoldings.setField(docField, fieldValue);
1014                         } else {
1015                             eHoldings.setDefaultField(docField, fieldValue);
1016                         }
1017 
1018                     }
1019                 }
1020 
1021             }
1022         }
1023     }
1024 
1025 
1026     /**
1027      * @param holdingsTreeList
1028      */
1029     public void prepareDocContent(List<HoldingsTree> holdingsTreeList) {
1030         List<Integer> holdingsCount = new ArrayList<>();
1031         Integer count = 0;
1032         for (HoldingsTree holdingsTree : holdingsTreeList) {
1033             holdingsTree.getHoldings().serializeContent();
1034             if (holdingsTree.getHoldings().getCreatedBy() == null) {
1035                 holdingsTree.getHoldings().setCreatedBy(userName);
1036             } else {
1037                 holdingsTree.getHoldings().setUpdatedBy(userName);
1038             }
1039 
1040             for (org.kuali.ole.docstore.common.document.Item item : holdingsTree.getItems()) {
1041 
1042                 if (item.getCreatedBy() == null) {
1043                     item.setCreatedBy(userName);
1044                 } else {
1045                     item.setUpdatedBy(userName);
1046                 }
1047                 // To Do set Item Content
1048                 item.serializeContent();
1049             }
1050             if (holdingsTree.getHoldings().getContent() == null) {
1051                 holdingsCount.add(count);
1052             }
1053             count++;
1054         }
1055         Collections.reverse(holdingsCount);
1056         for (Integer emptyHoldings : holdingsCount) {
1057             holdingsTreeList.remove(holdingsTreeList.get(emptyHoldings));
1058         }
1059 
1060     }
1061 
1062     /**
1063      * @param bibRecord
1064      * @param oleBatchProcessProfileDataMappingOptionsBos
1065      * @param holdingsTreeList
1066      * @param profile
1067      */
1068     private void buildEHoldings(BibMarcRecord bibRecord, List<OLEBatchProcessProfileDataMappingOptionsBo> oleBatchProcessProfileDataMappingOptionsBos, List<HoldingsTree> holdingsTreeList, OLEBatchProcessProfileBo profile) {
1069         String docField = null;
1070         String bibField = null;
1071         List<String> dataFields = new ArrayList<>();
1072         List<String> dataFieldsLinkText = new ArrayList<>();
1073         for (OLEBatchProcessProfileDataMappingOptionsBo oleBatchProcessProfileDataMappingOptionsBo : oleBatchProcessProfileDataMappingOptionsBos) {
1074 
1075             docField = oleBatchProcessProfileDataMappingOptionsBo.getDestinationField();
1076             if (!EHoldings.DESTINATION_FIELD_LINK_URL.equalsIgnoreCase(docField))
1077                 continue;
1078 
1079             bibField = oleBatchProcessProfileDataMappingOptionsBo.getSourceField();
1080             dataFields = BatchBibImportUtil.getMatchedDataField(bibRecord, bibField);
1081             dataFieldsLinkText = buildLinkText(bibRecord, oleBatchProcessProfileDataMappingOptionsBos);
1082 
1083             int count = 0;
1084 
1085             for (String dataField : dataFields) {
1086                 HoldingsTree eHoldingTree = BatchBibImportUtil.buildHoldingsTree(DocType.EHOLDINGS.getCode());
1087                 EHoldings eHoldings = (EHoldings) eHoldingTree.getHoldings();
1088                 bibImportStatistics.getTotalRecordsCreated().add(eHoldings);
1089                 if (eHoldings != null) {
1090                     eHoldingTree.getHoldings().setStaffOnly(profile.isInstanceStaffOnly());
1091                     eHoldings.setField(docField, dataField);
1092                     if (dataFieldsLinkText.size() > count) {
1093                         eHoldings.setField(EHoldings.DESTINATION_FIELD_LINK_TEXT, dataFieldsLinkText.get(count));
1094                     }
1095                 }
1096                 count++;
1097                 holdingsTreeList.add(eHoldingTree);
1098             }
1099             if (dataFields.size() > 1) {
1100                 bibImportStatistics.getRecordsCreatedWithMoreThanOneLink().add(bibRecord);
1101             }
1102         }
1103 
1104     }
1105 
1106     /**
1107      * @param holdingsTreeList
1108      * @param OleBatchProcessProfileDataMappingOptionsBoList
1109      */
1110     private void applyDataMapping(List<HoldingsTree> holdingsTreeList, List<OLEBatchProcessProfileDataMappingOptionsBo> OleBatchProcessProfileDataMappingOptionsBoList) {
1111         String docType = null;
1112         String docField = null;
1113 
1114         String bibFieldValue = null;
1115         for (OLEBatchProcessProfileDataMappingOptionsBo oleBatchProcessProfileDataMappingOptionsBo : OleBatchProcessProfileDataMappingOptionsBoList) {
1116             docType = oleBatchProcessProfileDataMappingOptionsBo.getDataTypeDestinationField();
1117             docField = oleBatchProcessProfileDataMappingOptionsBo.getDestinationField();
1118 
1119             bibFieldValue = oleBatchProcessProfileDataMappingOptionsBo.getSourceFieldValue();
1120             if (docType.equalsIgnoreCase(DocType.EHOLDINGS.getCode())) {
1121                 for (HoldingsTree holdingsTree : holdingsTreeList) {
1122                     EHoldings eHoldings = (EHoldings) BatchBibImportUtil.getHoldings(holdingsTree, DocType.EHOLDINGS.getCode());
1123 
1124                     if (EHoldings.DESTINATION_FIELD_LINK_URL.equalsIgnoreCase(docField))
1125                         continue;
1126 
1127                     if (EHoldings.DESTINATION_FIELD_LINK_TEXT.equalsIgnoreCase(docField))
1128                         continue;
1129 
1130                     if (eHoldings != null) {
1131                         eHoldings.setField(docField, bibFieldValue);
1132                     }
1133                 }
1134             }
1135         }
1136     }
1137 
1138     /**
1139      * Apply data mapping for eHoldings records for overlay
1140      *
1141      * @param holdingsTreeList
1142      * @param oleBatchProcessProfileDataMappingOptionsBoList
1143      */
1144     private void applyDataMappingOverlay(List<HoldingsTree> holdingsTreeList, List<OLEBatchProcessProfileDataMappingOptionsBo> oleBatchProcessProfileDataMappingOptionsBoList) {
1145         String docType = null;
1146         String docField = null;
1147         String bibFieldValue = null;
1148 
1149         if (!oleBatchProcessProfileDataMappingOptionsBoList.isEmpty()) {
1150             for (OLEBatchProcessProfileDataMappingOptionsBo oleBatchProcessProfileDataMappingOptionsBo : oleBatchProcessProfileDataMappingOptionsBoList) {
1151                 docType = oleBatchProcessProfileDataMappingOptionsBo.getDataTypeDestinationField();
1152                 docField = oleBatchProcessProfileDataMappingOptionsBo.getDestinationField();
1153                 bibFieldValue = oleBatchProcessProfileDataMappingOptionsBo.getSourceFieldValue();
1154                 if (DocType.EHOLDINGS.getCode().equalsIgnoreCase(docType)) {
1155                     if (!holdingsTreeList.isEmpty()) {
1156                         for (HoldingsTree holdingsTree : holdingsTreeList) {
1157                             EHoldings eHoldings = (EHoldings) BatchBibImportUtil.getHoldings(holdingsTree, DocType.EHOLDINGS.getCode());
1158                             if (eHoldings != null) {
1159                                 eHoldings.setField(docField, bibFieldValue);
1160                             }
1161                         }
1162                     }
1163                 }
1164             }
1165         }
1166     }
1167 
1168 
1169     /**
1170      * @param bibRecord
1171      * @param oleBatchProcessProfileDataMappingOptionsBos
1172      * @return
1173      */
1174     private List<String> buildLinkText(BibMarcRecord bibRecord, List<OLEBatchProcessProfileDataMappingOptionsBo> oleBatchProcessProfileDataMappingOptionsBos) {
1175         String docField = null;
1176         String bibField = null;
1177         List<String> dataFields = new ArrayList<>();
1178         for (OLEBatchProcessProfileDataMappingOptionsBo oleBatchProcessProfileDataMappingOptionsBo : oleBatchProcessProfileDataMappingOptionsBos) {
1179             bibField = oleBatchProcessProfileDataMappingOptionsBo.getSourceField();
1180             docField = oleBatchProcessProfileDataMappingOptionsBo.getDestinationField();
1181             if (!EHoldings.DESTINATION_FIELD_LINK_TEXT.equalsIgnoreCase(docField))
1182                 continue;
1183 
1184             dataFields = BatchBibImportUtil.getMatchedDataField(bibRecord, bibField);
1185 
1186         }
1187         return dataFields;
1188     }
1189 
1190     /**
1191      * Find Matching Bib by using Bib Match  from profile and returns only if only one Bib is Matched
1192      *
1193      * @param profile
1194      * @param bibRecord
1195      * @return
1196      */
1197     private Bib findMatchingBib(OLEBatchProcessProfileBo profile, BibMarcRecord bibRecord) {
1198         List<OLEBatchProcessProfileMatchPoint> bibMatchPointList = BatchBibImportUtil.buildMatchPointListByDataType(profile.getOleBatchProcessProfileMatchPointList(), DocType.BIB.getCode());
1199         String fieldValue = null;
1200         SearchParams searchParams = new SearchParams();
1201         for (OLEBatchProcessProfileMatchPoint matchPoint : bibMatchPointList) {
1202             if (BatchBibImportUtil.getBibDataFieldValue(bibRecord, matchPoint.getMatchPoint()) == null) {
1203                 continue;
1204             }
1205 
1206             fieldValue = BatchBibImportUtil.getBibDataFieldValue(bibRecord, matchPoint.getMatchPoint());
1207             if (StringUtils.isNotBlank(fieldValue)) {
1208                 searchParams.getSearchConditions().add(searchParams.buildSearchCondition("phrase", searchParams.buildSearchField(DocType.BIB.getCode(), BatchBibImportUtil.getDataFieldWithout$(matchPoint.getMatchPoint()), fieldValue), "OR"));
1209             }
1210 
1211         }
1212 
1213         if (fieldValue == null) {
1214           return null;
1215         }
1216         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(DocType.BIB.getCode(), Bib.BIBIDENTIFIER));
1217 
1218         List<SearchResult> searchResults = new ArrayList<>();
1219         try {
1220             SearchResponse response = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
1221             searchResults = response.getSearchResults();
1222         } catch (Exception e) {
1223             LOG.error("Error :", e);
1224         }
1225 
1226         if (searchResults.size() > 1) {
1227             throw new DocstoreSearchException(OLEConstants.OLEBatchProcess.MORE_THAN_ONE_MATCHING_FOUND_FROM_EXISTING_RECORD);
1228         }
1229 
1230         if (searchResults.size() == 1) {
1231             Bib bib = new Bib();
1232             String bibId = searchResults.get(0).getSearchResultFields().get(0).getFieldValue();
1233             try {
1234                 bib = getDocstoreClientLocator().getDocstoreClient().retrieveBib(bibId);
1235             } catch (Exception e) {
1236                 LOG.error("Error :", e);
1237             }
1238             return bib;
1239         }
1240         return null;
1241     }
1242 
1243     /**
1244      * Update Matched Bib
1245      *
1246      * @param bibRecord
1247      * @param matchedBib
1248      * @param profile
1249      * @return
1250      */
1251     private void overlayBib(BibMarcRecord bibRecord, Bib matchedBib, OLEBatchProcessProfileBo profile) {
1252 
1253 
1254         BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
1255 
1256         batchProcessBibImportService.deleteFields(bibRecord, profile);
1257         batchProcessBibImportService.renameFields(bibRecord, profile);
1258         batchProcessBibImportService.setDefaultOrConstants(bibRecord, profile);
1259         batchProcessBibImportService.process001(bibRecord, profile);
1260 
1261         BibMarcRecords bibMarcRecords = bibMarcRecordProcessor.fromXML(matchedBib.getContent());
1262         BibMarcRecord matchedBibMarcRecord = bibMarcRecords.getRecords().get(0);
1263 
1264 
1265         batchProcessBibImportService.overlayFields(bibRecord, matchedBibMarcRecord, profile);
1266 
1267         String bibXML = bibMarcRecordProcessor.generateXML(matchedBibMarcRecord);
1268         matchedBib.setContent(bibXML);
1269 
1270         batchProcessBibImportService.setBibStatus(matchedBib, profile, matchedBib.getId());
1271 
1272         if (StringUtils.isEmpty(matchedBib.getId())) {
1273             matchedBib.setStaffOnly(profile.isBibStaffOnly());
1274         } else {
1275             if (OLEConstants.OLEBatchProcess.CHANGE.equals(profile.getOverlayNoChangeOrSet())) {
1276                 matchedBib.setStaffOnly(profile.isOverlayBibStaffOnly());
1277                 matchedBib.setStatusUpdatedBy(userName);
1278             }
1279         }
1280         matchedBib.setUpdatedBy(userName);
1281     }
1282 
1283     /**
1284      * @param bibId
1285      * @param dataField
1286      * @param holdingsMatchPointList
1287      * @param docType
1288      * @return
1289      */
1290     private Holdings findMatchingForPHoldingsAndEholdings(String bibId, DataField dataField, List<OLEBatchProcessProfileMatchPoint> holdingsMatchPointList, String docType) {
1291         SearchParams searchParams = new SearchParams();
1292         for (OLEBatchProcessProfileMatchPoint matchPoint : holdingsMatchPointList) {
1293             String fieldValue = null;
1294             //Checking  for field value in Holdings constants
1295             fieldValue = getFieldValueFromConstantsOrDefaults(matchPoint, OLEConstants.OLEBatchProcess.CONSTANT, DocType.HOLDINGS.getCode());
1296 
1297             // Checking value from  data Mapping
1298             if (dataField != null && fieldValue == null) {
1299                 for (OLEBatchProcessProfileDataMappingOptionsBo mappingOptionsBo : dataMapping) {
1300                     if (mappingOptionsBo.getDataTypeDestinationField().equalsIgnoreCase(docType)) {
1301                         if (mappingOptionsBo.getDestinationField().equalsIgnoreCase(matchPoint.getMatchPoint())) {
1302                             String bibField = mappingOptionsBo.getSourceField();
1303                             fieldValue = BatchBibImportUtil.getSubFieldValue(bibField, dataField);
1304                         }
1305                     }
1306                 }
1307             }
1308 
1309             //Checking  for field value in Holdings Default
1310             if (fieldValue == null) {
1311                 fieldValue = getFieldValueFromConstantsOrDefaults(matchPoint, OLEConstants.OLEBatchProcess.DEFAULT, DocType.HOLDINGS.getCode());
1312             }
1313 
1314             if (fieldValue != null) {
1315                 searchParams.getSearchConditions().add(searchParams.buildSearchCondition("phrase", searchParams.buildSearchField(docType, matchPoint.getMatchPoint().replaceAll(" ", "").toUpperCase(), fieldValue), "OR"));
1316             }
1317         }
1318         if (searchParams.getSearchConditions().size() > 0) {
1319             int lastIndex = searchParams.getSearchConditions().size() - 1;
1320             SearchCondition searchCondition = searchParams.getSearchConditions().get(lastIndex);
1321             searchCondition.setOperator("AND");
1322         }
1323 
1324         searchParams.getSearchConditions().add(searchParams.buildSearchCondition("AND", searchParams.buildSearchField(docType, Bib.BIBIDENTIFIER, bibId), "AND"));
1325         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(docType, Holdings.HOLDINGSIDENTIFIER));
1326         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(docType, "DocType"));
1327 
1328         List<SearchResult> searchResults = new ArrayList<>();
1329         try {
1330             SearchResponse response = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
1331             searchResults = response.getSearchResults();
1332         } catch (Exception e) {
1333             LOG.error("Error :", e);
1334         }
1335 
1336         if (searchResults.size() > 1) {
1337             throw new DocstoreSearchException(OLEConstants.OLEBatchProcess.MORE_THAN_ONE_MATCHING_FOUND_FROM_EXISTING_RECORD);
1338         }
1339 
1340         if (searchResults.size() == 1) {
1341             if (searchResults.get(0).getSearchResultFields() != null && searchResults.get(0).getSearchResultFields().size() > 0) {
1342                 Holdings holdings = new Holdings();
1343 
1344                 String holdingsId = searchResults.get(0).getSearchResultFields().get(0).getFieldValue();
1345                 try {
1346                     holdings = getDocstoreClientLocator().getDocstoreClient().retrieveHoldings(holdingsId);
1347                 } catch (Exception e) {
1348                     LOG.error("Error :", e);
1349                 }
1350                 return holdings;
1351             }
1352         }
1353         return null;
1354     }
1355 
1356     /**
1357      * @param matchPoint
1358      * @param constantOrDefault
1359      * @param docType
1360      * @return
1361      */
1362     private String getFieldValueFromConstantsOrDefaults(OLEBatchProcessProfileMatchPoint matchPoint, String constantOrDefault, String docType) {
1363 
1364         String fieldValue = null;
1365         for (OLEBatchProcessProfileConstantsBo oLEBatchProcessProfileConstantsBo : constantsMapping) {
1366 
1367             String defaultValue = oLEBatchProcessProfileConstantsBo.getDefaultValue();
1368 
1369             if (!defaultValue.equalsIgnoreCase(constantOrDefault))
1370                 continue;
1371 
1372             String docTypeConstant = oLEBatchProcessProfileConstantsBo.getDataType();
1373 
1374             if (!docTypeConstant.equalsIgnoreCase(docType))
1375                 continue;
1376 
1377             String docField = oLEBatchProcessProfileConstantsBo.getAttributeName();
1378             if (docField.equalsIgnoreCase(matchPoint.getMatchPoint())) {
1379                 fieldValue = oLEBatchProcessProfileConstantsBo.getAttributeValue();
1380             }
1381         }
1382         return fieldValue;
1383     }
1384 
1385     /**
1386      * @param dataField
1387      * @param matchedHoldings
1388      */
1389     private void overlayHoldings(DataField dataField, Holdings matchedHoldings, OLEBatchProcessProfileBo profile) {
1390         List<HoldingsTree> holdingsTreeList = BatchBibImportUtil.getHoldingsTrees(matchedHoldings);
1391         buildHoldingsFromDataField(matchedHoldings, profile, dataField);
1392         applyDataMappingForPHoldings(holdingsTreeList, dataMapping);
1393         applyDataMappingOverlay(holdingsTreeList, dataMapping);
1394         applyDefaultsAndConstants(profile, holdingsTreeList);
1395     }
1396 
1397     /**
1398      * @param profile
1399      * @param holdingsTreeList
1400      */
1401     private void applyDefaultsAndConstants(OLEBatchProcessProfileBo profile, List<HoldingsTree> holdingsTreeList) {
1402         List<OLEBatchProcessProfileConstantsBo> constantsMapping = profile.getOleBatchProcessProfileConstantsList();
1403         BatchBibImportUtil.buildLocationLevels(holdingsTreeList);
1404         applyDefaultsAndConstants(holdingsTreeList, constantsMapping, OLEConstants.OLEBatchProcess.DEFAULT);
1405         applyDefaultsAndConstants(holdingsTreeList, constantsMapping, OLEConstants.OLEBatchProcess.CONSTANT);
1406         prepareDocContent(holdingsTreeList);
1407     }
1408 
1409     /**
1410      * @param profile
1411      * @param holdingId
1412      * @param dataField
1413      * @return
1414      */
1415     private Item findMatchingItem(OLEBatchProcessProfileBo profile, String holdingId, BibMarcRecord bibRecord, DataField dataField) {
1416         List<OLEBatchProcessProfileMatchPoint> itemMatchPointList = BatchBibImportUtil.buildMatchPointListByDataType(profile.getOleBatchProcessProfileMatchPointList(), DocType.ITEM.getCode());
1417 
1418         SearchParams searchParams = new SearchParams();
1419 
1420 
1421         for (OLEBatchProcessProfileMatchPoint matchPoint : itemMatchPointList) {
1422             String fieldValue = null;
1423             //Checking  for field value in Item constants
1424             fieldValue = getFieldValueFromConstantsOrDefaults(matchPoint, OLEConstants.OLEBatchProcess.CONSTANT, DocType.ITEM.getCode());
1425 
1426             // Checking value from  data Mapping
1427             if (dataField != null && fieldValue == null) {
1428                 for (OLEBatchProcessProfileDataMappingOptionsBo mappingOptionsBo : dataMapping) {
1429                     if (mappingOptionsBo.getDataTypeDestinationField().equalsIgnoreCase(DocType.ITEM.getCode())) {
1430                         if (mappingOptionsBo.getDestinationField().equalsIgnoreCase(matchPoint.getMatchPoint())) {
1431                             String bibField = mappingOptionsBo.getSourceField();
1432                             fieldValue = BatchBibImportUtil.getSubFieldValue(bibField, dataField);
1433                         }
1434                     }
1435                 }
1436             }
1437 
1438             if (fieldValue == null) {
1439                 //Checking  for field value in Item Defaults
1440                 fieldValue = getFieldValueFromConstantsOrDefaults(matchPoint, OLEConstants.OLEBatchProcess.DEFAULT, DocType.ITEM.getCode());
1441             }
1442 
1443             if (fieldValue != null) {
1444                 searchParams.getSearchConditions().add(searchParams.buildSearchCondition("phrase", searchParams.buildSearchField(DocType.ITEM.getCode(), matchPoint.getMatchPoint().replaceAll(" ", "").toUpperCase(), fieldValue), "OR"));
1445             }
1446 
1447         }
1448         if (searchParams.getSearchConditions().size() > 0) {
1449             int lastIndex = searchParams.getSearchConditions().size() - 1;
1450             SearchCondition searchCondition = searchParams.getSearchConditions().get(lastIndex);
1451             searchCondition.setOperator("AND");
1452         }
1453 
1454         searchParams.getSearchConditions().add(searchParams.buildSearchCondition("AND", searchParams.buildSearchField(DocType.ITEM.getCode(), Holdings.HOLDINGSIDENTIFIER, holdingId), "AND"));
1455         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(DocType.ITEM.getCode(), Item.ITEMIDENTIFIER));
1456 
1457         List<SearchResult> searchResults = new ArrayList<>();
1458         try {
1459             SearchResponse response = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
1460             searchResults = response.getSearchResults();
1461         } catch (Exception e) {
1462             LOG.error("Error :", e);
1463         }
1464 
1465         if (searchResults.size() > 1) {
1466             throw new DocstoreSearchException(OLEConstants.OLEBatchProcess.MORE_THAN_ONE_MATCHING_FOUND_FROM_EXISTING_RECORD);
1467         }
1468 
1469         if (searchResults.size() == 1) {
1470             Item item = new Item();
1471             String itemId = searchResults.get(0).getSearchResultFields().get(0).getFieldValue();
1472             try {
1473                 item = getDocstoreClientLocator().getDocstoreClient().retrieveItem(itemId);
1474             } catch (Exception e) {
1475                 bibImportStatistics.getMoreThanOneItemMatched().add(bibRecord);
1476                 LOG.error("Error :", e);
1477             }
1478             return item;
1479         }
1480         return null;
1481     }
1482 
1483     /**
1484      * Buiild list of Items
1485      *
1486      * @param profile
1487      * @param holdings
1488      * @return
1489      */
1490     private List<Item> buildItems(OLEBatchProcessProfileBo profile, Holdings holdings) {
1491         List<HoldingsTree> holdingsTreeList = BatchBibImportUtil.getHoldingsTrees(holdings);
1492         buildItemForHoldings(dataMapping, holdingsTreeList, profile);
1493         for (HoldingsTree holdingsTree : holdingsTreeList) {
1494             setItemOperation(DocstoreDocument.OperationType.CREATE, holdingsTree);
1495         }
1496 
1497         return BatchBibImportUtil.getItem(holdingsTreeList);
1498     }
1499 
1500     /**
1501      * @param matchedItem
1502      * @param profile
1503      * @param dataField
1504      */
1505     private void overlayItem(Item matchedItem, OLEBatchProcessProfileBo profile, DataField dataField) {
1506         applyItemDataMapping(matchedItem, dataField);
1507         applyDefaultConstantsandSerilazeContent(matchedItem, profile);
1508     }
1509 
1510     private void applyDefaultConstantsandSerilazeContent(Item matchedItem, OLEBatchProcessProfileBo profile) {
1511         List<OLEBatchProcessProfileConstantsBo> constantsMapping = profile.getOleBatchProcessProfileConstantsList();
1512         applyItemDefaultAndConstants(matchedItem, constantsMapping, OLEConstants.OLEBatchProcess.DEFAULT);
1513         applyItemDefaultAndConstants(matchedItem, constantsMapping, OLEConstants.OLEBatchProcess.CONSTANT);
1514         matchedItem.serializeContent();
1515     }
1516 
1517     /**
1518      * @param item
1519      * @param dataField
1520      */
1521 
1522     private void applyItemDataMapping(Item item, DataField dataField) {
1523         String docType = null;
1524         String docField = null;
1525 
1526         String bibFieldValue = null;
1527         if (dataField != null) {
1528             for (OLEBatchProcessProfileDataMappingOptionsBo oleBatchProcessProfileDataMappingOptionsBo : dataMapping) {
1529                 docType = oleBatchProcessProfileDataMappingOptionsBo.getDataTypeDestinationField();
1530 
1531                 if (!docType.equalsIgnoreCase(DocType.ITEM.getCode()))
1532                     continue;
1533 
1534 
1535                 docField = oleBatchProcessProfileDataMappingOptionsBo.getDestinationField();
1536                 bibFieldValue = BatchBibImportUtil.getSubFieldValue(oleBatchProcessProfileDataMappingOptionsBo.getSourceField(), dataField);
1537                 if (docType.equalsIgnoreCase(DocType.ITEM.getCode())) {
1538                     if (item != null) {
1539                         item.setField(docField, bibFieldValue);
1540                     }
1541                 }
1542             }
1543         }
1544     }
1545 
1546     /**
1547      * @param item
1548      * @param oleBatchProcessProfileConstantsBoLis
1549      * @param defaultOrConstant
1550      */
1551     private void applyItemDefaultAndConstants(Item item, List<OLEBatchProcessProfileConstantsBo> oleBatchProcessProfileConstantsBoLis, String defaultOrConstant) {
1552 
1553         for (OLEBatchProcessProfileConstantsBo oLEBatchProcessProfileConstantsBo : oleBatchProcessProfileConstantsBoLis) {
1554             String defaultValue = oLEBatchProcessProfileConstantsBo.getDefaultValue();
1555 
1556             if (!defaultValue.equalsIgnoreCase(defaultOrConstant))
1557                 continue;
1558 
1559             String docType = oLEBatchProcessProfileConstantsBo.getDataType();
1560 
1561             if (!docType.equalsIgnoreCase(DocType.ITEM.getCode()))
1562                 continue;
1563 
1564             String docField = oLEBatchProcessProfileConstantsBo.getAttributeName();
1565             String fieldValue = oLEBatchProcessProfileConstantsBo.getAttributeValue();
1566             if (docType.equalsIgnoreCase(DocType.ITEM.getCode())) {
1567                 if (item != null) {
1568                     if (defaultValue.equalsIgnoreCase(OLEConstants.OLEBatchProcess.CONSTANT)) {
1569                         item.setField(docField, fieldValue);
1570                     } else {
1571                         item.setDefaultField(docField, fieldValue);
1572                     }
1573 
1574                 }
1575             }
1576         }
1577     }
1578 
1579 
1580     public OLEBatchBibImportDataObjects processOrderBatch(List<OrderBibMarcRecord> orderBibMarcRecords, OLEBatchProcessProfileBo profile, OLEBatchBibImportStatistics batchBibImportStatistics, String user) {
1581         OLEBatchBibImportDataObjects oleBatchBibImportDataObjects = new OLEBatchBibImportDataObjects();
1582         bibImportStatistics = batchBibImportStatistics;
1583         MatchingProfile matchingProfile = profile.getMatchingProfileObj();
1584         for (OrderBibMarcRecord orderBibMarcRecord : orderBibMarcRecords) {
1585             BibMarcRecord bibRecord = orderBibMarcRecord.getBibMarcRecord();
1586             processBibMarcRecord(profile, oleBatchBibImportDataObjects, matchingProfile, bibRecord);
1587         }
1588         return oleBatchBibImportDataObjects;
1589     }
1590 }