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.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  		DiscoveryForm discForm = (DiscoveryForm) form;
50  		SearchParams searchParams = getSearchParams(request);
51  		// Get the user input from the http request.
52  		searchType = request.getParameter("searchType");
53  		LOG.debug("searchType......... " + searchType);
54  		LOG.debug("sortByTerms......."+request.getParameter("advancedSearch[advancedSearch][sortByTerms]"));
55  		LOG.debug("sortingorder........"+request.getParameter("advancedSearch[advancedSearch][sortingorder]"));
56  		if (StringUtils.isEmpty(searchType)) {
57  			searchType = request.getParameter("advancedSearch[advancedSearch][searchType]");
58  			
59  		}
60  		LOG.debug("searchType " + searchType);
61  		if (StringUtils.isEmpty(searchType)) {
62  			// Do not initialize from request.
63  			LOG.debug("in if StringUtils.isEmpty ");
64  			String action = request.getParameter("action");
65              if ("setPageSize".equals(action)) {
66                  LOG.debug("setPageSize..." + action);
67                  int resultFrmIdx = ((Integer.parseInt(request.getParameter("resultFromIndex")))/(Integer.parseInt(request.getParameter("resultPageSize"))) + 1);
68                  discForm.setResultPageIndex(Integer.toString(resultFrmIdx));
69                  discForm.setResultPageSize(request.getParameter("resultPageSize"));
70                  String sortingTerms = request.getParameter("sortByTerms");
71                  sortTerms(sortingTerms, discForm);
72              } else if ("goToPage".equals(action)) {
73                  LOG.debug("goToPage..." + action);
74                  String resultPgIdx = request.getParameter("resultPageIndex");
75                  int item_index = ((Integer.parseInt(resultPgIdx) - 1) * (Integer.parseInt(request.getParameter("resultPageSize"))));
76                  discForm.setResultFromIndex(Integer.toString(item_index));
77                  discForm.setResultPageIndex((resultPgIdx));
78                  String sortingTerms = request.getParameter("sortByTerms");
79                  sortTerms(sortingTerms, discForm);
80              }else if ("sorting".equals(action)) {
81                  LOG.debug("sorting..." + action);
82                  String sortingTerms = request.getParameter("sortByTerms");
83                  discForm.setSortByTerms(sortingTerms);
84                  sortTerms(sortingTerms, discForm);
85                  discForm.setResultPageSize(request.getParameter("resultPageSize"));
86              }
87              
88  			LOG.debug("discForm.getSearchType()" + discForm.getSearchType());
89  			if ("moreFacets".equals(action)) {
90                  forward = mapping.findForward("moreFacets");
91  			}
92  			else if (!StringUtils.isEmpty(discForm.getDocCategory())) {
93  				discForm.setSearchType(searchParams.getSearchType());
94  				discForm.initSearchParams(searchParams);
95  				// Call the search service.
96  				DiscoveryService discService = getDiscoveryService();
97  				String result = discService.search(searchParams);
98  				discForm.setSearchResult(result);
99                  discForm.setSearchQuery(searchParams.getSearchQuery());
100 				forward = mapping.findForward("searchResults");
101 			} 
102 			else {
103 				forward = mapping.findForward("search");
104 			}
105 		} else if (ACTION_BACK_TO_SEARCH.equals(searchType)) {
106 			forward = mapping.findForward("search");
107 		} else if (DiscoveryService.SEARCH_TYPE_NEW.equals(searchType)) {
108 			LOG.debug("in else if ACTION_NEW_SEARCH.equals ");
109 			searchParams.setFacetField(null);
110 			discForm.setFacetField(null);
111 			discForm.setFacetTermsMap(null);
112 			// Cleanup all search form.
113 			discForm.init();
114 			// Cleanup the search params bean.
115 			searchParams.init();
116 			forward = mapping.findForward("search");
117 		} else if (DiscoveryService.SEARCH_TYPE_QUICK.equals(searchType)
118 				|| DiscoveryService.SEARCH_TYPE_ADVANCED.equals(searchType)) {
119 			discForm.initFromRequest(request);
120 			discForm.setResultPageIndex("1");
121 			discForm.setResultFromIndex("0");
122 			String sortByTerms = request.getParameter("advancedSearch[advancedSearch][sortingorder]");
123 			sortTerms(sortByTerms, discForm);
124 			discForm.setSortByTerms(sortByTerms);
125 			LOG.debug("SortField()........." + discForm.getSortField());
126 			LOG.debug("SortOrder()........." + discForm.getSortOrder());
127 			// Populate the search params bean.
128 			discForm.initSearchParams(searchParams);
129 			// Call the search service.
130 			DiscoveryService discService = getDiscoveryService();
131 			String result = discService.search(searchParams);
132 			LOG.debug("search terms...."+searchParams.getSearchTerms());
133             if(searchParams.getSearchTerms()!=null){
134                 discForm.setSearchTerms(searchParams.getSearchTerms());
135             }
136 			discForm.setSearchResult(result);
137             discForm.setSearchQuery(searchParams.getSearchQuery());
138 			forward = mapping.findForward("searchResults");
139 		} else if (DiscoveryService.SEARCH_TYPE_FACET.equals(searchType)) {
140 			facetValue = request.getParameter("facetValue");
141 			facetFieldValue = request.getParameter("facetFieldValue");
142             facetValue= URLEncoder.encode(facetValue,"UTF-8");
143 			discForm.updateFacetParams(facetFieldValue, facetValue);
144 			discForm.setResultPageIndex("1");
145 			discForm.setResultFromIndex("0");
146 			discForm.initSearchParams(searchParams);
147 			// Call the search service.
148 			DiscoveryService discService = getDiscoveryService();
149 			String result = discService.search(searchParams);
150 			LOG.debug("result " + result);
151 			discForm.setSearchResult(result);
152             discForm.setSearchQuery(searchParams.getSearchQuery());
153 			forward = mapping.findForward("searchResults");
154 		} else if (DiscoveryService.SEARCH_TYPE_FACET_DELETE.equals(searchType)) {
155 			facetValue = request.getParameter("facetValue");
156 			facetFieldValue = request.getParameter("facetFieldValue");
157             facetValue= URLEncoder.encode(facetValue,"UTF-8");
158 			discForm.removeFacet(facetFieldValue, facetValue);
159 			discForm.setResultPageIndex("1");
160 			discForm.setResultFromIndex("0");
161 			discForm.initSearchParams(searchParams);
162 			// Call the search service.
163 			DiscoveryService discService = getDiscoveryService();
164 			String result = discService.search(searchParams);
165 			discForm.setSearchResult(result);
166             discForm.setSearchQuery(searchParams.getSearchQuery());
167 			forward = mapping.findForward("searchResults");
168 		} else if (DiscoveryService.SEARCH_TYPE_LINK.equals(searchType)) {
169             linkValue = request.getParameter("linkValue");
170             LOG.debug("linkValue'..................." + linkValue);
171             discForm.setResultPageIndex("1");
172             discForm.setResultFromIndex("0");
173             discForm.setSortField(searchParams.getSortField());
174             discForm.setSortOrder(searchParams.getSortOrder());
175             discForm.setLinkValue(linkValue);
176             discForm.initSearchParams(searchParams);
177             DiscoveryService discService = getDiscoveryService();
178             String result = discService.search(searchParams);
179             discForm.setSearchResult(result);
180             discForm.setSearchQuery(searchParams.getSearchQuery());
181             forward = mapping.findForward("searchResults");
182         } 
183 
184 		return forward;
185 	}
186 	
187 	
188 
189 	private SearchParams getSearchParams(HttpServletRequest request) {
190 		SearchParams searchParams = (SearchParams) request.getSession()
191 				.getAttribute(SEARCH_PARAMS);
192 		if (null == searchParams) {
193 			searchParams = new SearchParams();
194 			request.getSession().setAttribute(SEARCH_PARAMS, searchParams);
195 		}
196 		return searchParams;
197 	}
198 
199 	private DiscoveryService getDiscoveryService() {
200 		DiscoveryService discoveryService = DiscoveryServiceImpl.getInstance();
201 		return discoveryService;
202 	}
203 
204 	public void sortTerms(String sortingTerms,DiscoveryForm discForm){
205         if("authorasc".equals(sortingTerms)){
206             discForm.setSortField("Author_sort");
207             discForm.setSortOrder("asc");
208         }else if("authordesc".equals(sortingTerms)){
209             discForm.setSortField("Author_sort");
210             discForm.setSortOrder("desc");
211         }else if("titleasc".equals(sortingTerms)){
212             discForm.setSortField("Title_sort");
213             discForm.setSortOrder("asc");
214         }else if("titledesc".equals(sortingTerms)){
215             discForm.setSortField("Title_sort");
216             discForm.setSortOrder("desc");
217         }else if("pubdateasc".equals(sortingTerms)){
218             discForm.setSortField("PublicationDate_sort");
219             discForm.setSortOrder("asc");
220         }else if("pubdatedesc".equals(sortingTerms)){
221             discForm.setSortField("PublicationDate_sort");
222             discForm.setSortOrder("desc");
223         }else if("relevance".equals(sortingTerms)){
224             discForm.setSortField("score");
225             discForm.setSortOrder("desc");
226         }
227     }
228 }