001package org.kuali.ole.service.impl;
002
003import org.apache.log4j.Logger;
004import org.kuali.ole.OLEConstants;
005import org.kuali.ole.service.OverlayDataFieldService;
006import org.kuali.ole.util.StringUtil;
007
008import java.util.HashMap;
009import java.util.LinkedHashMap;
010import java.util.List;
011import java.util.TreeMap;
012
013import org.kuali.ole.docstore.common.document.content.bib.marc.DataField;
014import org.kuali.ole.docstore.common.document.content.bib.marc.SubField;
015
016/**
017 * Created by IntelliJ IDEA.
018 * User: premkb
019 * Date: 2/3/13
020 * Time: 6:17 AM
021 * To change this template use File | Settings | File Templates.
022 */
023public class OverlayDataFieldServiceImpl implements OverlayDataFieldService{
024
025    private static final Logger LOG = Logger.getLogger(OverlayDataFieldServiceImpl.class);
026
027    public LinkedHashMap<String,DataField> getDataFieldValueMap(List<DataField> dataFieldList)throws Exception{
028        LinkedHashMap<String,DataField> dataFieldValueMap = new LinkedHashMap<String,DataField>();
029        if(dataFieldList != null){
030            for(DataField dataField : dataFieldList){
031                dataFieldValueMap.put(getTagKey(dataField),dataField);
032            }
033        }
034        return dataFieldValueMap;
035    }
036
037    public LinkedHashMap<String,SubField> getSubFieldValueMap(List<DataField> dataFieldList)throws Exception{
038        LinkedHashMap<String,SubField> subFieldValueMap = new LinkedHashMap<String,SubField>();
039        String ind1 = null;
040        String ind2 = null;
041        StringBuffer key = new StringBuffer();
042        if(dataFieldList != null){
043            for(DataField dataField : dataFieldList){
044                List<SubField> subFieldList = dataField.getSubFields();
045                for(SubField subField : subFieldList){
046                    key.append(getTagKey(dataField));
047                    key.append(OLEConstants.DELIMITER_DOLLAR).append(subField.getCode());
048                    subFieldValueMap.put(key.toString(),subField);
049                    if(LOG.isDebugEnabled()){
050                        LOG.debug("field key---------->"+key.toString());
051                        LOG.debug("value-------------->"+subField.getValue());
052                    }
053                    key = new StringBuffer();
054                }
055            }
056        }
057        return subFieldValueMap;
058    }
059
060    public HashMap<String,HashMap<SubField,DataField>> getDataFieldValueSubFieldMap(List<DataField> dataFieldList){
061        HashMap<String,HashMap<SubField,DataField>> dataFieldSubFieldValueMap = new HashMap<String,HashMap<SubField,DataField>>();
062        String tagKey = null;
063        if(dataFieldList != null){
064            for(DataField dataField : dataFieldList){
065                tagKey =getTagKey(dataField);
066                for(SubField subField : dataField.getSubFields()){
067                    tagKey = tagKey+StringUtil.trimEmptyNullValues(subField.getCode());
068                    HashMap dataFieldSubFieldMap = new HashMap<SubField,DataField>();
069                    dataFieldSubFieldMap.put(subField,dataField);
070                    dataFieldSubFieldValueMap.put(tagKey,dataFieldSubFieldMap);
071                }
072            }
073        }
074        return dataFieldSubFieldValueMap;
075    }
076
077    public String getTagKey(DataField dataField){
078        StringBuffer keySB = new StringBuffer();
079        String ind1 = dataField.getInd1()!=null? StringUtil.trimHashNullValues(dataField.getInd1()):OLEConstants.DELIMITER_HASH;
080        String ind2 = dataField.getInd2()!=null? StringUtil.trimHashNullValues(dataField.getInd2()):OLEConstants.DELIMITER_HASH;
081        keySB.append(dataField.getTag()).append(ind1).append(ind2);
082        return keySB.toString();
083    }
084}