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