View Javadoc

1   package org.kuali.ole.batch.helper;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.log4j.Logger;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.batch.bo.OLEBatchProcessProfileBo;
7   import org.kuali.ole.batch.bo.OLEBatchProcessProfileDataMappingOptionsBo;
8   import org.kuali.ole.docstore.common.document.HoldingsTree;
9   import org.kuali.ole.docstore.common.document.content.instance.*;
10  import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
11  import org.kuali.ole.docstore.common.document.content.bib.marc.DataField;
12  import org.kuali.ole.docstore.common.document.content.bib.marc.SubField;
13  import org.springframework.util.CollectionUtils;
14  
15  import java.util.*;
16  
17  import static org.kuali.ole.OLEConstants.OLEBatchProcess.*;
18  import static org.kuali.ole.OLEConstants.OLEBatchProcess.lineSeparator;
19  
20  /**
21   * Created with IntelliJ IDEA.
22   * User: meenrajd
23   * Date: 9/2/13
24   * Time: 5:12 PM
25   * To change this template use File | Settings | File Templates.
26   */
27  public class EInstanceMappingHelper {
28      private List<DataField> dataFieldList = new ArrayList<DataField>();
29      private StringBuilder errBuilder;
30  
31      private static final Logger LOG = Logger.getLogger(InstanceMappingHelper.class);
32      private HoldingOlemlRecordProcessor workEHoldingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
33      
34      /*public List<DataField> generateDataField(InstanceCollection instanceCollection, OLEBatchProcessProfileBo profile, StringBuilder errBuilder) throws Exception {
35          dataFieldList.clear();
36          this.errBuilder = errBuilder;
37          if (instanceCollection != null && instanceCollection.getInstance() != null) {
38              OleHoldings oleHoldings;
39              String dataFieldInstance = null;
40              List<OLEBatchProcessProfileDataMappingOptionsBo> mappingOptionsBoList = profile.getOleBatchProcessProfileMappingOptionsList().get(0).getOleBatchProcessProfileDataMappingOptionsBoList();
41  
42              for (OLEBatchProcessProfileDataMappingOptionsBo mappingOptionsBo : mappingOptionsBoList) {
43                  if (mappingOptionsBo.getDataType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_PROCESS_PROFILE_DATATYPE_EHOLDINGS)) {
44                      dataFieldInstance = getTag(mappingOptionsBo.getDestinationField());
45                  }
46              }
47              if (dataFieldInstance == null) return Collections.EMPTY_LIST; //no subfields would be created for the bib
48  
49              for (int i = 0; i < instanceCollection.getInstance().size(); i++) {
50                  if ((oleHoldings = instanceCollection.getInstance().get(i)) == null) continue;
51                  if (oleHoldings == null) continue;
52                  DataField dataField = new DataField();
53                  dataField.setTag(dataFieldInstance);
54                  dataField.setInd1(" ");
55                  dataField.setInd2(" ");
56                  generateSubFields(mappingOptionsBoList, oleHoldings, dataField);
57                  if (!dataField.getSubFields().isEmpty())
58                      dataFieldList.add(dataField);
59  
60              }
61          }
62          return dataFieldList;
63      }*/
64  
65      public List<DataField> generateDataFieldForEHolding(HoldingsTree holdingsTree, OLEBatchProcessProfileBo profile, StringBuilder errBuilder) throws Exception {
66          dataFieldList.clear();
67          this.errBuilder = errBuilder;        
68          if (holdingsTree != null) {
69              OleHoldings oleHoldings = workEHoldingOlemlRecordProcessor.fromXML(holdingsTree.getHoldings().getContent());
70              Map<String, String> dataFieldEHoldingMap = new HashMap<>();
71              Map<String, String> dataFieldCoverageMap = new HashMap<>();
72              List<OLEBatchProcessProfileDataMappingOptionsBo> mappingOptionsBoList = profile.getOleBatchProcessProfileMappingOptionsList().get(0).getOleBatchProcessProfileDataMappingOptionsBoList();
73              for (OLEBatchProcessProfileDataMappingOptionsBo mappingOptionsBo : mappingOptionsBoList) {
74                  if (mappingOptionsBo.getDataType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_PROCESS_PROFILE_DATATYPE_EHOLDINGS)) {
75                      if (mappingOptionsBo.getSourceField().equalsIgnoreCase(DESTINATION_FIELD_COVERAGE_START_DATE)
76                              || mappingOptionsBo.getSourceField().equalsIgnoreCase(DESTINATION_FIELD_COVERAGE_END_DATE)
77                              || mappingOptionsBo.getSourceField().equalsIgnoreCase(DESTINATION_FIELD_COVERAGE_START_ISSUE)
78                              || mappingOptionsBo.getSourceField().equalsIgnoreCase(DESTINATION_FIELD_COVERAGE_END_ISSUE)
79                              || mappingOptionsBo.getSourceField().equalsIgnoreCase(DESTINATION_FIELD_COVERAGE_START_VOLUME)
80                              || mappingOptionsBo.getSourceField().equalsIgnoreCase(DESTINATION_FIELD_COVERAGE_END_VOLUME)) {
81                          dataFieldCoverageMap.put(mappingOptionsBo.getDestinationField(), mappingOptionsBo.getSourceField());
82                      } else {
83                          dataFieldEHoldingMap.put(mappingOptionsBo.getDestinationField(), mappingOptionsBo.getSourceField());
84                      }
85                  }
86              }
87              if (!CollectionUtils.isEmpty(dataFieldEHoldingMap)) {
88                  generateSubFieldsForEHolding(oleHoldings, dataFieldEHoldingMap, dataFieldCoverageMap);
89              } else {
90                  return Collections.EMPTY_LIST;
91              }
92          }
93          return dataFieldList;
94      }
95  
96      protected void generateSubFieldsForEHolding(OleHoldings oleHoldings, Map<String, String> dataFieldEHoldingMap, Map<String, String> dataFieldCoverageMap) throws Exception {
97          try {
98              for (Map.Entry<String, String> entry : dataFieldEHoldingMap.entrySet()) {
99                  DataField dataField;
100                 if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_CALL_NUMBER)) {
101                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
102                     if (dataField == null) {
103                         dataField = getDataField(entry);
104                         generateCallNumber(oleHoldings, getCode(entry.getKey()), dataField);
105                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
106                     } else {
107                         generateCallNumber(oleHoldings, getCode(entry.getKey()), dataField);
108                     }
109                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_HOLDING_CALL_NUMBER_TYPE)) {
110                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
111                     if (dataField == null) {
112                         dataField = getDataField(entry);
113                         generateCallNumberType(oleHoldings, getCode(entry.getKey()), dataField);
114                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
115                     } else {
116                         generateCallNumberType(oleHoldings, getCode(entry.getKey()), dataField);
117                     }
118                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_1)) {
119                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
120                     if (dataField == null) {
121                         dataField = getDataField(entry);
122                         generateLocationLevel1(oleHoldings, getCode(entry.getKey()), dataField);
123                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
124                     } else {
125                         generateLocationLevel1(oleHoldings, getCode(entry.getKey()), dataField);
126                     }
127                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_2)) {
128                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
129                     if (dataField == null) {
130                         dataField = getDataField(entry);
131                         generateLocationLevel2(oleHoldings, getCode(entry.getKey()), dataField);
132                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
133                     } else {
134                         generateLocationLevel2(oleHoldings, getCode(entry.getKey()), dataField);
135                     }
136                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_3)) {
137                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
138                     if (dataField == null) {
139                         dataField = getDataField(entry);
140                         generateLocationLevel3(oleHoldings, getCode(entry.getKey()), dataField);
141                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
142                     } else {
143                         generateLocationLevel3(oleHoldings, getCode(entry.getKey()), dataField);
144                     }
145                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_4)) {
146                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
147                     if (dataField == null) {
148                         dataField = getDataField(entry);
149                         generateLocationLevel4(oleHoldings, getCode(entry.getKey()), dataField);
150                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
151                     } else {
152                         generateLocationLevel4(oleHoldings, getCode(entry.getKey()), dataField);
153                     }
154                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_5)) {
155                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
156                     if (dataField == null) {
157                         dataField = getDataField(entry);
158                         generateLocationLevel5(oleHoldings, getCode(entry.getKey()), dataField);
159                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
160                     } else {
161                         generateLocationLevel5(oleHoldings, getCode(entry.getKey()), dataField);
162                     }
163                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_CALL_NUMBER_TYPE_PREFIX)) {
164                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
165                     if (dataField == null) {
166                         dataField = getDataField(entry);
167                         generateCallNumberPrefix(oleHoldings, getCode(entry.getKey()), dataField);
168                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
169                     } else {
170                         generateCallNumberPrefix(oleHoldings, getCode(entry.getKey()), dataField);
171                     }
172                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LINK_URL)) {
173                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
174                     if (dataField == null) {
175                         dataField = getDataField(entry);
176                         generateLink(oleHoldings, getCode(entry.getKey()), dataField);
177                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
178                     } else {
179                         generateLink(oleHoldings, getCode(entry.getKey()), dataField);
180                     }
181                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_PERSISTENTLINK)) {
182                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
183                     if (dataField == null) {
184                         dataField = getDataField(entry);
185                         generatePersistentLink(oleHoldings, getCode(entry.getKey()), dataField);
186                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
187                     } else {
188                         generatePersistentLink(oleHoldings, getCode(entry.getKey()), dataField);
189                     }
190                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LINK_TEXT)) {
191                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
192                     if (dataField == null) {
193                         dataField = getDataField(entry);
194                         generateLinkText(oleHoldings, getCode(entry.getKey()), dataField);
195                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
196                     } else {
197                         generateLinkText(oleHoldings, getCode(entry.getKey()), dataField);
198                     }
199                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_STATISTICAL_CODE)) {
200                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
201                     if (dataField == null) {
202                         dataField = getDataField(entry);
203                         generateStatisticalCode(oleHoldings, getCode(entry.getKey()), dataField);
204                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
205                     } else {
206                         generateStatisticalCode(oleHoldings, getCode(entry.getKey()), dataField);
207                     }
208                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ACCESS_STATUS_CODE)) {
209                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
210                     if (dataField == null) {
211                         dataField = getDataField(entry);
212                         generateAccessStatus(oleHoldings, getCode(entry.getKey()), dataField);
213                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
214                     } else {
215                         generateAccessStatus(oleHoldings, getCode(entry.getKey()), dataField);
216                     }
217                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_PUBLIC_DISPLAY_NOTE)) {
218                     for (Note note : oleHoldings.getNote()) {
219                         if (note.getType().equalsIgnoreCase(OLEConstants.NOTE_TYPE)) {
220                             dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
221                             if (dataField == null) {
222                                 dataField = getDataField(entry);
223                                 generatePublicDisplayNote(oleHoldings, note, getCode(entry.getKey()), dataField);
224                                 if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
225                             } else {
226                                 generatePublicDisplayNote(oleHoldings, note, getCode(entry.getKey()), dataField);
227                             }
228                         }
229                     }
230                 }
231             }
232             if (!CollectionUtils.isEmpty(dataFieldCoverageMap)) {
233                 generateCoverageFields(oleHoldings, dataFieldCoverageMap);
234             }
235         } catch (Exception ex) {
236             LOG.error("Error while mapping oleHoldings data ::" + oleHoldings.getHoldingsIdentifier(), ex);
237             buildError(ERR_INSTANCE, oleHoldings.getHoldingsIdentifier(), ERR_CAUSE, ex.getMessage(), TIME_STAMP, new Date().toString());
238             throw ex;
239         }
240     }
241 
242     private void generateCoverageFields(OleHoldings oleHoldings, Map<String, String> dataFieldCoverageMap) throws Exception {
243         List<DataField> coverageFieldList = new ArrayList<>();
244         try {
245             if (!CollectionUtils.isEmpty(oleHoldings.getExtentOfOwnership())) {
246                 for (ExtentOfOwnership extentOfOwnership : oleHoldings.getExtentOfOwnership()) {
247                     if (null != extentOfOwnership.getCoverages() && !CollectionUtils.isEmpty(extentOfOwnership.getCoverages().getCoverage())) {
248                         for (Coverage coverage : extentOfOwnership.getCoverages().getCoverage()) {
249                             for (Map.Entry<String, String> entry : dataFieldCoverageMap.entrySet()) {
250                                 DataField dataField;
251                                 if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_COVERAGE_START_DATE)) {
252                                     dataField = checkDataField(coverageFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
253                                     if (dataField == null) {
254                                         dataField = getDataField(entry);
255                                         generateCoverageStartDate(oleHoldings, coverage, getCode(entry.getKey()), dataField);
256                                         if (!dataField.getSubFields().isEmpty()) coverageFieldList.add(dataField);
257                                     } else {
258                                         generateCoverageStartDate(oleHoldings, coverage, getCode(entry.getKey()), dataField);
259                                     }
260                                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_COVERAGE_END_DATE)) {
261                                     dataField = checkDataField(coverageFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
262                                     if (dataField == null) {
263                                         dataField = getDataField(entry);
264                                         generateCoverageEndDate(oleHoldings, coverage, getCode(entry.getKey()), dataField);
265                                         if (!dataField.getSubFields().isEmpty()) coverageFieldList.add(dataField);
266                                     } else {
267                                         generateCoverageEndDate(oleHoldings, coverage, getCode(entry.getKey()), dataField);
268                                     }
269                                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_COVERAGE_START_ISSUE)) {
270                                     dataField = checkDataField(coverageFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
271                                     if (dataField == null) {
272                                         dataField = getDataField(entry);
273                                         generateCoverageStartIssue(oleHoldings, coverage, getCode(entry.getKey()), dataField);
274                                         if (!dataField.getSubFields().isEmpty()) coverageFieldList.add(dataField);
275                                     } else {
276                                         generateCoverageStartIssue(oleHoldings, coverage, getCode(entry.getKey()), dataField);
277                                     }
278                                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_COVERAGE_END_ISSUE)) {
279                                     dataField = checkDataField(coverageFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
280                                     if (dataField == null) {
281                                         dataField = getDataField(entry);
282                                         generateCoverageEndIssue(oleHoldings, coverage, getCode(entry.getKey()), dataField);
283                                         if (!dataField.getSubFields().isEmpty()) coverageFieldList.add(dataField);
284                                     } else {
285                                         generateCoverageEndIssue(oleHoldings, coverage, getCode(entry.getKey()), dataField);
286                                     }
287                                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_COVERAGE_START_VOLUME)) {
288                                     dataField = checkDataField(coverageFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
289                                     if (dataField == null) {
290                                         dataField = getDataField(entry);
291                                         generateCoverageStartVolume(oleHoldings, coverage, getCode(entry.getKey()), dataField);
292                                         if (!dataField.getSubFields().isEmpty()) coverageFieldList.add(dataField);
293                                     } else {
294                                         generateCoverageStartVolume(oleHoldings, coverage, getCode(entry.getKey()), dataField);
295                                     }
296                                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_COVERAGE_END_VOLUME)) {
297                                     dataField = checkDataField(coverageFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
298                                     if (dataField == null) {
299                                         dataField = getDataField(entry);
300                                         generateCoverageEndVolume(oleHoldings, coverage, getCode(entry.getKey()), dataField);
301                                         if (!dataField.getSubFields().isEmpty()) coverageFieldList.add(dataField);
302                                     } else {
303                                         generateCoverageEndVolume(oleHoldings, coverage, getCode(entry.getKey()), dataField);
304                                     }
305                                 }
306                             }
307                             dataFieldList.addAll(coverageFieldList);
308                             coverageFieldList.clear();
309                         }
310                     }
311                 }
312             }
313         } catch (Exception ex) {
314             LOG.error("Error while mapping oleHoldings data ::" + oleHoldings.getHoldingsIdentifier(), ex);
315             buildError(ERR_INSTANCE, oleHoldings.getHoldingsIdentifier(), ERR_CAUSE, ex.getMessage(), TIME_STAMP, new Date().toString());
316             throw ex;
317         }
318 
319     }
320 
321     protected DataField checkDataField(List<DataField> dataFieldList, String dataField) {
322         for (DataField field : dataFieldList) {
323             if (dataField.equals(field.getTag())) {
324                 return field;
325             }
326         }
327         return null;
328     }
329 
330     protected DataField getDataField(Map.Entry<String, String> entry) {
331         DataField dataField = new DataField();
332         dataField.setTag(StringUtils.trim(entry.getKey()).substring(0, 3));
333         dataField.setInd1(" ");
334         dataField.setInd2(" ");
335         return dataField;
336     }
337 
338     /**
339      * generates the subfields for the given oleHoldings and mapping options
340      *
341      * @param mappingOptionsBoList
342      * @param oleHoldings
343      * @param dataField
344      */
345     protected void generateSubFields(List<OLEBatchProcessProfileDataMappingOptionsBo> mappingOptionsBoList, OleHoldings oleHoldings, DataField dataField) throws Exception {
346         try {
347             for (OLEBatchProcessProfileDataMappingOptionsBo mappingField : mappingOptionsBoList) {
348                 if (!mappingField.getDataType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_PROCESS_PROFILE_DATATYPE_EHOLDINGS)) continue;
349                 if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_CALL_NUMBER)) {
350                     generateCallNumber(oleHoldings, getCode(mappingField.getDestinationField()), dataField);
351                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_HOLDING_CALL_NUMBER_TYPE)) {
352                     generateCallNumberType(oleHoldings, getCode(mappingField.getDestinationField()), dataField);
353                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_1)) {
354                     generateLocationLevel1(oleHoldings, getCode(mappingField.getDestinationField()), dataField);
355                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_2)) {
356                     generateLocationLevel2(oleHoldings, getCode(mappingField.getDestinationField()), dataField);
357                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_3)) {
358                     generateLocationLevel3(oleHoldings, getCode(mappingField.getDestinationField()), dataField);
359                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_4)) {
360                     generateLocationLevel4(oleHoldings, getCode(mappingField.getDestinationField()), dataField);
361                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_5)) {
362                     generateLocationLevel5(oleHoldings, getCode(mappingField.getDestinationField()), dataField);
363                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_CALL_NUMBER_TYPE_PREFIX)) {
364                     generateCallNumberPrefix(oleHoldings, getCode(mappingField.getDestinationField()), dataField);
365                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LINK_URL)) {
366                     generateLink(oleHoldings, getCode(mappingField.getDestinationField()), dataField);
367                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_PERSISTENTLINK)) {
368                     generatePersistentLink(oleHoldings, getCode(mappingField.getDestinationField()), dataField);
369                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LINK_TEXT)) {
370                     generateLinkText(oleHoldings, getCode(mappingField.getDestinationField()), dataField);
371                 }
372             }
373         } catch (Exception ex) {
374             LOG.error("Error while mapping oleHoldings data ::" + oleHoldings.getHoldingsIdentifier(), ex);
375             buildError(ERR_INSTANCE, oleHoldings.getHoldingsIdentifier(), ERR_CAUSE, ex.getMessage(), TIME_STAMP, new Date().toString());
376             throw ex;
377         }
378     }
379 
380     private void addDataField(DataField dataField, SubField subField) {
381         if (StringUtils.isEmpty(subField.getValue())) return;
382         dataField.getSubFields().add(subField);
383     }
384 
385     protected String getTag(String mappingField) {
386         return StringUtils.trim(StringUtils.substringBefore(mappingField, "$"));
387     }
388 
389     private String getCode(String mappingField) {
390         return StringUtils.trim(StringUtils.substringAfter(mappingField, "$"));
391     }
392 
393     /**
394      * generates the subfields for call number for the given oleHoldings
395      *
396      * @param oleHoldings
397      * @param code
398      * @param dataField
399      */
400     private void generateCallNumber(OleHoldings oleHoldings, String code, DataField dataField) throws Exception {
401         SubField subField = new SubField();
402         subField.setCode(code);
403         try {
404             if (oleHoldings != null && oleHoldings.getCallNumber() != null && StringUtils.isNotEmpty(oleHoldings.getCallNumber().getNumber())) {
405                 subField.setValue(oleHoldings.getCallNumber().getNumber());
406                 addDataField(dataField, subField);
407             }
408         } catch (Exception ex) {
409             logError(oleHoldings, ex, "generateCallNumber()");
410         }
411 
412     }
413     /**
414      * generates the call number prefix for the given oleHoldings
415      * @param oleHoldings
416      * @param code
417      * @param dataField
418      * @throws Exception
419      */
420     private void generateCallNumberPrefix(OleHoldings oleHoldings, String code, DataField dataField) throws Exception {
421         SubField subField = new SubField();
422         subField.setCode(code);
423         try {
424             if (oleHoldings != null && oleHoldings.getCallNumber() != null && StringUtils.isNotEmpty(oleHoldings.getCallNumber().getPrefix())) {
425                 subField.setValue(oleHoldings.getCallNumber().getPrefix());
426                 addDataField(dataField, subField);
427             }
428         } catch (Exception ex) {
429             logError(oleHoldings, ex, "generateCallNumberPrefix()");
430         }
431 
432     }
433 
434     /**
435      * generates the Link for the given oleHoldings
436      * @param oleHoldings
437      * @param code
438      * @param dataField
439      * @throws Exception
440      */
441     private void generateLink(OleHoldings oleHoldings, String code, DataField dataField) throws Exception {
442         SubField subField = new SubField();
443         subField.setCode(code);
444         try {
445             if (oleHoldings != null && oleHoldings.getLink() != null && StringUtils.isNotEmpty(oleHoldings.getLink().getUrl())) {
446                 subField.setValue(oleHoldings.getLink().getUrl());
447                 addDataField(dataField, subField);
448             }
449         } catch (Exception ex) {
450             logError(oleHoldings, ex, "generateLink()");
451         }
452 
453     }
454 
455     /**
456      * generates the persistent link for the given oleHoldings
457      * @param oleHoldings
458      * @param code
459      * @param dataField
460      * @throws Exception
461      */
462     private void generatePersistentLink(OleHoldings oleHoldings, String code, DataField dataField) throws Exception {
463         SubField subField = new SubField();
464         subField.setCode(code);
465         try {
466             if (oleHoldings != null && oleHoldings.getLocalPersistentLink() != null && StringUtils.isNotEmpty(oleHoldings.getLocalPersistentLink())) {
467                 subField.setValue(oleHoldings.getLocalPersistentLink());
468                 addDataField(dataField, subField);
469             }
470         } catch (Exception ex) {
471             logError(oleHoldings, ex, "generatePersistentLink()");
472         }
473 
474     }
475 
476     /**
477      * generates the Link Text for the given oleHoldings
478      * @param oleHoldings
479      * @param code
480      * @param dataField
481      * @throws Exception
482      */
483     private void generateLinkText(OleHoldings oleHoldings, String code, DataField dataField) throws Exception {
484         SubField subField = new SubField();
485         subField.setCode(code);
486         try {
487             if (oleHoldings != null && oleHoldings.getLink() != null && StringUtils.isNotEmpty(oleHoldings.getLink().getText())) {
488                 subField.setValue(oleHoldings.getLink().getText());
489                 addDataField(dataField, subField);
490             }
491         } catch (Exception ex) {
492             logError(oleHoldings, ex, "generateLinkText()");
493         }
494 
495     }
496 
497     /**
498      * generates the subfield for the call number type for the given oleHoldings
499      *
500      * @param oleHoldings
501      * @param code
502      * @param dataField
503      */
504     private void generateCallNumberType(OleHoldings oleHoldings, String code, DataField dataField) throws Exception {
505         SubField subField = new SubField();
506         subField.setCode(code);
507         try {
508             if (oleHoldings != null && oleHoldings.getCallNumber() != null && oleHoldings.getCallNumber().getShelvingScheme() != null && oleHoldings.getCallNumber().getShelvingScheme().getCodeValue() != null) {
509                 subField.setValue(oleHoldings.getCallNumber().getShelvingScheme().getCodeValue());
510                 addDataField(dataField, subField);
511             }
512         } catch (Exception ex) {
513             logError(oleHoldings, ex, "generateCallNumberType()");
514         }
515     }
516 
517     /**
518      * gets the locationlevel name for the given level and locationlevel
519      *
520      * @param locationLevel
521      * @param level
522      * @return
523      */
524     private String getLocationLevelName(LocationLevel locationLevel, String level) {
525         if (locationLevel == null || StringUtils.isEmpty(locationLevel.getLevel())) return null;
526         if (locationLevel.getLevel().toUpperCase().startsWith(level)) return locationLevel.getName();
527         return getLocationLevelName(locationLevel.getLocationLevel(), level);
528     }
529 
530     /**
531      * generates subfield for location level 3 - LIBRARY for the given oleHoldings
532      *
533      * @param oleHoldings
534      * @param code
535      * @param dataField
536      */
537     private void generateLocationLevel3(OleHoldings oleHoldings, String code, DataField dataField) throws Exception {
538         String locationLevelName;
539         try {
540             if (oleHoldings != null && oleHoldings.getLocation() != null
541                     && (locationLevelName = getLocationLevelName(oleHoldings.getLocation().getLocationLevel(), "LIBRARY")) != null) {
542                 SubField subField = new SubField();
543                 subField.setCode(code);
544                 subField.setValue(locationLevelName);
545                 addDataField(dataField, subField);
546             }
547         } catch (Exception ex) {
548             logError(oleHoldings, ex, "generateLocationLevel3()");
549         }
550     }
551 
552     /**
553      * generates subfield for location level 2 - CAMPUS for the given oleHoldings
554      *
555      * @param oleHoldings
556      * @param code
557      * @param dataField
558      * @throws Exception
559      */
560     private void generateLocationLevel2(OleHoldings oleHoldings, String code, DataField dataField) throws Exception {
561         String locationLevelName;
562         try {
563             if (oleHoldings != null && oleHoldings.getLocation() != null
564                     && (locationLevelName = getLocationLevelName(oleHoldings.getLocation().getLocationLevel(), "CAMPUS")) != null) {
565                 SubField subField = new SubField();
566                 subField.setCode(code);
567                 subField.setValue(locationLevelName);
568                 addDataField(dataField, subField);
569             }
570         } catch (Exception ex) {
571             logError(oleHoldings, ex, "generateLocationLevel2()");
572         }
573     }
574 
575     /**
576      * generates subfield for location level 1 - INSTITUTION for the given oleHoldings
577      *
578      * @param oleHoldings
579      * @param code
580      * @param dataField
581      * @throws Exception
582      */
583     private void generateLocationLevel1(OleHoldings oleHoldings, String code, DataField dataField) throws Exception {
584         String locationLevelName;
585         try {
586             if (oleHoldings != null && oleHoldings.getLocation() != null
587                     && (locationLevelName = getLocationLevelName(oleHoldings.getLocation().getLocationLevel(), "INSTITUTION")) != null) {
588                 SubField subField = new SubField();
589                 subField.setCode(code);
590                 subField.setValue(locationLevelName);
591                 addDataField(dataField, subField);
592             }
593         } catch (Exception ex) {
594             logError(oleHoldings, ex, "generateLocationLevel1()");
595         }
596     }
597 
598     /**
599      * generates subfield for location level 5 - SHELVING for the given oleHoldings
600      *
601      * @param oleHoldings
602      * @param code
603      * @param dataField
604      * @throws Exception
605      */
606     private void generateLocationLevel5(OleHoldings oleHoldings, String code, DataField dataField) throws Exception {
607         String locationLevelName;
608         try {
609             if (oleHoldings != null && oleHoldings.getLocation() != null
610                     && (locationLevelName = getLocationLevelName(oleHoldings.getLocation().getLocationLevel(), "SHELVING")) != null) {
611                 SubField subField = new SubField();
612                 subField.setCode(code);
613                 subField.setValue(locationLevelName);
614                 addDataField(dataField, subField);
615             }
616         } catch (Exception ex) {
617             logError(oleHoldings, ex, "generateLocationLevel5()");
618         }
619     }
620 
621     /**
622      * generates the subfields for the location level -4 COLLECTION for the given oleHoldings
623      *
624      * @param oleHoldings
625      * @param code
626      * @param dataField
627      */
628     private void generateLocationLevel4(OleHoldings oleHoldings, String code, DataField dataField) throws Exception {
629         String locationLevelName;
630         try {
631             if (oleHoldings != null && oleHoldings.getLocation() != null
632                     && (locationLevelName = getLocationLevelName(oleHoldings.getLocation().getLocationLevel(), "COLLECTION")) != null) {
633                 SubField subField = new SubField();
634                 subField.setCode(code);
635                 subField.setValue(locationLevelName);
636                 addDataField(dataField, subField);
637             }
638         } catch (Exception ex) {
639             logError(oleHoldings, ex, "generateLocationLevel4()");
640         }
641 
642     }
643 
644     /**
645      * generates the subfields for the Statistical Code for the given oleHoldings
646      *
647      * @param oleHoldings
648      * @param code
649      * @param dataField
650      */
651     private void generateStatisticalCode(OleHoldings oleHoldings, String code, DataField dataField) throws Exception {
652         try {
653             if (oleHoldings != null && oleHoldings.getStatisticalSearchingCode() != null && oleHoldings.getStatisticalSearchingCode().getCodeValue() != null) {
654                 SubField subField = new SubField();
655                 subField.setCode(code);
656                 subField.setValue(oleHoldings.getStatisticalSearchingCode().getCodeValue());
657                 addDataField(dataField, subField);
658             }
659         } catch (Exception ex) {
660             logError(oleHoldings, ex, "generateStatisticalCode()");
661         }
662     }
663 
664     /**
665      * generates the subfields for the Access Status for the given oleHoldings
666      *
667      * @param oleHoldings
668      * @param code
669      * @param dataField
670      */
671     private void generateAccessStatus(OleHoldings oleHoldings, String code, DataField dataField) throws Exception {
672         try {
673             if (oleHoldings != null && oleHoldings.getAccessStatus() != null) {
674                 SubField subField = new SubField();
675                 subField.setCode(code);
676                 subField.setValue(oleHoldings.getAccessStatus());
677                 addDataField(dataField, subField);
678             }
679         } catch (Exception ex) {
680             logError(oleHoldings, ex, "generateAccessStatus()");
681         }
682     }
683 
684     /**
685      * generates the subfields for the Coverage Start Date for the given oleHoldings
686      *
687      * @param oleHoldings
688      * @param code
689      * @param dataField
690      */
691     private void generateCoverageStartDate(OleHoldings oleHoldings, Coverage coverage, String code, DataField dataField) throws Exception {
692         try {
693             if (null != coverage) {
694                 SubField subField = new SubField();
695                 subField.setCode(code);
696                 subField.setValue(coverage.getCoverageStartDate());
697                 addDataField(dataField, subField);
698             }
699         } catch (Exception ex) {
700             logError(oleHoldings, ex, "generateCoverageStartDate()");
701         }
702     }
703 
704     /**
705      * generates the subfields for the Coverage End Date for the given oleHoldings
706      *
707      * @param oleHoldings
708      * @param code
709      * @param dataField
710      */
711     private void generateCoverageEndDate(OleHoldings oleHoldings, Coverage coverage, String code, DataField dataField) throws Exception {
712         try {
713             if (null != coverage) {
714                 SubField subField = new SubField();
715                 subField.setCode(code);
716                 subField.setValue(coverage.getCoverageEndDate());
717                 addDataField(dataField, subField);
718             }
719         } catch (Exception ex) {
720             logError(oleHoldings, ex, "generateCoverageEndDate()");
721         }
722     }
723 
724     /**
725      * generates the subfields for the Coverage Start Volume for the given oleHoldings
726      *
727      * @param oleHoldings
728      * @param code
729      * @param dataField
730      */
731     private void generateCoverageStartVolume(OleHoldings oleHoldings, Coverage coverage, String code, DataField dataField) throws Exception {
732         try {
733             if (null != coverage) {
734                 SubField subField = new SubField();
735                 subField.setCode(code);
736                 subField.setValue(coverage.getCoverageStartVolume());
737                 addDataField(dataField, subField);
738             }
739         } catch (Exception ex) {
740             logError(oleHoldings, ex, "generateCoverageStartVolume()");
741         }
742     }
743 
744     /**
745      * generates the subfields for the Coverage End Volume for the given oleHoldings
746      *
747      * @param oleHoldings
748      * @param code
749      * @param dataField
750      */
751     private void generateCoverageEndVolume(OleHoldings oleHoldings, Coverage coverage, String code, DataField dataField) throws Exception {
752         try {
753             if (null != coverage) {
754                 SubField subField = new SubField();
755                 subField.setCode(code);
756                 subField.setValue(coverage.getCoverageEndVolume());
757                 addDataField(dataField, subField);
758             }
759         } catch (Exception ex) {
760             logError(oleHoldings, ex, "generateCoverageEndVolume()");
761         }
762     }
763 
764     /**
765      * generates the subfields for the Coverage Start Issue for the given oleHoldings
766      *
767      * @param oleHoldings
768      * @param code
769      * @param dataField
770      */
771     private void generateCoverageStartIssue(OleHoldings oleHoldings, Coverage coverage, String code, DataField dataField) throws Exception {
772         try {
773             if (null != coverage) {
774                 SubField subField = new SubField();
775                 subField.setCode(code);
776                 subField.setValue(coverage.getCoverageStartIssue());
777                 addDataField(dataField, subField);
778             }
779         } catch (Exception ex) {
780             logError(oleHoldings, ex, "generateCoverageStartIssue()");
781         }
782     }
783 
784     /**
785      * generates the subfields for the Coverage End Issue for the given oleHoldings
786      *
787      * @param oleHoldings
788      * @param code
789      * @param dataField
790      */
791     private void generateCoverageEndIssue(OleHoldings oleHoldings, Coverage coverage, String code, DataField dataField) throws Exception {
792         try {
793             if (null != coverage) {
794                 SubField subField = new SubField();
795                 subField.setCode(code);
796                 subField.setValue(coverage.getCoverageEndIssue());
797                 addDataField(dataField, subField);
798             }
799         } catch (Exception ex) {
800             logError(oleHoldings, ex, "generateCoverageEndIssue()");
801         }
802     }
803 
804     /**
805      * generates the subfields for the Coverage End Issue for the given oleHoldings
806      *
807      * @param oleHoldings
808      * @param code
809      * @param dataField
810      */
811     private void generatePublicDisplayNote(OleHoldings oleHoldings, Note note, String code, DataField dataField) throws Exception {
812         try {
813             if (null != oleHoldings && null != note) {
814                 SubField subField = new SubField();
815                 subField.setCode(code);
816                 subField.setValue(note.getValue());
817                 addDataField(dataField, subField);
818             }
819         } catch (Exception ex) {
820             logError(oleHoldings, ex, "generatePublicDisplayNote()");
821         }
822     }
823 
824     /**
825      * @param errorString
826      */
827     private void buildError(String... errorString) {
828         for (String str : errorString) {
829             errBuilder.append(str).append(COMMA);
830         }
831         errBuilder.append(lineSeparator);
832     }
833 
834     /**
835      * Logs error for exception happening for oleHoldings mapping
836      *
837      * @param oleHoldings
838      * @param ex
839      */
840     private void logError(OleHoldings oleHoldings, Exception ex, String method) throws Exception {
841         if (oleHoldings != null) {
842             LOG.error("Error while " + method + " for eholding::" + oleHoldings.getHoldingsIdentifier(), ex);
843             buildError(ERR_HOLDING, oleHoldings.getHoldingsIdentifier(), ERR_CAUSE, ex.getMessage(), " ::At:: ", method, TIME_STAMP, new Date().toString());
844             throw ex;
845         } else {
846             LOG.error("Error while " + method + " for OleHoldings::" + oleHoldings.getHoldingsIdentifier(), ex);
847             throw ex;
848         }
849     }
850 }