View Javadoc
1   package org.kuali.ole.docstore.model.xstream.ingest;
2   
3   import com.thoughtworks.xstream.converters.Converter;
4   import com.thoughtworks.xstream.converters.MarshallingContext;
5   import com.thoughtworks.xstream.converters.UnmarshallingContext;
6   import com.thoughtworks.xstream.io.HierarchicalStreamReader;
7   import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
8   import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
9   import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
10  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
11  
12  import java.util.Iterator;
13  import java.util.List;
14  
15  /**
16   * Created by IntelliJ IDEA.
17   * User: pvsubrah
18   * Date: 2/28/12
19   * Time: 8:37 PM
20   * To change this template use File | Settings | File Templates.
21   */
22  public class IngestDocumentConverter implements Converter {
23      @Override
24      public void marshal(Object o, HierarchicalStreamWriter hierarchicalStreamWriter, MarshallingContext marshallingContext) {
25          RequestDocument requestDocument = (RequestDocument) o;
26          hierarchicalStreamWriter.addAttribute("id", requestDocument.getId());
27          hierarchicalStreamWriter.addAttribute("category", requestDocument.getCategory());
28          hierarchicalStreamWriter.addAttribute("type", requestDocument.getType());
29          hierarchicalStreamWriter.addAttribute("format", requestDocument.getFormat());
30          hierarchicalStreamWriter.startNode("content");
31          hierarchicalStreamWriter.setValue(requestDocument.getContent().getContent());
32          hierarchicalStreamWriter.endNode();
33          AdditionalAttributes additionalAttributes = requestDocument.getAdditionalAttributes();
34          if (additionalAttributes != null) {
35              hierarchicalStreamWriter.startNode("additionalAttributes");
36              hierarchicalStreamWriter.startNode("dateEntered");
37              if (additionalAttributes.getDateEntered() != null)
38                  hierarchicalStreamWriter.setValue(additionalAttributes.getDateEntered());
39              hierarchicalStreamWriter.endNode();
40              hierarchicalStreamWriter.startNode("lastUpdated");
41              if (additionalAttributes.getLastUpdated() != null)
42                  hierarchicalStreamWriter.setValue(additionalAttributes.getLastUpdated());
43              hierarchicalStreamWriter.endNode();
44              hierarchicalStreamWriter.startNode("fastAddFlag");
45              if (additionalAttributes.getFastAddFlag() != null)
46                  hierarchicalStreamWriter.setValue(additionalAttributes.getFastAddFlag());
47              hierarchicalStreamWriter.endNode();
48              hierarchicalStreamWriter.startNode("supressFromPublic");
49              if (additionalAttributes.getSupressFromPublic() != null)
50                  hierarchicalStreamWriter.setValue(additionalAttributes.getSupressFromPublic());
51              hierarchicalStreamWriter.endNode();
52              hierarchicalStreamWriter.startNode("harvestable");
53              if (additionalAttributes.getHarvestable() != null)
54                  hierarchicalStreamWriter.setValue(additionalAttributes.getHarvestable());
55              hierarchicalStreamWriter.endNode();
56              hierarchicalStreamWriter.startNode("status");
57              if (additionalAttributes.getStatus() != null)
58                  hierarchicalStreamWriter.setValue(additionalAttributes.getStatus());
59              hierarchicalStreamWriter.endNode();
60              hierarchicalStreamWriter.endNode();
61          }
62          List<RequestDocument> linkedRequestDocuments = requestDocument.getLinkedRequestDocuments();
63          if (linkedRequestDocuments != null && linkedRequestDocuments.size() > 0) {
64              hierarchicalStreamWriter.startNode("linkedIngestDocuments");
65              hierarchicalStreamWriter.startNode("linkedIngestDocument");
66              for (Iterator<RequestDocument> iterator = linkedRequestDocuments.iterator(); iterator.hasNext(); ) {
67                  RequestDocument requestDoc = iterator.next();
68                  marshal(requestDoc, hierarchicalStreamWriter, marshallingContext);
69              }
70              hierarchicalStreamWriter.endNode();
71              hierarchicalStreamWriter.endNode();
72          }
73      }
74  
75      @Override
76      public Object unmarshal(HierarchicalStreamReader hierarchicalStreamReader, UnmarshallingContext unmarshallingContext) {
77          RequestDocument requestDocument = new RequestDocument();
78          requestDocument.setId(hierarchicalStreamReader.getAttribute("id"));
79          requestDocument.setCategory(hierarchicalStreamReader.getAttribute("category"));
80          requestDocument.setType(hierarchicalStreamReader.getAttribute("type"));
81          requestDocument.setFormat(hierarchicalStreamReader.getAttribute("format"));
82          hierarchicalStreamReader.moveDown();
83          if (hierarchicalStreamReader.getNodeName().equals("content")) {
84              String value = hierarchicalStreamReader.getValue();
85              Content content = new Content(value);
86              requestDocument.setContent(content);
87          }
88          hierarchicalStreamReader.moveUp();
89          if (hierarchicalStreamReader.hasMoreChildren()) {
90              hierarchicalStreamReader.moveDown();
91              if (hierarchicalStreamReader.getNodeName().equals("additionalAttributes")) {
92                  AdditionalAttributes additionalAttributes = new AdditionalAttributes();
93                  while (hierarchicalStreamReader.hasMoreChildren()) {
94                      hierarchicalStreamReader.moveDown();
95                      if (hierarchicalStreamReader.getNodeName().equals("dateEntered")) {
96                          additionalAttributes.setDateEntered(hierarchicalStreamReader.getValue());
97                      } else if (hierarchicalStreamReader.getNodeName().equals("dateLastUpdated")) {
98                          additionalAttributes.setLastUpdated(hierarchicalStreamReader.getValue());
99                      } else if (hierarchicalStreamReader.getNodeName().equals("fastAddFlag")) {
100                         additionalAttributes.setFastAddFlag(hierarchicalStreamReader.getValue());
101                     } else if (hierarchicalStreamReader.getNodeName().equals("supressFromPublic")) {
102                         additionalAttributes.setSupressFromPublic(hierarchicalStreamReader.getValue());
103                     } else if (hierarchicalStreamReader.getNodeName().equals("harvestable")) {
104                         additionalAttributes.setHarvestable(hierarchicalStreamReader.getValue());
105                     } else if (hierarchicalStreamReader.getNodeName().equals("status")) {
106                         additionalAttributes.setStatus(hierarchicalStreamReader.getValue());
107                     }
108                     hierarchicalStreamReader.moveUp();
109                 }
110                 requestDocument.setAdditionalAttributes(additionalAttributes);
111 
112             }
113             hierarchicalStreamReader.moveUp();
114         }
115         if (hierarchicalStreamReader.hasMoreChildren()) {
116             hierarchicalStreamReader.moveDown();
117             if (hierarchicalStreamReader.getNodeName().equals("linkedIngestDocuments")) {
118                 while (hierarchicalStreamReader.hasMoreChildren()) {
119                     hierarchicalStreamReader.moveDown();
120                     requestDocument.addLinkedRequestDocument((RequestDocument) unmarshal(hierarchicalStreamReader,
121                             unmarshallingContext));
122                     hierarchicalStreamReader.moveUp();
123                 }
124             }
125             hierarchicalStreamReader.moveUp();
126         }
127         return requestDocument;
128     }
129 
130     @Override
131     public boolean canConvert(Class aClass) {
132         return aClass.equals(RequestDocument.class);
133     }
134 }