ListPropertyConverter.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.converters;

  20. import java.lang.reflect.InvocationTargetException;
  21. import java.util.ArrayList;
  22. import java.util.List;

  23. import org.apache.commons.beanutils.BeanUtils;
  24. import org.apache.commons.beanutils.PropertyUtilsBean;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;

  27. import com.vaadin.data.Converter;
  28. import com.vaadin.data.Result;
  29. import com.vaadin.data.ValueContext;
  30. import com.vaadin.data.ValueProvider;

  31. /**
  32.  * The Class ListPropertyRenderer.
  33.  */
  34. public final class ListPropertyConverter implements Converter<String, List<?>>,ValueProvider<Object, String> {

  35.     /** The Constant CONTENT_SEPARATOR. */
  36.     private static final char CONTENT_SEPARATOR = ' ';

  37.     /** The Constant END_TAG. */
  38.     private static final char END_TAG = ']';

  39.     /** The Constant LOGGER. */
  40.     private static final Logger LOGGER = LoggerFactory.getLogger(ListPropertyConverter.class);

  41.     /** The Constant PROPERTY_UTILS_BEAN. */
  42.     private static final PropertyUtilsBean PROPERTY_UTILS_BEAN= new PropertyUtilsBean();

  43.     /** The Constant serialVersionUID. */
  44.     private static final long serialVersionUID = 1L;

  45.     /** The Constant START_TAG. */
  46.     private static final char START_TAG = '[';

  47.     /** The column. */
  48.     private final String column;

  49.     /** The fallback column. */
  50.     private final String fallbackColumn;

  51.     /** The property. */
  52.     private final String property;

  53.     /**
  54.      * Instantiates a new collection property converter.
  55.      * @param property
  56.      *            the property
  57.      * @param column
  58.      *            the column
  59.      */
  60.     public ListPropertyConverter(final String property, final String column) {
  61.         super();
  62.         this.property = property;
  63.         this.column = column;
  64.         this.fallbackColumn = null;
  65.     }

  66.     /**
  67.      * Instantiates a new list property converter.
  68.      * @param property
  69.      *            the property
  70.      * @param column
  71.      *            the column
  72.      * @param fallbackColumn
  73.      *            the fallback column
  74.      */
  75.     public ListPropertyConverter(final String property, final String column, final String fallbackColumn) {
  76.         super();
  77.         this.property = property;
  78.         this.column = column;
  79.         this.fallbackColumn = fallbackColumn;
  80.     }


  81.     /**
  82.      * Adds the fallback value.
  83.      *
  84.      * @param stringBuilder the string builder
  85.      * @param object        the object
  86.      * @throws IllegalAccessException    the illegal access exception
  87.      * @throws InvocationTargetException the invocation target exception
  88.      * @throws NoSuchMethodException     the no such method exception
  89.      */
  90.     private void addFallbackValue(final StringBuilder stringBuilder, final Object object)
  91.             throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
  92.         if (fallbackColumn != null) {
  93.             final String beanPropertyFallBack = BeanUtils.getProperty(object, fallbackColumn);
  94.             if (beanPropertyFallBack != null) {
  95.                 stringBuilder.append(beanPropertyFallBack);
  96.             }
  97.         }
  98.     }

  99.     /**
  100.      * Append object presentation.
  101.      *
  102.      * @param stringBuilder
  103.      *            the string builder
  104.      * @param object
  105.      *            the object
  106.      */
  107.     private void appendObjectPresentation(final StringBuilder stringBuilder, final Object object) {
  108.         try {
  109.             final String beanProperty = BeanUtils.getProperty(object, property);

  110.             if (beanProperty != null) {
  111.                 stringBuilder.append(beanProperty);
  112.             } else {
  113.                 addFallbackValue(stringBuilder, object);
  114.             }

  115.         } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
  116.             LOGGER.warn("Problem getting property {}, object {} , exception {}", property, object, e);
  117.         }
  118.         stringBuilder.append(CONTENT_SEPARATOR);
  119.     }

  120.     @Override
  121.     public String apply(final Object source) {
  122.         List<?> list;
  123.         try {
  124.             list = (List<?>) PROPERTY_UTILS_BEAN.getProperty(source, column);
  125.             return convertToPresentation(list,null);
  126.         } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
  127.             LOGGER.warn("Problem getting list {}, object {} , exception {}", property, column, e);
  128.         }
  129.         return "";
  130.     }

  131.     @Override
  132.     public Result<List<?>> convertToModel(final String value, final ValueContext context) {
  133.         return Result.ok(new ArrayList<>());
  134.     }

  135.     @Override
  136.     public String convertToPresentation(final List<?> value, final ValueContext context) {
  137.         final StringBuilder stringBuilder = new StringBuilder();

  138.         if (value != null) {
  139.             stringBuilder.append(START_TAG);
  140.             for (final Object object : value) {
  141.                 appendObjectPresentation(stringBuilder, object);
  142.             }
  143.             stringBuilder.append(END_TAG);
  144.         }


  145.         return stringBuilder.toString();
  146.     }

  147.     /**
  148.      * Gets the column.
  149.      *
  150.      * @return the column
  151.      */
  152.     public String getColumn() {
  153.         return column;
  154.     }

  155. }