1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.docstore.discovery.web.struts.action;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.struts.action.Action;
20 import org.apache.struts.action.ActionForm;
21 import org.apache.struts.action.ActionForward;
22 import org.apache.struts.action.ActionMapping;
23 import org.kuali.ole.docstore.discovery.model.SearchParams;
24 import org.kuali.ole.docstore.discovery.service.DiscoveryService;
25 import org.kuali.ole.docstore.discovery.service.DiscoveryServiceImpl;
26 import org.kuali.ole.docstore.discovery.web.struts.form.DiscoveryForm;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import java.net.URLEncoder;
33 import java.util.Map;
34
35 public class DiscoveryAction extends Action {
36 private static final Logger LOG = LoggerFactory.getLogger(DiscoveryAction.class);
37 public static final String ACTION_BACK_TO_SEARCH = "backToSearch";
38
39 public static final String SEARCH_PARAMS = "searchParams";
40 public static final String DISCOVERY_SERVICE = "discoveryService";
41
42 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
43 ActionForward forward = null;
44 String facetValue = null;
45 String facetFieldValue = null;
46 String searchType = null;
47 String linkValue = null;
48 Map FacetTermsMap = null;
49 long startTime = System.currentTimeMillis();
50 DiscoveryForm discForm = (DiscoveryForm) form;
51 SearchParams searchParams = getSearchParams(request);
52
53 searchType = request.getParameter("searchType");
54 LOG.debug("searchType......... " + searchType);
55 LOG.debug("sortByTerms......." + request.getParameter("advancedSearch[advancedSearch][sortByTerms]"));
56 LOG.debug("sortingorder........" + request.getParameter("advancedSearch[advancedSearch][sortingorder]"));
57 if (StringUtils.isEmpty(searchType)) {
58 searchType = request.getParameter("advancedSearch[advancedSearch][searchType]");
59
60 }
61 LOG.debug("searchType " + searchType);
62 if (StringUtils.isEmpty(searchType)) {
63
64 LOG.debug("in if StringUtils.isEmpty ");
65 String action = request.getParameter("action");
66 if ("setPageSize".equals(action)) {
67 LOG.debug("setPageSize..." + action);
68 int resultFrmIdx = ((Integer.parseInt(request.getParameter("resultFromIndex"))) / (Integer.parseInt(request.getParameter("resultPageSize"))) + 1);
69 discForm.setResultPageIndex(Integer.toString(resultFrmIdx));
70 discForm.setResultPageSize(request.getParameter("resultPageSize"));
71 String sortingTerms = request.getParameter("sortByTerms");
72 sortTerms(sortingTerms, discForm);
73 } else if ("goToPage".equals(action)) {
74 LOG.debug("goToPage..." + action);
75 String resultPgIdx = request.getParameter("resultPageIndex");
76 int item_index = ((Integer.parseInt(resultPgIdx) - 1) * (Integer.parseInt(request.getParameter("resultPageSize"))));
77 discForm.setResultFromIndex(Integer.toString(item_index));
78 discForm.setResultPageIndex((resultPgIdx));
79 String sortingTerms = request.getParameter("sortByTerms");
80 sortTerms(sortingTerms, discForm);
81 } else if ("sorting".equals(action)) {
82 LOG.debug("sorting..." + action);
83 String sortingTerms = request.getParameter("sortByTerms");
84 discForm.setSortByTerms(sortingTerms);
85 sortTerms(sortingTerms, discForm);
86 discForm.setResultPageSize(request.getParameter("resultPageSize"));
87 }
88
89 LOG.debug("discForm.getSearchType()" + discForm.getSearchType());
90 if ("moreFacets".equals(action)) {
91 discForm.setSearchType(searchParams.getSearchType());
92 discForm.setFacetPrefix("");
93 discForm.setFacetPageSize("25");
94 discForm.setFacetOffset("0");
95 discForm.setFacetSort("lex");
96 discForm.setSearchType(DiscoveryService.SEARCH_TYPE_MORE_FACET);
97 discForm.setFacetField(request.getParameter("fieldName"));
98 discForm.initSearchParams(searchParams);
99 DiscoveryService discService = getDiscoveryService();
100 String result = discService.search(searchParams);
101 discForm.setSearchResult(result);
102 discForm.setSearchQuery(searchParams.getSearchQuery());
103 forward = mapping.findForward("moreFacets");
104 } else if (!StringUtils.isEmpty(discForm.getDocCategory())) {
105 if (!searchParams.getSearchType().equals(DiscoveryService.SEARCH_TYPE_MORE_FACET)) {
106 discForm.setSearchType(searchParams.getSearchType());
107 }
108 discForm.initSearchParams(searchParams);
109
110 DiscoveryService discService = getDiscoveryService();
111 String result = discService.search(searchParams);
112 discForm.setSearchResult(result);
113 discForm.setSearchQuery(searchParams.getSearchQuery());
114 forward = mapping.findForward("searchResults");
115 } else {
116 forward = mapping.findForward("search");
117 }
118 } else if (ACTION_BACK_TO_SEARCH.equals(searchType)) {
119 forward = mapping.findForward("search");
120 } else if (DiscoveryService.SEARCH_TYPE_NEW.equals(searchType)) {
121 LOG.debug("in else if ACTION_NEW_SEARCH.equals ");
122 searchParams.setFacetField(null);
123 discForm.setFacetField(null);
124 discForm.setFacetTermsMap(null);
125
126 discForm.init();
127
128 searchParams.init();
129 forward = mapping.findForward("search");
130 } else if (DiscoveryService.SEARCH_TYPE_QUICK.equals(searchType)
131 || DiscoveryService.SEARCH_TYPE_ADVANCED.equals(searchType)) {
132 discForm.initFromRequest(request);
133 discForm.setResultPageIndex("1");
134 discForm.setResultFromIndex("0");
135 discForm.setFacetSort("count");
136 String sortByTerms = request.getParameter("sortingorder");
137 sortTerms(sortByTerms, discForm);
138 discForm.setSortByTerms(sortByTerms);
139 LOG.debug("SortField()........." + discForm.getSortField());
140 LOG.debug("SortOrder()........." + discForm.getSortOrder());
141
142 discForm.initSearchParams(searchParams);
143
144 DiscoveryService discService = getDiscoveryService();
145 String result = discService.search(searchParams);
146 LOG.debug("search terms...." + searchParams.getSearchTerms());
147 if (searchParams.getSearchTerms() != null) {
148 discForm.setSearchTerms(searchParams.getSearchTerms());
149 }
150 discForm.setSearchResult(result);
151 discForm.setSearchQuery(searchParams.getSearchQuery());
152 forward = mapping.findForward("searchResults");
153 } else if (DiscoveryService.SEARCH_TYPE_FACET.equals(searchType)) {
154 facetValue = request.getParameter("facetValue");
155 facetFieldValue = request.getParameter("facetFieldValue");
156 facetValue = URLEncoder.encode(facetValue, "UTF-8");
157 discForm.updateFacetParams(facetFieldValue, facetValue);
158 discForm.setResultPageIndex("1");
159 discForm.setResultFromIndex("0");
160 discForm.setFacetPrefix("");
161 discForm.setFacetPageSize("25");
162 discForm.setFacetSort("count");
163 discForm.setFacetOffset("0");
164 discForm.initSearchParams(searchParams);
165
166 DiscoveryService discService = getDiscoveryService();
167 String result = discService.search(searchParams);
168 LOG.debug("result " + result);
169 discForm.setSearchResult(result);
170 discForm.setSearchQuery(searchParams.getSearchQuery());
171 forward = mapping.findForward("searchResults");
172 } else if (DiscoveryService.SEARCH_TYPE_FACET_DELETE.equals(searchType)) {
173 facetValue = request.getParameter("facetValue");
174 facetFieldValue = request.getParameter("facetFieldValue");
175 facetValue = URLEncoder.encode(facetValue, "UTF-8");
176 discForm.removeFacet(facetFieldValue, facetValue);
177 discForm.setResultPageIndex("1");
178 discForm.setResultFromIndex("0");
179 discForm.setFacetSort("count");
180 discForm.initSearchParams(searchParams);
181
182 DiscoveryService discService = getDiscoveryService();
183 String result = discService.search(searchParams);
184 discForm.setSearchResult(result);
185 discForm.setSearchQuery(searchParams.getSearchQuery());
186 forward = mapping.findForward("searchResults");
187 } else if (DiscoveryService.SEARCH_TYPE_LINK.equals(searchType)) {
188 linkValue = request.getParameter("linkValue");
189 LOG.debug("linkValue'..................." + linkValue);
190 discForm.setResultPageIndex("1");
191 discForm.setResultFromIndex("0");
192 discForm.setSortField(searchParams.getSortField());
193 discForm.setSortOrder(searchParams.getSortOrder());
194 discForm.setLinkValue(linkValue);
195 discForm.initSearchParams(searchParams);
196 DiscoveryService discService = getDiscoveryService();
197 String result = discService.search(searchParams);
198 discForm.setSearchResult(result);
199 discForm.setSearchQuery(searchParams.getSearchQuery());
200 forward = mapping.findForward("searchResults");
201 }
202
203
204 else if (DiscoveryService.BATCH_UPLOAD_LINK_SEARCH.equals(searchType)) {
205 linkValue = request.getParameter("linkValue");
206 if (LOG.isDebugEnabled()) {
207 LOG.debug("linkValue'..................." + linkValue);
208 }
209 discForm.setResultPageIndex("1");
210 discForm.setResultFromIndex("0");
211 discForm.setSortField(searchParams.getSortField());
212 discForm.setSortOrder(searchParams.getSortOrder());
213 discForm.setLinkValue(linkValue);
214 discForm.initSearchParams(searchParams);
215 DiscoveryService discService = getDiscoveryService();
216 if (linkValue == null || "".equals(linkValue)) {
217 String bibIds = request.getParameter("bibIds");
218 searchParams.setLinkValue("(id:" + bibIds + ")");
219 }
220 searchParams.setSearchType(DiscoveryService.SEARCH_TYPE_LINK);
221 String result = discService.search(searchParams);
222 discForm.setSearchResult(result);
223 if (LOG.isDebugEnabled()) {
224 LOG.info("Result" + result);
225 }
226 discForm.setSearchQuery(searchParams.getSearchQuery());
227 forward = mapping.findForward("searchResults");
228
229 } else if (DiscoveryService.SEARCH_TYPE_MORE_FACET.equals(searchType)) {
230 String facetPageSize = request.getParameter("facetPageSize");
231 String facetPrefix = request.getParameter("facetPrefix");
232 String facetSort = request.getParameter("facetSort");
233 discForm.setResultPageIndex("1");
234 discForm.setResultFromIndex("0");
235 if (facetPrefix != null) {
236 discForm.setFacetPrefix(facetPrefix);
237 }
238 if (facetPageSize != null) {
239 discForm.setFacetPageSize(facetPageSize);
240 } else {
241 facetPageSize = discForm.getFacetPageSize();
242 }
243 if (facetSort != null) {
244 discForm.setFacetSort(facetSort);
245 }
246 String facetPage = request.getParameter("facetPage");
247 if (facetPage != null) {
248 String offset = discForm.getFacetOffset();
249 int current = 0;
250 if (offset != null) {
251 current = Integer.parseInt(offset);
252 }
253 int limit = Integer.parseInt(facetPageSize);
254 if (facetPage.equals("previous")) {
255 int previous = current - limit;
256 if (previous > 0) {
257 discForm.setFacetOffset(String.valueOf(previous));
258 } else {
259 discForm.setFacetOffset("0");
260 }
261 } else if (facetPage.equals("next")) {
262 int next = current + limit;
263 discForm.setFacetOffset(String.valueOf(next));
264 }
265 } else {
266 discForm.setFacetOffset("0");
267 }
268 discForm.initSearchParams(searchParams);
269
270 DiscoveryService discService = getDiscoveryService();
271 String result = discService.search(searchParams);
272 LOG.debug("result " + result);
273 discForm.setSearchResult(result);
274 discForm.setSearchQuery(searchParams.getSearchQuery());
275 forward = mapping.findForward("moreFacets");
276 }
277 else {
278 forward = mapping.findForward("search");
279 }
280 long endTime = System.currentTimeMillis();
281 discForm.setSearchTime(String.valueOf(endTime - startTime));
282 return forward;
283 }
284
285
286 private SearchParams getSearchParams(HttpServletRequest request) {
287 SearchParams searchParams = (SearchParams) request.getSession()
288 .getAttribute(SEARCH_PARAMS);
289 if (null == searchParams) {
290 searchParams = new SearchParams();
291 request.getSession().setAttribute(SEARCH_PARAMS, searchParams);
292 }
293 return searchParams;
294 }
295
296 private DiscoveryService getDiscoveryService() {
297 DiscoveryService discoveryService = DiscoveryServiceImpl.getInstance();
298 return discoveryService;
299 }
300
301 public void sortTerms(String sortingTerms, DiscoveryForm discForm) {
302 if ("authorasc".equals(sortingTerms)) {
303 discForm.setSortField("Author_sort");
304 discForm.setSortOrder("asc");
305 } else if ("authordesc".equals(sortingTerms)) {
306 discForm.setSortField("Author_sort");
307 discForm.setSortOrder("desc");
308 } else if ("titleasc".equals(sortingTerms)) {
309 discForm.setSortField("Title_sort");
310 discForm.setSortOrder("asc");
311 } else if ("titledesc".equals(sortingTerms)) {
312 discForm.setSortField("Title_sort");
313 discForm.setSortOrder("desc");
314 } else if ("pubdateasc".equals(sortingTerms)) {
315 discForm.setSortField("PublicationDate_sort");
316 discForm.setSortOrder("asc");
317 } else if ("pubdatedesc".equals(sortingTerms)) {
318 discForm.setSortField("PublicationDate_sort");
319 discForm.setSortOrder("desc");
320 } else if ("relevance".equals(sortingTerms)) {
321 discForm.setSortField("score");
322 discForm.setSortOrder("desc");
323 }
324 }
325 }