View Javadoc
1   package org.kuali.ole.docstore.discovery.service;
2   
3   import org.kuali.ole.docstore.common.document.content.instance.Instance;
4   import org.kuali.ole.docstore.common.document.content.instance.Item;
5   import org.kuali.ole.docstore.common.document.content.instance.Items;
6   import org.kuali.ole.docstore.discovery.bo.OleDiscoveryMarcExportProfile;
7   import org.kuali.ole.docstore.discovery.bo.OleDiscoveryMarcMappingField;
8   import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
9   import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.DataField;
10  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.SubField;
11  import org.kuali.ole.docstore.common.document.content.instance.InstanceCollection;
12  
13  import java.util.ArrayList;
14  import java.util.List;
15  import java.util.logging.Logger;
16  
17  
18  /**
19   * Created with IntelliJ IDEA.
20   * User: ?
21   * Date: 2/20/13
22   * Time: 12:16 PM
23   * To change this template use File | Settings | File Templates.
24   */
25  public class OleInstanceToMarcConvertor {
26  
27      private static final Logger LOG = Logger.getLogger(String.valueOf(OleInstanceToMarcConvertor.class));
28  
29      public List<DataField> generateDataField(InstanceCollection instanceCollection, OleDiscoveryMarcExportProfile oleDiscoveryMarcExportProfile) {
30          List<Instance> instanceList = new ArrayList<Instance>();
31          List<DataField> dataFieldList = new ArrayList<DataField>();
32          if (instanceCollection != null) {
33              DataField dataField;
34              Instance instance;
35              List<SubField> SubFieldList;
36              for (int i = 0; i < instanceCollection.getInstance().size(); i++) {
37                  instance = instanceCollection.getInstance().get(i);
38                  Items items = (Items) instance.getItems();
39                  for (int j = 0; j < items.getItem().size(); j++) {
40                      dataField = new DataField();
41                      dataField.setTag(oleDiscoveryMarcExportProfile.getDataField());
42                      dataField.setSubFields(generateSubFields(oleDiscoveryMarcExportProfile, instance, items.getItem().get(j)));
43                      dataFieldList.add(dataField);
44                  }
45              }
46          }
47          return dataFieldList;
48      }
49  
50      private List<SubField> generateSubFields(OleDiscoveryMarcExportProfile oleDiscoveryMarcExportProfile, Instance instance, Item item) {
51          List<SubField> subFieldList = new ArrayList<SubField>();
52          List<OleDiscoveryMarcMappingField> oleDiscoveryMarcMappingFieldList = oleDiscoveryMarcExportProfile.getOleDiscoveryMarcMappingFields();
53  
54          for (OleDiscoveryMarcMappingField mappingField : oleDiscoveryMarcMappingFieldList) {
55              if (mappingField.getItemField().equalsIgnoreCase("Call Number")) {
56                  subFieldList.add(generateMarcCallNumber(instance, mappingField.getMarcField()));
57              }
58              if (mappingField.getItemField().equalsIgnoreCase("Class Scheme")) {
59                  subFieldList.add(generateMarcCallSchema(instance, mappingField.getMarcField()));
60              }
61              if (mappingField.getItemField().equalsIgnoreCase("Copy Number")) {
62                  subFieldList.add(generateMarcCopyNumber(item, mappingField.getMarcField()));
63              }
64              if (mappingField.getItemField().equalsIgnoreCase("Item Id")) {
65                  subFieldList.add(generateMarcItemNumber(item, mappingField.getMarcField()));
66              }
67              if (mappingField.getItemField().equalsIgnoreCase("Type")) {
68                  subFieldList.add(generateMarcItemType(item, mappingField.getMarcField()));
69              }
70              if (mappingField.getItemField().equalsIgnoreCase("Date Created")) {
71                  subFieldList.add(generateMarcDateCreated(instance, mappingField.getMarcField()));
72              }
73              if (mappingField.getItemField().equalsIgnoreCase("Current Location")) {
74                  subFieldList.add(generateCurrentLocation(item, mappingField.getMarcField()));
75              }
76              if (mappingField.getItemField().equalsIgnoreCase("Home Location")) {
77                  subFieldList.add(generateHomeLocation(instance, mappingField.getMarcField()));
78              }
79              if (mappingField.getItemField().equalsIgnoreCase("Library")) {
80                  subFieldList.add(generateLibraryLocation(instance, mappingField.getMarcField()));
81              }
82          }
83  
84          return subFieldList;
85      }
86  
87      private SubField generateMarcCallNumber(Instance instance, String code) {
88          SubField subField = new SubField();
89          subField.setCode(code);
90          subField.setValue("");
91          if (instance != null && instance.getOleHoldings() != null && instance.getOleHoldings().getCallNumber() != null && instance.getOleHoldings().getCallNumber().getNumber() != null) {
92              subField.setValue(instance.getOleHoldings().getCallNumber().getNumber());
93          }
94          return subField;
95      }
96  
97      private SubField generateMarcCallSchema(Instance instance, String code) {
98          SubField subField = new SubField();
99          subField.setCode(code);
100         subField.setValue("");
101         if (instance != null && instance.getOleHoldings() != null && instance.getOleHoldings().getCallNumber() != null && instance.getOleHoldings().getCallNumber().getType() != null) {
102             subField.setValue(instance.getOleHoldings().getCallNumber().getType());
103         }
104         return subField;
105     }
106 
107     private SubField generateMarcCopyNumber(Item item, String code) {
108         SubField subField = new SubField();
109         subField.setCode(code);
110         subField.setValue("");
111         if (item != null && item.getCopyNumber() != null) {
112             subField.setValue(item.getCopyNumber());
113         }
114         return subField;
115     }
116 
117     private SubField generateMarcEnumeration(Item item, String code) {
118         SubField subField = new SubField();
119         subField.setCode(code);
120         subField.setValue("");
121         if (item != null && item.getEnumeration() != null) {
122             subField.setValue(item.getEnumeration());
123         }
124         return subField;
125     }
126 
127     private SubField generateMarcItemNumber(Item item, String code) {
128         SubField subField = new SubField();
129         subField.setCode(code);
130         subField.setValue("");
131         if (item != null && item.getAccessInformation() != null && item.getAccessInformation().getBarcode() != null) {
132             subField.setValue(item.getAccessInformation().getBarcode());
133         }
134         return subField;
135     }
136 
137     private SubField generateMarcItemType(Item item, String code) {
138         SubField subField = new SubField();
139         subField.setCode(code);
140         subField.setValue("");
141         if (item != null && item.getItemType() != null) {
142             subField.setValue(item.getItemType().getCodeValue());
143         }
144         return subField;
145     }
146 
147 
148     private SubField generateMarcDateCreated(Instance instance, String code) {
149         SubField subField = new SubField();
150         subField.setCode(code);
151         subField.setValue("");
152         try {
153             if (instance != null && instance.getExtension().getContent() != null) {
154                 List<Object> additionalAttributes = (List<Object>) instance.getExtension().getContent();
155                 if (additionalAttributes.size() > 0) {
156                     AdditionalAttributes additionalAttributes1 = (AdditionalAttributes) additionalAttributes.get(0);
157                     subField.setValue(additionalAttributes1.getDateEntered());
158                 }
159             }
160         } catch (Exception e) {
161             LOG.info("Date Created value is not available" + e);
162         }
163         return subField;
164     }
165 
166     private SubField generateCurrentLocation(Item item, String code) {
167         SubField subField = new SubField();
168         subField.setCode(code);
169         subField.setValue("");
170         try {
171             if (item != null && item.getLocation().getLocationLevel().getName() != null) {
172                 if (!item.getLocation().getLocationLevel().equals("") && item.getLocation().getLocationLevel() != null) {
173                     if (item.getLocation().getLocationLevel().getName().equals("Shelving Location")) {
174                         subField.setValue(item.getLocation().getLocationLevel().getName());
175                     }
176                 }
177             }
178         } catch (Exception e) {
179             LOG.info("Current Location is not available" + e );
180         }
181         return subField;
182     }
183 
184     private SubField generateHomeLocation(Instance instance, String code) {
185         SubField subField = new SubField();
186         subField.setCode(code);
187         subField.setValue("");
188         try {
189             if (instance != null) {
190                 if (!instance.getOleHoldings().getLocation().equals("") && instance.getOleHoldings().getLocation().getLocationLevel() != null) {
191                     if (instance.getOleHoldings().getLocation().getLocationLevel().getName().equals("Collection")) {
192                         subField.setValue(instance.getOleHoldings().getLocation().getLocationLevel().getName());
193                     }
194                 }
195             }
196         } catch (Exception e) {
197             LOG.info("Home Location is not available" + e);
198         }
199         return subField;
200     }
201 
202 
203     private SubField generateLibraryLocation(Instance instance, String code) {
204         SubField subField = new SubField();
205         subField.setCode(code);
206         subField.setValue("");
207         try {
208             if (instance != null) {
209                 if (!instance.getOleHoldings().getLocation().equals("") && instance.getOleHoldings().getLocation().getLocationLevel() != null) {
210                     if (instance.getOleHoldings().getLocation().getLocationLevel().getName().equals("Library")) {
211                         subField.setValue(instance.getOleHoldings().getLocation().getLocationLevel().getName());
212                     }
213                 }
214             }
215         } catch (Exception e) {
216             LOG.info("Library Location is not available" + e);
217         }
218         return subField;
219     }
220 
221 }