001package org.kuali.ole;
002
003import com.thoughtworks.xstream.XStream;
004import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
005import org.kuali.ole.docstore.common.document.content.bib.marc.DataField;
006import org.kuali.ole.docstore.common.document.content.bib.marc.SubField;
007import org.kuali.ole.docstore.common.document.content.instance.CallNumber;
008import org.kuali.ole.docstore.common.document.content.instance.Note;
009import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
010import org.kuali.ole.pojo.ProfileAttribute;
011import java.util.*;
012
013
014/**
015 * Created by IntelliJ IDEA.
016 * User: pvsubrah
017 * Date: 4/10/12
018 * Time: 9:24 PM
019 * To change this template use File | Settings | File Templates.
020 */
021public class OleHoldingsRecordHandler {
022    Map<String, String> recordTypes = new HashMap<String, String>();
023    Map<String, String> encodingLevels = new HashMap<String, String>();
024    public static final String CALL_NUMBER_TYPE = "LC";
025
026    public OleHoldingsRecordHandler() {
027        recordTypes = new HashMap<String, String>();
028        recordTypes.put("u", "Unknown");
029        recordTypes.put("s", "Single-part item holdings");
030        recordTypes.put("y", "Serial item holdings");
031
032        encodingLevels.put("1", "Holdings level 1");
033        encodingLevels.put("2", "Holdings level 2");
034        encodingLevels.put("3", "Holdings level 3");
035        encodingLevels.put("4", "Holdings level 4");
036        encodingLevels.put("5", "Holdings level 5");
037        encodingLevels.put("m", "Mixed level");
038        encodingLevels.put("u", "Unknown");
039        encodingLevels.put("z", "Other level");
040    }
041
042    public String generateXML(List<ProfileAttribute> profileAttributes) {
043        OleHoldings oleHolding = getOleHoldings(profileAttributes);
044        return toXML(oleHolding);
045    }
046
047    public OleHoldings getOleHoldings(List<ProfileAttribute> profileAttributes) {
048        OleHoldings oleHolding = new OleHoldings();
049        /*oleHolding.setRecordType(resolveRecordType(getAttributeValue(profileAttributes, "recordType")));
050        oleHolding.setEncodingLevel(resolveEncodingLevel(getAttributeValue(profileAttributes, "encodingLevel")));
051        oleHolding.setReceiptStatus(getAttributeValue(profileAttributes, "recieptStatus"));
052        oleHolding.setAcquisitionMethod(getAttributeValue(profileAttributes, "acquisitionMethod"));
053        oleHolding.setReproductionPolicy(getAttributeValue(profileAttributes, "generalRetentionPolicy"));
054        SpecificRetentionPolicy specificRetentionPolicy = new SpecificRetentionPolicy();
055        specificRetentionPolicy.setPolicyType(getAttributeValue(profileAttributes, "policyType"));
056        oleHolding.setSpecificRetentionPolicy(specificRetentionPolicy);
057        oleHolding.setGeneralRetentionPolicy(getAttributeValue(profileAttributes, "generalRetentionPolicy"));*/
058
059        /*oleHolding.setReceiptStatus(getAttributeValue(profileAttributes, "receiptStatus"));
060
061        DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
062        Extension extension = new Extension();
063        AdditionalAttributes additionalAttributes = new AdditionalAttributes();
064        additionalAttributes.setDateEntered(String.valueOf(df.format(new Date())));
065        additionalAttributes.setLastUpdated(String.valueOf(df.format(new Date())));
066        additionalAttributes.setSupressFromPublic("false");
067        additionalAttributes.setFastAddFlag("false");
068        additionalAttributes.setHarvestable("true");
069        additionalAttributes.setStatus("n"); // new Record
070        additionalAttributes.setAttribute("dateEntered", additionalAttributes.getDateEntered());
071        additionalAttributes.setAttribute("lastUpdated", additionalAttributes.getLastUpdated());
072        additionalAttributes.setAttribute("fastAddFlag", additionalAttributes.getFastAddFlag());
073        additionalAttributes.setAttribute("supressFromPublic", additionalAttributes.getSupressFromPublic());
074        additionalAttributes.setAttribute("harvestable", additionalAttributes.getHarvestable());
075        additionalAttributes.setAttribute("status", additionalAttributes.getStatus());
076
077        extension.getContent().add(additionalAttributes);
078        oleHolding.setExtension(extension);*/
079        return oleHolding;
080    }
081
082    public OleHoldings getOleHoldings(BibMarcRecord bibMarcRecord, List<ProfileAttribute> profileAttributes) {
083        OleHoldings oleHolding = new OleHoldings();
084        /*oleHolding.setRecordType(resolveRecordType(getAttributeValue(profileAttributes, "recordType")));
085        oleHolding.setEncodingLevel(resolveEncodingLevel(getAttributeValue(profileAttributes, "encodingLevel")));
086        oleHolding.setReceiptStatus(getAttributeValue(profileAttributes, "recieptStatus"));
087        oleHolding.setAcquisitionMethod(getAttributeValue(profileAttributes, "acquisitionMethod"));
088        oleHolding.setReproductionPolicy(getAttributeValue(profileAttributes, "generalRetentionPolicy"));
089        SpecificRetentionPolicy specificRetentionPolicy = new SpecificRetentionPolicy();
090        specificRetentionPolicy.setPolicyType(getAttributeValue(profileAttributes, "policyType"));
091        oleHolding.setSpecificRetentionPolicy(specificRetentionPolicy);
092        oleHolding.setGeneralRetentionPolicy(getAttributeValue(profileAttributes, "generalRetentionPolicy"));*/
093
094        oleHolding.setReceiptStatus(getAttributeValue(profileAttributes, "receiptStatus"));
095
096        CallNumber callNumber = new CallNumber();
097        callNumber.setNumber(getSubFieldValueFor(bibMarcRecord, "050", "a"));
098        callNumber.setPrefix(getSubFieldValueFor(bibMarcRecord, "050", "b"));
099        callNumber.setType(getSubFieldValueFor(bibMarcRecord, "985", "q") == null ? CALL_NUMBER_TYPE : getSubFieldValueFor(bibMarcRecord, "985", "q"));
100
101        oleHolding.setCallNumber(callNumber);
102
103        Note note = new Note();
104        note.setValue(getSubFieldValueFor(bibMarcRecord, "856", "x"));
105        List<Note> notes = new ArrayList<Note>();
106        notes.add(note);
107        oleHolding.setNote(notes);
108
109        //oleHolding.setReceiptStatus(getSubFieldValueFor(bibliographicRecord, "856", "i"));
110/*
111        DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
112        Extension extension = new Extension();
113        AdditionalAttributes additionalAttributes = new AdditionalAttributes();
114        additionalAttributes.setDateEntered(String.valueOf(df.format(new Date())));
115        additionalAttributes.setLastUpdated(String.valueOf(df.format(new Date())));
116        additionalAttributes.setSupressFromPublic("false");
117        additionalAttributes.setFastAddFlag("false");
118        additionalAttributes.setHarvestable("true");
119        additionalAttributes.setStatus("n"); // new Record
120        additionalAttributes.setAttribute("dateEntered", additionalAttributes.getDateEntered());
121        additionalAttributes.setAttribute("lastUpdated", additionalAttributes.getLastUpdated());
122        additionalAttributes.setAttribute("fastAddFlag", additionalAttributes.getFastAddFlag());
123        additionalAttributes.setAttribute("supressFromPublic", additionalAttributes.getSupressFromPublic());
124        additionalAttributes.setAttribute("harvestable", additionalAttributes.getHarvestable());
125        additionalAttributes.setAttribute("status", additionalAttributes.getStatus());
126
127        extension.getContent().add(additionalAttributes);
128        oleHolding.setExtension(extension);*/
129        return oleHolding;
130    }
131
132    private String getSubFieldValueFor(BibMarcRecord bibMarcRecord, String dataField, String tag) {
133        String subFieldValue = null;
134        DataField dataFieldForTag = getDataFieldForTag(bibMarcRecord, dataField);
135        if (null != dataFieldForTag) {
136            List<SubField> subfields = dataFieldForTag.getSubFields();
137            for (Iterator<SubField> iterator = subfields.iterator(); iterator.hasNext(); ) {
138                SubField marcSubField = iterator.next();
139                if (marcSubField.getCode().equals(tag)) {
140                    return subFieldValue = marcSubField.getValue();
141                }
142            }
143        }
144        return subFieldValue;
145    }
146
147
148    public DataField getDataFieldForTag(BibMarcRecord bibMarcRecord, String tag) {
149        for (Iterator<DataField> iterator = bibMarcRecord.getDataFields().iterator(); iterator.hasNext(); ) {
150            DataField marcDataField = iterator.next();
151            if (marcDataField.getTag().equalsIgnoreCase(tag)) {
152                return marcDataField;
153            }
154        }
155        return null;
156    }
157
158    private String resolveEncodingLevel(String encodingLevel) {
159        return encodingLevels.get(encodingLevel);
160    }
161
162    private String resolveRecordType(String recordType) {
163        return recordTypes.get(recordType);
164    }
165
166    private String getAttributeValue(List<ProfileAttribute> profileAttributes, String attributeName) {
167        for (Iterator<ProfileAttribute> iterator = profileAttributes.iterator(); iterator.hasNext(); ) {
168            ProfileAttribute attribute = iterator.next();
169            if (attribute.getAttributeName().equals(attributeName)) {
170                return attribute.getAttributeValue();
171            }
172        }
173        return null;
174    }
175
176    public String toXML(OleHoldings oleHolding) {
177        XStream xs = new XStream();
178        xs.autodetectAnnotations(true);
179        xs.processAnnotations(OleHoldings.class);
180        String xml = xs.toXML(oleHolding);
181        return xml;
182    }
183
184}