src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java

changeset 71
dca186d3911f
parent 70
821c4950b619
child 73
672b5003cafe
     1.1 --- a/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Tue May 19 19:34:57 2020 +0200
     1.2 +++ b/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Fri May 22 16:21:31 2020 +0200
     1.3 @@ -230,12 +230,24 @@
     1.4       * @param req          the servlet request object
     1.5       * @param fragmentName the name of the fragment
     1.6       * @see Constants#DYN_FRAGMENT_PATH_PREFIX
     1.7 +     * @see Constants#REQ_ATTR_FRAGMENT
     1.8       */
     1.9      protected void setDynamicFragment(HttpServletRequest req, String fragmentName) {
    1.10          req.setAttribute(Constants.REQ_ATTR_FRAGMENT, Functions.dynFragmentPath(fragmentName));
    1.11      }
    1.12  
    1.13      /**
    1.14 +     * Sets the breadcrumbs menu.
    1.15 +     *
    1.16 +     * @param req         the servlet request object
    1.17 +     * @param breadcrumbs the menu entries for the breadcrumbs menu
    1.18 +     * @see Constants#REQ_ATTR_BREADCRUMBS
    1.19 +     */
    1.20 +    protected void setBreadcrumbs(HttpServletRequest req, List<MenuEntry> breadcrumbs) {
    1.21 +        req.setAttribute(Constants.REQ_ATTR_BREADCRUMBS, breadcrumbs);
    1.22 +    }
    1.23 +
    1.24 +    /**
    1.25       * @param req      the servlet request object
    1.26       * @param location the location where to redirect
    1.27       * @see Constants#REQ_ATTR_REDIRECT_LOCATION
    1.28 @@ -268,16 +280,16 @@
    1.29       * The specified type must have a single-argument constructor accepting a string to perform conversion.
    1.30       * The constructor of the specified type may throw an exception on conversion failures.
    1.31       *
    1.32 -     * @param req the servlet request object
    1.33 +     * @param req   the servlet request object
    1.34       * @param clazz the class object of the expected type
    1.35 -     * @param name the name of the parameter
    1.36 -     * @param <T> the expected type
    1.37 +     * @param name  the name of the parameter
    1.38 +     * @param <T>   the expected type
    1.39       * @return the parameter value or an empty optional, if no parameter with the specified name was found
    1.40       */
    1.41 -    protected<T> Optional<T> getParameter(HttpServletRequest req, Class<T> clazz, String name) {
    1.42 +    protected <T> Optional<T> getParameter(HttpServletRequest req, Class<T> clazz, String name) {
    1.43          final String paramValue = req.getParameter(name);
    1.44          if (paramValue == null) return Optional.empty();
    1.45 -        if (clazz.equals(String.class)) return Optional.of((T)paramValue);
    1.46 +        if (clazz.equals(String.class)) return Optional.of((T) paramValue);
    1.47          try {
    1.48              final Constructor<T> ctor = clazz.getConstructor(String.class);
    1.49              return Optional.of(ctor.newInstance(paramValue));
    1.50 @@ -290,16 +302,16 @@
    1.51      /**
    1.52       * Tries to look up an entity with a key obtained from a request parameter.
    1.53       *
    1.54 -     * @param req the servlet request object
    1.55 +     * @param req   the servlet request object
    1.56       * @param clazz the class representing the type of the request parameter
    1.57 -     * @param name the name of the request parameter
    1.58 -     * @param find the find function (typically a DAO function)
    1.59 -     * @param <T> the type of the request parameter
    1.60 -     * @param <R> the type of the looked up entity
    1.61 +     * @param name  the name of the request parameter
    1.62 +     * @param find  the find function (typically a DAO function)
    1.63 +     * @param <T>   the type of the request parameter
    1.64 +     * @param <R>   the type of the looked up entity
    1.65       * @return the retrieved entity or an empty optional if there is no such entity or the request parameter was missing
    1.66       * @throws SQLException if the find function throws an exception
    1.67       */
    1.68 -    protected<T,R> Optional<R> findByParameter(HttpServletRequest req, Class<T> clazz, String name, SQLFindFunction<? super T, ? extends R> find) throws SQLException {
    1.69 +    protected <T, R> Optional<R> findByParameter(HttpServletRequest req, Class<T> clazz, String name, SQLFindFunction<? super T, ? extends R> find) throws SQLException {
    1.70          final var param = getParameter(req, clazz, name);
    1.71          if (param.isPresent()) {
    1.72              return Optional.ofNullable(find.apply(param.get()));
    1.73 @@ -311,7 +323,13 @@
    1.74      private void forwardToFullView(HttpServletRequest req, HttpServletResponse resp)
    1.75              throws IOException, ServletException {
    1.76  
    1.77 -        req.setAttribute(Constants.REQ_ATTR_MENU, getModuleManager().getMainMenu());
    1.78 +        final var mainMenu = new ArrayList<MenuEntry>(getModuleManager().getMainMenu());
    1.79 +        for (var entry : mainMenu) {
    1.80 +            if (Functions.fullPath(req).startsWith("/" + entry.getPathName())) {
    1.81 +                entry.setActive(true);
    1.82 +            }
    1.83 +        }
    1.84 +        req.setAttribute(Constants.REQ_ATTR_MENU, mainMenu);
    1.85          req.getRequestDispatcher(SITE_JSP).forward(req, resp);
    1.86      }
    1.87  

mercurial