1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.ui.vaadin.integration.jcr;
35
36 import info.magnolia.cms.util.DateUtil;
37
38 import java.math.BigDecimal;
39 import java.text.ParseException;
40 import java.text.SimpleDateFormat;
41 import java.util.Arrays;
42 import java.util.Date;
43 import java.util.List;
44
45 import javax.jcr.Binary;
46 import javax.jcr.PropertyType;
47
48 import org.apache.commons.lang3.BooleanUtils;
49 import org.apache.commons.lang3.StringUtils;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 import com.google.common.collect.Sets;
54 import com.vaadin.v7.data.Property;
55 import com.vaadin.v7.data.util.ObjectProperty;
56
57
58
59
60
61
62 @Deprecated
63 public class DefaultPropertyUtil {
64
65 private static final Logger log = LoggerFactory.getLogger(DefaultPropertyUtil.class);
66
67
68
69
70 public static <T> Property<T> newProperty(String value, Class<T> type) {
71 Object typedValue = null;
72 try {
73 typedValue = createTypedValue(type, value);
74 } catch (Exception e) {
75 log.error("Exception during Value creation", e);
76 }
77 return new ObjectProperty<>((T) typedValue, type);
78 }
79
80
81
82
83
84
85 @Deprecated
86 public static <T> DefaultProperty<T> newDefaultProperty(Class<T> type, String defaultValue) {
87 Object value = null;
88 try {
89 value = createTypedValue(type, defaultValue);
90 } catch (Exception e) {
91 log.error("Exception during Value creation", e);
92 }
93 return new DefaultProperty<>(type, (T) value);
94 }
95
96
97
98
99
100
101
102
103
104 @Deprecated
105 public static DefaultProperty newDefaultProperty(String fieldType, String stringValue) throws NumberFormatException {
106 Object value = null;
107 try {
108 value = createTypedValue(fieldType, stringValue);
109 } catch (Exception e) {
110 log.error("Exception during Value creation", e);
111 }
112 return new DefaultProperty(getFieldTypeClass(fieldType), value);
113 }
114
115
116
117
118
119
120 @Deprecated
121 public static DefaultProperty newDefaultProperty(int fieldType, Object value) throws NumberFormatException {
122 return new DefaultProperty(getFieldTypeClass(fieldType), value);
123 }
124
125
126
127
128
129
130
131
132
133 @Deprecated
134 public static Object createTypedValue(String fieldType, String defaultValue) throws NumberFormatException {
135 if (StringUtils.isBlank(fieldType)) {
136 return defaultValue;
137 } else if (defaultValue != null) {
138 Class<?> type = getFieldTypeClass(fieldType);
139 return createTypedValue(type, defaultValue);
140 }
141 return null;
142 }
143
144
145
146
147
148
149
150
151 public static Object createTypedValue(Class<?> type, String defaultValue) throws NumberFormatException {
152 if (StringUtils.isBlank(defaultValue)) {
153 return defaultValue;
154 } else if (defaultValue != null) {
155 if (type.getName().equals(String.class.getName())) {
156 return defaultValue;
157 } else if (type.getName().equals(Long.class.getName())) {
158 return Long.decode(defaultValue);
159 } else if (type.isAssignableFrom(Binary.class)) {
160 return null;
161 } else if (type.getName().equals(Double.class.getName())) {
162 return Double.valueOf(defaultValue);
163 } else if (type.getName().equals(Date.class.getName())) {
164 try {
165 return new SimpleDateFormat(DateUtil.YYYY_MM_DD).parse(defaultValue);
166 } catch (ParseException e) {
167 throw new IllegalArgumentException(e);
168 }
169 } else if (type.getName().equals(Boolean.class.getName())) {
170 return BooleanUtils.toBoolean(defaultValue);
171 } else if (type.getName().equals(BigDecimal.class.getName())) {
172 return BigDecimal.valueOf(Long.decode(defaultValue));
173 } else if (type.isAssignableFrom(List.class)) {
174 return Arrays.asList(defaultValue.split(","));
175 } else {
176 throw new IllegalArgumentException("Unsupported property type " + type.getName());
177 }
178 }
179 return null;
180 }
181
182
183
184
185
186
187
188
189 public static boolean canConvertStringValue(Class<?> type) {
190
191 if (type.getName().equals(String.class.getName())
192 || type.getName().equals(Long.class.getName())
193 || type.isAssignableFrom(Binary.class)
194 || type.getName().equals(Double.class.getName())
195 || type.getName().equals(Date.class.getName())
196 || type.getName().equals(Boolean.class.getName())
197 || type.getName().equals(BigDecimal.class.getName())
198 || type.isAssignableFrom(List.class)) {
199 return true;
200 }
201 return false;
202 }
203
204 public static boolean isKnownJcrTypeName(String typeName) {
205 return Sets.newHashSet(
206 PropertyType.TYPENAME_STRING,
207 PropertyType.TYPENAME_BINARY,
208 PropertyType.TYPENAME_LONG,
209 PropertyType.TYPENAME_DOUBLE,
210 PropertyType.TYPENAME_DATE,
211 PropertyType.TYPENAME_BOOLEAN,
212 PropertyType.TYPENAME_DECIMAL
213 ).contains(typeName);
214 }
215
216
217
218
219
220
221
222 public static Class<?> getFieldTypeClass(String fieldType) {
223 if (StringUtils.isNotEmpty(fieldType)) {
224 int valueType = PropertyType.valueFromName(fieldType);
225 return getFieldTypeClass(valueType);
226 } else {
227 return String.class;
228 }
229 }
230
231
232
233
234
235
236
237 public static Class<?> getFieldTypeClass(int fieldType) {
238 if (fieldType > 0) {
239 switch (fieldType) {
240 case PropertyType.STRING:
241 return String.class;
242 case PropertyType.BINARY:
243 return Binary.class;
244 case PropertyType.LONG:
245 return Long.class;
246 case PropertyType.DOUBLE:
247 return Double.class;
248 case PropertyType.DATE:
249
250 return Date.class;
251 case PropertyType.BOOLEAN:
252 return Boolean.class;
253 case PropertyType.DECIMAL:
254 return BigDecimal.class;
255 default:
256 throw new IllegalArgumentException("Unsupported property type " + PropertyType.nameFromValue(fieldType));
257 }
258 } else {
259 return String.class;
260 }
261 }
262 }