1 package org.kuali.ole;
2
3 import com.thoughtworks.xstream.XStream;
4 import org.apache.commons.lang.StringUtils;
5 import org.kuali.ole.docstore.common.document.content.instance.*;
6 import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
7 import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
8 import org.kuali.ole.docstore.common.document.content.bib.marc.DataField;
9 import org.kuali.ole.docstore.common.document.content.bib.marc.SubField;
10 import org.kuali.ole.pojo.ProfileAttribute;
11
12 import org.kuali.ole.pojo.edi.LineItemOrder;
13
14 import java.text.DateFormat;
15 import java.text.SimpleDateFormat;
16 import java.util.ArrayList;
17 import java.util.Date;
18 import java.util.Iterator;
19 import java.util.List;
20
21
22
23
24
25
26
27
28
29
30
31 public class OleItemRecordHandler {
32
33 public static final String COPY_NUMBER = "1";
34 public static final String COPY_NUMBER_LABEL = "c";
35 public static final String CALL_NUMBER_TYPE = "LCC";
36 public static final String ITEM_STATUS = "AVAILABLE";
37 public static final String ITEM_TYPE = "NORMAL LOAN";
38
39 public String generateXML(BibMarcRecord bibliographicRecord, List<ProfileAttribute> profileAttributes) {
40 return toXML(getOleItem(bibliographicRecord, profileAttributes));
41 }
42
43 public Item getOleItem(BibMarcRecord bibMarcRecord, List<ProfileAttribute> profileAttributes) {
44
45
46 Item oleItem = new Item();
47
48 ItemType itemType = new ItemType();
49
50
51
52
53
54
55
56
57
58
59
60
61 itemType.setCodeValue(getSubFieldValueFor(bibMarcRecord, "985", "t") == null ? ITEM_TYPE : getSubFieldValueFor(bibMarcRecord, "985", "t"));
62 itemType.setFullValue(getSubFieldValueFor(bibMarcRecord, "985", "t") == null ? ITEM_TYPE : getSubFieldValueFor(bibMarcRecord, "985", "t"));
63 oleItem.setItemType(itemType);
64
65
66
67
68
69
70
71
72
73 oleItem.setCopyNumber(getSubFieldValueFor(bibMarcRecord, "985", "g") == null ? COPY_NUMBER : getSubFieldValueFor(bibMarcRecord, "985", "g"));
74 oleItem.setCopyNumberLabel(COPY_NUMBER_LABEL);
75
76 oleItem.setPurchaseOrderLineItemIdentifier("");
77 ItemStatus itemStatus = new ItemStatus();
78 String itemStatusValue = getSubFieldValueFor(bibMarcRecord, "985", "s") == null ? ITEM_STATUS : getSubFieldValueFor(bibMarcRecord, "985", "s");
79 itemStatus.setCodeValue(itemStatusValue);
80 itemStatus.setFullValue(itemStatusValue);
81 oleItem.setItemStatus(itemStatus);
82
83
84
85
86
87
88
89
90 DataField dataFieldForTag = getDataFieldForTag(bibMarcRecord, "852");
91 if (dataFieldForTag != null) {
92 String dataField = dataFieldForTag.getTag();
93 String valueFor = getSubFieldValueFor(bibMarcRecord, dataField, "$4");
94 if (null != valueFor) {
95
96 List<StatisticalSearchingCode> statisticalSearchingCodes = new ArrayList<StatisticalSearchingCode>();
97 StatisticalSearchingCode searchingCodes = new StatisticalSearchingCode();
98 searchingCodes.setCodeValue(valueFor);
99 searchingCodes.setFullValue(valueFor);
100 statisticalSearchingCodes.add(searchingCodes);
101 oleItem.setStatisticalSearchingCode(statisticalSearchingCodes);
102 }
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145 CallNumber callNumber = new CallNumber();
146
147
148
149
150 callNumber.setNumber(getSubFieldValueFor(bibMarcRecord, "050", "a"));
151 callNumber.setPrefix(getSubFieldValueFor(bibMarcRecord, "050", "b"));
152
153
154
155
156
157
158
159
160 callNumber.setType(getSubFieldValueFor(bibMarcRecord, "985", "q") == null ? CALL_NUMBER_TYPE : getSubFieldValueFor(bibMarcRecord, "985", "q"));
161 ShelvingScheme shelvingScheme = new ShelvingScheme();
162 shelvingScheme.setCodeValue(StringUtils.isBlank(getSubFieldValueFor(bibMarcRecord, "985", "q")) ? CALL_NUMBER_TYPE : getSubFieldValueFor(bibMarcRecord, "985", "q"));
163 callNumber.setShelvingScheme(shelvingScheme);
164
165 oleItem.setCallNumber(callNumber);
166
167 String locationLevelInstitution = getSubFieldValueFor(bibMarcRecord, "985", "a");
168 String locationLevelLibrary = getSubFieldValueFor(bibMarcRecord, "985", "c") == null ? "Jrl" : getSubFieldValueFor(bibMarcRecord, "985", "c");
169 String locationLevelShelving = getSubFieldValueFor(bibMarcRecord, "985", "d") == null ? "Gen" : getSubFieldValueFor(bibMarcRecord, "985", "d");
170
171
172
173
174
175
176 String locationLevelName = locationLevelInstitution + "/" + locationLevelLibrary + "/" + locationLevelShelving;
177 String locationLevelCode = "Institution" + "/" + "Library" + "/" + "Shelving Location";
178 LocationLevel locationLevel = new LocationLevel();
179 Location itemLocation = new Location();
180 itemLocation.setLocationLevel(setLocationLevels(locationLevel, locationLevelCode, locationLevelName));
181 itemLocation.setPrimary("true");
182 itemLocation.setStatus("temporary");
183 oleItem.setLocation(itemLocation);
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204 return oleItem;
205 }
206
207 public Item getOleItem(LineItemOrder lineItemOrder, BibMarcRecord bibMarcRecord, List<ProfileAttribute> profileAttributes) {
208
209 DataField dataFieldForTag = getDataFieldForTag(bibMarcRecord, "852");
210 String dataField = dataFieldForTag.getTag();
211
212 Item oleItem = new Item();
213 ItemType itemType = new ItemType();
214
215 itemType.setCodeValue(getSubFieldValueFor(bibMarcRecord, "985", "t") == null ? ITEM_TYPE : getSubFieldValueFor(bibMarcRecord, "985", "t"));
216 itemType.setFullValue(getSubFieldValueFor(bibMarcRecord, "985", "t") == null ? ITEM_TYPE : getSubFieldValueFor(bibMarcRecord, "985", "t"));
217 oleItem.setItemType(itemType);
218 if (null != lineItemOrder) {
219 oleItem.setVendorLineItemIdentifier(lineItemOrder.getBuyerReferenceInformation().get(0).getBuyerLineItemReference().get(0).getOrderLineNumber());
220 } else {
221 oleItem.setVendorLineItemIdentifier(null);
222 }
223
224 oleItem.setCopyNumber(getSubFieldValueFor(bibMarcRecord, "985", "g") == null ? COPY_NUMBER : getSubFieldValueFor(bibMarcRecord, "985", "g"));
225 oleItem.setCopyNumberLabel(COPY_NUMBER_LABEL);
226
227 oleItem.setPurchaseOrderLineItemIdentifier("");
228
229 ItemStatus itemStatus = new ItemStatus();
230 String itemStatusValue = getSubFieldValueFor(bibMarcRecord, "985", "s") == null ? ITEM_STATUS : getSubFieldValueFor(bibMarcRecord, "985", "s");
231 itemStatus.setCodeValue(itemStatusValue);
232 itemStatus.setFullValue(itemStatusValue);
233 oleItem.setItemStatus(itemStatus);
234
235 String valueFor = getSubFieldValueFor(bibMarcRecord, dataField, "$4");
236 if (null != valueFor) {
237 List<StatisticalSearchingCode> statisticalSearchingCodes = new ArrayList<StatisticalSearchingCode>();
238 StatisticalSearchingCode searchingCodes = new StatisticalSearchingCode();
239 searchingCodes.setCodeValue(valueFor);
240 searchingCodes.setFullValue(valueFor);
241 statisticalSearchingCodes.add(searchingCodes);
242 oleItem.setStatisticalSearchingCode(statisticalSearchingCodes);
243 }
244
245 CallNumber callNumber = new CallNumber();
246 callNumber.setNumber(getSubFieldValueFor(bibMarcRecord, "050", "a"));
247 callNumber.setPrefix(getSubFieldValueFor(bibMarcRecord, "050", "b"));
248
249 callNumber.setType(getSubFieldValueFor(bibMarcRecord, "985", "q") == null ? CALL_NUMBER_TYPE : getSubFieldValueFor(bibMarcRecord, "985", "q"));
250
251 oleItem.setCallNumber(callNumber);
252
253 String locationLevelInstitution = getSubFieldValueFor(bibMarcRecord, "985", "a");
254 String locationLevelLibrary = getSubFieldValueFor(bibMarcRecord, "985", "c") == null ? "Jrl" : getSubFieldValueFor(bibMarcRecord, "985", "c");
255 String locationLevelShelving = getSubFieldValueFor(bibMarcRecord, "985", "d") == null ? "Gen" : getSubFieldValueFor(bibMarcRecord, "985", "d");
256
257 String locationLevelName = locationLevelInstitution + "/" + locationLevelLibrary + "/" + locationLevelShelving;
258 String locationLevelCode = "Institution" + "/" + "Library" + "/" + "Shelving Location";
259 LocationLevel locationLevel = new LocationLevel();
260 Location itemLocation = new Location();
261 itemLocation.setLocationLevel(setLocationLevels(locationLevel, locationLevelCode, locationLevelName));
262 itemLocation.setPrimary("true");
263 itemLocation.setStatus("temporary");
264 oleItem.setLocation(itemLocation);
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285 return oleItem;
286 }
287
288 public LocationLevel setLocationLevels(LocationLevel locationLevel, String locationLevelCode, String locationLevelName) {
289
290 String[] levelNames = locationLevelName.split("/");
291 String[] levels = locationLevelCode.split("/");
292 locationLevel.setName(levelNames[0]);
293 locationLevel.setLevel(levels[0]);
294 String levlName = "";
295 String levl = "";
296 if (locationLevelName.contains("/") && locationLevelCode.contains("/")) {
297 levlName = locationLevelName.replace(levelNames[0] + "/", "");
298 levl = locationLevelCode.replace(levels[0] + "/", "");
299 } else {
300 levlName = locationLevelName.replace(levelNames[0], "");
301 levl = locationLevelCode.replace(levels[0], "");
302 }
303 if ((levlName != null && !levlName.equals("")) && (levl != null && !levl.equals(""))) {
304 LocationLevel newLocationLevel = new LocationLevel();
305 locationLevel.setLocationLevel(setLocationLevels(newLocationLevel, levl, levlName));
306 }
307 return locationLevel;
308 }
309
310 private String getAttributeValue(List<ProfileAttribute> profileAttributes, String attributeName) {
311 for (Iterator<ProfileAttribute> iterator = profileAttributes.iterator(); iterator.hasNext(); ) {
312 ProfileAttribute attribute = iterator.next();
313 if (attribute.getAttributeName().equals(attributeName)) {
314 return attribute.getAttributeValue();
315 }
316 }
317 return null;
318 }
319
320 private String getSubFieldValueFor(BibMarcRecord bibMarcRecord, String dataField, String tag) {
321 String subFieldValue = null;
322 DataField dataFieldForTag = getDataFieldForTag(bibMarcRecord, dataField);
323 if (null != dataFieldForTag) {
324 List<SubField> subfields = dataFieldForTag.getSubFields();
325 for (Iterator<SubField> iterator = subfields.iterator(); iterator.hasNext(); ) {
326 SubField marcSubField = iterator.next();
327 if (marcSubField.getCode().equals(tag)) {
328 return subFieldValue = marcSubField.getValue();
329 }
330 }
331 }
332 return subFieldValue;
333 }
334
335
336 public DataField getDataFieldForTag(BibMarcRecord bibMarcRecord, String tag) {
337 for (Iterator<DataField> iterator = bibMarcRecord.getDataFields().iterator(); iterator.hasNext(); ) {
338 DataField marcDataField = iterator.next();
339 if (marcDataField.getTag().equalsIgnoreCase(tag)) {
340 return marcDataField;
341 }
342 }
343 return null;
344 }
345
346
347 public String toXML(Item oleItem) {
348 XStream xs = new XStream();
349 xs.autodetectAnnotations(true);
350 xs.processAnnotations(Item.class);
351 String xml = xs.toXML(oleItem);
352 return xml;
353 }
354
355 public String getClassificationOrShelvingSchemeSource(BibMarcRecord bibMarcRecord) {
356 DataField dataFieldForTag = getDataFieldForTag(bibMarcRecord, "852");
357 if (dataFieldForTag.getInd1().equals("7")) {
358 List<SubField> subfields = dataFieldForTag.getSubFields();
359 for (Iterator<SubField> iterator = subfields.iterator(); iterator.hasNext(); ) {
360 SubField subField = iterator.next();
361 if (subField.getCode().equals("2")) {
362 return subField.getValue();
363 }
364 }
365 }
366
367 return "";
368 }
369 }