View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.controllers.sql;
12  
13  import jakarta.servlet.http.HttpServletRequest;
14  import jakarta.servlet.http.HttpServletResponse;
15  import jakarta.servlet.http.HttpSession;
16  
17  import java.util.List;
18  import java.util.Map;
19  
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  import org.springframework.beans.factory.annotation.Value;
23  import org.springframework.stereotype.Controller;
24  import org.springframework.web.bind.ServletRequestUtils;
25  import org.springframework.web.bind.annotation.RequestMapping;
26  import org.springframework.web.servlet.ModelAndView;
27  
28  import psiprobe.PostParameterizableViewController;
29  import psiprobe.model.sql.DataSourceTestInfo;
30  
31  /**
32   * Displays a result set cached in an attribute of HttpSession object to support result set
33   * pagination feature without re-executing a query that created the result set.
34   */
35  @Controller
36  public class CachedRecordSetController extends PostParameterizableViewController {
37  
38    /** The Constant logger. */
39    private static final Logger logger = LoggerFactory.getLogger(CachedRecordSetController.class);
40  
41    @RequestMapping(path = "/sql/cachedRecordset.ajax")
42    @Override
43    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
44        throws Exception {
45      return super.handleRequest(request, response);
46    }
47  
48    @Override
49    protected ModelAndView handleRequestInternal(HttpServletRequest request,
50        HttpServletResponse response) throws Exception {
51  
52      int rowsPerPage = ServletRequestUtils.getIntParameter(request, "rowsPerPage", 0);
53      List<Map<String, String>> results = null;
54      int rowsAffected = 0;
55      HttpSession sess = request.getSession(false);
56  
57      if (sess == null) {
58        request.setAttribute("errorMessage", getMessageSourceAccessor()
59            .getMessage("probe.src.dataSourceTest.cachedResultSet.failure"));
60        logger.error("Cannot retrieve a cached result set. Http session is NULL.");
61      } else {
62        DataSourceTestInfo sessData =
63            (DataSourceTestInfo) sess.getAttribute(DataSourceTestInfo.DS_TEST_SESS_ATTR);
64  
65        if (sessData == null) {
66          request.setAttribute("errorMessage", getMessageSourceAccessor()
67              .getMessage("probe.src.dataSourceTest.cachedResultSet.failure"));
68          logger.error("Cannot retrieve a cached result set. {} session attribute is NULL.",
69              DataSourceTestInfo.DS_TEST_SESS_ATTR);
70        } else {
71          synchronized (sess) {
72            sessData.setRowsPerPage(rowsPerPage);
73          }
74  
75          results = sessData.getResults();
76  
77          if (results == null) {
78            request.setAttribute("errorMessage", getMessageSourceAccessor()
79                .getMessage("probe.src.dataSourceTest.cachedResultSet.failure"));
80            logger.error("Cached results set is NULL.");
81          } else {
82            rowsAffected = results.size();
83          }
84        }
85      }
86  
87      ModelAndView mv = new ModelAndView(getViewName(), "results", results);
88      mv.addObject("rowsAffected", String.valueOf(rowsAffected));
89      mv.addObject("rowsPerPage", String.valueOf(rowsPerPage));
90  
91      return mv;
92    }
93  
94    @Value("ajax/sql/recordset")
95    @Override
96    public void setViewName(String viewName) {
97      super.setViewName(viewName);
98    }
99  
100 }