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          {
70              hierarchicalStreamWriter.startNode("uuid");
71              hierarchicalStreamWriter.setValue(requestDocument.getUuid());
72              hierarchicalStreamWriter.endNode();
73          }
74          if(requestDocument.getDocumentName() != null)
75          {
76              hierarchicalStreamWriter.startNode("documentName");
77              hierarchicalStreamWriter.setValue(requestDocument.getDocumentName());
78              hierarchicalStreamWriter.endNode();
79          }
80          if(requestDocument.getDocumentTitle() != null)
81          {
82              hierarchicalStreamWriter.startNode("documentTitle");
83              hierarchicalStreamWriter.setValue(requestDocument.getDocumentTitle());
84              hierarchicalStreamWriter.endNode();
85          }
86          if(requestDocument.getDocumentMimeType() != null)
87          {
88              hierarchicalStreamWriter.startNode("documentMimeType");
89              hierarchicalStreamWriter.setValue(requestDocument.getDocumentMimeType());
90              hierarchicalStreamWriter.endNode();
91          }
92  
93          //ends here...
94  
95          AdditionalAttributes additionalAttributes = requestDocument.getAdditionalAttributes();
96          addConverter.marshal(additionalAttributes, hierarchicalStreamWriter, marshallingContext);
97  
98          List<RequestDocument> linkedRequestDocuments = requestDocument.getLinkedRequestDocuments();
99          if (linkedRequestDocuments != null && linkedRequestDocuments.size() > 0) {
100             hierarchicalStreamWriter.startNode("linkedIngestDocuments");
101             hierarchicalStreamWriter.startNode("linkedIngestDocument");
102             for (Iterator<RequestDocument> iterator = linkedRequestDocuments.iterator(); iterator.hasNext(); ) {
103                 RequestDocument requestDoc = iterator.next();
104                 marshal(requestDoc, hierarchicalStreamWriter, marshallingContext);
105             }
106             hierarchicalStreamWriter.endNode();
107             hierarchicalStreamWriter.endNode();
108         }
109     }
110 
111     @Override
112     public Object unmarshal(HierarchicalStreamReader hierarchicalStreamReader,
113                             UnmarshallingContext unmarshallingContext) {
114         RequestDocument requestDocument = new RequestDocument();
115         requestDocument.setId(hierarchicalStreamReader.getAttribute("id"));
116         requestDocument.setCategory(hierarchicalStreamReader.getAttribute("category"));
117         requestDocument.setType(hierarchicalStreamReader.getAttribute("type"));
118         requestDocument.setFormat(hierarchicalStreamReader.getAttribute("format"));
119         while(hierarchicalStreamReader.hasMoreChildren())
120          {
121             hierarchicalStreamReader.moveDown();
122 
123             if (hierarchicalStreamReader.getNodeName().equals("uuid")) {
124                 requestDocument.setUuid(hierarchicalStreamReader.getValue());
125             } else if (hierarchicalStreamReader.getNodeName().equals("documentName")) {
126                 requestDocument.setDocumentName( hierarchicalStreamReader.getValue());
127              }
128              else if(hierarchicalStreamReader.getNodeName().equals("documentTitle"))
129              {
130                 requestDocument.setDocumentTitle( hierarchicalStreamReader.getValue());
131              }
132 
133              else if(hierarchicalStreamReader.getNodeName().equals("documentMimeType"))
134              {
135                 requestDocument.setDocumentMimeType( hierarchicalStreamReader.getValue());
136              }
137 
138             else if (hierarchicalStreamReader.getNodeName().equals("content")) {
139                 setContentObject(hierarchicalStreamReader, requestDocument);
140             }
141             else if (hierarchicalStreamReader.getNodeName().equals("additionalAttributes")) {
142                 setAdditionalAttributesObject(hierarchicalStreamReader, requestDocument,unmarshallingContext);
143             }
144             else if (hierarchicalStreamReader.getNodeName().equals("linkedIngestDocuments")) {
145                 setLinkedRequestDocuments(hierarchicalStreamReader, requestDocument, unmarshallingContext);
146             }
147             hierarchicalStreamReader.moveUp();
148         }
149 /*
150          if (hierarchicalStreamReader.hasMoreChildren()) {
151             hierarchicalStreamReader.moveDown();
152             if (hierarchicalStreamReader.getNodeName().equals("additionalAttributes")) {
153                 setAdditionalAttributesObject(hierarchicalStreamReader, requestDocument);
154             }
155             if (hierarchicalStreamReader.getNodeName().equals("linkedIngestDocuments")) {
156                 setLinkedRequestDocuments(hierarchicalStreamReader, requestDocument, unmarshallingContext);
157             }
158             hierarchicalStreamReader.moveUp();
159         }
160         if (hierarchicalStreamReader.hasMoreChildren()) {
161             hierarchicalStreamReader.moveDown();
162             if (hierarchicalStreamReader.getNodeName().equals("linkedIngestDocuments")) {
163                 setLinkedRequestDocuments(hierarchicalStreamReader, requestDocument, unmarshallingContext);
164             }
165             hierarchicalStreamReader.moveUp();
166         }
167 */
168 
169         return requestDocument;
170     }
171 
172 
173     private void setLinkedRequestDocuments(HierarchicalStreamReader hierarchicalStreamReader,
174                                            RequestDocument requestDocument, UnmarshallingContext unmarshallingContext) {
175         while (hierarchicalStreamReader.hasMoreChildren()) {
176             hierarchicalStreamReader.moveDown();
177             requestDocument.addLinkedRequestDocument((RequestDocument) unmarshal(hierarchicalStreamReader, unmarshallingContext));
178             hierarchicalStreamReader.moveUp();
179         }
180 
181     }
182 
183     private void setAdditionalAttributesObject(HierarchicalStreamReader hierarchicalStreamReader,
184                                                RequestDocument requestDocument,
185                                                UnmarshallingContext unmarshallingContext) {
186         AdditionalAttributeConverter addConverter = new AdditionalAttributeConverter();
187         AdditionalAttributes additionalAttributes = (AdditionalAttributes) addConverter
188                 .unmarshal(hierarchicalStreamReader, unmarshallingContext);
189 
190         /*
191                     if (hierarchicalStreamReader.getNodeName().equals("dateEntered")) {
192                         additionalAttributes.setDateEntered(hierarchicalStreamReader.getValue());
193                     }
194                     else if (hierarchicalStreamReader.getNodeName().equals("lastUpdated")) {
195                         additionalAttributes.setLastUpdated(hierarchicalStreamReader.getValue());
196                     }
197                     else if (hierarchicalStreamReader.getNodeName().equals("fastAddFlag")) {
198                         additionalAttributes.setFastAddFlag(hierarchicalStreamReader.getValue());
199                     }
200                     else if (hierarchicalStreamReader.getNodeName().equals("supressFromPublic")) {
201                         additionalAttributes.setSupressFromPublic(hierarchicalStreamReader.getValue());
202                     }
203                     else if (hierarchicalStreamReader.getNodeName().equals("harvestable")) {
204                         additionalAttributes.setHarvestable(hierarchicalStreamReader.getValue());
205                     }
206                     else if (hierarchicalStreamReader.getNodeName().equals("status")) {
207                         additionalAttributes.setStatus(hierarchicalStreamReader.getValue());
208                     }
209         */
210         requestDocument.setAdditionalAttributes(additionalAttributes);
211     }
212 
213 
214     private void setContentObject(HierarchicalStreamReader hierarchicalStreamReader, RequestDocument requestDocument) {
215         String value = hierarchicalStreamReader.getValue();
216         if (isValidXml(value)) {
217             Content content = new Content(value);
218             requestDocument.setContent(content);
219         }
220     }
221 
222     @Override
223     public boolean canConvert(Class aClass) {
224         return aClass.equals(RequestDocument.class);
225     }
226 
227     public boolean isValidXml(String xmlContent) {
228         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
229         try {
230             DocumentBuilder builder = factory.newDocumentBuilder();
231             InputSource is = new InputSource(new StringReader(xmlContent));
232             builder.parse(is);
233             return true;
234         }
235         catch (Exception e) {
236             throw new IllegalArgumentException(
237                     "Invalid XML content in RequestDocument. Try to replace (if any) '&' with '&amp;' and '<' with '&lt;'");
238         }
239     }
240 }