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.StringWithUoM;
29  import org.efaps.db.wrapper.AbstractSQLInsertUpdate;
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 StringWithUoMType
39      extends AbstractWithUoMType
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, 2);
56          final StringWithUoM value = eval(_values);
57          _insertUpdate.column(_attribute.getSqlColNames().get(0), value.getValue());
58          _insertUpdate.column(_attribute.getSqlColNames().get(1), value.getUoM().getId());
59      }
60  
61      /**
62       * The localized string and the internal string value are equal. So the
63       * internal value can be set directly with method {@link #setValue}.
64       *
65       * @param _values   values to evaluate
66       * @return string representation for the <code>_values</code>
67       */
68      protected StringWithUoM eval(final Object[] _values)
69      {
70          StringWithUoM ret;
71          if ((_values == null) || (_values.length == 0) || (_values.length < 2))  {
72              ret = null;
73          } else  {
74              final String value;
75              if ((_values[0] instanceof String) && !((String) _values[0]).isEmpty()) {
76                  value = (String) _values[0];
77              } else if (_values[0] != null) {
78                  value =  _values[0].toString();
79              } else  {
80                  value = null;
81              }
82  
83              final UoM uom;
84              if (_values[1] instanceof UoM) {
85                  uom = (UoM) _values[1];
86              } else if ((_values[1] instanceof String) && (((String) _values[1]).length() > 0)) {
87                  uom = Dimension.getUoM(Long.parseLong((String) _values[1]));
88              } else if (_values[1] instanceof Number) {
89                  uom = Dimension.getUoM(((Number) _values[1]).longValue());
90              } else  {
91                  uom = null;
92              }
93              ret = new StringWithUoM(value, uom);
94          }
95          return ret;
96      }
97  
98  
99      /**
100      * {@inheritDoc}
101      */
102     @Override
103     protected Object readValue(final Object _object)
104     {
105         final String ret;
106         if (_object instanceof String) {
107             ret = (String) _object;
108         } else if (_object != null) {
109             ret = _object.toString();
110         } else  {
111             ret = null;
112         }
113         return ret == null ? ret : ret.trim();
114     }
115 }