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.IAttributeType;
27  import org.efaps.db.Instance;
28  import org.efaps.db.wrapper.AbstractSQLInsertUpdate;
29  import org.efaps.db.wrapper.SQLInsert;
30  import org.efaps.db.wrapper.SQLUpdate;
31  import org.efaps.util.EFapsException;
32  
33  /**
34   * @author The eFaps Team
35   * @version $Id$
36   */
37  public abstract class AbstractType
38      implements IAttributeType
39  {
40      /**
41       * Needed for serialization.
42       */
43      private static final long serialVersionUID = 1L;
44  
45      /**
46       * {@inheritDoc}
47       */
48      @Override
49      public void prepareInsert(final SQLInsert _insert,
50                                final Attribute _attribute,
51                                final Object... _values)
52          throws SQLException
53      {
54          prepare(_insert, _attribute, _values);
55      }
56  
57      /**
58       * {@inheritDoc}
59       */
60      @Override
61      public void prepareUpdate(final SQLUpdate _update,
62                                final Attribute _attribute,
63                                final Object... _values)
64          throws SQLException
65      {
66          prepare(_update, _attribute, _values);
67      }
68  
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      public void valiate4Update(final Attribute _attribute,
75                                 final Instance _instance,
76                                 final Object[] _value)
77          throws EFapsException
78      {
79          // as default the value is valid and therefore no error must be thrown
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public void valiate4Insert(final Attribute _attribute,
87                                 final Instance _instance,
88                                 final Object[] _value)
89          throws EFapsException
90      {
91          // as default the value is valid and therefore no error must be thrown
92      }
93  
94      /**
95       *
96       * @param _insertUpdate SQL insert / update statement
97       * @param _attribute    SQL update statement
98       * @param _values       new object value to set; values are localized and
99       *                      are coming from the user interface
100      * @throws SQLException always, because the method must be overwritten
101      */
102     protected void prepare(final AbstractSQLInsertUpdate<?> _insertUpdate,
103                            final Attribute _attribute,
104                            final Object... _values)
105         throws SQLException
106     {
107         throw new SQLException("not implemented for " + this.getClass());
108     }
109 
110     /**
111      * Checks for expected <code>_size</code> of SQL columns for
112      * <code>_attribute</code>.
113      *
114      * @param _attribute    attribute to check
115      * @param _size         expected size of SQL columns
116      * @throws SQLException if the size of the SQL columns in
117      *                      <code>_attribute</code> is not the expected
118      *                      <code>_size</code>
119      */
120     public void checkSQLColumnSize(final Attribute _attribute,
121                                    final int _size)
122         throws SQLException
123     {
124         if ((_attribute.getSqlColNames() == null) || _attribute.getSqlColNames().isEmpty())  {
125             throw new SQLException("no SQL column for attribute defined");
126         }
127         if (_attribute.getSqlColNames().size() > _size)  {
128             throw new SQLException("more than " + _size + " SQL columns defined (is "
129                     + _attribute.getSqlColNames().size() + ")");
130         }
131     }
132 
133     /**
134      * {@inheritDoc}
135      */
136     @Override
137     public String toString4Where(final Object _value)
138         throws EFapsException
139     {
140         return _value.toString();
141     }
142 
143 }