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  
22  package org.efaps.db.search;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.efaps.db.AbstractObjectQuery;
28  import org.efaps.db.wrapper.SQLPart;
29  import org.efaps.db.wrapper.SQLSelect;
30  import org.efaps.util.EFapsException;
31  
32  
33  /**
34   * Represents a SQL "AND".
35   *
36   * @author The eFaps Team
37   * @version $Id$
38   */
39  public class QAnd
40      extends AbstractQPart
41  {
42      /**
43       * List of parts that will be connected by "AND".
44       */
45      private final List<AbstractQPart> parts = new ArrayList<AbstractQPart>();
46  
47  
48      /**
49       * Constructor setting the parts of this AND.
50       * @param _parts parts for this and
51       */
52      public QAnd(final AbstractQPart... _parts)
53      {
54          for (final AbstractQPart part : _parts) {
55              this.parts.add(part);
56          }
57      }
58  
59      /**
60       * Add a part to be included in the and.
61       * @param _part part to be include
62       * @return this
63       */
64      public AbstractQPart addPart(final AbstractQPart _part)
65      {
66          this.parts.add(_part);
67          return this;
68      }
69  
70      /**
71       * Getter method for the instance variable {@link #parts}.
72       *
73       * @return value of instance variable {@link #parts}
74       */
75      protected List<AbstractQPart> getParts()
76      {
77          return this.parts;
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public QAnd appendSQL(final SQLSelect _sql)
85          throws EFapsException
86      {
87          _sql.addPart(SQLPart.PARENTHESIS_OPEN);
88          boolean first = true;
89          for (final AbstractQPart part : this.parts) {
90              if (first) {
91                  first = false;
92              } else {
93                  _sql.addPart(SQLPart.AND);
94              }
95              part.appendSQL(_sql);
96          }
97          _sql.addPart(SQLPart.PARENTHESIS_CLOSE);
98          return this;
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public QAnd prepare(final AbstractObjectQuery<?> _query,
106                                  final AbstractQPart _part)
107         throws EFapsException
108     {
109         for (final AbstractQPart part : this.parts) {
110             part.prepare(_query, this);
111         }
112         return this;
113     }
114 }