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.task.app;
35  
36  import info.magnolia.context.Context;
37  import info.magnolia.objectfactory.Components;
38  import info.magnolia.task.Task;
39  import info.magnolia.ui.field.FieldDefinition;
40  import info.magnolia.ui.datasource.PropertySetFactory;
41  
42  import java.time.Instant;
43  import java.time.LocalDateTime;
44  import java.time.ZoneId;
45  import java.time.format.DateTimeFormatter;
46  import java.util.Calendar;
47  import java.util.Collection;
48  import java.util.HashMap;
49  import java.util.Locale;
50  import java.util.Map;
51  import java.util.Optional;
52  import java.util.stream.Stream;
53  
54  import org.apache.commons.lang3.StringUtils;
55  import org.apache.commons.lang3.time.FastDateFormat;
56  
57  import com.vaadin.data.BeanPropertySet;
58  import com.vaadin.data.PropertyDefinition;
59  import com.vaadin.data.PropertySet;
60  import com.vaadin.data.ValueProvider;
61  import com.vaadin.server.Setter;
62  
63  import lombok.AllArgsConstructor;
64  
65  /**
66   * Property set factory for tasks.
67   */
68  public class TasksPropertySetFactory implements PropertySetFactory<Task> {
69  
70      @Override
71      public PropertySet<Task> withProperties(Map<String, Class> properties) {
72          return BeanPropertySet.get(Task.class);
73      }
74  
75      @Override
76      public PropertySet<Task> fromFieldDefinitions(Collection<FieldDefinition> fieldDefinitions, Locale locale) {
77          return new PropertySet<Task>() {
78              private final PropertySet<Task> propertySet = BeanPropertySet.get(Task.class);
79  
80              @Override
81              public Stream<PropertyDefinition<Task, ?>> getProperties() {
82                  return fieldDefinitions.stream().map(editorPropertyDefinition -> new TaskPropertyDefinition(propertySet, editorPropertyDefinition.getName()));
83              }
84  
85              @Override
86              public Optional<PropertyDefinition<Task, ?>> getProperty(String name) {
87                  return getProperties().filter(taskPropertyDefinition -> taskPropertyDefinition.getName().equals(name)).findFirst();
88              }
89          };
90      }
91  
92      @AllArgsConstructor
93      private class TaskPropertyDefinition implements PropertyDefinition<Task, Object> {
94          private PropertySet<Task> propertySet;
95          private String name;
96  
97          @Override
98          public ValueProvider<Task, Object> getGetter() {
99              return name.contains(".") ? getTaskObjectValueProvider() : (ValueProvider<Task, Object>) propertySet.getProperty(name).map(PropertyDefinition::getGetter).get();
100         }
101 
102         private ValueProvider<Task, Object> getTaskObjectValueProvider() {
103             return (ValueProvider<Task, Object>) task -> {
104                 Map subPropertySet = propertySet.getProperty(StringUtils.substringBefore(name, "."))
105                         .map(PropertyDefinition::getGetter)
106                         .map(taskValueProvider -> taskValueProvider.apply(task))
107                         .filter(Map.class::isInstance)
108                         .map(Map.class::cast)
109                         .orElse(new HashMap());
110 
111                 return Optional.ofNullable(subPropertySet.get(StringUtils.substringAfter(name, ".")))
112                         .map(object -> object instanceof Calendar ? getLocalDateTime(((Calendar) object).getTimeInMillis()).format(DateTimeFormatter.ofPattern(getDateTimePattern())) : String.valueOf(object))
113                         .orElse(StringUtils.EMPTY);
114             };
115         }
116 
117         private String getDateTimePattern() {
118             Locale locale = Components.getComponent(Context.class).getLocale();
119             return FastDateFormat.getDateTimeInstance(FastDateFormat.MEDIUM, FastDateFormat.SHORT, locale).getPattern();
120         }
121 
122         private LocalDateTime getLocalDateTime(long ms) {
123             return Instant.ofEpochMilli(ms)
124                     .atZone(ZoneId.systemDefault())
125                     .toLocalDateTime();
126         }
127 
128         @Override
129         public Optional<Setter<Task, Object>> getSetter() {
130             return Optional.empty();
131         }
132 
133         @Override
134         public Class<Object> getType() {
135             return Object.class;
136         }
137 
138         public Class<?> getPropertyHolderType() {
139             return Task.class;
140         }
141 
142         @Override
143         public String getName() {
144             return name;
145         }
146 
147         @Override
148         public PropertySet<Task> getPropertySet() {
149             return propertySet;
150         }
151 
152         @Override
153         public String getCaption() {
154             return name.toUpperCase();
155         }
156 
157     }
158 
159 }
160