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.db.print.value;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.efaps.admin.datamodel.Attribute;
27  import org.efaps.admin.datamodel.Type;
28  import org.efaps.db.print.ISelectPart;
29  import org.efaps.db.print.OneSelect;
30  import org.efaps.db.wrapper.SQLSelect;
31  import org.efaps.util.EFapsException;
32  
33  /**
34   * Abstract class used as base for all different types of ValueSelect.
35   * A ValueSelct represents the part of a SQL-Statament that actual selects
36   * a value.
37   *
38   * @author The eFaps Team
39   * @version $Id$
40   */
41  public abstract class AbstractValueSelect
42  {
43  
44      /**
45       * List of column indexes the values have in the ResultSet returned from the
46       * eFaps database.
47       */
48      private final List<Integer> colIndexs = new ArrayList<Integer>();
49  
50      /**
51       * Parent of this AbstractValueSelect.
52       */
53      private AbstractValueSelect parent;
54  
55      /**
56       * Child of this AbstractValueSelect.
57       */
58      private AbstractValueSelect child;
59  
60      /**
61       *  OneSelect this ValueSelect belongs to.
62       */
63      private final OneSelect oneSelect;
64  
65      /**
66       * SelectPart this ValueSelect is connected to.
67       */
68      private ISelectPart parentSelectPart;
69  
70      /**
71       * Constructor setting the OneSelect this valueselect belongs to.
72       *
73       * @param _oneSelect OneSelect
74       */
75      public AbstractValueSelect(final OneSelect _oneSelect)
76      {
77          this.oneSelect = _oneSelect;
78          if (!this.oneSelect.getSelectParts().isEmpty()) {
79              this.parentSelectPart = this.oneSelect.getSelectParts().get(this.oneSelect.getSelectParts().size() - 1);
80          }
81      }
82  
83      /**
84       * Getter method for instance variable {@link #parent}.
85       *
86       * @return value of instance variable {@link #parent}
87       */
88      public AbstractValueSelect getParent()
89      {
90          return this.parent;
91      }
92  
93      /**
94       * Getter method for the instance variable {@link #parentSelectPart}.
95       *
96       * @return value of instance variable {@link #parentSelectPart}
97       */
98      public ISelectPart getParentSelectPart()
99      {
100         return this.parentSelectPart;
101     }
102 
103     /**
104      * Method must return a unique String to identify the class. This is used to
105      * determine which class was instantiated instead of using "instanceof".
106      *
107      * @return unique name.
108      */
109     public abstract String getValueType();
110 
111     /**
112      * Getter method for instance variable {@link #colIndexs}.
113      *
114      * @return instance variable {@link #colIndexs}
115      */
116     public List<Integer> getColIndexs()
117     {
118         return this.colIndexs;
119     }
120 
121     /**
122      * Method to set an AbstractValueSelect as the parent of this
123      * AbstractValueSelect.
124      *
125      * @param _parent AbstractValueSelect to be set as parent
126      */
127     public void setParentValueSelect(final AbstractValueSelect _parent)
128     {
129         this.parent = _parent;
130     }
131 
132     /**
133      * Method adds an AbstractValueSelect as a child of this chain of
134      * AbstractValueSelect.
135      *
136      * @param _valueSelect AbstractValueSelect to be added as child
137      * @throws EFapsException on error
138      */
139     public void addChildValueSelect(final AbstractValueSelect _valueSelect)
140         throws EFapsException
141     {
142         if (this.child == null) {
143             this.child = _valueSelect;
144             _valueSelect.setParentValueSelect(this);
145         } else {
146             this.child.addChildValueSelect(_valueSelect);
147         }
148     }
149 
150     /**
151      * Method is used to add the select part for this ValueSelect to the select
152      * statement. e.g. "select T0.ID, TO.TYPEID" etc.
153      *
154      * @param _type Type this ValueSelect belongs to
155      * @param _select SQL select statement to be appended to
156      * @param _tableIndex index of the table
157      * @param _colIndex last index of the column
158      * @return number of columns added to the select statement
159      * @throws EFapsException on error
160      */
161     public int append2SQLSelect(final Type _type,
162                                 final SQLSelect _select,
163                                 final int _tableIndex,
164                                 final int _colIndex)
165         throws EFapsException
166     {
167         return 0;
168     }
169 
170     /**
171      * Method to get the value for the current object.
172      *
173      * @param _object current object
174      * @throws EFapsException on error
175      * @return object
176      */
177     public Object getValue(final Object _object)
178         throws EFapsException
179     {
180         return _object;
181     }
182 
183     /**
184      * Method to get the value for a list of object.
185      *
186      * @param _objectList list of objects
187      * @throws EFapsException on error
188      * @return object
189      */
190     public Object getValue(final List<Object> _objectList)
191         throws EFapsException
192     {
193         final List<Object> ret = new ArrayList<Object>();
194         for (final Object object : _objectList) {
195             ret.add(getValue(object));
196         }
197         return _objectList.size() > 0 ? (ret.size() > 1 ? ret : ret.get(0)) : null;
198     }
199 
200     /**
201      * Getter method for instance variable {@link #child}.
202      *
203      * @return value of instance variable {@link #child}
204      */
205     public AbstractValueSelect getChildValueSelect()
206     {
207         return this.child;
208     }
209 
210     /**
211      * Method to return the attribute related to this AbstractValueSelect.
212      *
213      * @return Attribute if exists, else null
214      */
215     public Attribute getAttribute()
216     {
217         return null;
218     }
219 
220     /**
221      * Getter method for the instance variable {@link #oneSelect}.
222      *
223      * @return value of instance variable {@link #oneSelect}
224      */
225     public OneSelect getOneSelect()
226     {
227         return this.oneSelect;
228     }
229 }