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