UserHomeOverviewPageModContentFactoryImpl.java

  1. /*
  2.  * Copyright 2010-2025 James Pether Sörling
  3.  *
  4.  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
  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.  *  $Id$
  17.  *  $HeadURL$
  18. */
  19. package com.hack23.cia.web.impl.ui.application.views.user.home.pagemode;

  20. import java.util.Optional;

  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.security.access.annotation.Secured;
  23. import org.springframework.stereotype.Component;
  24. import org.springframework.web.context.request.RequestContextHolder;

  25. import com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup;
  26. import com.hack23.cia.model.internal.application.user.impl.UserAccount;
  27. import com.hack23.cia.service.api.action.application.LogoutRequest;
  28. import com.hack23.cia.web.impl.ui.application.action.ViewAction;
  29. import com.hack23.cia.web.impl.ui.application.views.common.menufactory.api.UserHomeMenuItemFactory;
  30. import com.hack23.cia.web.impl.ui.application.views.common.menufactory.api.pagecommands.PageCommandUserHomeConstants;
  31. import com.hack23.cia.web.impl.ui.application.views.common.pagemode.CardInfoRowUtil;
  32. import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
  33. import com.hack23.cia.web.impl.ui.application.views.pageclicklistener.LogoutClickListener;
  34. import com.vaadin.icons.VaadinIcons;
  35. import com.vaadin.server.Responsive;
  36. import com.vaadin.ui.Button;
  37. import com.vaadin.ui.HorizontalLayout;
  38. import com.vaadin.ui.Layout;
  39. import com.vaadin.ui.MenuBar;
  40. import com.vaadin.ui.Panel;
  41. import com.vaadin.ui.VerticalLayout;

  42. /**
  43.  * The Class UserHomeOverviewPageModContentFactoryImpl.
  44.  */
  45. @Component
  46. public final class UserHomeOverviewPageModContentFactoryImpl extends AbstractUserHomePageModContentFactoryImpl {

  47.     /** The user home menu item factory. */
  48.     @Autowired
  49.     private UserHomeMenuItemFactory userHomeMenuItemFactory;

  50.     /**
  51.      * Instantiates a new user home overview page mod content factory impl.
  52.      */
  53.     public UserHomeOverviewPageModContentFactoryImpl() {
  54.         super();
  55.     }

  56.     /**
  57.      * Creates the content.
  58.      *
  59.      * @param parameters the parameters
  60.      * @param menuBar the menu bar
  61.      * @param panel the panel
  62.      * @return the layout
  63.      */
  64.     @Secured({ "ROLE_USER", "ROLE_ADMIN" })
  65.     @Override
  66.     public Layout createContent(final String parameters, final MenuBar menuBar, final Panel panel) {
  67.         final VerticalLayout panelContent = createPanelContent();
  68.         panel.setContent(panelContent);

  69.         final String pageId = getPageId(parameters);
  70.         final Optional<UserAccount> userAccount = getActiveUserAccount();

  71.         if (userAccount.isPresent()) {
  72.             userHomeMenuItemFactory.createUserHomeMenuBar(menuBar, pageId);

  73.             CardInfoRowUtil.createPageHeader(panel, panelContent,
  74.                 UserHomeViewConstants.TITLE_PREFIX + UserHomeViewConstants.OVERVIEW_TITLE,
  75.                 UserHomeViewConstants.OVERVIEW_TITLE,
  76.                 UserHomeViewConstants.OVERVIEW_DESC);

  77.             final Button logoutButton = new Button(UserHomeViewConstants.BUTTON_LOGOUT, VaadinIcons.SIGN_OUT);
  78.             final LogoutRequest logoutRequest = new LogoutRequest();
  79.             logoutRequest.setSessionId(RequestContextHolder.currentRequestAttributes().getSessionId());
  80.             logoutButton.addClickListener(new LogoutClickListener(logoutRequest));
  81.             panelContent.addComponent(logoutButton);

  82.             // Create a card-style panel for user details
  83.             final Panel cardPanel = new Panel();
  84.             cardPanel.addStyleName("politician-overview-card");
  85.             cardPanel.setWidth("100%");
  86.             cardPanel.setHeightUndefined();
  87.             Responsive.makeResponsive(cardPanel);

  88.             final VerticalLayout cardContent = new VerticalLayout();
  89.             cardContent.setMargin(true);
  90.             cardContent.setSpacing(true);
  91.             cardContent.setWidth("100%");
  92.             cardPanel.setContent(cardContent);

  93.             panelContent.addComponent(cardPanel);
  94.             panelContent.setExpandRatio(cardPanel, ContentRatio.SMALL_GRID);

  95.             CardInfoRowUtil.createCardHeader(cardContent,"User Account Information");

  96.             // Two-column layout for user attributes
  97.             final HorizontalLayout attributesLayout = new HorizontalLayout();
  98.             attributesLayout.setSpacing(true);
  99.             attributesLayout.setWidth("100%");
  100.             cardContent.addComponent(attributesLayout);

  101.             // Left column: Basic Profile
  102.             final VerticalLayout profileLayout = CardInfoRowUtil.createSectionLayout(UserHomeViewConstants.PROFILE_SECTION_TITLE);

  103.             // Display key fields from user account in Profile Details
  104.             final UserAccount account = userAccount.get();
  105.             profileLayout.addComponent(CardInfoRowUtil.createInfoRow(
  106.                 UserHomeViewConstants.USERNAME_LABEL,
  107.                 account.getUsername(),
  108.                 VaadinIcons.USER,
  109.                 UserHomeViewConstants.USERNAME_DESC));
  110.             profileLayout.addComponent(CardInfoRowUtil.createInfoRow(
  111.                 UserHomeViewConstants.EMAIL_LABEL,
  112.                 account.getEmail(),
  113.                 VaadinIcons.ENVELOPE_O,
  114.                 UserHomeViewConstants.EMAIL_DESC));
  115.             profileLayout.addComponent(CardInfoRowUtil.createInfoRow(
  116.                 UserHomeViewConstants.COUNTRY_LABEL,
  117.                 account.getCountry(),
  118.                 VaadinIcons.GLOBE,
  119.                 UserHomeViewConstants.COUNTRY_DESC));
  120.             profileLayout.addComponent(CardInfoRowUtil.createInfoRow(
  121.                 UserHomeViewConstants.CREATED_DATE_LABEL,
  122.                 String.valueOf(account.getCreatedDate()),
  123.                 VaadinIcons.CALENDAR,
  124.                 UserHomeViewConstants.CREATED_DATE_DESC));

  125.             // Right column: Status & Statistics
  126.             final VerticalLayout statusLayout = CardInfoRowUtil.createSectionLayout(UserHomeViewConstants.STATUS_SECTION_TITLE);

  127.             statusLayout.addComponent(CardInfoRowUtil.createInfoRow(
  128.                 UserHomeViewConstants.USER_TYPE_LABEL,
  129.                 account.getUserType().toString(),
  130.                 VaadinIcons.INFO_CIRCLE,
  131.                 UserHomeViewConstants.USER_TYPE_DESC));
  132.             statusLayout.addComponent(CardInfoRowUtil.createInfoRow("User Role:", account.getUserRole().toString(), VaadinIcons.USER_CHECK, "Your assigned role in the system"));
  133.             statusLayout.addComponent(CardInfoRowUtil.createInfoRow("Email Status:", account.getUserEmailStatus().toString(), VaadinIcons.ENVELOPE, "Status of email verification"));
  134.             statusLayout.addComponent(CardInfoRowUtil.createInfoRow("Number of Visits:", String.valueOf(account.getNumberOfVisits()), VaadinIcons.CHART, "How many times you have visited"));

  135.             attributesLayout.addComponents(profileLayout, statusLayout);

  136.             panelContent.setExpandRatio(logoutButton, ContentRatio.SMALL);

  137.             // Overview layout after card
  138.             final VerticalLayout overviewLayout = new VerticalLayout();
  139.             overviewLayout.setSizeFull();

  140.             panelContent.addComponent(overviewLayout);
  141.             panelContent.setExpandRatio(overviewLayout, ContentRatio.LARGE_FORM);

  142.             userHomeMenuItemFactory.createOverviewPage(overviewLayout);
  143.         }

  144.         getPageActionEventHelper().createPageEvent(ViewAction.VISIT_USER_HOME_VIEW, ApplicationEventGroup.USER,
  145.                 NAME, parameters, pageId);

  146.         return panelContent;

  147.     }

  148.     /**
  149.      * Matches.
  150.      *
  151.      * @param page the page
  152.      * @param parameters the parameters
  153.      * @return true, if successful
  154.      */
  155.     @Override
  156.     public boolean matches(final String page, final String parameters) {
  157.         return PageCommandUserHomeConstants.COMMAND_USERHOME_OVERVIEW.matches(page, parameters);
  158.     }

  159. }