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.multi;
35
36 import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition;
37 import info.magnolia.ui.form.field.transformer.basic.BasicTransformer;
38
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.Iterator;
42 import java.util.List;
43
44 import javax.inject.Inject;
45
46 import org.apache.commons.lang3.StringUtils;
47
48 import com.vaadin.v7.data.Item;
49 import com.vaadin.v7.data.Property;
50 import com.vaadin.v7.data.util.ObjectProperty;
51 import com.vaadin.v7.data.util.PropertysetItem;
52
53
54
55
56
57
58
59
60 public class MultiValueJSONTransformer extends BasicTransformer<PropertysetItem> {
61
62 @Inject
63 public MultiValueJSONTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type) {
64 super(relatedFormItem, definition, type);
65 }
66
67 @Override
68 public void writeToItem(PropertysetItem newValue) {
69 Property<String> property = getOrCreateProperty(String.class);
70 property.setValue(StringUtils.join(removeComma(newValue), ","));
71 }
72
73 @Override
74 public PropertysetItem readFromItem() {
75 PropertysetItem newValues = new PropertysetItem();
76 List<String> list = new ArrayList<>();
77 Property<String> property = getOrCreateProperty(String.class);
78 String value = property.getValue();
79 if (StringUtils.isNotBlank(value)) {
80 list = Arrays.asList(value.split(","));
81 }
82 int position = 0;
83 for (String element : list) {
84 newValues.addItemProperty(position, new ObjectProperty<>(element));
85 position += 1;
86 }
87 return newValues;
88 }
89
90
91
92
93 private List<String> removeComma(PropertysetItem newValue) {
94 List<String> res = new ArrayList<>();
95 if (newValue != null) {
96 Iterator<?> it = newValue.getItemPropertyIds().iterator();
97 while (it.hasNext()) {
98 String element = newValue.getItemProperty(it.next()).getValue().toString();
99 res.add(StringUtils.replace(element, ",", " "));
100 }
101 }
102 return res;
103 }
104
105 }