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.composite;
35
36 import info.magnolia.objectfactory.Components;
37 import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
38 import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition;
39 import info.magnolia.ui.form.field.transformer.basic.BasicTransformer;
40
41 import java.util.Iterator;
42 import java.util.List;
43
44 import org.apache.commons.lang3.StringUtils;
45
46 import com.vaadin.data.Item;
47 import com.vaadin.data.Property;
48 import com.vaadin.data.util.PropertysetItem;
49
50
51
52
53
54
55
56
57
58
59
60 public class CompositeTransformer extends BasicTransformer<PropertysetItem> {
61
62 protected List<String> fieldsName;
63 protected String propertyPrefix;
64
65
66
67
68 @Deprecated
69 public CompositeTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type, List<String> fieldsName) {
70 this(relatedFormItem, definition, type, fieldsName, Components.getComponent(I18NAuthoringSupport.class));
71 }
72
73 public CompositeTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type, List<String> fieldsName, I18NAuthoringSupport i18NAuthoringSupport) {
74 super(relatedFormItem, definition, type, i18NAuthoringSupport);
75 this.fieldsName = fieldsName;
76 this.propertyPrefix = createPropertyPrefix(definition);
77 }
78
79
80
81
82 protected String createPropertyPrefix(ConfiguredFieldDefinition definition) {
83 return definition.getName();
84 }
85
86 @Override
87 public void writeToItem(PropertysetItem newValues) {
88
89 Iterator<?> propertyNames = newValues.getItemPropertyIds().iterator();
90
91 while (propertyNames.hasNext()) {
92 String propertyName = (String) propertyNames.next();
93 String compositePropertyName = getCompositePropertyName(propertyName);
94 Property<?> property = relatedFormItem.getItemProperty(compositePropertyName);
95 if (property == null && newValues.getItemProperty(propertyName) != null) {
96 relatedFormItem.addItemProperty(compositePropertyName, newValues.getItemProperty(propertyName));
97 }
98 }
99 }
100
101 @Override
102 public PropertysetItem readFromItem() {
103 PropertysetItem newValues = new PropertysetItem();
104 for (String propertyName : fieldsName) {
105 String compositePropertyName = getCompositePropertyName(propertyName);
106 if (relatedFormItem.getItemProperty(compositePropertyName) != null) {
107 newValues.addItemProperty(propertyName, relatedFormItem.getItemProperty(compositePropertyName));
108 }
109 }
110 return newValues;
111 }
112
113 protected String getCompositePropertyName(String propertyName) {
114 propertyName = propertyPrefix + propertyName;
115 if (hasI18NSupport()) {
116 propertyName = propertyName + StringUtils.difference(basePropertyName, i18NPropertyName);
117 }
118 return propertyName;
119 }
120 }