View Javadoc
1   /**
2    * This file Copyright (c) 2011-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.ui.form.field.factory;
35  
36  import static info.magnolia.ui.form.field.definition.DateFieldDefinition.NOW;
37  
38  import info.magnolia.cms.security.MgnlUserManager;
39  import info.magnolia.context.Context;
40  import info.magnolia.i18nsystem.SimpleTranslator;
41  import info.magnolia.objectfactory.Components;
42  import info.magnolia.ui.api.context.UiContext;
43  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
44  import info.magnolia.ui.form.field.definition.DateFieldDefinition;
45  
46  import java.text.ParseException;
47  import java.text.SimpleDateFormat;
48  import java.util.Date;
49  import java.util.TimeZone;
50  
51  import javax.inject.Inject;
52  
53  import org.apache.commons.lang3.StringUtils;
54  
55  import com.vaadin.v7.data.Item;
56  import com.vaadin.v7.shared.ui.datefield.Resolution;
57  import com.vaadin.v7.ui.Field;
58  import com.vaadin.v7.ui.PopupDateField;
59  
60  /**
61   * Creates and initializes a date field based on a field definition.
62   *
63   * @deprecated since 6.2 - use {@link info.magnolia.ui.field.factory.DateFieldFactory} instead.
64   *
65   * @see <a href="https://documentation.magnolia-cms.com/display/DOCS62/Upgrading+to+Magnolia+6.2.x">Upgrading to Magnolia 6.2.x</a>
66   */
67  @Deprecated
68  public class DateFieldFactory extends AbstractFieldFactory<DateFieldDefinition, Date> {
69  
70      /**
71       * @deprecated not used anymore in timezone options, though some user profiles might still have it.
72       */
73      private static final String BROWSER_TIMEZONE = "browser";
74  
75      private final SimpleTranslator i18n;
76      private final Context context;
77  
78      @Inject
79      public DateFieldFactory(DateFieldDefinition definition, Item relatedFieldItem, UiContext uiContext, I18NAuthoringSupport i18NAuthoringSupport, SimpleTranslator i18n, Context context) {
80          super(definition, relatedFieldItem, uiContext, i18NAuthoringSupport);
81          this.i18n = i18n;
82          this.context = context;
83      }
84  
85      /**
86       * @deprecated since 5.4.7. Use {@link #DateFieldFactory(info.magnolia.ui.form.field.definition.DateFieldDefinition, com.vaadin.v7.data.Item, info.magnolia.ui.api.context.UiContext, info.magnolia.ui.api.i18n.I18NAuthoringSupport, info.magnolia.i18nsystem.SimpleTranslator, Context)} instead.
87       */
88      @Deprecated
89      public DateFieldFactory(DateFieldDefinition definition, Item relatedFieldItem) {
90          this(definition, relatedFieldItem, null, Components.getComponent(I18NAuthoringSupport.class), Components.getComponent(SimpleTranslator.class), Components.getComponent(Context.class));
91      }
92  
93      @Override
94      public Field<Date> createField() {
95          Field<Date> field = super.createField();
96          field.setWidthUndefined();
97          return field;
98      }
99  
100     @Override
101     protected Field<Date> createFieldComponent() {
102         DateFieldDefinition definition = getFieldDefinition();
103         PopupDateField popupDateField = new PopupDateField();
104         setTimeZone(popupDateField);
105         String dateFormat;
106 
107         // set Resolution
108         if (definition.isTime()) {
109             popupDateField.setResolution(Resolution.MINUTE);
110             dateFormat = definition.getDateFormat() + " " + definition.getTimeFormat();
111         } else {
112             popupDateField.setResolution(Resolution.DAY);
113             dateFormat = definition.getDateFormat();
114         }
115         popupDateField.setDateFormat(dateFormat);
116         return popupDateField;
117     }
118 
119     @Override
120     protected Class<?> getDefaultFieldType() {
121         return Date.class;
122     }
123 
124     /**
125      * Sets the field's time zone according to user preference if set, or to system default otherwise.
126      * User is informed of the input field's expected time zone through the field description and input prompt.
127      *
128      * Do mind however, that when value is null, the client-side time selector always shows in "local" time,
129      * thus the default selected time might be off from the current instant. TODO: we might patch that from Vaadin/GWT
130      */
131     protected void setTimeZone(PopupDateField popupDateField) {
132 
133         final String timeZoneId = context.getUser().getProperty(MgnlUserManager.PROPERTY_TIMEZONE);
134         final TimeZone timeZone;
135 
136         if (timeZoneId == null || timeZoneId.isEmpty() || timeZoneId.equals(BROWSER_TIMEZONE)) {
137             timeZone = TimeZone.getDefault();
138         } else {
139             timeZone = TimeZone.getTimeZone(timeZoneId);
140         }
141         if (timeZone != null) {
142             popupDateField.setTimeZone(timeZone);
143 
144             // Show resolved TZ info in the description tooltip & input-prompt
145             popupDateField.setDescription(i18n.translate("ui-admincentral.dateField.timeZone.description", timeZone.getDisplayName(false, TimeZone.LONG, context.getLocale()), timeZone.getRawOffset() / 3600000));
146             popupDateField.setInputPrompt(i18n.translate("ui-admincentral.dateField.timeZone.description", timeZone.getDisplayName(false, TimeZone.SHORT, context.getLocale()), timeZone.getRawOffset() / 3600000));
147         }
148     }
149 
150     @Override
151     protected Object getConfiguredDefaultValue() {
152         if (NOW.equalsIgnoreCase(definition.getDefaultValue())) {
153             return new Date();
154         }
155         if (definition.isTime() && StringUtils.isNotEmpty(definition.getDefaultValue())) {
156             SimpleDateFormat dateFormatter = new SimpleDateFormat(definition.getDateFormat() + " " + definition.getTimeFormat());
157             try {
158                 return dateFormatter.parse(definition.getDefaultValue());
159             } catch (ParseException e) {
160             }
161         }
162         return super.getConfiguredDefaultValue();
163     }
164 }