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  
25  import org.efaps.admin.datamodel.Attribute;
26  import org.efaps.admin.datamodel.Dimension;
27  import org.efaps.admin.datamodel.Dimension.UoM;
28  import org.efaps.admin.datamodel.attributevalue.IntegerWithUoM;
29  import org.efaps.db.wrapper.AbstractSQLInsertUpdate;
30  
31  /**
32   * Implements the mapping between values in the database and
33   * {@link IntegerWithUoM} values in eFaps.
34   *
35   * @author The eFaps Team
36   * @version $Id$
37   */
38  public class IntegerWithUoMType
39      extends AbstractWithUoMType
40  {
41      /**
42       * Needed for serialization.
43       */
44      private static final long serialVersionUID = 1L;
45  
46      /**
47       * The method prepares the statement for insert the object in the database.
48       * It must be overwritten, because this type has at least two columns.
49       * {@inheritDoc}
50       */
51      @Override
52      public void prepare(final AbstractSQLInsertUpdate<?> _insertUpdate,
53                          final Attribute _attribute,
54                          final Object... _values)
55          throws SQLException
56      {
57          if (_attribute.getSqlColNames().size() == 3)  {
58              checkSQLColumnSize(_attribute, 3);
59          } else  {
60              checkSQLColumnSize(_attribute, 2);
61          }
62  
63          final IntegerWithUoM value = eval(_values);
64          _insertUpdate.column(_attribute.getSqlColNames().get(0), value.getValue());
65          _insertUpdate.column(_attribute.getSqlColNames().get(1), value.getUoM().getId());
66          if (_attribute.getSqlColNames().size() == 3) {
67              _insertUpdate.column(_attribute.getSqlColNames().get(2), value.getBaseDouble());
68          }
69      }
70  
71      /**
72       * The localized string and the internal string value are equal. So the
73       * internal value can be set directly with method {@link #setValue}.
74       *
75       * @param _values new value to set
76       * @return related value with unit of measure
77       */
78      protected IntegerWithUoM eval(final Object... _values)
79      {
80          final IntegerWithUoM ret;
81  
82          if ((_values == null) || (_values.length < 2))  {
83              ret = null;
84          } else  {
85              final Integer value;
86              if ((_values[0] instanceof String) && (((String) _values[0]).length() > 0)) {
87                  value = Integer.parseInt((String) _values[0]);
88              } else if (_values[0] instanceof Number) {
89                  value = ((Number) _values[0]).intValue();
90              } else  {
91                  value = null;
92              }
93  
94              final UoM uom;
95              if (_values[1] instanceof UoM) {
96                  uom = (UoM) _values[1];
97              } else if ((_values[1] instanceof String) && (((String) _values[1]).length() > 0)) {
98                  uom = Dimension.getUoM(Long.parseLong((String) _values[1]));
99              } else if (_values[1] instanceof Number) {
100                 uom = Dimension.getUoM(((Number) _values[1]).longValue());
101             } else  {
102                 uom = null;
103             }
104             ret = new IntegerWithUoM(value, uom);
105         }
106         return ret;
107     }
108 
109     /**
110      * {@inheritDoc}
111      */
112     @Override
113     protected Object readValue(final Object _object)
114     {
115         final Integer ret;
116         if (_object instanceof Number) {
117             ret = ((Number) _object).intValue();
118         } else if (_object != null) {
119             ret = Integer.parseInt(_object.toString());
120         } else  {
121             ret = null;
122         }
123         return ret;
124     }
125 }