View Javadoc

1   package org.kuali.ole.service.impl;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.BibliographicRecordHandler;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.OleItemRecordHandler;
7   import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
8   import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
9   import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
10  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.DataField;
11  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.SubField;
12  import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.*;
13  import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkHoldingOlemlRecordProcessor;
14  import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkInstanceOlemlRecordProcessor;
15  import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkItemOlemlRecordProcessor;
16  import org.kuali.ole.editor.service.DocstoreHelperService;
17  import org.kuali.ole.ingest.pojo.OleDataField;
18  import org.kuali.ole.ingest.pojo.OverlayOption;
19  import org.kuali.ole.pojo.bib.BibliographicRecord;
20  import org.kuali.ole.service.OverlayDataFieldService;
21  import org.kuali.ole.service.OverlayHelperService;
22  import org.kuali.ole.service.OverlayLookupTableService;
23  import org.kuali.ole.service.OverlayRetrivalService;
24  import org.kuali.ole.util.StringUtil;
25  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
26  
27  import java.util.*;
28  
29  /**
30   * Created with IntelliJ IDEA.
31   * User: ?
32   * Date: 11/29/12
33   * Time: 12:53 PM
34   * To change this template use File | Settings | File Templates.
35   */
36  public class OverlayHelperServiceImpl implements OverlayHelperService {
37  
38      private static final Logger LOG = Logger.getLogger(OverlayHelperServiceImpl.class);
39      private DocstoreHelperService docstoreHelperService;
40      private BibliographicRecordHandler bibliographicRecordHandler;
41      private OleItemRecordHandler oleItemRecordHandler;
42      private WorkInstanceOlemlRecordProcessor workInstanceOlemlRecordProcessor;
43      private WorkHoldingOlemlRecordProcessor workHoldingOlemlRecordProcessor;
44      private WorkItemOlemlRecordProcessor workItemOlemlRecordProcessor;
45      private OverlayRetrivalService overlayRetrivalService;
46      private OverlayDataFieldService overlayDataFieldService;
47      private OverlayLookupTableService overlayLookupTableService;
48      private List<String> gPFList;
49      private boolean isIgnoreGPF;
50  
51  
52      private String getResponseContent(Response response){
53          String responseString = null;
54          List<ResponseDocument> responseDocumentList = response.getDocuments();
55          for(ResponseDocument responseDocument : responseDocumentList){
56              Content contentObj = responseDocument.getContent();
57              responseString = contentObj.getContent();
58          }
59          return responseString;
60      }
61  
62      private InstanceCollection updateInstance(InstanceCollection oldInstanceCollection,InstanceCollection newInstanceCollection)throws Exception{
63          List<Instance> oldInstanceList = oldInstanceCollection.getInstance();
64          List<Instance> newInstanceList = newInstanceCollection.getInstance();
65          OleHoldings oldOleHoldings = null;
66          List<Item> oldItemList = null;
67          OleHoldings newOleHoldings = null;
68          List<Item> newItemList = null;
69          for(Instance oldInstance : oldInstanceList){
70              oldOleHoldings = oldInstance.getOleHoldings();
71              Items oldItems = oldInstance.getItems();
72              oldItemList = oldItems.getItem();
73          }
74          for(Instance newInstance : newInstanceList){
75              newOleHoldings = newInstance.getOleHoldings();
76              Items newItems = newInstance.getItems();
77              newItemList = newItems.getItem();
78          }
79          updateInstanceHolding(oldOleHoldings,newOleHoldings);
80          updateMatchedItem(oldItemList,newItemList);
81          return oldInstanceCollection;
82      }
83  
84      private void updateInstanceHolding(OleHoldings oldOleHoldings,OleHoldings newOleHoldings)throws Exception{
85  /*        if(newOleHoldings.getReceiptStatus()!=null){
86              oldOleHoldings.setReceiptStatus(overlayRetrivalService.getReceiptStatus());
87          }*/
88          if(newOleHoldings.getCallNumber()!=null){
89              oldOleHoldings.setCallNumber(newOleHoldings.getCallNumber());
90          }
91          if(newOleHoldings.getExtension()!=null){
92              oldOleHoldings.setExtension(newOleHoldings.getExtension());
93          }
94      }
95  
96      private void updateMatchedItem(List<Item> oldItemList,List<Item> newItemList)throws Exception{
97          List<Item> matchedItemList = new ArrayList<Item>();
98          List<Item> umMatchedItemList = new ArrayList<Item>();
99          for(Item oldItem : oldItemList){
100             for(Item newItem : newItemList){
101                 updateInstanceItem(oldItem,newItem);
102             }
103         }
104     }
105 
106     private void updateInstanceItem(Item oldItem,Item newItem)throws Exception{
107         if(newItem.getAccessInformation()!=null){
108             oldItem.getAccessInformation().setUri(newItem.getAccessInformation().getUri());
109         }
110         if(newItem.getBarcodeARSL()!=null){
111             oldItem.setBarcodeARSL(newItem.getBarcodeARSL());
112         }
113         if(newItem.getStatisticalSearchingCode()!=null){
114             oldItem.setStatisticalSearchingCode(newItem.getStatisticalSearchingCode());
115         }
116         if(newItem.getItemType()!=null){
117             oldItem.setItemType(newItem.getItemType());
118         }
119         if(newItem.getLocation()!=null){
120             oldItem.getLocation().setStatus(newItem.getLocation().getStatus());
121         }
122         if(newItem.getCallNumber()!=null){
123             oldItem.setCallNumber(newItem.getCallNumber());
124         }
125         if(newItem.getExtension()!=null){
126             oldItem.setExtension(newItem.getExtension());
127         }
128     }
129 
130     public String updateInstanceToDocstore(String instanceUUID,InstanceCollection oldInstanceCollection,InstanceCollection newInstanceCollection)throws Exception{
131         String response = null;
132         InstanceCollection updatedInstanceCollection = updateInstance(oldInstanceCollection, newInstanceCollection);
133         String instanceXMLString = getWorkInstanceOlemlRecordProcessor().toXML(updatedInstanceCollection);
134         OleHoldings oleHoldings = null;
135         List<Instance> instanceList = updatedInstanceCollection.getInstance();
136         for(Instance instance : instanceList){
137             oleHoldings = instance.getOleHoldings();
138             Items items = instance.getItems();
139             List<Item> itemList = items.getItem();
140             if(oleHoldings != null){
141                 response = getDocstoreHelperService().updateOleHoldingToDocstore(oleHoldings);
142             }
143             for(Item item : itemList){
144                 response = getDocstoreHelperService().updateOleItemToDocstore(item);
145             }
146         }
147 
148         //response = updateOleHoldingToDocstore(updatedInstanceCollection);
149         return response;
150 
151     }
152 
153     public String getUUID(Response response, String docType) throws Exception{
154         List<ResponseDocument> documents = response.getDocuments();
155         return getUUID(documents, docType);
156     }
157 
158     private String getUUID(List<ResponseDocument> documents, String docType)throws Exception{
159         String uuid = null;
160         for (Iterator<ResponseDocument> iterator = documents.iterator(); iterator.hasNext(); ) {
161             ResponseDocument responseDocument = iterator.next();
162             if (responseDocument.getType().equals(docType)) {
163                 uuid = responseDocument.getUuid();
164             } else {
165                 uuid = getUUID(responseDocument.getLinkedDocuments(), docType);
166             }
167         }
168         return uuid;
169     }
170 
171 
172     public BibliographicRecord updateBibliographicRecordIncludingGPF(BibliographicRecord oldBibliographicRecord, BibliographicRecord newBibliographicRecord,List<String> gpfFieldList,List<OverlayOption> overlayOptionList)throws Exception{
173         List<DataField> oldDatafields = oldBibliographicRecord.getDatafields();
174         List<DataField> newDatafields = newBibliographicRecord.getDatafields();
175         BibliographicRecord updatedBibliographicRecord = oldBibliographicRecord;
176         OverlayOption deleteOverlayOption = getOverlayRetrivalService().getDeleteOverlayOptionWithWildCardSearch(newDatafields, overlayOptionList);
177         HashMap<String,String> updationDataFieldMap = new HashMap<String,String>();
178         List<DataField> updatedDatafieldList = null;
179         updatedDatafieldList = addAllDataFieldsAndSubFields(oldDatafields, newDatafields);
180         oldDatafields = updatedDatafieldList != null?updatedDatafieldList:oldDatafields;
181         isIgnoreGPF = true;
182         updatedDatafieldList = deleteOverlayOptionDataField(oldDatafields,deleteOverlayOption);
183         updatedBibliographicRecord.setDatafields(updatedDatafieldList);
184         return updatedBibliographicRecord;
185     }
186 
187 
188     public BibliographicRecord updateBibliographicRecordExcludingGPF(BibliographicRecord oldBibliographicRecord, BibliographicRecord newBibliographicRecord,List<String> gpfFieldList,List<OverlayOption> overlayOptionList)throws Exception{
189         List<DataField> oldDatafields = oldBibliographicRecord.getDatafields();
190         List<DataField> newDatafields = newBibliographicRecord.getDatafields();
191         BibliographicRecord updatedBibliographicRecord = oldBibliographicRecord;
192         OverlayOption deleteOverlayOption = getOverlayRetrivalService().getDeleteOverlayOptionWithWildCardSearch(newDatafields, overlayOptionList);
193         LinkedHashMap<String,String> updationDataFieldMap = new LinkedHashMap<String,String>();
194         LinkedHashMap<String,String> gpfMap = getGPFMap(oldDatafields, gpfFieldList);
195         List<DataField> updatedDatafieldList = null;
196         isIgnoreGPF = false;
197         gPFList = gpfFieldList;
198         oldDatafields = excludeGPF(oldDatafields,gpfMap,true,updationDataFieldMap);
199         newDatafields = excludeGPF(newDatafields,gpfMap,false,updationDataFieldMap);
200         updatedDatafieldList = addAllDataFieldsAndSubFields(oldDatafields, newDatafields);
201         updatedDatafieldList = deleteOverlayOptionDataField(oldDatafields,deleteOverlayOption);
202         updatedBibliographicRecord.setDatafields(updatedDatafieldList);
203         //updateSubfieldValues(oldDatafields,newDatafields);
204         return updatedBibliographicRecord;
205     }
206 
207     private List<DataField> addAllDataFields(List<DataField> oldDatafields,List<DataField> newDatafields)throws Exception{
208         LinkedHashMap<String,DataField> oldDataFieldValueMap = getOverlayDataFieldService().getDataFieldValueMap(oldDatafields);
209         LinkedHashMap<String,DataField> newDataFieldValueMap = getOverlayDataFieldService().getDataFieldValueMap(newDatafields);
210         String updationField = null;
211         for(Map.Entry<String,DataField> newDataFieldEntry : newDataFieldValueMap.entrySet()){
212             updationField = newDataFieldEntry.getKey();
213             if(LOG.isInfoEnabled()){
214                 LOG.info("updationField------------->"+updationField);
215             }
216             if(oldDataFieldValueMap.containsKey(updationField)==false){
217                 oldDatafields.add(newDataFieldValueMap.get(updationField));
218             }
219         }
220         return oldDatafields;
221     }
222 
223     private List<DataField> addAllDataFieldsAndSubFields(List<DataField> oldDatafields,List<DataField> newDatafields)throws Exception{
224         LinkedHashMap<String,DataField> oldDataFieldMap = getOverlayDataFieldService().getDataFieldValueMap(oldDatafields);
225         LinkedHashMap<String,DataField> newDataFieldMap = getOverlayDataFieldService().getDataFieldValueMap(newDatafields);
226         LinkedHashMap<String,SubField> oleSubFieldMap = getOverlayDataFieldService().getSubFieldValueMap(oldDatafields);
227         LinkedHashMap<String,SubField> newSubFieldMap = getOverlayDataFieldService().getSubFieldValueMap(newDatafields);
228         LinkedHashMap<String,SubField> newUpdateSubFieldMap = new LinkedHashMap<String,SubField>();
229         String fieldKey = null;
230         String tempFieldKey = null;
231         int fieldKeyLength;
232         //for loop will add all the new datafields to the existing record from incoming record.
233         for(Map.Entry<String,DataField> newDataFieldEntry : newDataFieldMap.entrySet()){
234             fieldKey = newDataFieldEntry.getKey();
235             if(!oldDataFieldMap.containsKey(fieldKey)){
236                 oldDatafields.add(newDataFieldEntry.getValue());
237             }
238         }
239         //below for loops will add all the new subfields and update the existing subfield from incoming record.
240         for(Map.Entry<String,SubField> newSubFieldEntry : newSubFieldMap.entrySet()){
241             fieldKey = newSubFieldEntry.getKey();
242             if(oleSubFieldMap.containsKey(fieldKey)){
243                 SubField oldSubField = oleSubFieldMap.get(fieldKey);
244                 SubField newSubField = newSubFieldEntry.getValue();
245                 oldSubField.setValue(newSubField.getValue());
246             }else{
247 /*                fieldKeyLength = fieldKey.length();
248                 fieldKey = fieldKey.substring(0,fieldKeyLength-2);*/
249                 newUpdateSubFieldMap.put(fieldKey,newSubFieldEntry.getValue());
250             }
251 
252         }
253         for(Map.Entry<String,SubField> newUpdateSubField : newUpdateSubFieldMap.entrySet()){
254             fieldKey = newUpdateSubField.getKey();
255             fieldKeyLength = fieldKey.length();
256             tempFieldKey = fieldKey.substring(0,fieldKeyLength-2);
257             if(oldDataFieldMap.containsKey(tempFieldKey)){
258                 DataField dataField = oldDataFieldMap.get(tempFieldKey);
259                 List<SubField> subFieldList = dataField.getSubFields();
260                 subFieldList.add(newUpdateSubFieldMap.get(fieldKey));
261 
262             }
263         }
264         return oldDatafields;
265 
266     }
267 
268     private void updateSubfieldValues(List<DataField> oldDatafields,List<DataField> newDatafields)throws Exception{
269         LinkedHashMap<String,SubField> oldSubFieldValueMap = getOverlayDataFieldService().getSubFieldValueMap(oldDatafields);
270         LinkedHashMap<String,SubField> newSubFieldValueMap = getOverlayDataFieldService().getSubFieldValueMap(newDatafields);
271         String updationField = null;
272         for(Map.Entry<String,SubField> oleDataFieldEntry : oldSubFieldValueMap.entrySet()){
273             updationField = oleDataFieldEntry.getKey();
274             if(LOG.isInfoEnabled()){
275                 LOG.info("updationField------------->"+updationField);
276             }
277             if(newSubFieldValueMap.containsKey(updationField)){
278                 SubField oldSubField = oleDataFieldEntry.getValue();
279                 SubField newSubField = newSubFieldValueMap.get(updationField);
280                 oldSubField.setValue(newSubField.getValue());
281             }
282         }
283     }
284 
285     private List<DataField> excludeGPF(List<DataField> datafieldList,List<String> gpfFieldList,boolean isOldRecord)throws Exception{
286         String tagFieldValue = null;
287         String firstIndicatorValue = null;
288         String secondIndicatorValue = null;
289         String subFieldValue = null;
290         String[] gpfFieldArr = null;
291         List<DataField> updatedDatafieldList = new ArrayList<DataField>();
292         List<SubField> updatedSubfieldList = null;
293         SubField previousSubField = null;
294         if(gpfFieldList.size()>0){
295             for(DataField dataField : datafieldList){
296                 updatedSubfieldList = new ArrayList<SubField>();
297                 for(SubField subField : dataField.getSubFields()){
298                     for(String gpfField :gpfFieldList){
299                         gpfFieldArr = gpfField.split(OLEConstants.DELIMITER_DASH);
300                         tagFieldValue = gpfFieldArr[0];
301                         firstIndicatorValue = gpfFieldArr[1];
302                         secondIndicatorValue = gpfFieldArr[2];
303                         subFieldValue = gpfFieldArr[3];
304                         //if(!(dataField.getTag().equals(tagFieldValue) && !subField.getCode().equals(subFieldValue))){
305                         if(!((dataField.getTag()+subField.getCode()).equals(tagFieldValue+subFieldValue))){
306                             if(previousSubField != subField){
307                                 updatedSubfieldList.add(subField);//Add the non Globally Protected fields for update
308                             }
309                         }else if(((dataField.getTag()+subField.getCode()).equals(tagFieldValue+subFieldValue)) && isOldRecord){
310                             updatedSubfieldList.add(subField);//Add the Globally Protected fields to the list in the case of existing record to retain the existing value
311                         }
312                         previousSubField = subField;
313                     }
314                 }
315                 dataField.setSubFields(updatedSubfieldList);
316                 updatedDatafieldList.add(dataField);
317             }
318         }else{
319             updatedDatafieldList = datafieldList;
320         }
321         return updatedDatafieldList;
322     }
323 
324 /*    private List<DataField> excludeGPF(List<DataField> datafieldList,HashMap<String,String> gpfMap,boolean isOldRecord,HashMap<String,String> overlayOptionFieldMap)throws Exception{
325         String tagFieldValue = null;
326         String firstIndicator = null;
327         String secondIndicator = null;
328         String subFieldCode = null;
329         String[] gpfFieldArr = null;
330         String field = null;
331         boolean isGPF = false;
332         List<DataField> updatedDatafieldList = new ArrayList<DataField>();
333         List<SubField> updatedSubfieldList = null;
334         SubField previousSubField = null;
335         if(gpfMap!=null && gpfMap.size()>0 && !gpfMap.isEmpty()){
336             for(DataField dataField : datafieldList){
337                 updatedSubfieldList = new ArrayList<SubField>();
338                 for(SubField subField : dataField.getSubFields()){
339                     tagFieldValue = dataField.getTag();
340                     firstIndicator = dataField.getInd1()!=null?dataField.getInd1().trim():"";
341                     secondIndicator = dataField.getInd2()!=null?dataField.getInd2().trim():"";
342                     subFieldCode = subField.getCode().trim();
343                     field = tagFieldValue+OLEConstants.DELIMITER_DASH+firstIndicator+OLEConstants.DELIMITER_DASH+secondIndicator+OLEConstants.DELIMITER_DASH+subFieldCode;
344                     if(LOG.isInfoEnabled()){
345                         LOG.info("field------------>"+field);
346                         LOG.info("gpfMap field------------>"+gpfMap.get(field));
347                     }
348                     if(!gpfMap.containsKey(field)){
349                         updatedSubfieldList.add(subField);
350                     }else if(gpfMap.containsKey(field) && isOldRecord && !overlayOptionFieldMap.containsKey(field)){
351                         //Add the Globally Protected fields to the list in case of existing record to retain the existing value
352                         // and to exclude the overlay tag field which is also a globally protected field.
353                         updatedSubfieldList.add(subField);
354                     }
355                 }
356                 dataField.setSubFields(updatedSubfieldList);
357                 updatedDatafieldList.add(dataField);
358             }
359         }else{
360             updatedDatafieldList = datafieldList;
361         }
362         return updatedDatafieldList;
363     }*/
364 
365     private List<DataField> excludeGPF(List<DataField> datafieldList,LinkedHashMap<String,String> gpfMap,boolean isOldRecord,LinkedHashMap<String,String> overlayOptionFieldMap)throws Exception{
366         String tagFieldValue = null;
367         String firstIndicator = null;
368         String secondIndicator = null;
369         String subFieldCode = null;
370         String[] gpfFieldArr = null;
371         String field = null;
372         boolean isGPF = false;
373         List<DataField> updatedDatafieldList = new ArrayList<DataField>();
374         List<SubField> updatedSubfieldList = null;
375         SubField previousSubField = null;
376         if(gpfMap!=null && gpfMap.size()>0 && !gpfMap.isEmpty()){
377             for(DataField dataField : datafieldList){
378                 updatedSubfieldList = new ArrayList<SubField>();
379                 for(SubField subField : dataField.getSubFields()){
380                     tagFieldValue = dataField.getTag();
381                     firstIndicator = dataField.getInd1()!=null? StringUtil.trimHashNullValues(dataField.getInd1()):OLEConstants.DELIMITER_HASH;
382                     secondIndicator = dataField.getInd2()!=null?StringUtil.trimHashNullValues(dataField.getInd2()):OLEConstants.DELIMITER_HASH;
383                     subFieldCode = subField.getCode().trim();
384                     field = tagFieldValue+firstIndicator+secondIndicator+OLEConstants.DELIMITER_DOLLAR+subFieldCode;
385                     if(LOG.isInfoEnabled()){
386                         LOG.info("field------------>"+field);
387                         LOG.info("gpfMap field------------>"+gpfMap.get(field));
388                     }
389                     if(!gpfMap.containsKey(field)){
390                         updatedSubfieldList.add(subField);
391                     }else if(gpfMap.containsKey(field) && isOldRecord && !overlayOptionFieldMap.containsKey(field)){
392                         //Add the Globally Protected fields to the list in case of existing record to retain the existing value
393                         // and to exclude the overlay tag field which is also a globally protected field.
394                         updatedSubfieldList.add(subField);
395                     }
396                 }
397                 dataField.setSubFields(updatedSubfieldList);
398                 updatedDatafieldList.add(dataField);
399             }
400         }else{
401             updatedDatafieldList = datafieldList;
402         }
403         return updatedDatafieldList;
404     }
405 
406 /*    private HashMap<String,String> getGPFMap(List<DataField> oldDatafields, List<String> gpfFieldList)throws Exception{
407         HashMap<String,String> gpfMap = new HashMap<String,String>();
408         gpfFieldList = checkWildCardSearchForGPF(oldDatafields, gpfFieldList);
409         for(String gpfField :gpfFieldList){
410             if(LOG.isInfoEnabled()){
411                 LOG.info("gpfField in getGPFMap------------>" + gpfField);
412             }
413             gpfMap.put(gpfField,gpfField);
414         }
415         return gpfMap;
416     }*/
417 
418     private LinkedHashMap<String,String> getGPFMap(List<DataField> oldDatafields, List<String> gpfFieldList)throws Exception{
419         LinkedHashMap<String,String> gpfMap = new LinkedHashMap<String,String>();
420         gpfFieldList = checkWildCardSearchForGPF(oldDatafields, gpfFieldList);
421         for(String gpfField :gpfFieldList){
422             if(LOG.isInfoEnabled()){
423                 LOG.info("gpfField in getGPFMap------------>" + gpfField);
424             }
425             gpfMap.put(gpfField,gpfField);
426         }
427         return gpfMap;
428     }
429 
430     List<String> checkWildCardSearchForGPF(List<DataField> oldDatafields, List<String> gpfFieldList) {
431         List<String> gpfUpdatedList = new ArrayList<String>();
432         for(String gpfFields : gpfFieldList){
433             if(gpfFields.contains("*")){
434                 char[] gpfFieldSplit = gpfFields.toCharArray();
435                 for(DataField oldDataField : oldDatafields) {
436                     boolean oldDataFieldStartsWith = oldDataField.getTag().startsWith(String.valueOf(gpfFieldSplit[0]));
437                     if(oldDataFieldStartsWith){
438                         gpfUpdatedList.add(oldDataField.getTag());
439                     }
440                 }
441             }
442             else {
443                 gpfUpdatedList.add(gpfFields);
444             }
445         }
446         return gpfUpdatedList;
447     }
448 
449     private List<DataField> addOverlayOptionDataField(List<DataField> datafieldList,OverlayOption overlayOption,HashMap<String,String> updationDataFieldMap)throws Exception{
450         List<DataField> updatedDatafieldList = datafieldList;
451         String tagFieldValue = null;
452         String firstIndicatorValue = null;
453         String secondIndicatorValue = null;
454         String subFieldCode = null;
455         Boolean isUpdationField = false;
456         List<OleDataField> addOleDataFieldList = overlayOption.getOleDataFields();
457         DataField addDataField = null;
458         List<SubField> subFieldList = null;
459         if(addOleDataFieldList!=null && addOleDataFieldList.size()>0){
460             for(OleDataField oleDataField : addOleDataFieldList){
461                 tagFieldValue =  oleDataField.getDataFieldTag();
462                 firstIndicatorValue = oleDataField.getDataFieldInd1();
463                 secondIndicatorValue = oleDataField.getDataFieldInd2();
464                 subFieldCode = oleDataField.getSubFieldCode();
465                 if(!(addDataField!=null && addDataField.getTag().equals(tagFieldValue))){
466                     addDataField = new DataField();
467                     addDataField.setTag(tagFieldValue);
468                     addDataField.setInd1(firstIndicatorValue);
469                     addDataField.setInd2(secondIndicatorValue);
470                     subFieldList = new ArrayList<SubField>();
471                     SubField addSubField = new SubField();
472                     addSubField.setCode(subFieldCode);
473                     subFieldList.add(addSubField);
474                     addDataField.setSubFields(subFieldList);
475                     updationDataFieldMap.put(tagFieldValue+OLEConstants.DELIMITER_DASH+firstIndicatorValue+OLEConstants.DELIMITER_DASH+secondIndicatorValue+OLEConstants.DELIMITER_DASH+subFieldCode,
476                             OLEConstants.OVERLAY_OPTION_ADD);
477                 }else{
478                     SubField addSubField = new SubField();
479                     addSubField.setCode(subFieldCode);
480                     subFieldList.add(addSubField);
481                     addDataField.setSubFields(subFieldList);
482                     updationDataFieldMap.put(tagFieldValue+OLEConstants.DELIMITER_DASH+firstIndicatorValue+OLEConstants.DELIMITER_DASH+secondIndicatorValue+OLEConstants.DELIMITER_DASH+subFieldCode,
483                             OLEConstants.OVERLAY_OPTION_ADD);
484                 }
485                 updatedDatafieldList.add(addDataField);
486             }
487         }
488         return updatedDatafieldList;
489     }
490 
491 
492 
493     private List<DataField> deleteOverlayOptionDataField(List<DataField> datafieldList,OverlayOption overlayOption)throws Exception{
494         LinkedHashMap<String,SubField> oldSubFieldValueMap = getOverlayDataFieldService().getSubFieldValueMap(datafieldList);
495         LinkedHashMap<String,SubField> updatedSubFieldValueMap = new LinkedHashMap<String, SubField>();
496         List<DataField> updatedDatafieldList = new ArrayList<DataField>();
497         List<SubField> updatedSubfieldList = null;
498         SubField previousSubField = null;
499         String tagFieldValue = null;
500         String firstIndicator = null;
501         String secondIndicator = null;
502         String subFieldCode = null;
503         if(overlayOption != null && overlayOption.getOleDataFields()!=null){
504             List<OleDataField> deleteOleDataFieldList = overlayOption.getOleDataFields();
505             DataField addDataField = null;
506             List<String> keySBList = new ArrayList<String>();
507             for(OleDataField oleDataField : deleteOleDataFieldList){
508                 tagFieldValue =  oleDataField.getDataFieldTag();
509                 firstIndicator = StringUtil.trimHashNullValues(oleDataField.getDataFieldInd1());
510                 secondIndicator = StringUtil.trimHashNullValues(oleDataField.getDataFieldInd2());
511                 subFieldCode = oleDataField.getSubFieldCode();
512                 StringBuffer keySB = new StringBuffer();
513                 keySB.append(tagFieldValue).append(firstIndicator)
514                        .append(secondIndicator).append(OLEConstants.DELIMITER_DOLLAR).append(subFieldCode);
515                 if(isIgnoreGPF){
516                     keySBList.add(keySB.toString());
517                 }
518                 else if(!gPFList.contains(keySB.toString())) {
519                     keySBList.add(keySB.toString());
520                 }
521             }
522             for(Map.Entry<String,SubField> oldSubFieldValueMapEntry : oldSubFieldValueMap.entrySet()){
523                 if(!keySBList.contains(oldSubFieldValueMapEntry.getKey())){
524                     updatedSubFieldValueMap.put(oldSubFieldValueMapEntry.getKey(),oldSubFieldValueMapEntry.getValue());
525                 }
526             }
527 
528             for(Map.Entry<String,SubField> updatedSubFieldValueMapEntry : updatedSubFieldValueMap.entrySet()){
529                 String dataFieldTags = updatedSubFieldValueMapEntry.getKey();
530                 String tag=dataFieldTags.substring(0,3);
531                 String ind1 = dataFieldTags.substring(3,4);
532                 String ind2 = dataFieldTags.substring(4,5);
533                 boolean flag=false;
534                 for(DataField dataField1:updatedDatafieldList){
535                     if (dataField1.getInd1().equals(ind1)&&dataField1.getInd2().equals(ind2)&&dataField1.getTag().equals(tag)){
536                         SubField newsubField=new SubField();
537                         newsubField.setCode(dataFieldTags.substring(6,7));
538                         newsubField.setValue(updatedSubFieldValueMapEntry.getValue().getValue());
539                         dataField1.getSubFields().add(newsubField);
540                         flag=true;
541                     }
542                 }
543                 if(flag==false){
544                     DataField newDataField = new DataField();
545                     newDataField.setTag(tag);
546                     newDataField.setInd1(ind1);
547                     newDataField.setInd2(ind2);
548 
549                     SubField newsubField=new SubField();
550                     newsubField.setCode(dataFieldTags.substring(6,7));
551                     newsubField.setValue(updatedSubFieldValueMapEntry.getValue().getValue());
552                     List<SubField> subFieldList=new ArrayList<SubField>();
553                     subFieldList.add(newsubField);
554 
555                     newDataField.setSubFields(subFieldList);
556                     updatedDatafieldList.add(newDataField);
557                 }
558             }
559             return updatedDatafieldList;
560         }else{
561             return null;
562         }
563     }
564 
565     private DocstoreHelperService getDocstoreHelperService(){
566         if (docstoreHelperService == null) {
567             docstoreHelperService = GlobalResourceLoader.getService(OLEConstants.DOCSTORE_HELPER_SERVICE);
568         }
569         return docstoreHelperService;
570     }
571 
572     public void setDocstoreHelperService(DocstoreHelperService docstoreHelperService) {
573         this.docstoreHelperService = docstoreHelperService;
574     }
575 
576     public OverlayRetrivalService getOverlayRetrivalService() {
577         if(overlayRetrivalService == null){
578             overlayRetrivalService = GlobalResourceLoader.getService(OLEConstants.OVERLAY_RETRIVAL_SERVICE);
579         }
580         return overlayRetrivalService;
581     }
582 
583     public void setOverlayRetrivalService(OverlayRetrivalService overlayRetrivalService) {
584         this.overlayRetrivalService = overlayRetrivalService;
585     }
586 
587     public OverlayDataFieldService getOverlayDataFieldService() {
588         if(overlayDataFieldService == null){
589             overlayDataFieldService = GlobalResourceLoader.getService(OLEConstants.OVERLAY_DATAFIELD_SERVICE);
590         }
591         return overlayDataFieldService;
592     }
593 
594     public void setOverlayDataFieldService(OverlayDataFieldService overlayDataFieldService) {
595         this.overlayDataFieldService = overlayDataFieldService;
596     }
597 
598     public OverlayLookupTableService getOverlayLookupTableService() {
599         if(overlayLookupTableService == null){
600             overlayLookupTableService = GlobalResourceLoader.getService(OLEConstants.OVERLAY_LOOKUPTABLE_SERVICE);
601         }
602         return overlayLookupTableService;
603     }
604 
605     public void setOverlayLookupTableService(OverlayLookupTableService overlayLookupTableService) {
606         this.overlayLookupTableService = overlayLookupTableService;
607     }
608 
609     private BibliographicRecordHandler getBibliographicRecordHandler(){
610         if(bibliographicRecordHandler == null){
611             bibliographicRecordHandler = new BibliographicRecordHandler();
612         }
613         return bibliographicRecordHandler;
614     }
615 
616     private WorkInstanceOlemlRecordProcessor getWorkInstanceOlemlRecordProcessor() {
617         if(workInstanceOlemlRecordProcessor == null){
618             workInstanceOlemlRecordProcessor = new WorkInstanceOlemlRecordProcessor();
619         }
620         return workInstanceOlemlRecordProcessor;
621     }
622 
623     private OleItemRecordHandler getOleItemRecordHandler() {
624         if(oleItemRecordHandler == null){
625             oleItemRecordHandler = new OleItemRecordHandler();
626         }
627         return oleItemRecordHandler;
628     }
629 
630     public WorkHoldingOlemlRecordProcessor getWorkHoldingOlemlRecordProcessor() {
631         if(workHoldingOlemlRecordProcessor == null){
632             workHoldingOlemlRecordProcessor = new WorkHoldingOlemlRecordProcessor();
633         }
634         return workHoldingOlemlRecordProcessor;
635     }
636 
637     public WorkItemOlemlRecordProcessor getWorkItemOlemlRecordProcessor() {
638         if(workItemOlemlRecordProcessor == null){
639             workItemOlemlRecordProcessor = new WorkItemOlemlRecordProcessor();
640         }
641         return workItemOlemlRecordProcessor;
642     }
643 
644 }