001package org.kuali.ole.docstore.discovery.service;
002
003import org.kuali.ole.docstore.discovery.bo.OleDiscoveryMarcExportProfile;
004import org.kuali.ole.docstore.discovery.bo.OleDiscoveryMarcMappingField;
005import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
006import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.DataField;
007import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.SubField;
008import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.Instance;
009import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.InstanceCollection;
010import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.Item;
011import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.Items;
012
013import java.util.ArrayList;
014import java.util.List;
015import java.util.logging.Logger;
016
017
018/**
019 * Created with IntelliJ IDEA.
020 * User: ?
021 * Date: 2/20/13
022 * Time: 12:16 PM
023 * To change this template use File | Settings | File Templates.
024 */
025public class OleInstanceToMarcConvertor {
026
027    private static final Logger LOG = Logger.getLogger(String.valueOf(OleInstanceToMarcConvertor.class));
028
029    public List<DataField> generateDataField(InstanceCollection instanceCollection, OleDiscoveryMarcExportProfile oleDiscoveryMarcExportProfile) {
030        List<Instance> instanceList = new ArrayList<Instance>();
031        List<DataField> dataFieldList = new ArrayList<DataField>();
032        if (instanceCollection != null) {
033            DataField dataField;
034            Instance instance;
035            List<SubField> SubFieldList;
036            for (int i = 0; i < instanceCollection.getInstance().size(); i++) {
037                instance = instanceCollection.getInstance().get(i);
038                Items items = (Items) instance.getItems();
039                for (int j = 0; j < items.getItem().size(); j++) {
040                    dataField = new DataField();
041                    dataField.setTag(oleDiscoveryMarcExportProfile.getDataField());
042                    dataField.setSubFields(generateSubFields(oleDiscoveryMarcExportProfile, instance, items.getItem().get(j)));
043                    dataFieldList.add(dataField);
044                }
045            }
046        }
047        return dataFieldList;
048    }
049
050    private List<SubField> generateSubFields(OleDiscoveryMarcExportProfile oleDiscoveryMarcExportProfile, Instance instance, Item item) {
051        List<SubField> subFieldList = new ArrayList<SubField>();
052        List<OleDiscoveryMarcMappingField> oleDiscoveryMarcMappingFieldList = oleDiscoveryMarcExportProfile.getOleDiscoveryMarcMappingFields();
053
054        for (OleDiscoveryMarcMappingField mappingField : oleDiscoveryMarcMappingFieldList) {
055            if (mappingField.getItemField().equalsIgnoreCase("Call Number")) {
056                subFieldList.add(generateMarcCallNumber(instance, mappingField.getMarcField()));
057            }
058            if (mappingField.getItemField().equalsIgnoreCase("Class Scheme")) {
059                subFieldList.add(generateMarcCallSchema(instance, mappingField.getMarcField()));
060            }
061            if (mappingField.getItemField().equalsIgnoreCase("Copy Number")) {
062                subFieldList.add(generateMarcCopyNumber(item, mappingField.getMarcField()));
063            }
064            if (mappingField.getItemField().equalsIgnoreCase("Item Id")) {
065                subFieldList.add(generateMarcItemNumber(item, mappingField.getMarcField()));
066            }
067            if (mappingField.getItemField().equalsIgnoreCase("Type")) {
068                subFieldList.add(generateMarcItemType(item, mappingField.getMarcField()));
069            }
070            if (mappingField.getItemField().equalsIgnoreCase("Date Created")) {
071                subFieldList.add(generateMarcDateCreated(instance, mappingField.getMarcField()));
072            }
073            if (mappingField.getItemField().equalsIgnoreCase("Current Location")) {
074                subFieldList.add(generateCurrentLocation(item, mappingField.getMarcField()));
075            }
076            if (mappingField.getItemField().equalsIgnoreCase("Home Location")) {
077                subFieldList.add(generateHomeLocation(instance, mappingField.getMarcField()));
078            }
079            if (mappingField.getItemField().equalsIgnoreCase("Library")) {
080                subFieldList.add(generateLibraryLocation(instance, mappingField.getMarcField()));
081            }
082        }
083
084        return subFieldList;
085    }
086
087    private SubField generateMarcCallNumber(Instance instance, String code) {
088        SubField subField = new SubField();
089        subField.setCode(code);
090        subField.setValue("");
091        if (instance != null && instance.getOleHoldings() != null && instance.getOleHoldings().getCallNumber() != null && instance.getOleHoldings().getCallNumber().getNumber() != null) {
092            subField.setValue(instance.getOleHoldings().getCallNumber().getNumber());
093        }
094        return subField;
095    }
096
097    private SubField generateMarcCallSchema(Instance instance, String code) {
098        SubField subField = new SubField();
099        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}