GridFactoryImpl.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.common.gridfactory.impl;

  20. import java.io.Serializable;
  21. import java.lang.reflect.InvocationTargetException;
  22. import java.util.List;
  23. import java.util.Objects;

  24. import org.apache.commons.beanutils.BeanUtils;
  25. import org.apache.commons.text.WordUtils;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import org.springframework.stereotype.Service;
  29. import org.vaadin.gridutil.cell.GridCellFilter;

  30. import com.hack23.cia.web.impl.ui.application.views.common.converters.ListPropertyConverter;
  31. import com.hack23.cia.web.impl.ui.application.views.common.gridfactory.api.GridFactory;
  32. import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
  33. import com.hack23.cia.web.impl.ui.application.views.pageclicklistener.PageItemRendererClickListener;
  34. import com.vaadin.data.BeanPropertySet;
  35. import com.vaadin.data.ValueProvider;
  36. import com.vaadin.event.selection.SelectionListener;
  37. import com.vaadin.ui.AbstractOrderedLayout;
  38. import com.vaadin.ui.Grid;
  39. import com.vaadin.ui.Grid.Column;
  40. import com.vaadin.ui.Grid.SelectionMode;

  41. /**
  42.  * The Class GridFactoryImpl.
  43.  */
  44. @Service
  45. public final class GridFactoryImpl implements GridFactory {

  46.     /** The Constant LOGGER. */
  47.     private static final Logger LOGGER = LoggerFactory.getLogger(GridFactoryImpl.class);

  48.     /** The Constant serialVersionUID. */
  49.     private static final long serialVersionUID = 1L;

  50.     /**
  51.      * Instantiates a new grid factory impl.
  52.      */
  53.     public GridFactoryImpl() {
  54.         super();
  55.     }

  56.     /**
  57.      * Configure column orders and hidden fields.
  58.      *
  59.      * @param columnOrder
  60.      *            the column order
  61.      * @param hideColumns
  62.      *            the hide columns
  63.      * @param grid
  64.      *            the grid
  65.      */
  66.     @SuppressWarnings("rawtypes")
  67.     private static void configureColumnOrdersAndHiddenFields(final String[] columnOrder, final String[] hideColumns,
  68.             final Grid grid) {
  69.         if (columnOrder != null) {
  70.             grid.setColumnOrder(columnOrder);
  71.         }

  72.         if (hideColumns != null) {
  73.             for (final String o : hideColumns) {
  74.                 grid.removeColumn(o);
  75.             }
  76.         }
  77.     }

  78.     /**
  79.      * Configure listeners.
  80.      *
  81.      * @param listener
  82.      *            the listener
  83.      * @param grid
  84.      *            the grid
  85.      */
  86.     @SuppressWarnings({ "unchecked", "rawtypes" })
  87.     private static void configureListeners(final SelectionListener listener, final Grid grid) {

  88.         if (listener != null) {
  89.             grid.addSelectionListener(listener);
  90.         }
  91.     }

  92.     /**
  93.      * Creates the grid cell filter.
  94.      *
  95.      * @param columnOrder
  96.      *            the column order
  97.      * @param grid
  98.      *            the grid
  99.      * @param dataType
  100.      *            the data type
  101.      */
  102.     @SuppressWarnings({ "unchecked", "rawtypes" })
  103.     private static void createGridCellFilter(final String[] columnOrder, final Grid grid, final Class dataType) {
  104.         if (columnOrder != null) {
  105.             final GridCellFilter filter = new GridCellFilter(grid, dataType);
  106.             for (final String column : columnOrder) {
  107.                 if (grid.getColumn(column) != null) {
  108.                     filter.setTextFilter(column, true, true);
  109.                 }
  110.             }
  111.         }
  112.     }

  113.     /**
  114.      * Creates the nested properties.
  115.      *
  116.      * @param <T> the generic type
  117.      * @param grid the grid
  118.      * @param nestedProperties the nested properties
  119.      */
  120.     private static <T extends Serializable> void createNestedProperties(final Grid<T> grid,
  121.             final String[] nestedProperties) {
  122.         if (nestedProperties != null) {
  123.             for (final String property : nestedProperties) {
  124.                 final Column<T, ?> addColumn = grid.addColumn(new BeanNestedPropertyValueProvider<T>(property));
  125.                 addColumn.setId(property);
  126.             }
  127.         }
  128.     }

  129.     /**
  130.      * Sets the column converters.
  131.      *
  132.      * @param collectionPropertyConverter
  133.      *            the collection property converter
  134.      * @param grid
  135.      *            the grid
  136.      */
  137.     @SuppressWarnings({ "unchecked", "rawtypes" })
  138.     private static void setColumnConverters(final ListPropertyConverter[] collectionPropertyConverter,
  139.             final Grid grid) {
  140.         if (collectionPropertyConverter != null) {
  141.             for (final ListPropertyConverter converter : collectionPropertyConverter) {
  142.                 grid.removeColumn(converter.getColumn());
  143.                 final Column column = grid.addColumn(converter);
  144.                 column.setCaption(WordUtils.capitalize(converter.getColumn()));
  145.                 column.setId(converter.getColumn());
  146.             }
  147.         }
  148.     }

  149.     @Override
  150.     public <T extends Serializable> void createBasicBeanItemGrid(final AbstractOrderedLayout panelContent,
  151.             final Class<T> dataType, final List<T> datasource, final String caption, final String[] columnOrder,
  152.             final String[] hideColumns, final PageItemRendererClickListener<?> listener, final String actionId,
  153.             final ListPropertyConverter[] collectionPropertyConverters) {
  154.         createBasicBeanItemNestedPropertiesGrid(panelContent, dataType, datasource, caption, null, columnOrder,
  155.                 hideColumns, listener, actionId, collectionPropertyConverters);

  156.     }

  157.     @Override
  158.     public <T extends Serializable> void createBasicBeanItemNestedPropertiesGrid(
  159.             final AbstractOrderedLayout panelContent, final Class<T> dataType, final List<T> datasource,
  160.             final String caption, final String[] nestedProperties, final String[] columnOrder,
  161.             final String[] hideColumns, final PageItemRendererClickListener<?> listener, final String actionId,
  162.             final ListPropertyConverter[] collectionPropertyConverters) {

  163.         final Grid<T> grid = Grid.withPropertySet(BeanPropertySet.get(dataType));
  164.         grid.setCaption(caption);

  165.         grid.setItems(datasource.stream().filter(Objects::nonNull).toList());

  166.         grid.setSelectionMode(SelectionMode.SINGLE);

  167.         createNestedProperties(grid, nestedProperties);

  168.         setColumnConverters(collectionPropertyConverters, grid);

  169.         configureColumnOrdersAndHiddenFields(columnOrder, hideColumns, grid);

  170.         configureListeners(listener, grid);


  171.         grid.setSizeFull();

  172.         grid.setStyleName("Level2Header");

  173.         createGridCellFilter(columnOrder, grid, dataType);

  174.         grid.setResponsive(true);

  175.         panelContent.addComponent(grid);
  176.         panelContent.setExpandRatio(grid, ContentRatio.GRID);
  177.     }

  178.     /**
  179.      * The Class BeanNestedPropertyValueProvider.
  180.      *
  181.      * @param <T>
  182.      *            the generic type
  183.      */
  184.     public static final class BeanNestedPropertyValueProvider<T> implements ValueProvider<T, String> {

  185.         /** The Constant serialVersionUID. */
  186.         private static final long serialVersionUID = 1L;

  187.         /** The property. */
  188.         private final String property;

  189.         /**
  190.          * Instantiates a new bean nested property value provider.
  191.          *
  192.          * @param property
  193.          *            the property
  194.          */
  195.         public BeanNestedPropertyValueProvider(final String property) {
  196.             super();
  197.             this.property = property;
  198.         }

  199.         @Override
  200.         public String apply(final T source) {
  201.             try {
  202.                 return BeanUtils.getProperty(source, property);
  203.             } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
  204.                 LOGGER.warn("Problem getting property : {} from source : {} , exception: {}", property, source, e);
  205.                 return "";
  206.             }
  207.         }

  208.     }

  209. }