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.search.compare;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.commons.lang3.builder.ToStringBuilder;
27  import org.efaps.db.AbstractObjectQuery;
28  import org.efaps.db.search.AbstractQPart;
29  import org.efaps.db.search.QAttribute;
30  import org.efaps.db.search.value.AbstractQValue;
31  import org.efaps.db.search.value.QBitValue;
32  import org.efaps.db.wrapper.SQLPart;
33  import org.efaps.db.wrapper.SQLSelect;
34  import org.efaps.util.EFapsException;
35  
36  
37  /**
38   * TODO comment!
39   *
40   * @author The eFaps Team
41   * @version $Id$
42   */
43  public class QEqual
44      extends AbstractQAttrCompare
45  {
46  
47     /**
48      * The values the given attribute must be equal to.
49      */
50      private final List<AbstractQValue> values = new ArrayList<AbstractQValue>();
51  
52      /**
53       * Constructor setting attribute and value.
54       * @param _attribute Attribute to be checked for equal
55       * @param _values    values as criteria
56       */
57      public QEqual(final QAttribute _attribute,
58                    final AbstractQValue... _values)
59      {
60          super(_attribute, null);
61          for (final AbstractQValue value : _values) {
62              this.values.add(value);
63          }
64      }
65  
66      /**
67       * Get the first value from the value list.
68       *
69       * @return null if list is empty else first value
70       */
71      @Override
72      public AbstractQValue getValue()
73      {
74          return this.values.isEmpty() ? null : this.values.get(0);
75      }
76  
77      /**
78       * Getter method for the instance variable {@link #values}.
79       *
80       * @return value of instance variable {@link #values}
81       */
82      public List<AbstractQValue> getValues()
83      {
84          return this.values;
85      }
86  
87      /**
88       * Add a value to be included in the equal.
89       * @param _value value to be include
90       * @return this
91       */
92      public AbstractQPart addValue(final AbstractQValue _value)
93      {
94          this.values.add(_value);
95          return this;
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public QEqual appendSQL(final SQLSelect _sql)
103         throws EFapsException
104     {
105         getAttribute().appendSQL(_sql);
106         if (this.values.size() > 1
107                         || this.values.size() > 0 && this.values.get(0) instanceof QBitValue) {
108             _sql.addPart(SQLPart.IN).addPart(SQLPart.PARENTHESIS_OPEN);
109             boolean first = true;
110             for (final AbstractQValue value : this.values) {
111                 if (first) {
112                     first = false;
113                 } else {
114                     _sql.addPart(SQLPart.COMMA);
115                 }
116                 value.appendSQL(_sql);
117             }
118             _sql.addPart(SQLPart.PARENTHESIS_CLOSE);
119         } else {
120             _sql.addPart(SQLPart.EQUAL);
121             getValue().appendSQL(_sql);
122         }
123         return this;
124     }
125 
126     /**
127      * {@inheritDoc}
128      */
129     @Override
130     public QEqual prepare(final AbstractObjectQuery<?> _query,
131                                  final AbstractQPart _part)
132         throws EFapsException
133     {
134         getAttribute().prepare(_query, this);
135         for (final AbstractQValue value : this.values) {
136             value.prepare(_query, this);
137         }
138         return this;
139     }
140 
141     @Override
142     public String toString()
143     {
144         return new ToStringBuilder(this).append("values", this.values).toString();
145     }
146 }