View Javadoc
1   /*
2    * Copyright 2014 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  
21  import java.lang.reflect.InvocationTargetException;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Locale;
25  
26  import org.apache.commons.beanutils.BeanUtils;
27  
28  import com.vaadin.data.util.converter.Converter;
29  
30  /**
31   * The Class ListPropertyRenderer.
32   */
33  public final class ListPropertyConverter implements Converter<String, List> {
34  
35  	private static final char CONTENT_SEPARATOR = ' ';
36  
37  	private static final String START_TAG = "[ ";
38  
39  	private static final char END_TAG = ']';
40  
41  	/** The Constant serialVersionUID. */
42  	private static final long serialVersionUID = 1L;
43  
44  	/** The model type. */
45  	private final Class<List> modelType;
46  
47  	/** The property. */
48  	private final String property;
49  
50  	/** The column. */
51  	private final String column;
52  
53  	private final String fallbackColumn;
54  
55  	/**
56  	 * Instantiates a new collection property converter.
57  	 *
58  	 * @param modelType
59  	 *            the model type
60  	 * @param property
61  	 *            the property
62  	 * @param column
63  	 *            the column
64  	 */
65  	public ListPropertyConverter(final Class<List> modelType, final String property, final String column) {
66  		super();
67  		this.modelType = modelType;
68  		this.property = property;
69  		this.column = column;
70  		this.fallbackColumn = null;
71  	}
72  
73  	/**
74  	 * Instantiates a new list property converter.
75  	 *
76  	 * @param modelType
77  	 *            the model type
78  	 * @param property
79  	 *            the property
80  	 * @param column
81  	 *            the column
82  	 * @param fallbackColumn
83  	 *            the fallback column
84  	 */
85  	public ListPropertyConverter(final Class<List> modelType, final String property, final String column,final String fallbackColumn) {
86  		super();
87  		this.modelType = modelType;
88  		this.property = property;
89  		this.column = column;
90  		this.fallbackColumn = fallbackColumn;
91  	}
92  
93  
94  	/**
95  	 * Gets the presentation type.
96  	 *
97  	 * @return the presentation type
98  	 */
99  	@Override
100 	public Class<String> getPresentationType() {
101 		return String.class;
102 	}
103 
104 	/**
105 	 * Gets the column.
106 	 *
107 	 * @return the column
108 	 */
109 	public String getColumn() {
110 		return column;
111 	}
112 
113 	@Override
114 	public List convertToModel(final String value, final Class<? extends List> targetType, final Locale locale)
115 			throws ConversionException {
116 		return new ArrayList<>();
117 	}
118 
119 	@Override
120 	public String convertToPresentation(final List value, final Class<? extends String> targetType, final Locale locale)
121 			throws ConversionException {
122 		final StringBuilder stringBuilder = new StringBuilder().append(START_TAG);
123 
124 		if (value != null) {
125 			for (final Object object : value) {
126 				try {
127 					final String beanProperty = BeanUtils.getProperty(object, property);
128 
129 					if (beanProperty != null) {
130 						stringBuilder.append(beanProperty);
131 					} else {
132 						if (fallbackColumn != null) {
133 							final String beanPropertyFallBack = BeanUtils.getProperty(object, fallbackColumn);
134 							if (beanPropertyFallBack != null) {
135 								stringBuilder.append(beanPropertyFallBack);
136 							}
137 						}
138 
139 
140 					}
141 
142 				} catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
143 					throw new ConversionException(e);
144 				}
145 				stringBuilder.append(CONTENT_SEPARATOR);
146 			}
147 		}
148 
149 		stringBuilder.append(END_TAG);
150 
151 		return stringBuilder.toString();
152 	}
153 
154 	@Override
155 	public Class<List> getModelType() {
156 		return modelType;
157 	}
158 
159 }