1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.docstore.model.xstream.ingest;
17
18 import com.thoughtworks.xstream.converters.Converter;
19 import com.thoughtworks.xstream.converters.MarshallingContext;
20 import com.thoughtworks.xstream.converters.UnmarshallingContext;
21 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
22 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
23 import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
24 import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
25 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
26
27 import java.util.Iterator;
28 import java.util.List;
29
30
31
32
33
34
35
36
37 public class RequestDocumentConverter
38 implements Converter {
39 @Override
40 public void marshal(Object o, HierarchicalStreamWriter hierarchicalStreamWriter,
41 MarshallingContext marshallingContext) {
42 AdditionalAttributeConverter addConverter = new AdditionalAttributeConverter();
43 RequestDocument requestDocument = (RequestDocument) o;
44 if (requestDocument.getId() != null) {
45 hierarchicalStreamWriter.addAttribute("id", requestDocument.getId());
46 }
47 if (requestDocument.getCategory() != null) {
48 hierarchicalStreamWriter.addAttribute("category", requestDocument.getCategory());
49 }
50 if (requestDocument.getType() != null) {
51 hierarchicalStreamWriter.addAttribute("type", requestDocument.getType());
52 }
53 if (requestDocument.getFormat() != null) {
54 hierarchicalStreamWriter.addAttribute("format", requestDocument.getFormat());
55 }
56
57 if (requestDocument.getContent() != null && requestDocument.getContent().getContent() != null) {
58 hierarchicalStreamWriter.startNode("content");
59 hierarchicalStreamWriter.setValue(requestDocument.getContent().getContent());
60 hierarchicalStreamWriter.endNode();
61 }
62
63
64 if(requestDocument.getUuid() != null)
65 {
66 hierarchicalStreamWriter.startNode("uuid");
67 hierarchicalStreamWriter.setValue(requestDocument.getUuid());
68 hierarchicalStreamWriter.endNode();
69 }
70 if(requestDocument.getDocumentName() != null)
71 {
72 hierarchicalStreamWriter.startNode("documentName");
73 hierarchicalStreamWriter.setValue(requestDocument.getDocumentName());
74 hierarchicalStreamWriter.endNode();
75 }
76 if(requestDocument.getDocumentTitle() != null)
77 {
78 hierarchicalStreamWriter.startNode("documentTitle");
79 hierarchicalStreamWriter.setValue(requestDocument.getDocumentTitle());
80 hierarchicalStreamWriter.endNode();
81 }
82 if(requestDocument.getDocumentMimeType() != null)
83 {
84 hierarchicalStreamWriter.startNode("documentMimeType");
85 hierarchicalStreamWriter.setValue(requestDocument.getDocumentMimeType());
86 hierarchicalStreamWriter.endNode();
87 }
88
89
90
91 AdditionalAttributes additionalAttributes = requestDocument.getAdditionalAttributes();
92 addConverter.marshal(additionalAttributes, hierarchicalStreamWriter, marshallingContext);
93
94 List<RequestDocument> linkedRequestDocuments = requestDocument.getLinkedRequestDocuments();
95 if (linkedRequestDocuments != null && linkedRequestDocuments.size() > 0) {
96 hierarchicalStreamWriter.startNode("linkedIngestDocuments");
97 hierarchicalStreamWriter.startNode("linkedIngestDocument");
98 for (Iterator<RequestDocument> iterator = linkedRequestDocuments.iterator(); iterator.hasNext(); ) {
99 RequestDocument requestDoc = iterator.next();
100 marshal(requestDoc, hierarchicalStreamWriter, marshallingContext);
101 }
102 hierarchicalStreamWriter.endNode();
103 hierarchicalStreamWriter.endNode();
104 }
105 }
106
107 @Override
108 public Object unmarshal(HierarchicalStreamReader hierarchicalStreamReader,
109 UnmarshallingContext unmarshallingContext) {
110 RequestDocument requestDocument = new RequestDocument();
111 requestDocument.setId(hierarchicalStreamReader.getAttribute("id"));
112 requestDocument.setCategory(hierarchicalStreamReader.getAttribute("category"));
113 requestDocument.setType(hierarchicalStreamReader.getAttribute("type"));
114 requestDocument.setFormat(hierarchicalStreamReader.getAttribute("format"));
115 while(hierarchicalStreamReader.hasMoreChildren())
116 {
117 hierarchicalStreamReader.moveDown();
118
119 if (hierarchicalStreamReader.getNodeName().equals("uuid")) {
120 requestDocument.setUuid(hierarchicalStreamReader.getValue());
121 } else if (hierarchicalStreamReader.getNodeName().equals("documentName")) {
122 requestDocument.setDocumentName( hierarchicalStreamReader.getValue());
123 }
124 else if(hierarchicalStreamReader.getNodeName().equals("documentTitle"))
125 {
126 requestDocument.setDocumentTitle( hierarchicalStreamReader.getValue());
127 }
128
129 else if(hierarchicalStreamReader.getNodeName().equals("documentMimeType"))
130 {
131 requestDocument.setDocumentMimeType( hierarchicalStreamReader.getValue());
132 }
133
134 else if (hierarchicalStreamReader.getNodeName().equals("content")) {
135 setContentObject(hierarchicalStreamReader, requestDocument);
136 }
137 else if (hierarchicalStreamReader.getNodeName().equals("additionalAttributes")) {
138 setAdditionalAttributesObject(hierarchicalStreamReader, requestDocument,unmarshallingContext);
139 }
140 else if (hierarchicalStreamReader.getNodeName().equals("linkedIngestDocuments")) {
141 setLinkedRequestDocuments(hierarchicalStreamReader, requestDocument, unmarshallingContext);
142 }
143 hierarchicalStreamReader.moveUp();
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 return requestDocument;
166 }
167
168
169 private void setLinkedRequestDocuments(HierarchicalStreamReader hierarchicalStreamReader,
170 RequestDocument requestDocument, UnmarshallingContext unmarshallingContext) {
171 while (hierarchicalStreamReader.hasMoreChildren()) {
172 hierarchicalStreamReader.moveDown();
173 requestDocument.addLinkedRequestDocument((RequestDocument) unmarshal(hierarchicalStreamReader, unmarshallingContext));
174 hierarchicalStreamReader.moveUp();
175 }
176
177 }
178
179 private void setAdditionalAttributesObject(HierarchicalStreamReader hierarchicalStreamReader,
180 RequestDocument requestDocument,
181 UnmarshallingContext unmarshallingContext) {
182 AdditionalAttributeConverter addConverter = new AdditionalAttributeConverter();
183 AdditionalAttributes additionalAttributes = (AdditionalAttributes) addConverter
184 .unmarshal(hierarchicalStreamReader, unmarshallingContext);
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206 requestDocument.setAdditionalAttributes(additionalAttributes);
207 }
208
209
210 private void setContentObject(HierarchicalStreamReader hierarchicalStreamReader, RequestDocument requestDocument) {
211 String value = hierarchicalStreamReader.getValue();
212 Content content = new Content(value);
213 requestDocument.setContent(content);
214 }
215
216 @Override
217 public boolean canConvert(Class aClass) {
218 return aClass.equals(RequestDocument.class);
219 }
220 }