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