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
16
17
18
19
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 return oleHolding;
80 }
81
82 public OleHoldings getOleHoldings(BibMarcRecord bibMarcRecord, List<ProfileAttribute> profileAttributes) {
83 OleHoldings oleHolding = new OleHoldings();
84
85
86
87
88
89
90
91
92
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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 }