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 DateTime for the user interface.
32   *
33   * @author The eFaps Team
34   * @version $Id$
35   */
36  public class DateTimeUI
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  
54          if (_fieldValue.getValue() instanceof DateTime) {
55              final DateTime datetime = (DateTime) _fieldValue.getValue();
56              if (datetime != null) {
57                  final DateTimeFormatter formatter = DateTimeFormat.mediumDateTime();
58                  // format the Date with the Locale and Chronology from the user
59                  // context
60                  ret = datetime.withChronology(Context.getThreadContext().getChronology()).toString(
61                                  formatter.withLocale(Context.getThreadContext().getLocale()));
62              }
63          } else if (_fieldValue.getValue() instanceof String) {
64              ret = (String) _fieldValue.getValue();
65          } else if (_fieldValue.getValue() != null) {
66              throw new EFapsException(this.getClass(), "getViewHtml.noDateTime", (Object[]) null);
67          }
68          return ret;
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      @Override
75      public String getStringValue(final FieldValue _fieldValue)
76          throws EFapsException
77      {
78          String ret = null;
79          if (_fieldValue.getValue() != null) {
80              ret = _fieldValue.getValue().toString();
81          }
82          return ret;
83      }
84  
85      /**
86       *  {@inheritDoc}
87       */
88      @Override
89      public Object getObject4Compare(final FieldValue _fieldValue)
90          throws EFapsException
91      {
92          Object ret;
93          if (_fieldValue != null && _fieldValue.getValue() instanceof DateTime) {
94              final DateTime datetime = (DateTime) _fieldValue.getValue();
95              ret = datetime.withChronology(Context.getThreadContext().getChronology());
96          } else {
97              ret = _fieldValue.getValue();
98          }
99          return ret;
100     }
101 
102     /**
103      *  {@inheritDoc}
104      */
105     @Override
106     public int compare(final FieldValue _fieldValue,
107                        final FieldValue _fieldValue2)
108     {
109         int ret = 0;
110         if (_fieldValue.getValue() instanceof DateTime && _fieldValue2.getValue() instanceof DateTime) {
111             ret = DateTimeComparator.getInstance().compare(_fieldValue.getValue(), _fieldValue2.getValue());
112         }
113         return ret;
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     public Object format(final Object _object,
121                          final String _pattern)
122         throws EFapsException
123     {
124         Object ret;
125         if (_object instanceof DateTime) {
126             ret = ((DateTime) _object).toString(_pattern, Context.getThreadContext().getLocale());
127         } else {
128             ret = _object;
129         }
130         return ret;
131     }
132 }