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.batch.impl.OLEBatchProcess;
9   import org.kuali.ole.docstore.common.document.Holdings;
10  import org.kuali.ole.docstore.common.document.HoldingsTree;
11  import org.kuali.ole.docstore.common.document.content.instance.*;
12  import org.kuali.ole.docstore.common.document.content.bib.marc.DataField;
13  import org.kuali.ole.docstore.common.document.content.bib.marc.SubField;
14  import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
15  import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
16  import org.springframework.util.CollectionUtils;
17  
18  import java.util.*;
19  
20  import static org.kuali.ole.OLEConstants.OLEBatchProcess.*;
21  import static org.kuali.ole.OLEConstants.OLEBatchProcess.ERR_ITEM;
22  
23  /**
24   * Created with IntelliJ IDEA.
25   * User: meenrajd
26   * Date: 9/2/13
27   * Time: 5:12 PM
28   * To change this template use File | Settings | File Templates.
29   */
30  public class InstanceMappingHelper {
31      private List<DataField> dataFieldList = new ArrayList<DataField>();
32      private String location3, location4, callNumber, callNumberType;
33      private StringBuilder errBuilder;
34      private ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
35      private HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
36  
37      private static final Logger LOG = Logger.getLogger(InstanceMappingHelper.class);
38  
39  
40      public List<DataField> generateDataFieldForHolding(HoldingsTree holdingsTree, OLEBatchProcessProfileBo profile, StringBuilder errBuilder) throws Exception {
41          dataFieldList.clear();
42          this.errBuilder = errBuilder;
43  
44          Item item = null;
45          if (holdingsTree != null) {
46              Map<String, String> dataFieldsHoldingsMap = new HashMap<>();
47              Map<String, String> dataFieldsItemsMap = new HashMap<>();
48              Map<String, String> dataFieldsDonorMap = new HashMap<>();
49              List<OLEBatchProcessProfileDataMappingOptionsBo> mappingOptionsBoList = profile.getOleBatchProcessProfileMappingOptionsList().get(0).getOleBatchProcessProfileDataMappingOptionsBoList();
50              for (OLEBatchProcessProfileDataMappingOptionsBo mappingOptionsBo : mappingOptionsBoList) {
51                  if (mappingOptionsBo.getDataType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_PROCESS_PROFILE_DATATYPE_HOLDINGS)) {
52                      dataFieldsHoldingsMap.put(mappingOptionsBo.getDestinationField(), mappingOptionsBo.getSourceField());
53                  } else if (mappingOptionsBo.getDataType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_PROCESS_PROFILE_DATATYPE_ITEM)) {
54                      if (mappingOptionsBo.getSourceField().equalsIgnoreCase(DESTINATION_FIELD_DONOR_PUBLIC_DISPLAY)
55                              || mappingOptionsBo.getSourceField().equalsIgnoreCase(DESTINATION_FIELD_DONOR_NOTE)
56                              || mappingOptionsBo.getSourceField().equalsIgnoreCase(DESTINATION_FIELD_DONOR_CODE)) {
57                          dataFieldsDonorMap.put(mappingOptionsBo.getDestinationField(), mappingOptionsBo.getSourceField());
58                      } else {
59                          dataFieldsItemsMap.put(mappingOptionsBo.getDestinationField(), mappingOptionsBo.getSourceField());
60                      }
61  
62                  }
63              }
64              if (!CollectionUtils.isEmpty(dataFieldsHoldingsMap)) {
65                  generateSubFieldsForHolding(holdingsTree.getHoldings(), dataFieldsHoldingsMap);
66              }
67              if (!CollectionUtils.isEmpty(dataFieldsItemsMap) || !CollectionUtils.isEmpty(dataFieldsDonorMap)) {
68                  for (org.kuali.ole.docstore.common.document.Item itemDoc : holdingsTree.getItems()) {
69  
70                      if (itemDoc == null) continue;
71  
72  
73                      boolean isStaffOnly = false;
74                      if (profile.getExportScope().equalsIgnoreCase(OLEBatchProcess.INCREMENTAL_EXPORT_EX_STAFF)) {
75                          if (itemDoc.isStaffOnly()) {
76                              isStaffOnly = true;
77                          }
78                      }
79  
80                      if (!isStaffOnly) {
81                          if (itemDoc.getContent() != null && !itemDoc.getContent().isEmpty()) {
82                              item = itemOlemlRecordProcessor.fromXML(itemDoc.getContent());
83                          } else {
84                              item = (Item) itemDoc.getContentObject();
85                          }
86                          List<DataField> dataFieldsItemList = generateSubFieldsForItem(holdingsTree.getHoldings(), item, dataFieldsItemsMap, dataFieldsDonorMap, new ArrayList<DataField>());
87                          if (!CollectionUtils.isEmpty(dataFieldsItemList))
88                              dataFieldList.addAll(dataFieldsItemList);
89                          dataFieldsItemList.clear();
90                      }
91                  }
92              }
93              if (CollectionUtils.isEmpty(dataFieldsHoldingsMap) && CollectionUtils.isEmpty(dataFieldsItemsMap) && CollectionUtils.isEmpty(dataFieldsDonorMap)) {
94                  return Collections.EMPTY_LIST;
95              }
96          }
97          return dataFieldList;
98      }
99  
100     protected void generateSubFieldsForHolding(Holdings holdingsDocument, Map<String, String> dataFieldsHoldingsMap) throws Exception {
101         OleHoldings holdings = null;
102         try {
103             if (holdingsDocument.getContentObject() != null) {
104                 holdings= holdingsDocument.getContentObject();
105             } else {
106                 holdings = holdingOlemlRecordProcessor.fromXML(holdingsDocument.getContent());
107             }
108             for (Map.Entry<String, String> entry : dataFieldsHoldingsMap.entrySet()) {
109                 DataField dataField;
110                 if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_CALL_NUMBER)) {
111                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
112                     if (dataField == null) {
113                         dataField = getDataField(entry);
114                         generateCallNumber(holdings, getCode(entry.getKey()), dataField);
115                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
116                     } else {
117                         generateCallNumber(holdings, getCode(entry.getKey()), dataField);
118                     }
119                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_HOLDING_CALL_NUMBER_TYPE)) {
120                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
121                     if (dataField == null) {
122                         dataField = getDataField(entry);
123                         generateCallNumberType(holdings, getCode(StringUtils.trim(entry.getKey())), dataField);
124                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
125                     } else {
126                         generateCallNumberType(holdings, getCode(StringUtils.trim(entry.getKey())), dataField);
127                     }
128                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_1)) {
129                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
130                     if (dataField == null) {
131                         dataField = getDataField(entry);
132                         generateLocationLevel1(holdings, getCode(entry.getKey()), dataField);
133                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
134                     } else {
135                         generateLocationLevel1(holdings, getCode(entry.getKey()), dataField);
136                     }
137                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_2)) {
138                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
139                     if (dataField == null) {
140                         dataField = getDataField(entry);
141                         generateLocationLevel2(holdings, getCode(entry.getKey()), dataField);
142                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
143                     } else {
144                         generateLocationLevel2(holdings, getCode(entry.getKey()), dataField);
145                     }
146                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_3)) {
147                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
148                     if (dataField == null) {
149                         dataField = getDataField(entry);
150                         generateLocationLevel3(holdings, getCode(entry.getKey()), dataField);
151                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
152                     } else {
153                         generateLocationLevel3(holdings, getCode(entry.getKey()), dataField);
154                     }
155                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_4)) {
156                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
157                     if (dataField == null) {
158                         dataField = getDataField(entry);
159                         generateLocationLevel4(holdings, getCode(entry.getKey()), dataField);
160                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
161                     } else {
162                         generateLocationLevel4(holdings, getCode(entry.getKey()), dataField);
163                     }
164                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_5)) {
165                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
166                     if (dataField == null) {
167                         dataField = getDataField(entry);
168                         generateLocationLevel5(holdings, getCode(entry.getKey()), dataField);
169                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
170                     } else {
171                         generateLocationLevel5(holdings, getCode(entry.getKey()), dataField);
172                     }
173                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_CALL_NUMBER_TYPE_PREFIX)) {
174                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
175                     if (dataField == null) {
176                         dataField = getDataField(entry);
177                         generateCallNumberPrefix(holdings, getCode(entry.getKey()), dataField);
178                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
179                     } else {
180                         generateCallNumberPrefix(holdings, getCode(entry.getKey()), dataField);
181                     }
182                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_COPY_NUMBER)) {
183                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
184                     if (dataField == null) {
185                         dataField = getDataField(entry);
186                         generateCopyNumber(holdings, getCode(entry.getKey()), dataField);
187                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
188                     } else {
189                         generateCopyNumber(holdings, getCode(entry.getKey()), dataField);
190                     }
191                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.SOURCE_FIELD_DATE_CREATED)) {
192                     dataField = checkDataField(dataFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
193                     if (dataField == null) {
194                         dataField = getDataField(entry);
195                         generateDateCreated(holdingsDocument, getCode(entry.getKey()), dataField);
196                         if (!dataField.getSubFields().isEmpty()) dataFieldList.add(dataField);
197                     } else {
198                         generateDateCreated(holdingsDocument, getCode(entry.getKey()), dataField);
199                     }
200                 }
201             }
202         } catch (Exception ex) {
203             LOG.error("Error while mapping instance data ::" + holdings.getHoldingsIdentifier(), ex);
204             buildError(ERR_INSTANCE, holdings.getHoldingsIdentifier(), ERR_CAUSE, ex.getMessage(), TIME_STAMP, new Date().toString());
205             throw ex;
206         }
207     }
208 
209     protected DataField checkDataField(List<DataField> dataFieldList, String dataField) {
210         for (DataField field : dataFieldList) {
211             if (dataField.equals(field.getTag())) {
212                 return field;
213             }
214         }
215         return null;
216     }
217 
218     protected DataField getDataField(Map.Entry<String, String> entry) {
219         DataField dataField = new DataField();
220         dataField.setTag(StringUtils.trim(entry.getKey()).substring(0, 3));
221         dataField.setInd1(" ");
222         dataField.setInd2(" ");
223         return dataField;
224     }
225 
226     protected List<DataField> generateSubFieldsForItem(Holdings holdingsDocument, Item item, Map<String, String> dataFieldsItemsMap, Map<String, String> dataFieldsDonorMap, List<DataField> dataFieldItemList) throws Exception {
227         OleHoldings holdings = null;
228         //List<DataField> donorFieldList = new ArrayList<>();
229         try {
230             if (holdingsDocument.getContent() != null && !holdingsDocument.getContent().isEmpty()) {
231                 holdings = holdingOlemlRecordProcessor.fromXML(holdingsDocument.getContent());
232             } else {
233                 holdings = holdingsDocument.getContentObject();
234             }
235             for (Map.Entry<String, String> entry : dataFieldsItemsMap.entrySet()) {
236                 DataField dataField;
237                 if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_CALL_NUMBER)) {
238                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
239                     if (item.getCallNumber() == null) continue;
240                     if (callNumber != null && StringUtils.isNotEmpty(item.getCallNumber().getNumber()) && item.getCallNumber().getNumber().equals(callNumber))
241                         continue;
242                     if (dataField == null) {
243                         dataField = getDataField(entry);
244                         generateCallNumber(item, getCode(entry.getKey()), dataField);
245                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
246                     } else {
247                         generateCallNumber(item, getCode(entry.getKey()), dataField);
248                     }
249                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_HOLDING_CALL_NUMBER_TYPE)) {
250                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
251                     if (item.getCallNumber() == null) continue;
252                     if (callNumberType != null && item.getCallNumber().getShelvingScheme() != null && StringUtils.isNotEmpty(item.getCallNumber().getShelvingScheme().getCodeValue()) && item.getCallNumber().getShelvingScheme().getCodeValue().equals(callNumberType))
253                         continue;
254                     if (dataField == null) {
255                         dataField = getDataField(entry);
256                         generateCallNumberType(item, getCode(entry.getKey()), dataField);
257                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
258                     } else {
259                         generateCallNumberType(item, getCode(entry.getKey()), dataField);
260                     }
261                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_COPY_NUMBER)) {
262                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
263                     if (dataField == null) {
264                         dataField = getDataField(entry);
265                         generateCopyNumber(item, getCode(entry.getKey()), dataField);
266                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
267                     } else {
268                         generateCopyNumber(item, getCode(entry.getKey()), dataField);
269                     }
270                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_ITEM_BARCODE)) {
271                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
272                     if (dataField == null) {
273                         dataField = getDataField(entry);
274                         generateItemNumber(item, getCode(entry.getKey()), dataField);
275                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
276                     } else {
277                         generateItemNumber(item, getCode(entry.getKey()), dataField);
278                     }
279                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.ITEM_TYPE)) {
280                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
281                     if (dataField == null) {
282                         dataField = getDataField(entry);
283                         generateItemType(item, getCode(entry.getKey()), dataField);
284                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
285                     } else {
286                         generateItemType(item, getCode(entry.getKey()), dataField);
287                     }
288                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_1)) {
289                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
290                     if (dataField == null) {
291                         dataField = getDataField(entry);
292                         generateLocationLevel1(item, getCode(entry.getKey()), dataField);
293                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
294                     } else {
295                         generateLocationLevel1(item, getCode(entry.getKey()), dataField);
296                     }
297                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_2)) {
298                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
299                     if (dataField == null) {
300                         dataField = getDataField(entry);
301                         generateLocationLevel2(item, getCode(entry.getKey()), dataField);
302                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
303                     } else {
304                         generateLocationLevel2(item, getCode(entry.getKey()), dataField);
305                     }
306                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_3)) {
307                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
308                     if (dataField == null) {
309                         dataField = getDataField(entry);
310                         generateLocationLevel3(item, getCode(entry.getKey()), dataField);
311                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
312                     } else {
313                         generateLocationLevel3(item, getCode(entry.getKey()), dataField);
314                     }
315                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_4)) {
316                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
317                     if (dataField == null) {
318                         dataField = getDataField(entry);
319                         generateLocationLevel4(item, getCode(entry.getKey()), dataField);
320                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
321                     } else {
322                         generateLocationLevel4(item, getCode(entry.getKey()), dataField);
323                     }
324                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_5)) {
325                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
326                     if (dataField == null) {
327                         dataField = getDataField(entry);
328                         generateLocationLevel5(item, getCode(entry.getKey()), dataField);
329                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
330                     } else {
331                         generateLocationLevel5(item, getCode(entry.getKey()), dataField);
332                     }
333                 } /*else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_DONOR_PUBLIC_DISPLAY)) {
334                     for (DonorInfo donorInfo : item.getDonorInfo()) {
335                         DataField dataField1 = getDataField(entry);
336                         generateDonorPublicDisplay(item, donorInfo, getCode(entry.getKey()), dataField1);
337                         if (!dataField1.getSubFields().isEmpty()) donorFieldList.add(dataField1);
338                     }
339                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_DONOR_NOTE)) {
340                     for (DonorInfo donorInfo : item.getDonorInfo()) {
341                         DataField dataField1 = getDataField(entry);
342                         generateDonorNote(item, donorInfo, getCode(entry.getKey()), dataField1);
343                         if (!dataField1.getSubFields().isEmpty()) donorFieldList.add(dataField1);
344                     }
345                 }*/ else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_CALL_NUMBER_TYPE_PREFIX)) {
346                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
347                     if (dataField == null) {
348                         dataField = getDataField(entry);
349                         generateCallNumberPrefix(item, getCode(entry.getKey()), dataField);
350                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
351                     } else {
352                         generateCallNumberPrefix(item, getCode(entry.getKey()), dataField);
353                     }
354                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_HOLDINGS_CALL_NUMBER)) {
355                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
356                     if (dataField == null) {
357                         dataField = getDataField(entry);
358                         generateCallNumber(holdings, getCode(entry.getKey()), dataField);
359                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
360                     } else {
361                         generateCallNumber(holdings, getCode(entry.getKey()), dataField);
362                     }
363                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_HOLDINGS_CALL_NUMBER_TYPE)) {
364                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
365                     if (dataField == null) {
366                         dataField = getDataField(entry);
367                         generateCallNumberType(holdings, getCode(entry.getKey()), dataField);
368                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
369                     } else {
370                         generateCallNumberType(holdings, getCode(entry.getKey()), dataField);
371                     }
372                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_HOLDINGS_COPY_NUMBER)) {
373                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
374                     if (dataField == null) {
375                         dataField = getDataField(entry);
376                         generateCopyNumber(holdings, getCode(entry.getKey()), dataField);
377                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
378                     } else {
379                         generateCopyNumber(holdings, getCode(entry.getKey()), dataField);
380                     }
381                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_HOLDINGS_CALL_NUMBER_PREFIX)) {
382                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
383                     if (dataField == null) {
384                         dataField = getDataField(entry);
385                         generateCallNumberPrefix(holdings, getCode(entry.getKey()), dataField);
386                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
387                     } else {
388                         generateCallNumberPrefix(holdings, getCode(entry.getKey()), dataField);
389                     }
390                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_1)) {
391                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
392                     if (dataField == null) {
393                         dataField = getDataField(entry);
394                         generateLocationLevel1(holdings, getCode(entry.getKey()), dataField);
395                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
396                     } else {
397                         generateLocationLevel1(holdings, getCode(entry.getKey()), dataField);
398                     }
399                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_2)) {
400                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
401                     if (dataField == null) {
402                         dataField = getDataField(entry);
403                         generateLocationLevel2(holdings, getCode(entry.getKey()), dataField);
404                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
405                     } else {
406                         generateLocationLevel2(holdings, getCode(entry.getKey()), dataField);
407                     }
408                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_3)) {
409                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
410                     if (dataField == null) {
411                         dataField = getDataField(entry);
412                         generateLocationLevel3(holdings, getCode(entry.getKey()), dataField);
413                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
414                     } else {
415                         generateLocationLevel3(holdings, getCode(entry.getKey()), dataField);
416                     }
417                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_4)) {
418                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
419                     if (dataField == null) {
420                         dataField = getDataField(entry);
421                         generateLocationLevel4(holdings, getCode(entry.getKey()), dataField);
422                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
423                     } else {
424                         generateLocationLevel4(holdings, getCode(entry.getKey()), dataField);
425                     }
426                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_5)) {
427                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
428                     if (dataField == null) {
429                         dataField = getDataField(entry);
430                         generateLocationLevel5(holdings, getCode(entry.getKey()), dataField);
431                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
432                     } else {
433                         generateLocationLevel5(holdings, getCode(entry.getKey()), dataField);
434                     }
435                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_STATUS)) {
436                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
437                     if (dataField == null) {
438                         dataField = getDataField(entry);
439                         generateItemStatus(item, getCode(entry.getKey()), dataField);
440                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
441                     } else {
442                         generateItemStatus(item, getCode(entry.getKey()), dataField);
443                     }
444                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_ENUMERATION)) {
445                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
446                     if (dataField == null) {
447                         dataField = getDataField(entry);
448                         generateEnumeration(item, getCode(entry.getKey()), dataField);
449                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
450                     } else {
451                         generateEnumeration(item, getCode(entry.getKey()), dataField);
452                     }
453                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_CHRONOLOGY)) {
454                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
455                     if (dataField == null) {
456                         dataField = getDataField(entry);
457                         generateChronology(item, getCode(entry.getKey()), dataField);
458                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
459                     } else {
460                         generateChronology(item, getCode(entry.getKey()), dataField);
461                     }
462                 } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_VENDOR_LINE_ITEM_IDENTIFIER)) {
463                     dataField = checkDataField(dataFieldItemList, StringUtils.trim(entry.getKey()).substring(0, 3));
464                     if (dataField == null) {
465                         dataField = getDataField(entry);
466                         generateVendorLineItemIdentifier(item, getCode(entry.getKey()), dataField);
467                         if (!dataField.getSubFields().isEmpty()) dataFieldItemList.add(dataField);
468                     } else {
469                         generateVendorLineItemIdentifier(item, getCode(entry.getKey()), dataField);
470                     }
471                 }
472             }
473             if (!CollectionUtils.isEmpty(dataFieldsDonorMap)) {
474                 //dataFieldItemList.addAll(donorFieldList);
475                 generateDonorFields(item, dataFieldsDonorMap);
476             }
477         } catch (Exception ex) {
478             LOG.error("Error while mapping item data ::" + item.getItemIdentifier(), ex);
479             buildError(ERR_INSTANCE, item.getItemIdentifier(), ERR_CAUSE, ex.getMessage(), TIME_STAMP, new Date().toString());
480             throw ex;
481         }
482         return dataFieldItemList;
483     }
484 
485     private void generateDonorFields(Item item, Map<String, String> dataFieldsDonorMap) {
486         List<DataField> donorFieldList = new ArrayList<>();
487         try {
488             if (!CollectionUtils.isEmpty(item.getDonorInfo())) {
489                 for (DonorInfo donorInfo : item.getDonorInfo()) {
490                     for (Map.Entry<String, String> entry : dataFieldsDonorMap.entrySet()) {
491                         DataField dataField;
492                         if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_DONOR_PUBLIC_DISPLAY)) {
493                             dataField = checkDataField(donorFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
494                             if (dataField == null) {
495                                 dataField = getDataField(entry);
496                                 generateDonorPublicDisplay(item, donorInfo, getCode(entry.getKey()), dataField);
497                                 if (!dataField.getSubFields().isEmpty()) donorFieldList.add(dataField);
498                             } else {
499                                 generateDonorPublicDisplay(item, donorInfo, getCode(entry.getKey()), dataField);
500                             }
501                         } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_DONOR_NOTE)) {
502                             dataField = checkDataField(donorFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
503                             if (dataField == null) {
504                                 dataField = getDataField(entry);
505                                 generateDonorNote(item, donorInfo, getCode(entry.getKey()), dataField);
506                                 if (!dataField.getSubFields().isEmpty()) donorFieldList.add(dataField);
507                             } else {
508                                 generateDonorNote(item, donorInfo, getCode(entry.getKey()), dataField);
509                             }
510                         } else if (entry.getValue().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_DONOR_CODE)) {
511                             dataField = checkDataField(donorFieldList, StringUtils.trim(entry.getKey()).substring(0, 3));
512                             if (dataField == null) {
513                                 dataField = getDataField(entry);
514                                 generateDonorCode(item, donorInfo, getCode(entry.getKey()), dataField);
515                                 if (!dataField.getSubFields().isEmpty()) donorFieldList.add(dataField);
516                             } else {
517                                 generateDonorCode(item, donorInfo, getCode(entry.getKey()), dataField);
518                             }
519                         }
520                     }
521                     dataFieldList.addAll(donorFieldList);
522                     donorFieldList.clear();
523                 }
524             }
525         } catch (Exception ex) {
526             LOG.error("Error while mapping item data ::" + item.getItemIdentifier(), ex);
527             buildError(ERR_INSTANCE, item.getItemIdentifier(), ERR_CAUSE, ex.getMessage(), TIME_STAMP, new Date().toString());
528             throw ex;
529         }
530     }
531 
532     /**
533      * generates the subfields for the given instance and mapping options
534      *
535      * @param mappingOptionsBoList
536      * @param instance
537      * @param dataField
538      */
539     protected void generateSubFields(List<OLEBatchProcessProfileDataMappingOptionsBo> mappingOptionsBoList, Instance instance, DataField dataField) throws Exception {
540         try {
541             for (OLEBatchProcessProfileDataMappingOptionsBo mappingField : mappingOptionsBoList) {
542                 if (!mappingField.getDataType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_PROCESS_PROFILE_DATATYPE_HOLDINGS)) continue;
543                 /*if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_CALL_NUMBER)) {
544                     generateCallNumber(instance, getCode(mappingField.getDestinationField()), dataField);
545                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_HOLDING_CALL_NUMBER_TYPE)) {
546                     generateCallNumberType(instance, getCode(mappingField.getDestinationField()), dataField);
547                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_1)) {
548                     generateLocationLevel1(instance, getCode(mappingField.getDestinationField()), dataField);
549                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_2)) {
550                     generateLocationLevel2(instance, getCode(mappingField.getDestinationField()), dataField);
551                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_3)) {
552                     generateLocationLevel3(instance, getCode(mappingField.getDestinationField()), dataField);
553                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_4)) {
554                     generateLocationLevel4(instance, getCode(mappingField.getDestinationField()), dataField);
555                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_5)) {
556                     generateLocationLevel5(instance, getCode(mappingField.getDestinationField()), dataField);
557                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_CALL_NUMBER_TYPE_PREFIX)) {
558                     generateCallNumberPrefix(instance, getCode(mappingField.getDestinationField()), dataField);
559                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_COPY_NUMBER)) {
560                     generateCopyNumber(instance, getCode(mappingField.getDestinationField()), dataField);
561                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.SOURCE_FIELD_DATE_CREATED)) {
562                     generateDateCreated(instance, getCode(mappingField.getDestinationField()), dataField);
563                 }*/
564             }
565         } catch (Exception ex) {
566             LOG.error("Error while mapping instance data ::" + instance.getInstanceIdentifier(), ex);
567             buildError(ERR_INSTANCE, instance.getInstanceIdentifier(), ERR_CAUSE, ex.getMessage(), TIME_STAMP, new Date().toString());
568             throw ex;
569         }
570     }
571 
572     protected void generateSubFields(List<OLEBatchProcessProfileDataMappingOptionsBo> mappingOptionsBoList, Instance instance, Item item, DataField dataField) throws Exception {
573         try {
574             for (OLEBatchProcessProfileDataMappingOptionsBo mappingField : mappingOptionsBoList) {
575                 if (!mappingField.getDataType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_PROCESS_PROFILE_DATATYPE_ITEM)) continue;
576                 if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_CALL_NUMBER)) {
577                     if (item.getCallNumber() == null) continue;
578                     if (callNumber != null && StringUtils.isNotEmpty(item.getCallNumber().getNumber()) && item.getCallNumber().getNumber().equals(callNumber))
579                         continue;
580                     generateCallNumber(item, getCode(mappingField.getDestinationField()), dataField);
581                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_HOLDING_CALL_NUMBER_TYPE)) {
582                     if (item.getCallNumber() == null) continue;
583                     if (callNumberType != null && item.getCallNumber().getShelvingScheme() != null && StringUtils.isNotEmpty(item.getCallNumber().getShelvingScheme().getCodeValue()) && item.getCallNumber().getShelvingScheme().getCodeValue().equals(callNumberType))
584                         continue;
585                     generateCallNumberType(item, getCode(mappingField.getDestinationField()), dataField);
586                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_COPY_NUMBER)) {
587                     generateCopyNumber(item, getCode(mappingField.getDestinationField()), dataField);
588                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_ITEM_BARCODE)) {
589                     generateItemNumber(item, getCode(mappingField.getDestinationField()), dataField);
590                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.ITEM_TYPE)) {
591                     generateItemType(item, getCode(mappingField.getDestinationField()), dataField);
592                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_1)) {
593                     generateLocationLevel1(item, getCode(mappingField.getDestinationField()), dataField);
594                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_2)) {
595                     generateLocationLevel2(item, getCode(mappingField.getDestinationField()), dataField);
596                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_3)) {
597                     generateLocationLevel3(item, getCode(mappingField.getDestinationField()), dataField);
598                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_4)) {
599                     generateLocationLevel4(item, getCode(mappingField.getDestinationField()), dataField);
600                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_LOCATION_LEVEL_5)) {
601                     generateLocationLevel5(item, getCode(mappingField.getDestinationField()), dataField);
602                 } /*else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_DONOR_PUBLIC_DISPLAY)) {
603                     getDonorPubDisp(item, getCode(mappingField.getDestinationField()), dataField);
604                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_DONOR_CODE)) {
605                     getDonorNote(item, getCode(mappingField.getDestinationField()), dataField);
606                 }*/ else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_CALL_NUMBER_TYPE_PREFIX)) {
607                     generateCallNumberPrefix(item, getCode(mappingField.getDestinationField()), dataField);
608                 } /*else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_HOLDINGS_CALL_NUMBER)) {
609                     generateCallNumber(instance, getCode(mappingField.getDestinationField()), dataField);
610                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_HOLDINGS_CALL_NUMBER_TYPE)) {
611                     generateCallNumberType(instance, getCode(mappingField.getDestinationField()), dataField);
612                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_HOLDINGS_COPY_NUMBER)) {
613                     generateCopyNumber(instance, getCode(mappingField.getDestinationField()), dataField);
614                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_1)) {
615                     generateLocationLevel1(instance, getCode(mappingField.getDestinationField()), dataField);
616                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_2)) {
617                     generateLocationLevel2(instance, getCode(mappingField.getDestinationField()), dataField);
618                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_3)) {
619                     generateLocationLevel3(instance, getCode(mappingField.getDestinationField()), dataField);
620                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_4)) {
621                     generateLocationLevel4(instance, getCode(mappingField.getDestinationField()), dataField);
622                 } else if (mappingField.getSourceField().equalsIgnoreCase(OLEConstants.OLEBatchProcess.DESTINATION_FIELD_ITEM_HOLDINGS_LOCATION_LEVEL_5)) {
623                     generateLocationLevel5(instance, getCode(mappingField.getDestinationField()), dataField);
624                 }*/
625             }
626         } catch (Exception ex) {
627             LOG.error("Error while mapping item data ::" + item.getItemIdentifier(), ex);
628             buildError(ERR_INSTANCE, item.getItemIdentifier(), ERR_CAUSE, ex.getMessage(), TIME_STAMP, new Date().toString());
629             throw ex;
630         }
631     }
632 
633     /**
634      * generates subfields for the call number for the given item
635      *
636      * @param item
637      * @param code
638      * @param dataField
639      */
640     private void generateCallNumber(Item item, String code, DataField dataField) {
641         try {
642             if (item != null && item.getCallNumber() != null && StringUtils.isNotEmpty(item.getCallNumber().getNumber())) {
643                 SubField subField = new SubField();
644                 subField.setCode(code);
645                 subField.setValue(item.getCallNumber().getNumber());
646                 addDataFieldForItem(dataField, subField);
647             }
648         } catch (Exception ex) {
649             logError(item, ex, "generateCallNumber()");
650         }
651 
652     }
653 
654     /**
655      * generates the subfields for the call number type for the given item
656      *
657      * @param item
658      * @param code
659      * @param dataField
660      */
661     private void generateCallNumberType(Item item, String code, DataField dataField) {
662         try {
663             if (item != null && item.getCallNumber() != null && item.getCallNumber().getShelvingScheme() != null && StringUtils.isNotEmpty(item.getCallNumber().getShelvingScheme().getCodeValue())) {
664                 SubField subField = new SubField();
665                 subField.setCode(code);
666                 subField.setValue(item.getCallNumber().getShelvingScheme().getCodeValue());
667                 addDataFieldForItem(dataField, subField);
668             }
669         } catch (Exception ex) {
670             logError(item, ex, "generateCallNumberType()");
671         }
672     }
673 
674     /**
675      * generates the subfields for call number for the given instance
676      *
677      * @param holdings
678      * @param code
679      * @param dataField
680      */
681     private void generateCallNumber(OleHoldings holdings, String code, DataField dataField) throws Exception {
682         SubField subField = new SubField();
683         subField.setCode(code);
684         try {
685             if (holdings != null && holdings.getCallNumber() != null && StringUtils.isNotEmpty(holdings.getCallNumber().getNumber())) {
686                 subField.setValue(holdings.getCallNumber().getNumber());
687                 callNumber = holdings.getCallNumber().getNumber();
688                 addDataField(dataField, subField);
689             }
690         } catch (Exception ex) {
691             logError(holdings, ex, "generateCallNumber()");
692         }
693 
694     }
695 
696     /**
697      * generates the call number prefix for the given instance
698      * @param holdings
699      * @param code
700      * @param dataField
701      * @throws Exception
702      */
703     private void generateCallNumberPrefix(OleHoldings holdings, String code, DataField dataField) throws Exception {
704         SubField subField = new SubField();
705         subField.setCode(code);
706         try {
707             if (holdings != null && holdings.getCallNumber() != null && StringUtils.isNotEmpty(holdings.getCallNumber().getPrefix())) {
708                 subField.setValue(holdings.getCallNumber().getPrefix());
709                 addDataField(dataField, subField);
710             }
711         } catch (Exception ex) {
712             logError(holdings, ex, "generateCallNumberPrefix()");
713         }
714 
715     }
716 
717     /**
718      * generates the copy number for the given instance
719      * @param holdings
720      * @param code
721      * @param dataField
722      * @throws Exception
723      */
724     private void generateCopyNumber(OleHoldings holdings, String code, DataField dataField) throws Exception {
725         SubField subField = new SubField();
726         subField.setCode(code);
727         try {
728             if (holdings != null && StringUtils.isNotEmpty(holdings.getCopyNumber())) {
729                 subField.setValue(holdings.getCopyNumber());
730                 addDataField(dataField, subField);
731             }
732         } catch (Exception ex) {
733             logError(holdings, ex, "generateCopyNumber()");
734         }
735 
736     }
737 
738     /**
739      * generates the call number prefix for the given item
740      * @param item
741      * @param code
742      * @param dataField
743      * @throws Exception
744      */
745     private void generateCallNumberPrefix(Item item, String code, DataField dataField) throws Exception {
746         SubField subField = new SubField();
747         subField.setCode(code);
748         try {
749             if (item != null && item.getCallNumber() != null && StringUtils.isNotEmpty(item.getCallNumber().getPrefix())) {
750                 subField.setValue(item.getCallNumber().getPrefix());
751                 addDataFieldForItem(dataField, subField);
752             }
753         } catch (Exception ex) {
754             logError(item, ex, "generateCallNumberPrefix()");
755         }
756 
757     }
758 
759     /**
760      * generates the subfield for the call number type for the given instance
761      *
762      * @param holdings
763      * @param code
764      * @param dataField
765      */
766     private void generateCallNumberType(OleHoldings holdings, String code, DataField dataField) throws Exception {
767         SubField subField = new SubField();
768         subField.setCode(code);
769         try {
770             if (holdings != null && holdings.getCallNumber() != null &&
771                     holdings.getCallNumber().getShelvingScheme() != null && StringUtils.isNotEmpty(holdings.getCallNumber().getShelvingScheme().getCodeValue())) {
772                 subField.setValue(holdings.getCallNumber().getShelvingScheme().getCodeValue());
773                 callNumberType = holdings.getCallNumber().getShelvingScheme().getCodeValue();
774                 addDataField(dataField, subField);
775             }
776         } catch (Exception ex) {
777             logError(holdings, ex, "generateCallNumberType()");
778         }
779     }
780 
781     /**
782      * creates the subfields for the given item
783      *
784      * @param item
785      * @param code
786      * @param dataField
787      */
788     private void generateCopyNumber(Item item, String code, DataField dataField) {
789         try {
790             if (item != null && StringUtils.isNotEmpty(item.getCopyNumber())) {
791                 SubField subField = new SubField();
792                 subField.setCode(code);
793                 subField.setValue(item.getCopyNumber());
794                 addDataFieldForItem(dataField, subField);
795             }
796         } catch (Exception ex) {
797             logError(item, ex, "generateCopyNumber()");
798         }
799     }
800 
801     private SubField generateMarcEnumeration(Item item, String code) {
802         SubField subField = new SubField();
803         subField.setCode(code);
804         subField.setValue("");
805         if (item != null && item.getEnumeration() != null) {
806             subField.setValue(item.getEnumeration());
807         }
808         return subField;
809     }
810 
811     /**
812      * generates the subfields for all item numbers for all items for the given instance
813      *
814      * @param instance
815      * @param code
816      * @param dataField
817      */
818     private void generateItemNumber(Instance instance, String code, DataField dataField) {
819         Items items = instance.getItems();
820         for (Item item : items.getItem()) {
821             generateItemNumber(item, code, dataField);
822         }
823     }
824 
825     /**
826      * generates the subfield for the given item
827      *
828      * @param item
829      * @param code
830      * @param dataField
831      */
832     private void generateItemNumber(Item item, String code, DataField dataField) {
833         try {
834             if (item != null && item.getAccessInformation() != null && item.getAccessInformation().getBarcode() != null) {
835                 SubField subField = new SubField();
836                 subField.setCode(code);
837                 subField.setValue(item.getAccessInformation().getBarcode());
838                 addDataFieldForItem(dataField, subField);
839             }
840         } catch (Exception ex) {
841             logError(item, ex, "generateItemNumber()");
842         }
843     }
844 
845     /**
846      * generates subfields for item type for the given item
847      *
848      * @param item
849      * @param code
850      * @param dataField
851      */
852     private void generateItemType(Item item, String code, DataField dataField) {
853         try {
854             if (item != null && item.getItemType() != null) {
855                 SubField subField = new SubField();
856                 subField.setCode(code);
857                 subField.setValue(item.getItemType().getCodeValue());
858                 addDataFieldForItem(dataField, subField);
859             }
860         } catch (Exception ex) {
861             logError(item, ex, "generateItemType()");
862         }
863     }
864 
865     /**
866      * generates subfields for item status for the given item
867      *
868      * @param item
869      * @param code
870      * @param dataField
871      */
872     private void generateItemStatus(Item item, String code, DataField dataField) {
873         try {
874             if (item != null && item.getItemStatus() != null) {
875                 SubField subField = new SubField();
876                 subField.setCode(code);
877                 subField.setValue(item.getItemStatus().getCodeValue());
878                 addDataFieldForItem(dataField, subField);
879             }
880         } catch (Exception ex) {
881             logError(item, ex, "generateItemStatus()");
882         }
883     }
884 
885     /**
886      * generates subfields for item type for all items in the given Instance
887      *
888      * @param instance
889      * @param code
890      * @param dataField
891      */
892     private void generateItemType(Instance instance, String code, DataField dataField) {
893         Items items = instance.getItems();
894         for (Item item : items.getItem()) {
895             generateItemType(item, code, dataField);
896         }
897     }
898 
899     /**
900      * generates the subfields for the location level -4 COLLECTION for the given instance
901      *
902      * @param holdings
903      * @param code
904      * @param dataField
905      */
906     private void generateLocationLevel4(OleHoldings holdings, String code, DataField dataField) throws Exception {
907         String locationLevelName;
908         try {
909             if (holdings != null && holdings.getLocation() != null
910                     && (locationLevelName = getLocationLevelName(holdings.getLocation().getLocationLevel(), "COLLECTION")) != null) {
911                 SubField subField = new SubField();
912                 subField.setCode(code);
913                 subField.setValue(locationLevelName);
914                 location4 = locationLevelName;
915                 addDataField(dataField, subField);
916             }
917         } catch (Exception ex) {
918             logError(holdings, ex, "generateLocationLevel4()");
919         }
920 
921     }
922 
923     /**
924      * generates the subfield for the location level 4 - COLLECTION for the given item
925      *
926      * @param item
927      * @param code
928      * @param dataField
929      */
930     private void generateLocationLevel4(Item item, String code, DataField dataField) {
931         String locationLevelName;
932         try {
933             if (item != null && item.getLocation() != null && (locationLevelName = getLocationLevelName(item.getLocation().getLocationLevel(), "COLLECTION")) != null) {
934                 //if (location4 != null && location4.equals(locationLevelName)) return;
935                 SubField subField = new SubField();
936                 subField.setCode(code);
937                 subField.setValue(locationLevelName);
938                 addDataFieldForItem(dataField, subField);
939             }
940         } catch (Exception ex) {
941             logError(item, ex, "generateLocationLevel4()");
942         }
943 
944     }
945 
946     /**
947      * gets the locationlevel name for the given level and locationlevel
948      *
949      * @param locationLevel
950      * @param level
951      * @return
952      */
953     private String getLocationLevelName(LocationLevel locationLevel, String level) {
954         if (locationLevel == null || StringUtils.isEmpty(locationLevel.getLevel())) return null;
955         if (locationLevel.getLevel().toUpperCase().startsWith(level)) return locationLevel.getName();
956         return getLocationLevelName(locationLevel.getLocationLevel(), level);
957     }
958 
959     /**
960      * generates subfield for location level 3 - LIBRARY for the given instance
961      *
962      * @param holdings
963      * @param code
964      * @param dataField
965      */
966     private void generateLocationLevel3(OleHoldings holdings, String code, DataField dataField) throws Exception {
967         String locationLevelName;
968         try {
969             if (holdings != null && holdings.getLocation() != null
970                     && (locationLevelName = getLocationLevelName(holdings.getLocation().getLocationLevel(), "LIBRARY")) != null) {
971                 SubField subField = new SubField();
972                 subField.setCode(code);
973                 subField.setValue(locationLevelName);
974                 location3 = locationLevelName;
975                 addDataField(dataField, subField);
976             }
977         } catch (Exception ex) {
978             logError(holdings, ex, "generateLocationLevel3()");
979         }
980     }
981 
982     /**
983      * generates subfield for location level 2 - CAMPUS for the given instance
984      *
985      * @param holdings
986      * @param code
987      * @param dataField
988      * @throws Exception
989      */
990     private void generateLocationLevel2(OleHoldings holdings, String code, DataField dataField) throws Exception {
991         String locationLevelName;
992         try {
993             if (holdings != null && holdings.getLocation() != null
994                     && (locationLevelName = getLocationLevelName(holdings.getLocation().getLocationLevel(), "CAMPUS")) != null) {
995                 SubField subField = new SubField();
996                 subField.setCode(code);
997                 subField.setValue(locationLevelName);
998                 addDataField(dataField, subField);
999             }
1000         } catch (Exception ex) {
1001             logError(holdings, ex, "generateLocationLevel2()");
1002         }
1003     }
1004 
1005     /**
1006      * generates subfield for location level 1 - INSTITUTION for the given instance
1007      *
1008      * @param holdings
1009      * @param code
1010      * @param dataField
1011      * @throws Exception
1012      */
1013     private void generateLocationLevel1(OleHoldings holdings, String code, DataField dataField) throws Exception {
1014         String locationLevelName;
1015         try {
1016             if (holdings != null && holdings.getLocation() != null
1017                     && (locationLevelName = getLocationLevelName(holdings.getLocation().getLocationLevel(), "INSTITUTION")) != null) {
1018                 SubField subField = new SubField();
1019                 subField.setCode(code);
1020                 subField.setValue(locationLevelName);
1021                 addDataField(dataField, subField);
1022             }
1023         } catch (Exception ex) {
1024             logError(holdings, ex, "generateLocationLevel1()");
1025         }
1026     }
1027 
1028     /**
1029      * generates subfield for location level 5 - SHELVING for the given instance
1030      *
1031      * @param holdings
1032      * @param code
1033      * @param dataField
1034      * @throws Exception
1035      */
1036     private void generateLocationLevel5(OleHoldings holdings, String code, DataField dataField) throws Exception {
1037         String locationLevelName;
1038         try {
1039             if (holdings != null && holdings.getLocation() != null
1040                     && (locationLevelName = getLocationLevelName(holdings.getLocation().getLocationLevel(), "SHELVING")) != null) {
1041                 SubField subField = new SubField();
1042                 subField.setCode(code);
1043                 subField.setValue(locationLevelName);
1044                 addDataField(dataField, subField);
1045             }
1046         } catch (Exception ex) {
1047             logError(holdings, ex, "generateLocationLevel5()");
1048         }
1049     }
1050 
1051     /**
1052      * generates the subfields for the location level 3 - LIBRARY for the given item
1053      *
1054      * @param item
1055      * @param code
1056      * @param dataField
1057      */
1058     private void generateLocationLevel3(Item item, String code, DataField dataField) {
1059         String locationLevelName;
1060         try {
1061             if (item != null && item.getLocation() != null && (locationLevelName = getLocationLevelName(item.getLocation().getLocationLevel(), "LIBRARY")) != null) {
1062                 //if (location3 != null && location3.equals(locationLevelName)) return;
1063                 SubField subField = new SubField();
1064                 subField.setCode(code);
1065                 subField.setValue(locationLevelName);
1066                 addDataFieldForItem(dataField, subField);
1067             }
1068         } catch (Exception ex) {
1069             logError(item, ex, "generateLocationLevel3()");
1070         }
1071     }
1072 
1073     /**
1074      * generates the subfields for the location level 2 - CAMPUS for the given item
1075      *
1076      * @param item
1077      * @param code
1078      * @param dataField
1079      */
1080     private void generateLocationLevel2(Item item, String code, DataField dataField) {
1081         String locationLevelName;
1082         try {
1083             if (item != null && item.getLocation() != null && (locationLevelName = getLocationLevelName(item.getLocation().getLocationLevel(), "CAMPUS")) != null) {
1084                 SubField subField = new SubField();
1085                 subField.setCode(code);
1086                 subField.setValue(locationLevelName);
1087                 addDataFieldForItem(dataField, subField);
1088             }
1089         } catch (Exception ex) {
1090             logError(item, ex, "generateLocationLevel2()");
1091         }
1092     }
1093 
1094     /**
1095      * generates the subfields for the location level 1 - INSTITUTION for the given item
1096      *
1097      * @param item
1098      * @param code
1099      * @param dataField
1100      */
1101     private void generateLocationLevel1(Item item, String code, DataField dataField) {
1102         String locationLevelName;
1103         try {
1104             if (item != null && item.getLocation() != null && (locationLevelName = getLocationLevelName(item.getLocation().getLocationLevel(), "INSTITUTION")) != null) {
1105                 SubField subField = new SubField();
1106                 subField.setCode(code);
1107                 subField.setValue(locationLevelName);
1108                 addDataFieldForItem(dataField, subField);
1109             }
1110         } catch (Exception ex) {
1111             logError(item, ex, "generateLocationLevel1()");
1112         }
1113     }
1114 
1115     /**
1116      * generates the subfields for the location level 5 - SHELVING for the given item
1117      *
1118      * @param item
1119      * @param code
1120      * @param dataField
1121      */
1122     private void generateLocationLevel5(Item item, String code, DataField dataField) {
1123         String locationLevelName;
1124         try {
1125             if (item != null && item.getLocation() != null && (locationLevelName = getLocationLevelName(item.getLocation().getLocationLevel(), "SHELVING")) != null) {
1126                 SubField subField = new SubField();
1127                 subField.setCode(code);
1128                 subField.setValue(locationLevelName);
1129                 addDataFieldForItem(dataField, subField);
1130             }
1131         } catch (Exception ex) {
1132             logError(item, ex, "generateLocationLevel5()");
1133         }
1134     }
1135 
1136     /**
1137      * generates the subfields for the Date Created for the given holdings
1138      *
1139      * @param holdings
1140      * @param code
1141      * @param dataField
1142      */
1143     private void generateDateCreated(Holdings holdings, String code, DataField dataField) throws Exception {
1144         SubField subField = new SubField();
1145         subField.setCode(code);
1146         try {
1147             if (null != holdings && null != holdings.getCreatedOn()) {
1148                 subField.setValue(holdings.getCreatedOn());
1149                 addDataField(dataField, subField);
1150             }
1151         } catch (Exception ex) {
1152             logError(holdings, ex, "generateDateCreated()");
1153         }
1154     }
1155 
1156     /**
1157      * generates the subfield for the item donor public display
1158      *
1159      * @param item
1160      * @param code
1161      * @param dataField
1162      */
1163     private void generateDonorPublicDisplay(Item item, DonorInfo donorInfo, String code, DataField dataField) {
1164         try {
1165             if (null != donorInfo) {
1166                 SubField subField = new SubField();
1167                 subField.setCode(code);
1168                 subField.setValue(donorInfo.getDonorPublicDisplay());
1169                 addDataFieldForItem(dataField, subField);
1170             }
1171         } catch (Exception ex) {
1172             logError(item, ex, "generateDonorPublicDisplay()");
1173         }
1174     }
1175 
1176     /**
1177      * generates the subfield for the item donor note
1178      *
1179      * @param item
1180      * @param code
1181      * @param dataField
1182      */
1183     private void generateDonorNote(Item item, DonorInfo donorInfo, String code, DataField dataField) {
1184         try {
1185             if (null != donorInfo) {
1186                 SubField subField = new SubField();
1187                 subField.setCode(code);
1188                 subField.setValue(donorInfo.getDonorNote());
1189                 addDataFieldForItem(dataField, subField);
1190             }
1191         } catch (Exception ex) {
1192             logError(item, ex, "generateDonorNote()");
1193         }
1194     }
1195 
1196     /**
1197      * generates the subfield for the item donor code
1198      *
1199      * @param item
1200      * @param code
1201      * @param dataField
1202      */
1203     private void generateDonorCode(Item item, DonorInfo donorInfo, String code, DataField dataField) {
1204         try {
1205             if (null != donorInfo) {
1206                 SubField subField = new SubField();
1207                 subField.setCode(code);
1208                 subField.setValue(donorInfo.getDonorCode());
1209                 addDataFieldForItem(dataField, subField);
1210             }
1211         } catch (Exception ex) {
1212             logError(item, ex, "generateDonorCode()");
1213         }
1214     }
1215 
1216     /**
1217      * generates subfield for Enumeration for the given item
1218      * @param item
1219      * @param code
1220      * @param dataField
1221      */
1222     private void generateEnumeration(Item item, String code, DataField dataField) {
1223         try {
1224             if (item != null && StringUtils.isNotEmpty(item.getEnumeration())) {
1225                 SubField subField = new SubField();
1226                 subField.setCode(code);
1227                 subField.setValue(item.getEnumeration());
1228                 addDataFieldForItem(dataField, subField);
1229             }
1230         } catch (Exception ex) {
1231             logError(item, ex, "generateEnumeration()");
1232         }
1233     }
1234 
1235     /**
1236      * generates subfield for Chronology for the given item
1237      * @param item
1238      * @param code
1239      * @param dataField
1240      */
1241     private void generateChronology(Item item, String code, DataField dataField) {
1242         try {
1243             if (item != null && StringUtils.isNotEmpty(item.getChronology())) {
1244                 SubField subField = new SubField();
1245                 subField.setCode(code);
1246                 subField.setValue(item.getChronology());
1247                 addDataFieldForItem(dataField, subField);
1248             }
1249         } catch (Exception ex) {
1250             logError(item, ex, "generateChronology()");
1251         }
1252     }
1253 
1254     /**
1255      * generates subfield for Vendor line item identifier for the given item
1256      * @param item
1257      * @param code
1258      * @param dataField
1259      */
1260     private void generateVendorLineItemIdentifier(Item item, String code, DataField dataField) {
1261         try {
1262             if (item != null && StringUtils.isNotEmpty(item.getVendorLineItemIdentifier())) {
1263                 SubField subField = new SubField();
1264                 subField.setCode(code);
1265                 subField.setValue(item.getVendorLineItemIdentifier());
1266                 addDataFieldForItem(dataField, subField);
1267             }
1268         } catch (Exception ex) {
1269             logError(item, ex, "generateVendorLineItemIdentifier()");
1270         }
1271     }
1272 
1273     /**
1274      * Logs error for exception happening for holdings or item mapping
1275      *
1276      * @param object
1277      * @param ex
1278      */
1279     private void logError(Object object, Exception ex, String method) {
1280         if (object != null) {
1281             if (object instanceof OleHoldings) {
1282                 OleHoldings oleHoldings = (OleHoldings) object;
1283                 LOG.error("Error while " + method + " for holding::" + oleHoldings.getHoldingsIdentifier(), ex);
1284                 buildError(ERR_HOLDING, oleHoldings.getHoldingsIdentifier(), ERR_CAUSE, ex.getMessage(), " ::At:: ", method, TIME_STAMP, new Date().toString());
1285                 try {
1286                     throw ex;
1287                 } catch (Exception e) {
1288                     e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
1289                 }
1290             } else if (object instanceof Holdings) {
1291                 Holdings holdings = (Holdings) object;
1292                 OleHoldings oleHoldings = null;
1293                 if (holdings.getContent() != null && !holdings.getContent().isEmpty()) {
1294                     oleHoldings = holdingOlemlRecordProcessor.fromXML(holdings.getContent());
1295                 } else {
1296                     oleHoldings = holdings.getContentObject();
1297                 }
1298                 LOG.error("Error while " + method + " for instance::" + oleHoldings.getHoldingsIdentifier(), ex);
1299                 buildError(ERR_HOLDING, oleHoldings.getHoldingsIdentifier(), ERR_CAUSE, ex.getMessage(), " ::At:: ", method, TIME_STAMP, new Date().toString());
1300                 // buildError(ERR_INSTANCE,instance.getInstanceIdentifier(),ERR_CAUSE,ex.getMessage()," ::At:: ",method,TIME_STAMP,new Date().toString());
1301                 try {
1302                     throw ex;
1303                 } catch (Exception e) {
1304                     e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
1305                 }
1306             } else if (object instanceof Item) {
1307                 Item item = (Item) object;
1308                 LOG.error("Error while " + method + " for item data ::" + item.getItemIdentifier(), ex);
1309                 buildError(ERR_ITEM, item.getItemIdentifier(), ERR_CAUSE, ex.getMessage(), " ::At:: ", method, TIME_STAMP, new Date().toString());
1310             }
1311         }
1312     }
1313 
1314     /*private void logError(Item item, Exception ex, String method) {
1315         LOG.error("Error while " + method + " for item data ::" + item.getItemIdentifier(), ex);
1316         buildError(ERR_ITEM, item.getItemIdentifier(), ERR_CAUSE, ex.getMessage(), " ::At:: ", method, TIME_STAMP, new Date().toString());
1317     }*/
1318 
1319     private void buildError(String... errorString) {
1320         for (String str : errorString) {
1321             errBuilder.append(str).append(COMMA);
1322         }
1323         errBuilder.append(lineSeparator);
1324     }
1325 
1326 
1327     private void addDataField(DataField dataField, SubField subField) {
1328         if (StringUtils.isEmpty(subField.getValue())) return;
1329         dataField.getSubFields().add(subField);
1330     }
1331 
1332     private void addDataFieldForItem(DataField dataField, SubField subField) {
1333         if (StringUtils.isEmpty(subField.getValue())) return;
1334         dataField.getSubFields().add(subField);
1335     }
1336 
1337     protected String getTag(String mappingField) {
1338         return StringUtils.trim(StringUtils.substringBefore(mappingField, "$"));
1339     }
1340 
1341     private String getCode(String mappingField) {
1342         return StringUtils.trim(StringUtils.substringAfter(mappingField, "$"));
1343     }
1344 
1345     public String getTagForExportFilter(String mappingField) {
1346         return StringUtils.trim(StringUtils.substringBefore(mappingField, " "));
1347     }
1348 
1349     public String getCodeForExportFilter(String mappingField) {
1350         int index = mappingField.indexOf('$');
1351         return String.valueOf(mappingField.charAt(index + 1));
1352     }
1353 }