AdminCountryPageModContentFactoryImpl.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.admin.system.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.external.worldbank.countries.impl.CountryElement;
  24. import com.hack23.cia.model.external.worldbank.countries.impl.CountryElement_;
  25. import com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup;
  26. import com.hack23.cia.service.api.DataContainer;
  27. import com.hack23.cia.web.impl.ui.application.action.ViewAction;
  28. import com.hack23.cia.web.impl.ui.application.views.admin.AdminViewConstants;
  29. import com.hack23.cia.web.impl.ui.application.views.common.pagemode.CardInfoRowUtil;
  30. import com.hack23.cia.web.impl.ui.application.views.common.viewnames.AdminViews;
  31. import com.hack23.cia.web.impl.ui.application.views.pageclicklistener.PageItemPropertyClickListener;
  32. import com.vaadin.icons.VaadinIcons;
  33. import com.vaadin.server.Responsive;
  34. import com.vaadin.ui.Layout;
  35. import com.vaadin.ui.MenuBar;
  36. import com.vaadin.ui.Panel;
  37. import com.vaadin.ui.VerticalLayout;

  38. /**
  39.  * The Class AdminCountryPageModContentFactoryImpl.
  40.  */
  41. @Component
  42. public final class AdminCountryPageModContentFactoryImpl extends AbstractAdminSystemPageModContentFactoryImpl {

  43.     /** The Constant COLUMN_ORDER. */
  44.     private static final String[] COLUMN_ORDER = {
  45.             "hjid",
  46.             "id",
  47.             "countryName",
  48.             "iso2Code",
  49.             "capitalCity",
  50.             "longitude",
  51.             "latitude"
  52.     };

  53.     /** The Constant COUNTRY2. */
  54.     private static final String COUNTRY2 = "Country";

  55.     /** The Constant HIDE_COLUMNS. */
  56.     private static final String[] HIDE_COLUMNS = {
  57.             "hjid",
  58.             "id",
  59.             "region",
  60.             "adminregion",
  61.             "incomeLevel",
  62.             "lendingType",
  63.             "longitude",
  64.             "latitude"
  65.     };

  66.     /** The Constant LISTENER. */
  67.     private static final PageItemPropertyClickListener LISTENER = new PageItemPropertyClickListener(AdminViews.ADMIN_COUNTRY_VIEW_NAME, "hjid");

  68.     /** The Constant NAME. */
  69.     public static final String NAME = AdminViews.ADMIN_COUNTRY_VIEW_NAME;

  70.     /**
  71.      * Instantiates a new admin country page mod content factory impl.
  72.      */
  73.     public AdminCountryPageModContentFactoryImpl() {
  74.         super(NAME);
  75.     }

  76.     @Secured({ "ROLE_ADMIN" })
  77.     @Override
  78.     public Layout createContent(final String parameters, final MenuBar menuBar, final Panel panel) {
  79.         final VerticalLayout content = createPanelContent();
  80.         content.setSizeFull();
  81.         content.addStyleName("admin-country-content");

  82.         final String pageId = getPageId(parameters);
  83.         final int pageNr = getPageNr(parameters);

  84.         getMenuItemFactory().createMainPageMenuBar(menuBar);

  85.         CardInfoRowUtil.createPageHeader(panel, content,
  86.                  AdminViewConstants.ADMIN_COUNTRY_MANAGEMENT,
  87.                  AdminViewConstants.COUNTRY_OVERVIEW,
  88.                  AdminViewConstants.COUNTRY_OVERVIEW_DESCRIPTION);

  89.         final DataContainer<CountryElement, Long> dataContainer = getApplicationManager()
  90.                 .getDataContainer(CountryElement.class);

  91.         final List<CountryElement> pageOrderBy = dataContainer.getPageOrderBy(
  92.                 pageNr,
  93.                 DEFAULT_RESULTS_PER_PAGE,
  94.                 CountryElement_.countryName);

  95.         getPagingUtil().createPagingControls(
  96.                 content,
  97.                 NAME,
  98.                 pageId,
  99.                 dataContainer.getSize(),
  100.                 pageNr,
  101.                 DEFAULT_RESULTS_PER_PAGE);

  102.         getGridFactory()
  103.                 .createBasicBeanItemNestedPropertiesGrid(
  104.                         content,
  105.                         CountryElement.class,
  106.                         pageOrderBy,
  107.                         COUNTRY2,
  108.                         null,
  109.                         COLUMN_ORDER,
  110.                         HIDE_COLUMNS,
  111.                         LISTENER,
  112.                         null,
  113.                         null);

  114.         if (pageId != null && !pageId.isEmpty()) {
  115.             final CountryElement country = dataContainer.load(Long.valueOf(pageId));
  116.             if (country != null) {

  117.                 // Create a card panel for country details
  118.                 final Panel cardPanel = new Panel();
  119.                 cardPanel.addStyleName("politician-overview-card");
  120.                 cardPanel.setWidth("100%");
  121.                 cardPanel.setHeightUndefined();
  122.                 Responsive.makeResponsive(cardPanel);

  123.                 final VerticalLayout cardContent = new VerticalLayout();
  124.                 cardContent.setMargin(true);
  125.                 cardContent.setSpacing(true);
  126.                 cardContent.setWidth("100%");
  127.                 cardPanel.setContent(cardContent);

  128.                 content.addComponent(cardPanel);

  129.                 CardInfoRowUtil.createCardHeader(cardContent,"Country Details");

  130.                 // Attributes layout
  131.                 final VerticalLayout attributesLayout = new VerticalLayout();
  132.                 attributesLayout.setSpacing(true);
  133.                 attributesLayout.setWidth("100%");
  134.                 cardContent.addComponent(attributesLayout);

  135.                 // Show fields if not null or empty
  136.                 CardInfoRowUtil.addInfoRowIfNotNull(attributesLayout, "Country ID:", country.getId(), VaadinIcons.FLAG_O);
  137.                 CardInfoRowUtil.addInfoRowIfNotNull(attributesLayout, "Name:", country.getCountryName(), VaadinIcons.GLOBE);
  138.                 CardInfoRowUtil.addInfoRowIfNotNull(attributesLayout, "ISO2 Code:", country.getIso2Code(), VaadinIcons.CODE);
  139.                 CardInfoRowUtil.addInfoRowIfNotNull(attributesLayout, "Capital:", country.getCapitalCity(), VaadinIcons.BUILDING);
  140.                 CardInfoRowUtil.addInfoRowIfNotNull(attributesLayout, "Longitude:", country.getLongitude(), VaadinIcons.ARROWS_LONG_H);
  141.                 CardInfoRowUtil.addInfoRowIfNotNull(attributesLayout, "Latitude:", country.getLatitude(), VaadinIcons.ARROWS_LONG_H);
  142.             }
  143.         }

  144.         getPageActionEventHelper().createPageEvent(
  145.                 ViewAction.VISIT_ADMIN_COUNTRY_VIEW,
  146.                 ApplicationEventGroup.ADMIN,
  147.                 NAME,
  148.                 null,
  149.                 pageId);

  150.         return content;
  151.     }

  152. }