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.pojo.ProfileAttribute;
6
7 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.Extension;
8
9 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.OleHolding;
10
11 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.SpecificRetentionPolicy;
12
13 import java.text.DateFormat;
14 import java.text.SimpleDateFormat;
15 import java.util.*;
16
17
18
19
20
21
22
23
24 public class OleHoldingsRecordHandler {
25 Map<String, String> recordTypes = new HashMap<String, String>();
26 Map<String, String> encodingLevels = new HashMap<String, String>();
27
28 public OleHoldingsRecordHandler() {
29 recordTypes = new HashMap<String, String>();
30 recordTypes.put("u", "Unknown");
31 recordTypes.put("s", "Single-part item holdings");
32 recordTypes.put("y", "Serial item holdings");
33
34 encodingLevels.put("1", "Holdings level 1");
35 encodingLevels.put("2", "Holdings level 2");
36 encodingLevels.put("3", "Holdings level 3");
37 encodingLevels.put("4", "Holdings level 4");
38 encodingLevels.put("5", "Holdings level 5");
39 encodingLevels.put("m", "Mixed level");
40 encodingLevels.put("u", "Unknown");
41 encodingLevels.put("z", "Other level");
42 }
43
44 public String generateXML(List<ProfileAttribute> profileAttributes) {
45 OleHolding oleHolding = getOleHoldings(profileAttributes);
46 return toXML(oleHolding);
47 }
48
49 public OleHolding getOleHoldings(List<ProfileAttribute> profileAttributes) {
50 OleHolding oleHolding = new OleHolding();
51 oleHolding.setRecordType(resolveRecordType(getAttributeValue(profileAttributes, "recordType")));
52 oleHolding.setEncodingLevel(resolveEncodingLevel(getAttributeValue(profileAttributes, "encodingLevel")));
53 oleHolding.setReceiptStatus(getAttributeValue(profileAttributes, "recieptStatus"));
54 oleHolding.setAcquisitionMethod(getAttributeValue(profileAttributes, "acquisitionMethod"));
55 oleHolding.setReproductionPolicy(getAttributeValue(profileAttributes, "generalRetentionPolicy"));
56 SpecificRetentionPolicy specificRetentionPolicy = new SpecificRetentionPolicy();
57 specificRetentionPolicy.setPolicyType(getAttributeValue(profileAttributes, "policyType"));
58 oleHolding.setSpecificRetentionPolicy(specificRetentionPolicy);
59 oleHolding.setGeneralRetentionPolicy(getAttributeValue(profileAttributes, "generalRetentionPolicy"));
60
61 DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
62 Extension extension = new Extension();
63 AdditionalAttributes additionalAttributes = new AdditionalAttributes();
64 additionalAttributes.setDateEntered(String.valueOf(df.format(new Date())));
65 additionalAttributes.setLastUpdated(String.valueOf(df.format(new Date())));
66 additionalAttributes.setSupressFromPublic("false");
67 additionalAttributes.setFastAddFlag("false");
68 additionalAttributes.setHarvestable("true");
69 additionalAttributes.setStatus("n");
70 additionalAttributes.setAttribute("dateEntered",additionalAttributes.getDateEntered());
71 additionalAttributes.setAttribute("lastUpdated",additionalAttributes.getLastUpdated());
72 additionalAttributes.setAttribute("fastAddFlag",additionalAttributes.getFastAddFlag());
73 additionalAttributes.setAttribute("supressFromPublic",additionalAttributes.getSupressFromPublic());
74 additionalAttributes.setAttribute("harvestable",additionalAttributes.getHarvestable());
75 additionalAttributes.setAttribute("status",additionalAttributes.getStatus());
76
77 extension.getContent().add(additionalAttributes);
78 oleHolding.setExtension(Arrays.asList(extension));
79 return oleHolding;
80 }
81
82 private String resolveEncodingLevel(String encodingLevel) {
83 return encodingLevels.get(encodingLevel);
84 }
85
86 private String resolveRecordType(String recordType) {
87 return recordTypes.get(recordType);
88 }
89
90 private String getAttributeValue(List<ProfileAttribute> profileAttributes, String attributeName) {
91 for (Iterator<ProfileAttribute> iterator = profileAttributes.iterator(); iterator.hasNext(); ) {
92 ProfileAttribute attribute = iterator.next();
93 if (attribute.getAttributeName().equals(attributeName)) {
94 return attribute.getAttributeValue();
95 }
96 }
97 return null;
98 }
99
100 public String toXML(OleHolding oleHolding) {
101 XStream xs = new XStream();
102 xs.autodetectAnnotations(true);
103 xs.processAnnotations(OleHolding.class);
104 String xml = xs.toXML(oleHolding);
105 return xml;
106 }
107
108 }