View Javadoc

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  //import org.kuali.ole.pojo.item.Extension;
19  //import org.kuali.ole.pojo.item.OleItem;
20  
21  /**
22   * Created by IntelliJ IDEA.
23   * User: pvsubrah
24   * Date: 4/9/12
25   * Time: 3:22 PM
26   * To change this template use File | Settings | File Templates.
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  //        oleItem.setItemType(getSubFieldValueFor(bibliographicRecord, dataField, "$5"));
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  //            oleItem.setStatisticalSearchingCodes(valueFor);
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  //        PhysicalLocation location = new PhysicalLocation();
67  //        location.getLocationStatus().add(getAttributeValue(profileAttributes, "locationStatus"));
68  
69  //        LocationLevel locationLevel = new LocationLevel();
70  //        CodeOrIdentifier levelName = new CodeOrIdentifier();
71  
72  //        levelName.setValue(getAttributeValue(profileAttributes, "levelName"));
73  //
74  //        TypeOrSource typeOrSource1 = new TypeOrSource();
75  //        typeOrSource1.setPointer("Pointer 1");
76  //        typeOrSource1.setText("Text for Level Name 1");
77  //        levelName.setTypeOrSource(typeOrSource1);
78  //
79  //        locationLevel.setLevelName(levelName);
80  //        location.getLocationLevel().add(locationLevel);
81  //
82  //        location.getCallNumberPrefix().add(getSubFieldValueFor(bibliographicRecord, dataField, "$k"));
83  //        location.getCallNumberSuffix().add(getSubFieldValueFor(bibliographicRecord, dataField, "$,"));
84  
85  //        CodeOrIdentifier classification = new CodeOrIdentifier();
86  //        TypeOrSource typeOrSource4 = new TypeOrSource();
87  //        typeOrSource4.setPointer("Classification Pointer -1");
88  //        typeOrSource4.setText("Classification Text -1");
89  //        classification.setTypeOrSource(typeOrSource4);
90  //        classification.setValue(getClassificationOrShelvingSchemeSource(bibliographicRecord));
91  //        location.setClassificationOrShelvingSchemeSource(classification);
92  
93  //        location.setClassificationPart(getSubFieldValueFor(bibliographicRecord, dataField, "$h"));
94  //        location.getItemPart().add(getSubFieldValueFor(bibliographicRecord, dataField, "$i"));
95  //        location.setShelvingControlNumber(getSubFieldValueFor(bibliographicRecord, dataField, "$j"));
96  //
97  //        oleItem.getLocation().add(location);
98  
99          //Eill
100 //        oleItem.getVendorLineItemIdentifier().add("Vendoer Line Item Identifier-1");
101 
102         // Extension
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"); // new Record
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 }