PageRenderTime 79ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/java/node-app/node-app-wna/src/main/java/com/windsor/node/admin/ActivitySearchController.java

http://opennode2.googlecode.com/
Java | 341 lines | 213 code | 91 blank | 37 comment | 28 complexity | 95249e446281a16f88a08128a817af98 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0, Apache-2.0, LGPL-2.1, CC-BY-SA-3.0, MPL-2.0-no-copyleft-exception
  1. /*
  2. Copyright (c) 2009, The Environmental Council of the States (ECOS)
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of the ECOS nor the names of its contributors may
  13. be used to endorse or promote products derived from this software
  14. without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  18. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  19. COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  21. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  25. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. package com.windsor.node.admin;
  29. import java.text.SimpleDateFormat;
  30. import java.util.Date;
  31. import java.util.HashMap;
  32. import java.util.Hashtable;
  33. import java.util.List;
  34. import java.util.Map;
  35. import javax.servlet.ServletException;
  36. import javax.servlet.http.HttpServletRequest;
  37. import javax.servlet.http.HttpServletResponse;
  38. import javax.servlet.http.HttpSession;
  39. import org.apache.commons.lang3.StringUtils;
  40. import org.slf4j.LoggerFactory;
  41. import org.springframework.beans.factory.InitializingBean;
  42. import org.springframework.beans.propertyeditors.CustomDateEditor;
  43. import org.springframework.validation.BindException;
  44. import org.springframework.web.bind.ServletRequestDataBinder;
  45. import org.springframework.web.servlet.ModelAndView;
  46. import com.windsor.node.BaseSimpleFormController;
  47. import com.windsor.node.admin.domain.ActivitySearchForm;
  48. import com.windsor.node.admin.editor.CustomTimestampEditor;
  49. import com.windsor.node.admin.util.AdminConstants;
  50. import com.windsor.node.admin.util.SiteTabUtils;
  51. import com.windsor.node.admin.util.VisitUtils;
  52. import com.windsor.node.common.domain.ActivitySearchCriteria;
  53. import com.windsor.node.common.domain.DataFlow;
  54. import com.windsor.node.common.domain.NodeVisit;
  55. import com.windsor.node.common.domain.UserAccount;
  56. import com.windsor.node.common.service.admin.AccountService;
  57. import com.windsor.node.common.service.admin.ActivityService;
  58. import com.windsor.node.common.service.admin.FlowService;
  59. public class ActivitySearchController extends BaseSimpleFormController
  60. implements InitializingBean {
  61. private static final String HAS_SESSION = "has session";
  62. private static final String FORMAT = "yyyy-MM-dd";
  63. private static final String SESSION_LAST_SEARCH_CRITERIA = "LAST_SEARCH";
  64. private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
  65. FORMAT);
  66. private ActivityService activityService;
  67. private AccountService accountService;
  68. private FlowService flowService;
  69. private String resultView;
  70. public ActivitySearchController() {
  71. super();
  72. logger = LoggerFactory.getLogger(ActivitySearchController.class);
  73. setCommandClass(ActivitySearchForm.class);
  74. setSessionForm(true);
  75. }
  76. public void afterPropertiesSet() throws Exception {
  77. if (activityService == null) {
  78. throw new Exception("activityService not set");
  79. }
  80. if (accountService == null) {
  81. throw new Exception("accountService not set");
  82. }
  83. if (flowService == null) {
  84. throw new Exception("flowService not set");
  85. }
  86. }
  87. protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception
  88. {
  89. if("reset".equalsIgnoreCase(request.getParameter("reset")))//if they ask for a reset blank out the criteria object
  90. {
  91. request.getSession().removeAttribute(SESSION_LAST_SEARCH_CRITERIA);
  92. }
  93. return super.handleRequestInternal(request, response);
  94. }
  95. protected void initBinder(HttpServletRequest request,
  96. ServletRequestDataBinder binder) throws Exception {
  97. binder.registerCustomEditor(Date.class, new CustomDateEditor(
  98. new SimpleDateFormat(FORMAT), false));
  99. CustomTimestampEditor timestampEditor = new CustomTimestampEditor(
  100. getMessageSourceAccessor().getMessage(AdminConstants.SCHEDULE_DATETIME_FORMAT_KEY,
  101. AdminConstants.DATETIME_FORMAT));
  102. binder.registerCustomEditor(java.sql.Timestamp.class, timestampEditor);
  103. }
  104. private ActivitySearchCriteria getSearchCriteria(HttpServletRequest request) {
  105. ActivitySearchCriteria criteria = null;
  106. HttpSession session = request.getSession(false);
  107. if (session != null
  108. && session.getAttribute(SESSION_LAST_SEARCH_CRITERIA) != null) {
  109. logger.debug(HAS_SESSION);
  110. criteria = (ActivitySearchCriteria) session
  111. .getAttribute(SESSION_LAST_SEARCH_CRITERIA);
  112. logger.debug("Criteria: " + criteria);
  113. }
  114. return criteria;
  115. }
  116. private void setSearchCriteria(HttpServletRequest request,
  117. ActivitySearchCriteria criteria) {
  118. HttpSession session = request.getSession(false);
  119. if (session != null) {
  120. logger.debug(HAS_SESSION);
  121. if (session.getAttribute(SESSION_LAST_SEARCH_CRITERIA) != null) {
  122. session.removeAttribute(SESSION_LAST_SEARCH_CRITERIA);
  123. }
  124. session.setAttribute(SESSION_LAST_SEARCH_CRITERIA, criteria);
  125. }
  126. }
  127. private String makeReadable(ActivitySearchCriteria criteria,
  128. NodeVisit visit) {
  129. StringBuffer sb = new StringBuffer();
  130. if (StringUtils.isNotBlank(criteria.getFlowId())) {
  131. DataFlow flow = flowService
  132. .getDataFlow(criteria.getFlowId(), visit);
  133. sb.append("<b>Exchange</b> = " + flow.getName() + AdminConstants.SEMICOLON);
  134. }
  135. if (StringUtils.isNotBlank(criteria.getIp())) {
  136. sb.append("<b>IP</b> = " + criteria.getIp() + AdminConstants.SEMICOLON);
  137. }
  138. if (StringUtils.isNotBlank(criteria.getTransactionId())) {
  139. sb.append("<b>Transaction Id</b> like "
  140. + criteria.getTransactionId() + AdminConstants.SEMICOLON);
  141. }
  142. if (StringUtils.isNotBlank(criteria.getCreatedById())) {
  143. sb.append("<b>Creator</b> = " + criteria.getCreatedById()
  144. + AdminConstants.SEMICOLON);
  145. }
  146. if (StringUtils.isNotBlank(criteria.getType())) {
  147. sb.append("<b>Type</b> = " + criteria.getType() + AdminConstants.SEMICOLON);
  148. }
  149. sb.append("<b>Max. Records</b> = " + criteria.getMaxRecords()
  150. + AdminConstants.SEMICOLON);
  151. sb.append("<b>Between</b> "
  152. + DATE_FORMAT.format(criteria.getCreatedFrom())
  153. + " <b>and</b> " + DATE_FORMAT.format(criteria.getCreatedTo())
  154. + "");
  155. return sb.toString();
  156. }
  157. protected ModelAndView onSubmit(HttpServletRequest request,
  158. HttpServletResponse response, Object command, BindException errors)
  159. throws Exception {
  160. logger.debug(AdminConstants.SUBMITTING + command);
  161. NodeVisit visit = VisitUtils.getVisit(request);
  162. if (visit == null) {
  163. logger.debug(AdminConstants.UNAUTHED_ACCESS);
  164. return VisitUtils.getUnauthedView(request);
  165. }
  166. request.setAttribute(AdminConstants.COMMAND_KEY, command);
  167. String viewToReturn;
  168. try {
  169. ActivitySearchCriteria criteria = ((ActivitySearchForm) command)
  170. .getCriteria();
  171. /*
  172. * the form contains only NAASUserNames, so we swap it for the
  173. * accountId before submitting. TODO: Change the User list to be a
  174. * map and than this will not be required
  175. */
  176. String createdByName = criteria.getCreatedById();
  177. if (StringUtils.isNotBlank(createdByName)) {
  178. UserAccount account = accountService.getByUserName(
  179. createdByName, visit);
  180. criteria.setCreatedById(account.getId());
  181. }
  182. logger.debug("Search criteria: " + criteria);
  183. List results = activityService.search(criteria, visit);
  184. logger.debug("Found " + results.size() + " results.");
  185. logger.debug("Search results: " + results);
  186. request.setAttribute("result", results);
  187. if (StringUtils.isNotBlank(createdByName)) {
  188. criteria.setCreatedById(createdByName);
  189. }
  190. // Save it for later
  191. request.setAttribute("criteria", makeReadable(criteria,
  192. visit));
  193. setSearchCriteria(request, criteria);
  194. viewToReturn = getResultView();
  195. } catch (Exception ex) {
  196. logger.error(ex.getMessage(), ex);
  197. request.setAttribute(AdminConstants.ERROR_KEY, ex.getMessage());
  198. viewToReturn = getFormView();
  199. }
  200. return new ModelAndView(viewToReturn, AdminConstants.MODEL_KEY, getModel(request));
  201. }
  202. protected Map referenceData(HttpServletRequest request) throws Exception {
  203. Map modelHolder = new Hashtable();
  204. modelHolder.put(AdminConstants.MODEL_KEY, getModel(request));
  205. return modelHolder;
  206. }
  207. private Map getModel(HttpServletRequest request) throws Exception {
  208. NodeVisit visit = VisitUtils.getVisit(request);
  209. if (visit == null) {
  210. logger.debug(AdminConstants.UNAUTHED_ACCESS);
  211. return null;
  212. }
  213. Map model = new HashMap();
  214. model.put(AdminConstants.VISIT_KEY, visit);
  215. // Set the selected tab
  216. model.put(AdminConstants.TAB_KEY, SiteTabUtils.TAB_ACTIVITY);
  217. model.put("lookup", activityService.getLookups(visit));
  218. return model;
  219. }
  220. protected Object formBackingObject(HttpServletRequest request)
  221. throws ServletException {
  222. NodeVisit visit = VisitUtils.getVisit(request);
  223. ActivitySearchForm form = new ActivitySearchForm();
  224. ActivitySearchCriteria criteria = getSearchCriteria(request);
  225. if (criteria != null) {
  226. form.setCriteria(criteria);
  227. }
  228. form.setLookups(activityService.getLookups(visit));
  229. form.setExchanges(flowService.getFlows(visit, false));
  230. return form;
  231. }
  232. public void setActivityService(ActivityService activityService) {
  233. this.activityService = activityService;
  234. }
  235. public String getResultView() {
  236. return resultView;
  237. }
  238. public void setResultView(String resultView) {
  239. this.resultView = resultView;
  240. }
  241. public void setAccountService(AccountService accountService) {
  242. this.accountService = accountService;
  243. }
  244. public void setFlowService(FlowService flowService) {
  245. this.flowService = flowService;
  246. }
  247. }