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 com.vaadin.v7.data.Item;
45 import com.vaadin.v7.data.Property;
46 import com.vaadin.v7.data.util.PropertysetItem;
47
48
49
50
51
52
53
54
55
56
57
58 public class CompositeTransformer extends BasicTransformer<PropertysetItem> {
59
60 protected List<String> fieldsName;
61 protected String propertyPrefix;
62
63
64
65
66 @Deprecated
67 public CompositeTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type, List<String> fieldsName) {
68 this(relatedFormItem, definition, type, fieldsName, Components.getComponent(I18NAuthoringSupport.class));
69 }
70
71 public CompositeTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type, List<String> fieldsName, I18NAuthoringSupport i18NAuthoringSupport) {
72 super(relatedFormItem, definition, type, i18NAuthoringSupport);
73 this.fieldsName = fieldsName;
74 this.propertyPrefix = createPropertyPrefix(definition);
75 }
76
77
78
79
80 protected String createPropertyPrefix(ConfiguredFieldDefinition definition) {
81 return definition.getName();
82 }
83
84 @Override
85 public void writeToItem(PropertysetItem newValues) {
86
87 Iterator<?> propertyNames = newValues.getItemPropertyIds().iterator();
88
89 while (propertyNames.hasNext()) {
90 String propertyName = (String) propertyNames.next();
91 String compositePropertyName = getCompositePropertyName(propertyName);
92 Property<?> property = relatedFormItem.getItemProperty(compositePropertyName);
93 if (property == null && newValues.getItemProperty(propertyName) != null) {
94 relatedFormItem.addItemProperty(compositePropertyName, newValues.getItemProperty(propertyName));
95 }
96 }
97 }
98
99 @Override
100 public PropertysetItem readFromItem() {
101 PropertysetItem newValues = new PropertysetItem();
102 for (String propertyName : fieldsName) {
103 String compositePropertyName = getCompositePropertyName(propertyName);
104 if (relatedFormItem.getItemProperty(compositePropertyName) != null) {
105 newValues.addItemProperty(propertyName, relatedFormItem.getItemProperty(compositePropertyName));
106 }
107 }
108 return newValues;
109 }
110
111 protected String getCompositePropertyName(String propertyName) {
112 propertyName = propertyPrefix + deriveLocaleAwareName(propertyName);
113 return propertyName;
114 }
115 }