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  
22  package org.efaps.admin.datamodel.attributetype;
23  
24  import java.sql.Timestamp;
25  import java.util.Date;
26  
27  import org.efaps.util.EFapsException;
28  import org.joda.time.DateTime;
29  import org.joda.time.format.ISODateTimeFormat;
30  
31  
32  /**
33   * Attribute Type for Date. It uses DateTime by cutting of the Time information.
34   *
35   * @author The eFaps Team
36   * @version $Id$
37   */
38  public class DateType
39      extends DateTimeType
40  {
41      /**
42       * Needed for serialization.
43       */
44      private static final long serialVersionUID = 1L;
45  
46      /**
47       * The value that can be set is a Date, a DateTime or a String
48       * yyyy-MM-dd'T'HH:mm:ss.SSSZZ. It will be normalized to ISO Calender with
49       * TimeZone from SystemAttribute Admin_Common_DataBaseTimeZone. In case that
50       * the SystemAttribute is missing UTC will be used.
51       * For storing the value in the database the time is set to 00:00;
52       *
53       * @param _value value to evaluate
54       * @return evaluated value
55       * @throws EFapsException on error
56       */
57      @Override
58      protected Timestamp eval(final Object[] _value)
59          throws EFapsException
60      {
61          final Timestamp ret;
62          if ((_value == null) || (_value.length == 0) || (_value[0] == null)) {
63              ret = null;
64          } else  {
65              DateTime dateTime = new DateTime();
66              if (_value[0] instanceof Date) {
67                  dateTime = new DateTime(_value[0]);
68              } else if (_value[0] instanceof DateTime) {
69                  dateTime = (DateTime) _value[0];
70              } else if (_value[0] instanceof String) {
71                  dateTime = ISODateTimeFormat.dateTime().withOffsetParsed().parseDateTime((String) _value[0]);
72              }
73              // until now we have a time that depends on the timezone of the application server
74              // to convert it in a timestamp for the efaps database the timezone information (mainly the offset)
75              // must be removed. This is done by creating a local date with the same, date and time.
76              // this guarantees that the datetime inserted into the database depends on the setting
77              // in the configuration and not on the timezone for the application server.
78              final DateTime localized = new DateTime(dateTime.getYear(),
79                                                      dateTime.getMonthOfYear(),
80                                                      dateTime.getDayOfMonth(),
81                                                      0,
82                                                      0,
83                                                      0,
84                                                      0);
85              ret = (localized != null) ? new Timestamp(localized.getMillis()) : null;
86          }
87          return ret;
88      }
89  }