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.lang3.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 public Item getRelatedFormItem() {
82 return relatedFormItem;
83 }
84
85 @Override
86 public void writeToItem(T newValue) {
87 Property<T> p = getOrCreateProperty(type);
88 p.setValue(newValue);
89 }
90
91 @Override
92 public T readFromItem() {
93 Property<T> p = getOrCreateProperty(type);
94 if (definition.isReadOnly()) {
95 p.setReadOnly(true);
96 }
97 return p.getValue();
98 }
99
100
101
102
103
104
105 protected void setType(Class<T> typeFromDefinition) {
106 if (typeFromDefinition.isAssignableFrom(UndefinedPropertyType.class)) {
107 String propertyName = definePropertyName();
108 Property<T> property = relatedFormItem.getItemProperty(propertyName);
109 if (property != null) {
110 this.type = (Class<T>) property.getType();
111 } else {
112 this.type = (Class<T>) String.class;
113 }
114 } else {
115 this.type = typeFromDefinition;
116 }
117 }
118
119
120
121
122
123
124
125
126 protected <T> Property<T> getOrCreateProperty(Class<T> type) {
127 return getOrCreateProperty(type, true);
128 }
129
130
131
132
133
134
135 protected <T> Property<T> getOrCreateProperty(Class<T> type, boolean checkTypes) {
136 String propertyName = definePropertyName();
137 Property<T> property = relatedFormItem.getItemProperty(propertyName);
138
139 if (property == null) {
140 property = new DefaultProperty<T>(type, null);
141 relatedFormItem.addItemProperty(propertyName, property);
142 } else if (checkTypes && !type.isAssignableFrom(property.getType())) {
143
144
145
146 String stringValue = ((property.getValue() != null && StringUtils.isNotBlank(property.getValue().toString()))
147 ? property.getValue().toString()
148 : null);
149 T value = null;
150 try {
151
152 value = (T) DefaultPropertyUtil.createTypedValue(type, stringValue);
153 } catch (Exception e) {
154
155 }
156 property = new DefaultProperty<T>(type, value);
157
158 relatedFormItem.addItemProperty(propertyName, property);
159 }
160
161 return property;
162 }
163
164
165
166
167
168 protected String definePropertyName() {
169 if (hasI18NSupport()) {
170 return this.i18NPropertyName;
171 }
172 return this.basePropertyName;
173 }
174
175
176
177
178
179 @Override
180 public void setLocale(Locale locale) {
181 this.locale = locale;
182 }
183
184 @Override
185 public void setI18NPropertyName(String i18nPropertyName) {
186 this.i18NPropertyName = i18nPropertyName;
187 }
188
189 @Override
190 public Locale getLocale() {
191 return this.locale;
192 }
193
194 @Override
195 public String getBasePropertyName() {
196 return basePropertyName;
197 }
198
199 @Override
200 public boolean hasI18NSupport() {
201 return definition.isI18n();
202 }
203
204 @Override
205 public Class<T> getType() {
206 return type;
207 }
208
209 }