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.annotation.deprecation.MgnlDeprecated;
37
38 import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
39 import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition;
40
41 import java.util.Collection;
42 import java.util.List;
43
44 import org.apache.commons.lang3.StringUtils;
45
46 import com.vaadin.v7.data.Item;
47 import com.vaadin.v7.data.util.PropertysetItem;
48
49
50
51
52
53
54
55
56
57
58 @Deprecated
59 @MgnlDeprecated(since = "6.2.3.", description = "Use new framework and PropertyNameDecorator instead.")
60 public class SwitchableTransformer extends CompositeTransformer {
61
62
63
64
65 @Deprecated
66 public SwitchableTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type, List<String> fieldsName) {
67 super(relatedFormItem, definition, type, fieldsName);
68 }
69
70 public SwitchableTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type, List<String> fieldsName, I18NAuthoringSupport i18NAuthoringSupport) {
71 super(relatedFormItem, definition, type, fieldsName, i18NAuthoringSupport);
72 }
73
74 @Override
75 public void writeToItem(PropertysetItem newValues) {
76
77 String currentSelection = (String) newValues.getItemProperty(propertyPrefix).getValue();
78
79
80 Collection<String> propertyNames = (Collection<String>) newValues.getItemPropertyIds();
81 for (String propertyName : propertyNames) {
82
83 String compositePropertyName = getCompositePropertyName(propertyName);
84 if (!propertyName.equals(currentSelection) && !propertyName.equals(propertyPrefix)) {
85 relatedFormItem.removeItemProperty(compositePropertyName);
86 } else {
87 if (newValues.getItemProperty(propertyName) != null) {
88 relatedFormItem.addItemProperty(compositePropertyName, newValues.getItemProperty(propertyName));
89 }
90 }
91 }
92 }
93
94 @Override
95 public PropertysetItem readFromItem() {
96 return super.readFromItem();
97 }
98
99 @Override
100 protected String getCompositePropertyName(String propertyName) {
101 if (StringUtils.equals(propertyName, propertyPrefix)) {
102 propertyName = StringUtils.EMPTY;
103 }
104 return super.getCompositePropertyName(propertyName);
105 }
106 }