View Javadoc
1   /**
2    * This file Copyright (c) 2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Extract property set from {@link Message}, could be improved.
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 }