View Javadoc
1   /**
2    * This file Copyright (c) 2012-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.workbench.column;
35  
36  import info.magnolia.cms.security.MgnlUserManager;
37  import info.magnolia.context.Context;
38  import info.magnolia.objectfactory.Components;
39  import info.magnolia.ui.workbench.column.definition.AbstractColumnDefinition;
40  import info.magnolia.ui.workbench.column.definition.MetaDataColumnDefinition;
41  
42  import java.util.Date;
43  import java.util.Locale;
44  import java.util.SimpleTimeZone;
45  import java.util.TimeZone;
46  
47  import javax.inject.Inject;
48  
49  import org.apache.commons.lang3.StringUtils;
50  import org.apache.commons.lang3.time.FastDateFormat;
51  
52  import com.vaadin.server.Page;
53  import com.vaadin.v7.data.Item;
54  import com.vaadin.v7.data.Property;
55  import com.vaadin.v7.ui.Table;
56  
57  /**
58   * Formats a column's value as a date in a compact form.
59   */
60  public class DateColumnFormatter extends AbstractColumnFormatter<AbstractColumnDefinition> {
61  
62      private static final String BROWSER_TIMEZONE = "browser";
63  
64      private final FastDateFormat dateFormatter;
65      private final FastDateFormat timeFormatter;
66      private final TimeZone timeZone;
67  
68      private final Context context;
69  
70      @Inject
71      public DateColumnFormatter(AbstractColumnDefinition definition, Context context) {
72          super(definition);
73          this.context = context;
74  
75          final Locale locale = context.getLocale();
76          final String timeZoneId = context.getUser().getProperty(MgnlUserManager.PROPERTY_TIMEZONE);
77          if (timeZoneId == null || BROWSER_TIMEZONE.equals(timeZoneId)) {
78              if (Page.getCurrent() != null) {
79                  int offset = Page.getCurrent().getWebBrowser().getTimezoneOffset(); //only offset, not raw offset since we don't know the  daylight settings of user timezone
80                  timeZone = new SimpleTimeZone(offset, BROWSER_TIMEZONE);
81              } else {
82                  timeZone = null;
83              }
84          } else {
85              timeZone = TimeZone.getTimeZone(timeZoneId);
86          }
87          if (definition instanceof MetaDataColumnDefinition) {
88              MetaDataColumnDefinition metaDataColumnDefinition = (MetaDataColumnDefinition) definition;
89  
90              // in case a property wasn't specified, set the formatter to null so that generateCell() doesn't use it like it normally would
91              if (StringUtils.isEmpty(metaDataColumnDefinition.getDateFormat())) {
92                  dateFormatter = null;
93              } else {
94                  dateFormatter = FastDateFormat.getInstance(metaDataColumnDefinition.getDateFormat(), timeZone, locale);
95              }
96              if (StringUtils.isEmpty(metaDataColumnDefinition.getTimeFormat())) {
97                  timeFormatter = null;
98              } else {
99                  timeFormatter = FastDateFormat.getInstance(metaDataColumnDefinition.getTimeFormat(), timeZone, locale);
100             }
101         } else {
102             dateFormatter = FastDateFormat.getDateInstance(FastDateFormat.MEDIUM, timeZone, locale);
103             timeFormatter = FastDateFormat.getTimeInstance(FastDateFormat.SHORT, timeZone, locale);
104         }
105     }
106 
107     /**
108      * @deprecated since 5.4.7. Use {@link #DateColumnFormatter(AbstractColumnDefinition, Context)} instead.
109      */
110     @Deprecated
111     public DateColumnFormatter(AbstractColumnDefinition definition) {
112         this(definition, Components.getComponent(Context.class));
113     }
114 
115     @Override
116     public Object generateCell(Table source, Object itemId, Object columnId) {
117         Item item = source.getItem(itemId);
118         Property prop = (item == null) ? null : item.getItemProperty(columnId);
119 
120         // Need to check prop.getValue() before prop.getType() to avoid NPE if value is null.
121         if (prop != null && prop.getValue() != null && Date.class.equals(prop.getType())) {
122             String date = dateFormatter == null ? StringUtils.EMPTY : dateFormatter.format(prop.getValue());
123             String time = timeFormatter == null ? StringUtils.EMPTY : timeFormatter.format(prop.getValue());
124             String dateHtml = StringUtils.isEmpty(date) ? StringUtils.EMPTY : String.format("<span class=\"datefield\">%s</span>", date);
125             String timeHtml = StringUtils.isEmpty(time) ? StringUtils.EMPTY : String.format("<span class=\"timefield\">%s</span>", time);
126             String tooltip = timeZone == null ? StringUtils.EMPTY : "title=\"" + date + " " + time + "  (" + timeZone.getDisplayName(context.getLocale()) + ")" + "\"";
127             return String.format("<span class=\"datetimefield\" %s>%s%s</span>", tooltip, dateHtml, timeHtml);
128         }
129         return null;
130     }
131 }