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.form.field.transformer.basic;
35
36 import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition;
37 import info.magnolia.ui.form.field.transformer.Transformer;
38 import info.magnolia.ui.form.field.transformer.UndefinedPropertyType;
39 import info.magnolia.ui.vaadin.integration.jcr.DefaultProperty;
40 import info.magnolia.ui.vaadin.integration.jcr.DefaultPropertyUtil;
41
42 import java.util.Locale;
43
44 import javax.inject.Inject;
45
46 import org.apache.commons.lang.StringUtils;
47
48 import com.vaadin.data.Item;
49 import com.vaadin.data.Property;
50
51
52
53
54
55
56
57
58
59
60 public class BasicTransformer<T> implements Transformer<T> {
61
62 protected Item relatedFormItem;
63 protected final ConfiguredFieldDefinition definition;
64
65 protected String basePropertyName;
66 protected String i18NPropertyName;
67 private Locale locale;
68 protected Class<T> type;
69
70 @Inject
71 public BasicTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<T> type) {
72 this.definition = definition;
73 this.relatedFormItem = relatedFormItem;
74 this.basePropertyName = definition.getName();
75 if (hasI18NSupport()) {
76 this.i18NPropertyName = this.basePropertyName;
77 }
78 setType(type);
79 }
80
81 @Override
82 public void writeToItem(T newValue) {
83 Property<T> p = getOrCreateProperty(type);
84 p.setValue(newValue);
85 }
86
87 @Override
88 public T readFromItem() {
89 Property<T> p = getOrCreateProperty(type);
90 if (definition.isReadOnly()) {
91 p.setReadOnly(true);
92 }
93 return p.getValue();
94 }
95
96
97
98
99
100
101 protected void setType(Class<T> typeFromDefinition) {
102 if (typeFromDefinition.isAssignableFrom(UndefinedPropertyType.class)) {
103 String propertyName = definePropertyName();
104 Property<T> property = relatedFormItem.getItemProperty(propertyName);
105 if (property != null) {
106 this.type = (Class<T>) property.getType();
107 } else {
108 this.type = (Class<T>) String.class;
109 }
110 } else {
111 this.type = typeFromDefinition;
112 }
113 }
114
115
116
117
118
119
120
121
122 protected <T> Property<T> getOrCreateProperty(Class<T> type) {
123 return getOrCreateProperty(type, true);
124 }
125
126
127
128
129
130
131 protected <T> Property<T> getOrCreateProperty(Class<T> type, boolean checkTypes) {
132 String propertyName = definePropertyName();
133 Property<T> property = relatedFormItem.getItemProperty(propertyName);
134
135 if (property == null) {
136 property = new DefaultProperty<T>(type, null);
137 relatedFormItem.addItemProperty(propertyName, property);
138 } else if (checkTypes && !type.isAssignableFrom(property.getType())) {
139
140
141
142 String stringValue = ((property.getValue() != null && StringUtils.isNotBlank(property.getValue().toString()))
143 ? property.getValue().toString()
144 : null);
145 T value = null;
146 try {
147
148 value = (T) DefaultPropertyUtil.createTypedValue(type, stringValue);
149 } catch (Exception e) {
150
151 }
152 property = new DefaultProperty<T>(type, value);
153
154 relatedFormItem.addItemProperty(propertyName, property);
155 }
156
157 return property;
158 }
159
160
161
162
163
164 protected String definePropertyName() {
165 String propertyName = this.basePropertyName;
166
167 if (hasI18NSupport()) {
168 propertyName = this.i18NPropertyName;
169 }
170 return propertyName;
171 }
172
173
174
175
176
177 @Override
178 public void setLocale(Locale locale) {
179 this.locale = locale;
180 }
181
182 @Override
183 public void setI18NPropertyName(String i18nPropertyName) {
184 this.i18NPropertyName = i18nPropertyName;
185 }
186
187 @Override
188 public Locale getLocale() {
189 return this.locale;
190 }
191
192 @Override
193 public String getBasePropertyName() {
194 return basePropertyName;
195 }
196
197 @Override
198 public boolean hasI18NSupport() {
199 return definition.isI18n();
200 }
201
202 @Override
203 public Class<T> getType() {
204 return type;
205 }
206
207 }