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