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.annotation.deprecation.MgnlDeprecated;
37 import info.magnolia.objectfactory.Components;
38 import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
39 import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition;
40 import info.magnolia.ui.form.field.transformer.Transformer;
41 import info.magnolia.ui.form.field.transformer.UndefinedPropertyType;
42 import info.magnolia.ui.vaadin.integration.jcr.DefaultPropertyUtil;
43
44 import java.util.Locale;
45 import java.util.Objects;
46
47 import javax.inject.Inject;
48
49 import org.apache.commons.lang3.StringUtils;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 import com.vaadin.v7.data.Item;
54 import com.vaadin.v7.data.Property;
55 import com.vaadin.v7.data.util.ObjectProperty;
56
57
58
59
60
61
62
63
64
65
66
67 @Deprecated
68 @MgnlDeprecated(since = "6.2.3.", description = "Use new framework.")
69 public class BasicTransformer<T> implements Transformer<T> {
70 private static final Logger log = LoggerFactory.getLogger(BasicTransformer.class);
71
72 protected final ConfiguredFieldDefinition definition;
73
74 protected Item relatedFormItem;
75
76 protected String basePropertyName;
77
78
79
80
81 @Deprecated
82 protected String i18NPropertyName;
83 private Locale locale;
84 protected Class<T> type;
85
86 private boolean isReadOnly = false;
87
88 private I18NAuthoringSupport i18NAuthoringSupport;
89
90 @Inject
91 public BasicTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<T> type, I18NAuthoringSupport i18NAuthoringSupport) {
92 this.definition = definition;
93 this.relatedFormItem = relatedFormItem;
94 this.i18NAuthoringSupport = i18NAuthoringSupport;
95 this.basePropertyName = definition.getName();
96
97 setType(type);
98 }
99
100
101
102
103 @Deprecated
104 public BasicTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<T> type) {
105 this(relatedFormItem, definition, type, Components.getComponent(I18NAuthoringSupport.class));
106 }
107
108 public Item getRelatedFormItem() {
109 return relatedFormItem;
110 }
111
112 @Override
113 public void writeToItem(T newValue) {
114 Property<T> p = getOrCreateProperty(type);
115 p.setValue(newValue);
116 }
117
118 @Override
119 public T readFromItem() {
120 return getOrCreateProperty(type).getValue();
121 }
122
123
124
125
126
127
128 protected void setType(Class<T> typeFromDefinition) {
129 if (typeFromDefinition.isAssignableFrom(UndefinedPropertyType.class)) {
130 String propertyName = definePropertyName();
131 Property<T> property = relatedFormItem.getItemProperty(propertyName);
132 if (property != null) {
133 this.type = (Class<T>) property.getType();
134 } else {
135 this.type = (Class<T>) String.class;
136 }
137 } else {
138 this.type = typeFromDefinition;
139 }
140 }
141
142
143
144
145
146
147
148
149 protected <T> Property<T> getOrCreateProperty(Class<T> type) {
150 return getOrCreateProperty(type, true);
151 }
152
153
154
155
156
157
158 protected <T> Property<T> getOrCreateProperty(Class<T> type, boolean checkTypes) {
159 String propertyName = definePropertyName();
160 Property<T> property = relatedFormItem.getItemProperty(propertyName);
161
162 if (property == null) {
163 property = new ObjectProperty<>(null, type);
164 relatedFormItem.addItemProperty(propertyName, property);
165
166 } else if (checkTypes && !type.isAssignableFrom(property.getType())) {
167
168
169
170 String stringValue = ((property.getValue() != null && StringUtils.isNotBlank(property.getValue().toString()))
171 ? property.getValue().toString()
172 : null);
173 T value = null;
174 try {
175
176 value = (T) DefaultPropertyUtil.createTypedValue(type, stringValue);
177 } catch (Exception e) {
178
179 }
180 if (!Objects.equals(value, property.getValue())) {
181 property = new ObjectProperty<>(value, type, property.isReadOnly());
182
183 relatedFormItem.addItemProperty(propertyName, property);
184 }
185 }
186
187
188 this.isReadOnly = property.isReadOnly();
189 return property;
190 }
191
192
193
194
195 protected String definePropertyName() {
196 final String propertyName = deriveLocaleAwareName(this.basePropertyName);
197 this.i18NPropertyName = propertyName;
198 return propertyName;
199 }
200
201 protected String deriveLocaleAwareName(String baseName) {
202 if (hasI18NSupport() && locale != null && !i18NAuthoringSupport.isDefaultLocale(locale, relatedFormItem)) {
203 return i18NAuthoringSupport.deriveLocalisedPropertyName(baseName, locale);
204 }
205 return baseName;
206 }
207
208 protected I18NAuthoringSupport getI18NAuthoringSupport() {
209 return i18NAuthoringSupport;
210 }
211
212
213
214 @Override
215 public void setLocale(Locale locale) {
216 this.locale = locale;
217 }
218
219 @Override
220 public void setI18NPropertyName(String i18nPropertyName) {
221 log.warn("BasicTransformer.setI18NPropertyName() is deprecated since 5.4.2 without replacement, override BasicTransformer.definePropertyName() and construct the locale-specific property name there if needed.");
222 }
223
224 @Override
225 public Locale getLocale() {
226 return this.locale;
227 }
228
229 @Override
230 public String getBasePropertyName() {
231 return basePropertyName;
232 }
233
234 @Override
235 public boolean hasI18NSupport() {
236 return definition.isI18n();
237 }
238
239 @Override
240 public Class<T> getType() {
241 return type;
242 }
243
244 @Override
245 public boolean isReadOnly() {
246 return this.isReadOnly;
247 }
248
249 @Override
250 @Deprecated
251 public void setReadOnly(boolean isReadOnly) {
252 log.warn("Transformer #readOnly cannot be set, it just mirrors read-only state of the underlying Property.");
253 }
254 }