1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
42 private static final long serialVersionUID = 1L;
43
44
45 private final Class<List> modelType;
46
47
48 private final String property;
49
50
51 private final String column;
52
53 private final String fallbackColumn;
54
55
56
57
58
59
60
61
62
63
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
75
76
77
78
79
80
81
82
83
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
96
97
98
99 @Override
100 public Class<String> getPresentationType() {
101 return String.class;
102 }
103
104
105
106
107
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 }