View Javadoc
1   package org.kuali.ole.docstore.common.search;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.docstore.common.document.factory.JAXBContextFactory;
5   
6   import java.io.ByteArrayInputStream;
7   import java.io.StringWriter;
8   import java.util.ArrayList;
9   import java.util.List;
10  import javax.xml.bind.Marshaller;
11  import javax.xml.bind.Unmarshaller;
12  import javax.xml.bind.annotation.*;
13  import javax.xml.transform.stream.StreamSource;
14  
15  
16  /**
17   * <p>Java class for searchResponse complex type.
18   *
19   * <p>The following schema fragment specifies the expected content contained within this class.
20   *
21   * <pre>
22   * &lt;complexType name="searchResponse">
23   *   &lt;complexContent>
24   *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
25   *       &lt;sequence>
26   *         &lt;element name="endIndex" type="{http://www.w3.org/2001/XMLSchema}int"/>
27   *         &lt;element name="pageSize" type="{http://www.w3.org/2001/XMLSchema}int"/>
28   *         &lt;element name="searchResults" type="{}searchResult" maxOccurs="unbounded" minOccurs="0"/>
29   *         &lt;element name="startIndex" type="{http://www.w3.org/2001/XMLSchema}int"/>
30   *         &lt;element name="totalRecordCount" type="{http://www.w3.org/2001/XMLSchema}int"/>
31   *       &lt;/sequence>
32   *     &lt;/restriction>
33   *   &lt;/complexContent>
34   * &lt;/complexType>
35   * </pre>
36   *
37   *
38   */
39  @XmlAccessorType(XmlAccessType.FIELD)
40  @XmlType(name = "searchResponse", propOrder = {
41          "endIndex",
42          "pageSize",
43          "searchResults",
44          "startIndex",
45          "totalRecordCount",
46          "facetResult",
47          "time"
48  })
49  @XmlRootElement
50  public class SearchResponse {
51  
52      private static final Logger LOG = Logger.getLogger(SearchResponse.class);
53      protected int endIndex;
54      protected int pageSize;
55  
56      @XmlElementWrapper(name = "searchResults")
57      @XmlElement(name = "searchResult")
58      protected List<SearchResult> searchResults;
59      protected int startIndex;
60      protected int totalRecordCount;
61      protected FacetResult facetResult;
62  
63      protected int time;
64  
65      public FacetResult getFacetResult() {
66          return facetResult;
67      }
68  
69      public void setFacetResult(FacetResult facetResult) {
70          this.facetResult = facetResult;
71      }
72  
73      /**
74       * Gets the value of the endIndex property.
75       *
76       */
77      public int getEndIndex() {
78          return endIndex;
79      }
80  
81      /**
82       * Sets the value of the endIndex property.
83       *
84       */
85      public void setEndIndex(int value) {
86          this.endIndex = value;
87      }
88  
89      /**
90       * Gets the value of the pageSize property.
91       *
92       */
93      public int getPageSize() {
94          return pageSize;
95      }
96  
97      /**
98       * Sets the value of the pageSize property.
99       *
100      */
101     public void setPageSize(int value) {
102         this.pageSize = value;
103     }
104 
105     /**
106      * Gets the value of the searchResults property.
107      *
108      * <p>
109      * This accessor method returns a reference to the live list,
110      * not a snapshot. Therefore any modification you make to the
111      * returned list will be present inside the JAXB object.
112      * This is why there is not a <CODE>set</CODE> method for the searchResults property.
113      *
114      * <p>
115      * For example, to add a new item, do as follows:
116      * <pre>
117      *    getSearchResults().add(newItem);
118      * </pre>
119      *
120      *
121      * <p>
122      * Objects of the following type(s) are allowed in the list
123      * {@link SearchResult }
124      *
125      *
126      */
127     public List<SearchResult> getSearchResults() {
128         if (searchResults == null) {
129             searchResults = new ArrayList<SearchResult>();
130         }
131         return this.searchResults;
132     }
133 
134     /**
135      * Gets the value of the startIndex property.
136      *
137      */
138     public int getStartIndex() {
139         return startIndex;
140     }
141 
142     /**
143      * Sets the value of the startIndex property.
144      *
145      */
146     public void setStartIndex(int value) {
147         this.startIndex = value;
148     }
149 
150     /**
151      * Gets the value of the totalRecordCount property.
152      *
153      */
154     public int getTotalRecordCount() {
155         return totalRecordCount;
156     }
157 
158     /**
159      * Sets the value of the totalRecordCount property.
160      *
161      */
162     public void setTotalRecordCount(int value) {
163         this.totalRecordCount = value;
164     }
165 
166     public int getTime() {
167         return time;
168     }
169 
170     public void setTime(int time) {
171         this.time = time;
172     }
173 
174     public String serialize(Object object) {
175         String result = null;
176         SearchResponse searchResponse = (SearchResponse) object;
177         try {
178             StringWriter sw = new StringWriter();
179             JAXBContextFactory jaxbContextFactory = JAXBContextFactory.getInstance();
180             Marshaller jaxbMarshaller = jaxbContextFactory.getMarshaller(SearchResponse.class);
181             synchronized (jaxbMarshaller) {
182                 jaxbMarshaller.marshal(searchResponse, sw);
183             }
184 
185             result = sw.toString();
186         } catch (Exception e) {
187             LOG.error("Exception :", e);
188         }
189         return result;
190     }
191 
192 
193     public Object deserialize(String content) {
194         SearchResponse searchResponse = new SearchResponse();
195         try {
196             Unmarshaller unmarshaller = JAXBContextFactory.getInstance().getUnMarshaller(SearchResponse.class);
197             ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
198             synchronized (unmarshaller) {
199                 searchResponse = unmarshaller.unmarshal(new StreamSource(input), SearchResponse.class).getValue();
200             }
201 
202         } catch (Exception e) {
203             LOG.error("Exception :", e);
204         }
205         return searchResponse;
206     }
207 }