1 package org.kuali.ole;
2
3 import com.thoughtworks.xstream.XStream;
4 import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
5 import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.DataField;
6 import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.SubField;
7 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.*;
8 import org.kuali.ole.pojo.ProfileAttribute;
9 import org.kuali.ole.pojo.bib.BibliographicRecord;
10
11 import java.text.DateFormat;
12 import java.text.SimpleDateFormat;
13 import java.util.ArrayList;
14 import java.util.Date;
15 import java.util.Iterator;
16 import java.util.List;
17
18
19
20
21
22
23
24
25
26
27
28 public class OleItemRecordHandler {
29
30 public static final String COPY_NUMBER = "1";
31 public static final String COPY_NUMBER_LABEL = "c";
32
33 public String generateXML(BibliographicRecord bibliographicRecord, List<ProfileAttribute> profileAttributes) {
34 return toXML(getOleItem(bibliographicRecord, profileAttributes));
35 }
36
37 public Item getOleItem(BibliographicRecord bibliographicRecord, List<ProfileAttribute> profileAttributes ){
38
39 DataField dataFieldForTag = getDataFieldForTag(bibliographicRecord, "852");
40 String dataField = dataFieldForTag.getTag();
41
42 Item oleItem = new Item();
43
44 ItemType itemType= new ItemType();
45 itemType.setCodeValue(getSubFieldValueFor(bibliographicRecord, dataField, "$5"));
46 itemType.setFullValue(getSubFieldValueFor(bibliographicRecord, dataField, "$5"));
47 oleItem.setItemType(itemType);
48 oleItem.setCopyNumber(COPY_NUMBER);
49 oleItem.setCopyNumberLabel(COPY_NUMBER_LABEL);
50
51 oleItem.setPurchaseOrderLineItemIdentifier("");
52
53 String valueFor = getSubFieldValueFor(bibliographicRecord, dataField, "$4");
54 if (null != valueFor) {
55
56 List<StatisticalSearchingCode> statisticalSearchingCodes = new ArrayList<StatisticalSearchingCode>();
57 StatisticalSearchingCode searchingCodes = new StatisticalSearchingCode();
58 searchingCodes.setCodeValue(valueFor);
59 searchingCodes.setFullValue(valueFor);
60 statisticalSearchingCodes.add(searchingCodes);
61 oleItem.setStatisticalSearchingCode(statisticalSearchingCodes);
62 }
63 oleItem.setCopyNumber(COPY_NUMBER);
64 oleItem.setCopyNumberLabel(COPY_NUMBER_LABEL);
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
104 Extension extension = new Extension();
105 AdditionalAttributes additionalAttributes = new AdditionalAttributes();
106 additionalAttributes.setDateEntered(String.valueOf(df.format(new Date())));
107 additionalAttributes.setLastUpdated(String.valueOf(df.format(new Date())));
108 additionalAttributes.setSupressFromPublic("false");
109 additionalAttributes.setFastAddFlag("false");
110 additionalAttributes.setHarvestable("true");
111 additionalAttributes.setStatus("n");
112 additionalAttributes.setAttribute("dateEntered",additionalAttributes.getDateEntered());
113 additionalAttributes.setAttribute("lastUpdated",additionalAttributes.getLastUpdated());
114 additionalAttributes.setAttribute("fastAddFlag",additionalAttributes.getFastAddFlag());
115 additionalAttributes.setAttribute("supressFromPublic",additionalAttributes.getSupressFromPublic());
116 additionalAttributes.setAttribute("harvestable",additionalAttributes.getHarvestable());
117 additionalAttributes.setAttribute("status",additionalAttributes.getStatus());
118 extension.getContent().add(additionalAttributes);
119 oleItem.setExtension(extension);
120
121 return oleItem;
122 }
123
124 private String getAttributeValue(List<ProfileAttribute> profileAttributes, String attributeName) {
125 for (Iterator<ProfileAttribute> iterator = profileAttributes.iterator(); iterator.hasNext(); ) {
126 ProfileAttribute attribute = iterator.next();
127 if (attribute.getAttributeName().equals(attributeName)) {
128 return attribute.getAttributeValue();
129 }
130 }
131 return null;
132 }
133
134 private String getSubFieldValueFor(BibliographicRecord bibliographicRecord, String dataField, String tag) {
135 String subFieldValue = null;
136 DataField dataFieldForTag = getDataFieldForTag(bibliographicRecord, dataField);
137 if (null != dataFieldForTag) {
138 List<SubField> subfields = dataFieldForTag.getSubFields();
139 for (Iterator<SubField> iterator = subfields.iterator(); iterator.hasNext(); ) {
140 SubField marcSubField = iterator.next();
141 if (marcSubField.getCode().equals(tag)) {
142 return subFieldValue = marcSubField.getValue();
143 }
144 }
145 }
146 return subFieldValue;
147 }
148
149
150 public DataField getDataFieldForTag(BibliographicRecord bibliographicRecord, String tag) {
151 for (Iterator<DataField> iterator = bibliographicRecord.getDatafields().iterator(); iterator.hasNext(); ) {
152 DataField marcDataField = iterator.next();
153 if (marcDataField.getTag().equalsIgnoreCase(tag)) {
154 return marcDataField;
155 }
156 }
157 return null;
158 }
159
160
161 public String toXML(Item oleItem) {
162 XStream xs = new XStream();
163 xs.autodetectAnnotations(true);
164 xs.processAnnotations(Item.class);
165 String xml = xs.toXML(oleItem);
166 return xml;
167 }
168
169 public String getClassificationOrShelvingSchemeSource(BibliographicRecord bibliographicRecord) {
170 DataField dataFieldForTag = getDataFieldForTag(bibliographicRecord, "852");
171 if (dataFieldForTag.getInd1().equals("7")) {
172 List<SubField> subfields = dataFieldForTag.getSubFields();
173 for (Iterator<SubField> iterator = subfields.iterator(); iterator.hasNext(); ) {
174 SubField subField = iterator.next();
175 if (subField.getCode().equals("2")) {
176 return subField.getValue();
177 }
178 }
179 }
180
181 return "";
182 }
183 }