1   /*
2    * Copyright 2003 - 2013 The eFaps Team
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  package org.efaps.admin.datamodel.ui;
22  
23  import org.efaps.db.Context;
24  import org.efaps.util.EFapsException;
25  import org.joda.time.DateTime;
26  import org.joda.time.DateTimeComparator;
27  import org.joda.time.format.DateTimeFormat;
28  import org.joda.time.format.DateTimeFormatter;
29  
30  /**
31   * Class to represent a Date for the user interface.
32   *
33   * @author The eFaps Team
34   * @version $Id$
35   */
36  public class DateUI
37      extends AbstractUI
38  {
39  
40      /**
41       * Needed for serialization.
42       */
43      private static final long serialVersionUID = 1L;
44  
45      /**
46       * {@inheritDoc}
47       */
48      @Override
49      public String getReadOnlyHtml(final FieldValue _fieldValue)
50          throws EFapsException
51      {
52          String ret = null;
53          if (_fieldValue.getValue() instanceof DateTime) {
54              final DateTime datetime = (DateTime) _fieldValue.getValue();
55              if (datetime != null) {
56                  // format the Date with the Locale and Chronology from the user
57                  // context
58                  final DateTime dateTmp = new DateTime().withTimeAtStartOfDay()
59                                  .withYear(datetime.getYear())
60                                  .withMonthOfYear(datetime.getMonthOfYear())
61                                  .withDayOfMonth(datetime.getDayOfMonth())
62                                  .withChronology(Context.getThreadContext().getChronology());
63  
64                  final DateTimeFormatter formatter = DateTimeFormat.mediumDate();
65  
66                  ret = dateTmp.toString(formatter.withLocale(Context.getThreadContext().getLocale()));
67              }
68          } else if (_fieldValue.getValue() instanceof String) {
69              ret = (String) _fieldValue.getValue();
70          } else if (_fieldValue.getValue() != null) {
71              throw new EFapsException(this.getClass(), "getViewHtml.noDateTime", (Object[]) null);
72          }
73          return ret;
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public String getStringValue(final FieldValue _fieldValue)
81          throws EFapsException
82      {
83          String ret = null;
84          if (_fieldValue.getValue() != null) {
85              ret = _fieldValue.getValue().toString();
86          }
87          return ret;
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public Object getObject4Compare(final FieldValue _fieldValue)
95          throws EFapsException
96      {
97          Object ret;
98          if (_fieldValue != null && _fieldValue.getValue() instanceof DateTime) {
99              final DateTime datetime = (DateTime) _fieldValue.getValue();
100             ret = new DateTime().withTimeAtStartOfDay()
101                             .withYear(datetime.getYear())
102                             .withMonthOfYear(datetime.getMonthOfYear())
103                             .withDayOfMonth(datetime.getDayOfMonth())
104                             .withChronology(Context.getThreadContext().getChronology());
105         } else {
106             ret = _fieldValue.getValue();
107         }
108         return ret;
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public int compare(final FieldValue _fieldValue,
116                        final FieldValue _fieldValue2)
117     {
118         int ret = 0;
119         if (_fieldValue.getValue() instanceof DateTime && _fieldValue2.getValue() instanceof DateTime) {
120             ret = DateTimeComparator.getDateOnlyInstance().compare(_fieldValue.getValue(), _fieldValue2.getValue());
121         }
122         return ret;
123     }
124 
125     /**
126      * {@inheritDoc}
127      */
128     @Override
129     public Object format(final Object _object,
130                          final String _pattern)
131         throws EFapsException
132     {
133         Object ret;
134         if (_object instanceof DateTime) {
135             ret = ((DateTime) _object).toString(_pattern, Context.getThreadContext().getLocale());
136         } else {
137             ret = _object;
138         }
139         return ret;
140     }
141 }