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 String}
33   * values in eFaps.
34  
35   * @author The eFaps Team
36   * @version $Id$
37   */
38  public class StringType
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      protected 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       * The localized string and the internal string value are equal. So the
61       * internal value can be set directly with method {@link #setValue}.
62       *
63       * @param _values   values to evaluate
64       * @return string representation for the <code>_values</code>
65       */
66      protected String eval(final Object[] _values)
67      {
68          final String ret;
69          if ((_values == null) || (_values.length == 0) || (_values[0] == null))  {
70              ret = null;
71          } else if (_values[0] instanceof String) {
72              ret = (String) _values[0];
73          } else if (_values[0] != null) {
74              ret = _values[0].toString();
75          } else  {
76              ret = null;
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          throws EFapsException
88      {
89          Object ret = null;
90          if (_objectList.size() < 1) {
91              ret = null;
92          } else if (_objectList.size() > 1) {
93              final List<String> list = new ArrayList<String>();
94              for (final Object object : _objectList) {
95                  list.add(object == null ? "" : object.toString().trim());
96              }
97              ret = list;
98          } else {
99              final Object object = _objectList.get(0);
100             ret = object == null ? "" : object.toString().trim();
101         }
102         return ret;
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public String toString4Where(final Object _value)
110         throws EFapsException
111     {
112         String ret = "";
113         if (_value instanceof String) {
114             ret = (String) _value;
115         } else if (_value != null) {
116             ret = _value.toString();
117         }
118         return ret;
119     }
120 }