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
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
75
76
77 public int getEndIndex() {
78 return endIndex;
79 }
80
81
82
83
84
85 public void setEndIndex(int value) {
86 this.endIndex = value;
87 }
88
89
90
91
92
93 public int getPageSize() {
94 return pageSize;
95 }
96
97
98
99
100
101 public void setPageSize(int value) {
102 this.pageSize = value;
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
136
137
138 public int getStartIndex() {
139 return startIndex;
140 }
141
142
143
144
145
146 public void setStartIndex(int value) {
147 this.startIndex = value;
148 }
149
150
151
152
153
154 public int getTotalRecordCount() {
155 return totalRecordCount;
156 }
157
158
159
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 }