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.attributetype;
22  
23  import java.sql.SQLException;
24  import java.util.List;
25  
26  import org.efaps.admin.datamodel.Attribute;
27  import org.efaps.db.wrapper.AbstractSQLInsertUpdate;
28  import org.efaps.util.EFapsException;
29  
30  /**
31   * Implements the mapping between values in the database and {@link Double}
32   * values in eFaps.
33   *
34   * @author The eFaps Team
35   * @version $Id$
36   */
37  public class RealType
38      extends AbstractType
39  {
40      /**
41       * Needed for serialization.
42       */
43      private static final long serialVersionUID = 1L;
44  
45      /**
46       * {@inheritDoc}
47       */
48      @Override
49      protected void prepare(final AbstractSQLInsertUpdate<?> _insertUpdate,
50                             final Attribute _attribute,
51                             final Object... _values)
52          throws SQLException
53      {
54          checkSQLColumnSize(_attribute, 1);
55          _insertUpdate.column(_attribute.getSqlColNames().get(0), eval(_values));
56      }
57  
58      /**
59       * Evaluate the value.
60       * @param _value value to be evaluated
61       * @return Long value
62       */
63      protected Double eval(final Object[] _value)
64      {
65          final Double ret;
66  
67          if (_value == null) {
68              ret = null;
69          } else if ((_value[0] instanceof String) && (((String) _value[0]).length() > 0)) {
70              ret = Double.parseDouble((String) _value[0]);
71          } else if (_value[0] instanceof Number) {
72              ret = ((Number) _value[0]).doubleValue();
73          } else  {
74              ret = null;
75          }
76  
77          return ret;
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public Object readValue(final Attribute _attribute,
85                              final List<Object> _objectList)
86      {
87          Double ret = null;
88          final Object obj = _objectList.get(0);
89          if (obj instanceof Number) {
90              ret = ((Number) obj).doubleValue();
91          } else if (obj != null) {
92              ret = Double.parseDouble(obj.toString());
93          }
94          return ret;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public String toString4Where(final Object _value)
102         throws EFapsException
103     {
104         String ret = "";
105         if (_value instanceof Number) {
106             ret =  ((Number) _value).toString();
107         } else if (_value instanceof String) {
108             ret = (String) _value;
109         } else if (_value != null) {
110             ret = _value.toString();
111         }
112         return ret;
113     }
114 }