View Javadoc
1   package org.kuali.ole;
2   
3   import com.thoughtworks.xstream.XStream;
4   import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
5   import org.kuali.ole.docstore.common.document.content.bib.marc.DataField;
6   import org.kuali.ole.docstore.common.document.content.bib.marc.SubField;
7   import org.kuali.ole.docstore.common.document.content.instance.CallNumber;
8   import org.kuali.ole.docstore.common.document.content.instance.Note;
9   import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
10  import org.kuali.ole.pojo.ProfileAttribute;
11  import java.util.*;
12  
13  
14  /**
15   * Created by IntelliJ IDEA.
16   * User: pvsubrah
17   * Date: 4/10/12
18   * Time: 9:24 PM
19   * To change this template use File | Settings | File Templates.
20   */
21  public class OleHoldingsRecordHandler {
22      Map<String, String> recordTypes = new HashMap<String, String>();
23      Map<String, String> encodingLevels = new HashMap<String, String>();
24      public static final String CALL_NUMBER_TYPE = "LC";
25  
26      public OleHoldingsRecordHandler() {
27          recordTypes = new HashMap<String, String>();
28          recordTypes.put("u", "Unknown");
29          recordTypes.put("s", "Single-part item holdings");
30          recordTypes.put("y", "Serial item holdings");
31  
32          encodingLevels.put("1", "Holdings level 1");
33          encodingLevels.put("2", "Holdings level 2");
34          encodingLevels.put("3", "Holdings level 3");
35          encodingLevels.put("4", "Holdings level 4");
36          encodingLevels.put("5", "Holdings level 5");
37          encodingLevels.put("m", "Mixed level");
38          encodingLevels.put("u", "Unknown");
39          encodingLevels.put("z", "Other level");
40      }
41  
42      public String generateXML(List<ProfileAttribute> profileAttributes) {
43          OleHoldings oleHolding = getOleHoldings(profileAttributes);
44          return toXML(oleHolding);
45      }
46  
47      public OleHoldings getOleHoldings(List<ProfileAttribute> profileAttributes) {
48          OleHoldings oleHolding = new OleHoldings();
49          /*oleHolding.setRecordType(resolveRecordType(getAttributeValue(profileAttributes, "recordType")));
50          oleHolding.setEncodingLevel(resolveEncodingLevel(getAttributeValue(profileAttributes, "encodingLevel")));
51          oleHolding.setReceiptStatus(getAttributeValue(profileAttributes, "recieptStatus"));
52          oleHolding.setAcquisitionMethod(getAttributeValue(profileAttributes, "acquisitionMethod"));
53          oleHolding.setReproductionPolicy(getAttributeValue(profileAttributes, "generalRetentionPolicy"));
54          SpecificRetentionPolicy specificRetentionPolicy = new SpecificRetentionPolicy();
55          specificRetentionPolicy.setPolicyType(getAttributeValue(profileAttributes, "policyType"));
56          oleHolding.setSpecificRetentionPolicy(specificRetentionPolicy);
57          oleHolding.setGeneralRetentionPolicy(getAttributeValue(profileAttributes, "generalRetentionPolicy"));*/
58  
59          /*oleHolding.setReceiptStatus(getAttributeValue(profileAttributes, "receiptStatus"));
60  
61          DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
62          Extension extension = new Extension();
63          AdditionalAttributes additionalAttributes = new AdditionalAttributes();
64          additionalAttributes.setDateEntered(String.valueOf(df.format(new Date())));
65          additionalAttributes.setLastUpdated(String.valueOf(df.format(new Date())));
66          additionalAttributes.setSupressFromPublic("false");
67          additionalAttributes.setFastAddFlag("false");
68          additionalAttributes.setHarvestable("true");
69          additionalAttributes.setStatus("n"); // new Record
70          additionalAttributes.setAttribute("dateEntered", additionalAttributes.getDateEntered());
71          additionalAttributes.setAttribute("lastUpdated", additionalAttributes.getLastUpdated());
72          additionalAttributes.setAttribute("fastAddFlag", additionalAttributes.getFastAddFlag());
73          additionalAttributes.setAttribute("supressFromPublic", additionalAttributes.getSupressFromPublic());
74          additionalAttributes.setAttribute("harvestable", additionalAttributes.getHarvestable());
75          additionalAttributes.setAttribute("status", additionalAttributes.getStatus());
76  
77          extension.getContent().add(additionalAttributes);
78          oleHolding.setExtension(extension);*/
79          return oleHolding;
80      }
81  
82      public OleHoldings getOleHoldings(BibMarcRecord bibMarcRecord, List<ProfileAttribute> profileAttributes) {
83          OleHoldings oleHolding = new OleHoldings();
84          /*oleHolding.setRecordType(resolveRecordType(getAttributeValue(profileAttributes, "recordType")));
85          oleHolding.setEncodingLevel(resolveEncodingLevel(getAttributeValue(profileAttributes, "encodingLevel")));
86          oleHolding.setReceiptStatus(getAttributeValue(profileAttributes, "recieptStatus"));
87          oleHolding.setAcquisitionMethod(getAttributeValue(profileAttributes, "acquisitionMethod"));
88          oleHolding.setReproductionPolicy(getAttributeValue(profileAttributes, "generalRetentionPolicy"));
89          SpecificRetentionPolicy specificRetentionPolicy = new SpecificRetentionPolicy();
90          specificRetentionPolicy.setPolicyType(getAttributeValue(profileAttributes, "policyType"));
91          oleHolding.setSpecificRetentionPolicy(specificRetentionPolicy);
92          oleHolding.setGeneralRetentionPolicy(getAttributeValue(profileAttributes, "generalRetentionPolicy"));*/
93  
94          oleHolding.setReceiptStatus(getAttributeValue(profileAttributes, "receiptStatus"));
95  
96          CallNumber callNumber = new CallNumber();
97          callNumber.setNumber(getSubFieldValueFor(bibMarcRecord, "050", "a"));
98          callNumber.setPrefix(getSubFieldValueFor(bibMarcRecord, "050", "b"));
99          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 }