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.ArrayList;
26  import java.util.Date;
27  import java.util.List;
28  
29  import org.efaps.admin.datamodel.Attribute;
30  import org.efaps.util.EFapsException;
31  import org.joda.time.DateTime;
32  import org.joda.time.LocalTime;
33  import org.joda.time.format.ISODateTimeFormat;
34  
35  
36  /**
37   * Attribute Type for Date. It uses DateTime by cutting of the Time information.
38   *
39   * @author The eFaps Team
40   * @version $Id$
41   */
42  public class TimeType
43      extends DateTimeType
44  {
45      /**
46       * Needed for serialization.
47       */
48      private static final long serialVersionUID = 1L;
49  
50  
51      @Override
52      public Object readValue(final Attribute _attribute,
53                              final List<Object> _objectList)
54          throws EFapsException
55      {
56          final List<LocalTime> ret = new ArrayList<LocalTime>();
57          for (final Object object : _objectList) {
58              if (object instanceof Timestamp || object instanceof Date) {
59                  ret.add(new DateTime(object).toLocalTime());
60              } else if (ret != null) {
61                  ret.add(new LocalTime());
62              }
63          }
64          return _objectList.size() > 0 ? (ret.size() > 1 ? ret : ret.get(0)) : null;
65      }
66  
67  
68      /**
69       * The value that can be set is a Date, a DateTime or a String
70       * yyyy-MM-dd'T'HH:mm:ss.SSSZZ. It will be normalized to ISO Calender with
71       * TimeZone from SystemAttribute Admin_Common_DataBaseTimeZone. In case that
72       * the SystemAttribute is missing UTC will be used.
73       * For storing the value in the database the time is set to 00:00;
74       *
75       * @param _value value to evaluate
76       * @return evaluated value
77       * @throws EFapsException on error
78       */
79      @Override
80      protected Timestamp eval(final Object[] _value)
81          throws EFapsException
82      {
83          final Timestamp ret;
84          if ((_value == null) || (_value.length == 0) || (_value[0] == null)) {
85              ret = null;
86          } else  {
87              LocalTime time = new LocalTime();
88              if (_value[0] instanceof Date) {
89                  time = new DateTime(_value[0]).toLocalTime();
90              } else if (_value[0] instanceof DateTime) {
91                  time = ((DateTime) _value[0]).toLocalTime();
92              } else if (_value[0] instanceof String) {
93                  time = ISODateTimeFormat.localTimeParser().parseLocalTime((String) _value[0]);
94              }
95              ret = new Timestamp(time.toDateTimeToday().getMillis());
96          }
97          return ret;
98      }
99  }