GovernmentBodyOverviewPageModContentFactoryImpl.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.govermentbody.pagemode;

  20. import java.util.List;

  21. import org.springframework.security.access.annotation.Secured;
  22. import org.springframework.stereotype.Component;

  23. import com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup;
  24. import com.hack23.cia.service.external.esv.api.GovernmentBodyAnnualSummary;
  25. import com.hack23.cia.web.impl.ui.application.action.ViewAction;
  26. import com.hack23.cia.web.impl.ui.application.views.common.menufactory.api.pagecommands.PageCommandGovernmentBodyConstants;
  27. import com.hack23.cia.web.impl.ui.application.views.common.pagemode.CardInfoRowUtil;
  28. import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
  29. import com.vaadin.icons.VaadinIcons;
  30. import com.vaadin.server.Responsive;
  31. import com.vaadin.ui.HorizontalLayout;
  32. import com.vaadin.ui.Layout;
  33. import com.vaadin.ui.MenuBar;
  34. import com.vaadin.ui.Panel;
  35. import com.vaadin.ui.VerticalLayout;

  36. /**
  37.  * The Class GovernmentBodyOverviewPageModContentFactoryImpl.
  38.  */
  39. @Component
  40. public final class GovernmentBodyOverviewPageModContentFactoryImpl
  41.         extends AbstractGovernmentBodyPageModContentFactoryImpl {

  42.     /**
  43.      * Instantiates a new government body overview page mod content factory impl.
  44.      */
  45.     public GovernmentBodyOverviewPageModContentFactoryImpl() {
  46.         super();
  47.     }

  48.     @Secured({ "ROLE_ANONYMOUS", "ROLE_USER", "ROLE_ADMIN" })
  49.     @Override
  50.     public Layout createContent(final String parameters, final MenuBar menuBar, final Panel panel) {
  51.         final VerticalLayout panelContent = createPanelContent();
  52.         panel.setContent(panelContent);

  53.         final String pageId = getPageId(parameters);
  54.         final List<GovernmentBodyAnnualSummary> item = getItem(parameters);

  55.         if (item != null && !item.isEmpty()) {
  56.             final GovernmentBodyAnnualSummary governmentBodyAnnualSummary = item.get(0);

  57.             getGovernmentBodyMenuItemFactory().createGovernmentBodyMenuBar(menuBar, pageId, governmentBodyAnnualSummary.getName());

  58.             createPageHeader(panel, panelContent, governmentBodyAnnualSummary);
  59.             createMainCard(panelContent, governmentBodyAnnualSummary, pageId);

  60.             getPageActionEventHelper().createPageEvent(ViewAction.VISIT_GOVERNMENT_BODY_VIEW,
  61.                     ApplicationEventGroup.USER, NAME, parameters, pageId);
  62.         }

  63.         return panelContent;
  64.     }

  65.     /**
  66.      * Creates the page header.
  67.      *
  68.      * @param panel the panel
  69.      * @param panelContent the panel content
  70.      * @param govBody the gov body
  71.      */
  72.     private void createPageHeader(final Panel panel, final VerticalLayout panelContent, final GovernmentBodyAnnualSummary govBody) {
  73.         CardInfoRowUtil.createPageHeader(panel, panelContent,
  74.             formatTitle(govBody, GovernmentBodyDescriptionConstants.OVERVIEW_HEADER),
  75.             GovernmentBodyDescriptionConstants.OVERVIEW_SUBTITLE,
  76.             GovernmentBodyDescriptionConstants.OVERVIEW_DESC);
  77.     }

  78.     /**
  79.      * Creates the main card.
  80.      *
  81.      * @param panelContent the panel content
  82.      * @param govBody the gov body
  83.      * @param pageId the page id
  84.      */
  85.     private void createMainCard(final VerticalLayout panelContent, final GovernmentBodyAnnualSummary govBody, final String pageId) {
  86.         final Panel cardPanel = createCardPanel();
  87.         final VerticalLayout cardContent = createCardContent(cardPanel);
  88.         panelContent.addComponent(cardPanel);
  89.         panelContent.setExpandRatio(cardPanel, ContentRatio.SMALL_GRID);

  90.         CardInfoRowUtil.createCardHeader(cardContent, GovernmentBodyViewConstants.BODY_INFO);

  91.         final HorizontalLayout attributesLayout = createAttributesLayout();
  92.         cardContent.addComponent(attributesLayout);

  93.         addProfileAndStatsColumns(attributesLayout, govBody);
  94.         addOverviewLayout(panelContent, pageId);
  95.     }

  96.     /**
  97.      * Creates the card panel.
  98.      *
  99.      * @return the panel
  100.      */
  101.     private Panel createCardPanel() {
  102.         final Panel cardPanel = new Panel();
  103.         cardPanel.addStyleName(GovernmentBodyViewConstants.STYLE_GOVERNMENT_BODY_CARD);
  104.         cardPanel.setWidth(GovernmentBodyViewConstants.WIDTH_100_PERCENT);
  105.         cardPanel.setHeightUndefined();
  106.         Responsive.makeResponsive(cardPanel);
  107.         return cardPanel;
  108.     }

  109.     /**
  110.      * Creates the card content.
  111.      *
  112.      * @param cardPanel the card panel
  113.      * @return the vertical layout
  114.      */
  115.     private VerticalLayout createCardContent(final Panel cardPanel) {
  116.         final VerticalLayout cardContent = new VerticalLayout();
  117.         cardContent.setMargin(GovernmentBodyLayoutConstants.USE_MARGIN);
  118.         cardContent.setSpacing(GovernmentBodyLayoutConstants.USE_SPACING);
  119.         cardContent.setWidth(GovernmentBodyLayoutConstants.WIDTH_100_PERCENT);
  120.         cardPanel.setContent(cardContent);
  121.         return cardContent;
  122.     }

  123.     /**
  124.      * Creates the attributes layout.
  125.      *
  126.      * @return the horizontal layout
  127.      */
  128.     private HorizontalLayout createAttributesLayout() {
  129.         final HorizontalLayout attributesLayout = new HorizontalLayout();
  130.         attributesLayout.setSpacing(GovernmentBodyLayoutConstants.USE_SPACING);
  131.         attributesLayout.setWidth(GovernmentBodyLayoutConstants.WIDTH_100_PERCENT);
  132.         return attributesLayout;
  133.     }

  134.     /**
  135.      * Adds the profile and stats columns.
  136.      *
  137.      * @param attributesLayout the attributes layout
  138.      * @param govBody the gov body
  139.      */
  140.     private void addProfileAndStatsColumns(final HorizontalLayout attributesLayout, final GovernmentBodyAnnualSummary govBody) {
  141.         final VerticalLayout profileLayout = createProfileColumn(govBody);
  142.         final VerticalLayout statsLayout = createStatsColumn(govBody);

  143.         attributesLayout.addComponents(profileLayout, statsLayout);
  144.     }

  145.     /**
  146.      * Creates the profile column.
  147.      *
  148.      * @param govBody the gov body
  149.      * @return the vertical layout
  150.      */
  151.     private VerticalLayout createProfileColumn(final GovernmentBodyAnnualSummary govBody) {
  152.         final VerticalLayout profileLayout = CardInfoRowUtil.createSectionLayout(
  153.             GovernmentBodyViewConstants.ORG_PROFILE);

  154.         addProfileDetails(profileLayout, govBody);
  155.         return profileLayout;
  156.     }

  157.     /**
  158.      * Adds the profile details.
  159.      *
  160.      * @param profileLayout the profile layout
  161.      * @param govBody the gov body
  162.      */
  163.     private void addProfileDetails(final VerticalLayout profileLayout, final GovernmentBodyAnnualSummary govBody) {
  164.         profileLayout.addComponents(
  165.             createProfileInfoRow(GovernmentBodyHeaderConstants.NAME_FIELD,
  166.                 govBody.getName(),
  167.                 GovernmentBodyIconConstants.INFO_ICON,
  168.                 GovernmentBodyHeaderConstants.NAME_DESC),
  169.             createProfileInfoRow(GovernmentBodyHeaderConstants.ID_FIELD,
  170.                 govBody.getGovermentBodyId(),
  171.                 GovernmentBodyIconConstants.USER_ICON,
  172.                 GovernmentBodyHeaderConstants.ID_DESC),
  173.             // ...add other profile fields...
  174.             createProfileInfoRow(GovernmentBodyHeaderConstants.VAT_FIELD,
  175.                 govBody.getVat(),
  176.                 GovernmentBodyIconConstants.MONEY_ICON,
  177.                 GovernmentBodyHeaderConstants.VAT_DESC)
  178.         );
  179.     }

  180.     /**
  181.      * Creates the profile info row.
  182.      *
  183.      * @param label the label
  184.      * @param value the value
  185.      * @param icon the icon
  186.      * @param description the description
  187.      * @return the com.vaadin.ui. component
  188.      */
  189.     private com.vaadin.ui.Component createProfileInfoRow(final String label, final String value, final VaadinIcons icon, final String description) {
  190.         return CardInfoRowUtil.createInfoRow(label, value, icon, description);
  191.     }

  192.     /**
  193.      * Creates the stats column.
  194.      *
  195.      * @param govBody the gov body
  196.      * @return the vertical layout
  197.      */
  198.     private VerticalLayout createStatsColumn(final GovernmentBodyAnnualSummary govBody) {
  199.         final VerticalLayout statsLayout = CardInfoRowUtil.createSectionLayout(
  200.             GovernmentBodyViewConstants.ANNUAL_STATS);

  201.         addStatsDetails(statsLayout, govBody);
  202.         return statsLayout;
  203.     }

  204.     /**
  205.      * Adds the stats details.
  206.      *
  207.      * @param statsLayout the stats layout
  208.      * @param govBody the gov body
  209.      */
  210.     private void addStatsDetails(final VerticalLayout statsLayout, final GovernmentBodyAnnualSummary govBody) {
  211.         statsLayout.addComponents(
  212.             createStatsInfoRow(GovernmentBodyHeaderConstants.YEAR_FIELD,
  213.                 formatYear(govBody.getYear()),
  214.                 GovernmentBodyIconConstants.CALENDAR_ICON,
  215.                 GovernmentBodyHeaderConstants.YEAR_DESC),
  216.             createStatsInfoRow(GovernmentBodyHeaderConstants.HEAD_COUNT_FIELD,
  217.                 formatCount(govBody.getHeadCount()),
  218.                 GovernmentBodyIconConstants.GROUP_ICON,
  219.                 GovernmentBodyHeaderConstants.HEAD_COUNT_DESC),
  220.             createStatsInfoRow(GovernmentBodyHeaderConstants.ANNUAL_HEADCOUNT_FIELD,
  221.                 formatCount(govBody.getAnnualWorkHeadCount()),
  222.                 GovernmentBodyIconConstants.USER_CHECK_ICON,
  223.                 GovernmentBodyHeaderConstants.ANNUAL_HEAD_DESC)
  224.         );

  225.         if (govBody.getComment() != null && !govBody.getComment().isEmpty()) {
  226.             statsLayout.addComponent(createStatsInfoRow(
  227.                 GovernmentBodyHeaderConstants.COMMENT_FIELD,
  228.                 govBody.getComment(),
  229.                 GovernmentBodyIconConstants.COMMENT_ICON,
  230.                 GovernmentBodyHeaderConstants.COMMENT_DESC));
  231.         }
  232.     }

  233.     /**
  234.      * Creates the stats info row.
  235.      *
  236.      * @param label the label
  237.      * @param value the value
  238.      * @param icon the icon
  239.      * @param description the description
  240.      * @return the com.vaadin.ui. component
  241.      */
  242.     private com.vaadin.ui.Component createStatsInfoRow(final String label, final String value, final VaadinIcons icon, final String description) {
  243.         return CardInfoRowUtil.createInfoRow(label, value, icon, description);
  244.     }

  245.     /**
  246.      * Adds the overview layout.
  247.      *
  248.      * @param panelContent the panel content
  249.      * @param pageId the page id
  250.      */
  251.     private void addOverviewLayout(final VerticalLayout panelContent, final String pageId) {
  252.         final VerticalLayout overviewLayout = new VerticalLayout();
  253.         overviewLayout.setSizeFull();
  254.         panelContent.addComponent(overviewLayout);
  255.         panelContent.setExpandRatio(overviewLayout, ContentRatio.LARGE_FORM);
  256.         getGovernmentBodyMenuItemFactory().createOverviewPage(overviewLayout, pageId);
  257.     }

  258.     @Override
  259.     public boolean matches(final String page, final String parameters) {
  260.         return  PageCommandGovernmentBodyConstants.COMMAND_GOVERNMENT_BODY_OVERVIEW.matches(page, parameters);
  261.     }

  262. }