01: package com.technoetic.xplanner.actions;
02:
03: import java.util.List;
04: import java.util.Locale;
05: import javax.servlet.http.HttpServletRequest;
06: import javax.servlet.http.HttpServletResponse;
07:
08: import org.apache.commons.lang.StringUtils;
09: import org.apache.struts.Globals;
10: import org.apache.struts.action.ActionForm;
11: import org.apache.struts.action.ActionForward;
12: import org.apache.struts.action.ActionMapping;
13: import org.apache.struts.util.MessageResources;
14:
15: import com.technoetic.xplanner.XPlannerProperties;
16: import com.technoetic.xplanner.db.ContentSearchHelper;
17: import com.technoetic.xplanner.domain.repository.RepositoryException;
18: import com.technoetic.xplanner.security.SecurityHelper;
19:
20: /**
21: * @noinspection UnusedAssignment
22: */
23: public class ContentSearchAction extends AbstractAction {
24: public static final String SEARCH_CRITERIA_KEY = "searchedContent";
25: private ContentSearchHelper searchHelper;
26: protected static final String RESTRICTED_PROJECT_ID_KEY = "restrictToProjectId";
27:
28: public void setContentSearchHelper(ContentSearchHelper searchHelper) {
29: this .searchHelper = searchHelper;
30: }
31:
32: protected ActionForward doExecute(ActionMapping mapping,
33: ActionForm form, HttpServletRequest request,
34: HttpServletResponse response) throws Exception {
35: int remoteUserId = SecurityHelper.getRemoteUserId(request);
36: String searchCriteria = request
37: .getParameter(SEARCH_CRITERIA_KEY);
38: if (StringUtils.isEmpty(searchCriteria)) {
39: request
40: .setAttribute("exception",
41: produceException(request));
42: return mapping.findForward("error");
43: }
44: XPlannerProperties xPlannerProperties = new XPlannerProperties();
45: Boolean isGlobalSearchScope = Boolean
46: .valueOf(xPlannerProperties
47: .getProperty("search.content.globalScopeEnable"));
48: int projectId = 0;
49: if (!isGlobalSearchScope.booleanValue())
50: projectId = Integer.parseInt(request
51: .getParameter(RESTRICTED_PROJECT_ID_KEY));
52: List results = search(searchCriteria, remoteUserId, projectId);
53:
54: request.setAttribute("searchResults", results);
55: request.setAttribute(SEARCH_CRITERIA_KEY, searchCriteria);
56:
57: return mapping.findForward("success");
58: }
59:
60: protected List search(String searchCriteria, int userId,
61: int restrictedProjectId) throws RepositoryException {
62: searchHelper
63: .search(searchCriteria, userId, restrictedProjectId);
64: return searchHelper.getSearchResults();
65: }
66:
67: private Exception produceException(HttpServletRequest request) {
68: return produceException(((MessageResources) request
69: .getAttribute(Globals.MESSAGES_KEY)), request
70: .getLocale(), "missing content");
71: }
72:
73: private Exception produceException(
74: MessageResources messageResources, Locale locale,
75: String message) {
76: String invalidMessage = messageResources.getMessage(locale,
77: "contentsearch.invalid_id");
78: String exceptionMessage = invalidMessage
79: + (message != null ? ": " + message : message);
80: return new Exception(exceptionMessage);
81: }
82: }
|