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  import org.xml.sax.InputSource;
27  
28  import javax.xml.parsers.DocumentBuilder;
29  import javax.xml.parsers.DocumentBuilderFactory;
30  import java.io.StringReader;
31  import java.util.Iterator;
32  import java.util.List;
33  
34  /**
35   * Created by IntelliJ IDEA.
36   * User: pvsubrah
37   * Date: 9/9/11
38   * Time: 10:39 AM
39   * To change this template use File | Settings | File Templates.
40   */
41  public class RequestDocumentConverter
42          implements Converter {
43      @Override
44      public void marshal(Object o, HierarchicalStreamWriter hierarchicalStreamWriter,
45                          MarshallingContext marshallingContext) {
46          AdditionalAttributeConverter addConverter = new AdditionalAttributeConverter();
47          RequestDocument requestDocument = (RequestDocument) o;
48          if (requestDocument.getId() != null) {
49              hierarchicalStreamWriter.addAttribute("id", requestDocument.getId());
50          }
51          if (requestDocument.getCategory() != null) {
52              hierarchicalStreamWriter.addAttribute("category", requestDocument.getCategory());
53          }
54          if (requestDocument.getType() != null) {
55              hierarchicalStreamWriter.addAttribute("type", requestDocument.getType());
56          }
57          if (requestDocument.getFormat() != null) {
58              hierarchicalStreamWriter.addAttribute("format", requestDocument.getFormat());
59          }
60  
61          if (requestDocument.getContent() != null && requestDocument.getContent().getContent() != null) {
62              hierarchicalStreamWriter.startNode("content");
63              hierarchicalStreamWriter.setValue(requestDocument.getContent().getContent());
64              hierarchicalStreamWriter.endNode();
65          }
66  
67          //for License
68          if (requestDocument.getUuid() != null) {
69              hierarchicalStreamWriter.startNode("uuid");
70              hierarchicalStreamWriter.setValue(requestDocument.getUuid());
71              hierarchicalStreamWriter.endNode();
72          }
73          if (requestDocument.getDocumentName() != null) {
74              hierarchicalStreamWriter.startNode("documentName");
75              hierarchicalStreamWriter.setValue(requestDocument.getDocumentName());
76              hierarchicalStreamWriter.endNode();
77          }
78          if (requestDocument.getDocumentTitle() != null) {
79              hierarchicalStreamWriter.startNode("documentTitle");
80              hierarchicalStreamWriter.setValue(requestDocument.getDocumentTitle());
81              hierarchicalStreamWriter.endNode();
82          }
83          if (requestDocument.getDocumentMimeType() != null) {
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  
98              for (Iterator<RequestDocument> iterator = linkedRequestDocuments.iterator(); iterator.hasNext(); ) {
99                  hierarchicalStreamWriter.startNode("linkedIngestDocument");
100                 RequestDocument requestDoc = iterator.next();
101                 marshal(requestDoc, hierarchicalStreamWriter, marshallingContext);
102                 hierarchicalStreamWriter.endNode();
103             }
104 
105             hierarchicalStreamWriter.endNode();
106         }
107     }
108 
109     @Override
110     public Object unmarshal(HierarchicalStreamReader hierarchicalStreamReader,
111                             UnmarshallingContext unmarshallingContext) {
112         RequestDocument requestDocument = new RequestDocument();
113         requestDocument.setId(hierarchicalStreamReader.getAttribute("id"));
114         requestDocument.setCategory(hierarchicalStreamReader.getAttribute("category"));
115         requestDocument.setType(hierarchicalStreamReader.getAttribute("type"));
116         requestDocument.setFormat(hierarchicalStreamReader.getAttribute("format"));
117         while (hierarchicalStreamReader.hasMoreChildren()) {
118             hierarchicalStreamReader.moveDown();
119 
120             if (hierarchicalStreamReader.getNodeName().equals("uuid")) {
121                 requestDocument.setUuid(hierarchicalStreamReader.getValue());
122             } else if (hierarchicalStreamReader.getNodeName().equals("documentName")) {
123                 requestDocument.setDocumentName(hierarchicalStreamReader.getValue());
124             } else if (hierarchicalStreamReader.getNodeName().equals("documentTitle")) {
125                 requestDocument.setDocumentTitle(hierarchicalStreamReader.getValue());
126             } else if (hierarchicalStreamReader.getNodeName().equals("documentMimeType")) {
127                 requestDocument.setDocumentMimeType(hierarchicalStreamReader.getValue());
128             } else if (hierarchicalStreamReader.getNodeName().equals("content")) {
129                 setContentObject(hierarchicalStreamReader, requestDocument);
130             } else if (hierarchicalStreamReader.getNodeName().equals("additionalAttributes")) {
131                 setAdditionalAttributesObject(hierarchicalStreamReader, requestDocument, unmarshallingContext);
132             } else if (hierarchicalStreamReader.getNodeName().equals("linkedIngestDocuments")) {
133                 setLinkedRequestDocuments(hierarchicalStreamReader, requestDocument, unmarshallingContext);
134             }
135             hierarchicalStreamReader.moveUp();
136         }
137 /*
138          if (hierarchicalStreamReader.hasMoreChildren()) {
139             hierarchicalStreamReader.moveDown();
140             if (hierarchicalStreamReader.getNodeName().equals("additionalAttributes")) {
141                 setAdditionalAttributesObject(hierarchicalStreamReader, requestDocument);
142             }
143             if (hierarchicalStreamReader.getNodeName().equals("linkedIngestDocuments")) {
144                 setLinkedRequestDocuments(hierarchicalStreamReader, requestDocument, unmarshallingContext);
145             }
146             hierarchicalStreamReader.moveUp();
147         }
148         if (hierarchicalStreamReader.hasMoreChildren()) {
149             hierarchicalStreamReader.moveDown();
150             if (hierarchicalStreamReader.getNodeName().equals("linkedIngestDocuments")) {
151                 setLinkedRequestDocuments(hierarchicalStreamReader, requestDocument, unmarshallingContext);
152             }
153             hierarchicalStreamReader.moveUp();
154         }
155 */
156 
157         return requestDocument;
158     }
159 
160 
161     private void setLinkedRequestDocuments(HierarchicalStreamReader hierarchicalStreamReader,
162                                            RequestDocument requestDocument, UnmarshallingContext unmarshallingContext) {
163         while (hierarchicalStreamReader.hasMoreChildren()) {
164             hierarchicalStreamReader.moveDown();
165             requestDocument.addLinkedRequestDocument((RequestDocument) unmarshal(hierarchicalStreamReader, unmarshallingContext));
166             hierarchicalStreamReader.moveUp();
167         }
168 
169     }
170 
171     private void setAdditionalAttributesObject(HierarchicalStreamReader hierarchicalStreamReader,
172                                                RequestDocument requestDocument,
173                                                UnmarshallingContext unmarshallingContext) {
174         AdditionalAttributeConverter addConverter = new AdditionalAttributeConverter();
175         AdditionalAttributes additionalAttributes = (AdditionalAttributes) addConverter
176                 .unmarshal(hierarchicalStreamReader, unmarshallingContext);
177 
178         /*
179                     if (hierarchicalStreamReader.getNodeName().equals("dateEntered")) {
180                         additionalAttributes.setDateEntered(hierarchicalStreamReader.getValue());
181                     }
182                     else if (hierarchicalStreamReader.getNodeName().equals("lastUpdated")) {
183                         additionalAttributes.setLastUpdated(hierarchicalStreamReader.getValue());
184                     }
185                     else if (hierarchicalStreamReader.getNodeName().equals("fastAddFlag")) {
186                         additionalAttributes.setFastAddFlag(hierarchicalStreamReader.getValue());
187                     }
188                     else if (hierarchicalStreamReader.getNodeName().equals("supressFromPublic")) {
189                         additionalAttributes.setSupressFromPublic(hierarchicalStreamReader.getValue());
190                     }
191                     else if (hierarchicalStreamReader.getNodeName().equals("harvestable")) {
192                         additionalAttributes.setHarvestable(hierarchicalStreamReader.getValue());
193                     }
194                     else if (hierarchicalStreamReader.getNodeName().equals("status")) {
195                         additionalAttributes.setStatus(hierarchicalStreamReader.getValue());
196                     }
197         */
198         requestDocument.setAdditionalAttributes(additionalAttributes);
199     }
200 
201 
202     private void setContentObject(HierarchicalStreamReader hierarchicalStreamReader, RequestDocument requestDocument) {
203         String value = hierarchicalStreamReader.getValue();
204         if (isValidXml(value)) {
205             Content content = new Content(value);
206             requestDocument.setContent(content);
207         }
208     }
209 
210     @Override
211     public boolean canConvert(Class aClass) {
212         return aClass.equals(RequestDocument.class);
213     }
214 
215     public boolean isValidXml(String xmlContent) {
216         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
217         try {
218             DocumentBuilder builder = factory.newDocumentBuilder();
219             InputSource is = new InputSource(new StringReader(xmlContent));
220             builder.parse(is);
221             return true;
222         } catch (Exception e) {
223             throw new IllegalArgumentException(
224                     "Invalid XML content in RequestDocument. Try to replace (if any) '&' with '&amp;' and '<' with '&lt;'");
225         }
226     }
227 }