View Javadoc
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  //import org.kuali.ole.pojo.item.Extension;
22  //import org.kuali.ole.pojo.item.OleItem;
23  
24  /**
25   * Created by IntelliJ IDEA.
26   * User: pvsubrah
27   * Date: 4/9/12
28   * Time: 3:22 PM
29   * To change this template use File | Settings | File Templates.
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  //        oleItem.setItemType(getSubFieldValueFor(bibliographicRecord, dataField, "$5"));
48          ItemType itemType = new ItemType();
49          /*itemType.setCodeValue(getSubFieldValueFor(bibliographicRecord, dataField, "$5"));
50          itemType.setFullValue(getSubFieldValueFor(bibliographicRecord, dataField, "$5"));*/
51          /*String itmType = getSubFieldValueFor(bibliographicRecord, "985", "t");
52          if(itmType != null){
53              itemType.setCodeValue(itmType);
54              itemType.setFullValue(itmType);
55          }
56          else {
57              itemType.setCodeValue(ITEM_TYPE);
58              itemType.setFullValue(ITEM_TYPE);
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          // String copyNumber = getSubFieldValueFor(bibliographicRecord, "876", "t");
65          /*String copyNumber = getSubFieldValueFor(bibliographicRecord, "985", "g");
66          if(copyNumber != null) {
67              oleItem.setCopyNumber(copyNumber);
68          }
69          else {
70              oleItem.setCopyNumber(COPY_NUMBER);
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          /*String itemStatus = getSubFieldValueFor(bibliographicRecord, "985", "s");
84          if(itemStatus != null){
85              oleItem.setItemStatus(itemStatus);
86          }
87          else {
88              oleItem.setItemStatus(ITEM_STATUS);
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  //            oleItem.setStatisticalSearchingCodes(valueFor);
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         /*oleItem.setCopyNumber(COPY_NUMBER);
105         oleItem.setCopyNumberLabel(COPY_NUMBER_LABEL);*/
106 //
107 //        PhysicalLocation location = new PhysicalLocation();
108 //        location.getLocationStatus().add(getAttributeValue(profileAttributes, "locationStatus"));
109 
110 //        LocationLevel locationLevel = new LocationLevel();
111 //        CodeOrIdentifier levelName = new CodeOrIdentifier();
112 
113 //        levelName.setValue(getAttributeValue(profileAttributes, "levelName"));
114 //
115 //        TypeOrSource typeOrSource1 = new TypeOrSource();
116 //        typeOrSource1.setPointer("Pointer 1");
117 //        typeOrSource1.setText("Text for Level Name 1");
118 //        levelName.setTypeOrSource(typeOrSource1);
119 //
120 //        locationLevel.setLevelName(levelName);
121 //        location.getLocationLevel().add(locationLevel);
122 //
123 //        location.getCallNumberPrefix().add(getSubFieldValueFor(bibliographicRecord, dataField, "$k"));
124 //        location.getCallNumberSuffix().add(getSubFieldValueFor(bibliographicRecord, dataField, "$,"));
125 
126 //        CodeOrIdentifier classification = new CodeOrIdentifier();
127 //        TypeOrSource typeOrSource4 = new TypeOrSource();
128 //        typeOrSource4.setPointer("Classification Pointer -1");
129 //        typeOrSource4.setText("Classification Text -1");
130 //        classification.setTypeOrSource(typeOrSource4);
131 //        classification.setValue(getClassificationOrShelvingSchemeSource(bibliographicRecord));
132 //        location.setClassificationOrShelvingSchemeSource(classification);
133 
134 //        location.setClassificationPart(getSubFieldValueFor(bibliographicRecord, dataField, "$h"));
135 //        location.getItemPart().add(getSubFieldValueFor(bibliographicRecord, dataField, "$i"));
136 //        location.setShelvingControlNumber(getSubFieldValueFor(bibliographicRecord, dataField, "$j"));
137 //
138 //        oleItem.getLocation().add(location);
139 
140         //Eill
141 //        oleItem.getVendorLineItemIdentifier().add("Vendoer Line Item Identifier-1");
142 
143         //CallNumber
144 
145         CallNumber callNumber = new CallNumber();
146         /*callNumber.setPrefix(getSubFieldValueFor(bibliographicRecord, dataField, "k"));
147         callNumber.setClassificationPart(getSubFieldValueFor(bibliographicRecord, dataField, "h"));
148         callNumber.setItemPart(getSubFieldValueFor(bibliographicRecord, dataField, "i"));
149         callNumber.setType(getSubFieldValueFor(bibliographicRecord, dataField, "2"));*/
150         callNumber.setNumber(getSubFieldValueFor(bibMarcRecord, "050", "a"));
151         callNumber.setPrefix(getSubFieldValueFor(bibMarcRecord, "050", "b"));
152         //String callNumberType = getSubFieldValueFor(bibliographicRecord, "985", "q") == null?CALL_NUMBER_TYPE:getSubFieldValueFor(bibliographicRecord, "985", "q");
153         /*if(callNumberType != null){
154             callNumber.setType(callNumberType);
155         }
156         else {
157             callNumber.setType(CALL_NUMBER_TYPE);
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         /*Map<String,String> locationLevelMap = new HashMap<String,String>();
172         locationLevelMap.put(locationLevelInstitution, "Institution");
173         locationLevelMap.put(locationLevelLibrary, "Library");
174         locationLevelMap.put(locationLevelShelving, "Shelving Location");*/
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         // Extension
186        /* DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
187         Extension extension = new Extension();
188         AdditionalAttributes additionalAttributes = new AdditionalAttributes();
189         additionalAttributes.setDateEntered(String.valueOf(df.format(new Date())));
190         additionalAttributes.setLastUpdated(String.valueOf(df.format(new Date())));
191         additionalAttributes.setSupressFromPublic("false");
192         additionalAttributes.setFastAddFlag("false");
193         additionalAttributes.setHarvestable("true");
194         additionalAttributes.setStatus("n"); // new Record
195         additionalAttributes.setAttribute("dateEntered", additionalAttributes.getDateEntered());
196         additionalAttributes.setAttribute("lastUpdated", additionalAttributes.getLastUpdated());
197         additionalAttributes.setAttribute("fastAddFlag", additionalAttributes.getFastAddFlag());
198         additionalAttributes.setAttribute("supressFromPublic", additionalAttributes.getSupressFromPublic());
199         additionalAttributes.setAttribute("harvestable", additionalAttributes.getHarvestable());
200         additionalAttributes.setAttribute("status", additionalAttributes.getStatus());
201         extension.getContent().add(additionalAttributes);
202         oleItem.setExtension(extension);*/
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         // Extension
267        /* DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
268         Extension extension = new Extension();
269         AdditionalAttributes additionalAttributes = new AdditionalAttributes();
270         additionalAttributes.setDateEntered(String.valueOf(df.format(new Date())));
271         additionalAttributes.setLastUpdated(String.valueOf(df.format(new Date())));
272         additionalAttributes.setSupressFromPublic("false");
273         additionalAttributes.setFastAddFlag("false");
274         additionalAttributes.setHarvestable("true");
275         additionalAttributes.setStatus("n"); // new Record
276         additionalAttributes.setAttribute("dateEntered", additionalAttributes.getDateEntered());
277         additionalAttributes.setAttribute("lastUpdated", additionalAttributes.getLastUpdated());
278         additionalAttributes.setAttribute("fastAddFlag", additionalAttributes.getFastAddFlag());
279         additionalAttributes.setAttribute("supressFromPublic", additionalAttributes.getSupressFromPublic());
280         additionalAttributes.setAttribute("harvestable", additionalAttributes.getHarvestable());
281         additionalAttributes.setAttribute("status", additionalAttributes.getStatus());
282         extension.getContent().add(additionalAttributes);
283         oleItem.setExtension(extension);*/
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 }