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.admincentral.apps.notifications;
35
36 import info.magnolia.ui.api.message.Message;
37 import info.magnolia.ui.field.FieldDefinition;
38 import info.magnolia.ui.datasource.PropertySetFactory;
39
40 import java.util.Collection;
41 import java.util.Locale;
42 import java.util.Map;
43 import java.util.Optional;
44 import java.util.stream.Stream;
45
46 import org.apache.commons.lang3.StringUtils;
47
48 import com.vaadin.data.BeanPropertySet;
49 import com.vaadin.data.PropertyDefinition;
50 import com.vaadin.data.PropertySet;
51 import com.vaadin.data.ValueProvider;
52 import com.vaadin.server.Setter;
53
54 import lombok.AllArgsConstructor;
55
56
57
58
59 public class NotificationPropertySetFactory implements PropertySetFactory<Message> {
60
61 @Override
62 public PropertySet<Message> withProperties(Map<String, Class> properties) {
63 return BeanPropertySet.get(Message.class);
64 }
65
66 @Override
67 public PropertySet<Message> fromFieldDefinitions(Collection<FieldDefinition> fieldDefinitions, Locale locale) {
68 return new PropertySet<Message>() {
69 private final PropertySet<Message> propertySet = BeanPropertySet.get(Message.class);
70
71 @Override
72 public Stream<PropertyDefinition<Message, ?>> getProperties() {
73 return fieldDefinitions.stream().map(editorPropertyDefinition -> new MessagePropertyDefinition(propertySet, editorPropertyDefinition.getName()));
74 }
75
76 @Override
77 public Optional<PropertyDefinition<Message, ?>> getProperty(String name) {
78 return getProperties().filter(messagePropertyDefinition -> messagePropertyDefinition.getName().equals(name)).findFirst();
79 }
80 };
81 }
82
83 @AllArgsConstructor
84 private class MessagePropertyDefinition implements PropertyDefinition<Message, Object> {
85 private PropertySet<Message> propertySet;
86 private String name;
87
88 @Override
89 public ValueProvider<Message, Object> getGetter() {
90 return message -> propertySet.getProperty(name)
91 .map(PropertyDefinition::getGetter)
92 .map(messageValueProvider -> messageValueProvider.apply(message))
93 .map(String::valueOf)
94 .orElseGet(() -> message.hasProperty(name) ? String.valueOf(message.getProperty(name)) : StringUtils.EMPTY);
95 }
96
97
98 @Override
99 public Optional<Setter<Message, Object>> getSetter() {
100 return Optional.empty();
101 }
102
103 @Override
104 public Class<Object> getType() {
105 return Object.class;
106 }
107
108 public Class<?> getPropertyHolderType() {
109 return Message.class;
110 }
111
112 @Override
113 public String getName() {
114 return name;
115 }
116
117 @Override
118 public PropertySet<Message> getPropertySet() {
119 return propertySet;
120 }
121
122 @Override
123 public String getCaption() {
124 return name.toUpperCase();
125 }
126
127 }
128 }