View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Created by IntelliJ IDEA.
32   * User: pvsubrah
33   * Date: 9/9/11
34   * Time: 10:39 AM
35   * To change this template use File | Settings | File Templates.
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          //for License
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          //ends here...
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          if (hierarchicalStreamReader.hasMoreChildren()) {
147             hierarchicalStreamReader.moveDown();
148             if (hierarchicalStreamReader.getNodeName().equals("additionalAttributes")) {
149                 setAdditionalAttributesObject(hierarchicalStreamReader, requestDocument);
150             }
151             if (hierarchicalStreamReader.getNodeName().equals("linkedIngestDocuments")) {
152                 setLinkedRequestDocuments(hierarchicalStreamReader, requestDocument, unmarshallingContext);
153             }
154             hierarchicalStreamReader.moveUp();
155         }
156         if (hierarchicalStreamReader.hasMoreChildren()) {
157             hierarchicalStreamReader.moveDown();
158             if (hierarchicalStreamReader.getNodeName().equals("linkedIngestDocuments")) {
159                 setLinkedRequestDocuments(hierarchicalStreamReader, requestDocument, unmarshallingContext);
160             }
161             hierarchicalStreamReader.moveUp();
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                     if (hierarchicalStreamReader.getNodeName().equals("dateEntered")) {
188                         additionalAttributes.setDateEntered(hierarchicalStreamReader.getValue());
189                     }
190                     else if (hierarchicalStreamReader.getNodeName().equals("lastUpdated")) {
191                         additionalAttributes.setLastUpdated(hierarchicalStreamReader.getValue());
192                     }
193                     else if (hierarchicalStreamReader.getNodeName().equals("fastAddFlag")) {
194                         additionalAttributes.setFastAddFlag(hierarchicalStreamReader.getValue());
195                     }
196                     else if (hierarchicalStreamReader.getNodeName().equals("supressFromPublic")) {
197                         additionalAttributes.setSupressFromPublic(hierarchicalStreamReader.getValue());
198                     }
199                     else if (hierarchicalStreamReader.getNodeName().equals("harvestable")) {
200                         additionalAttributes.setHarvestable(hierarchicalStreamReader.getValue());
201                     }
202                     else if (hierarchicalStreamReader.getNodeName().equals("status")) {
203                         additionalAttributes.setStatus(hierarchicalStreamReader.getValue());
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 }