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.compare;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.efaps.admin.datamodel.Classification;
28  import org.efaps.admin.datamodel.Type;
29  import org.efaps.db.AbstractObjectQuery;
30  import org.efaps.db.search.AbstractQPart;
31  import org.efaps.db.search.QAttribute;
32  import org.efaps.db.search.value.AbstractQValue;
33  import org.efaps.db.search.value.QClassValue;
34  import org.efaps.db.wrapper.SQLPart;
35  import org.efaps.db.wrapper.SQLSelect;
36  import org.efaps.util.EFapsException;
37  
38  /**
39   * Compare that a classification id is equal to the given value.
40   *
41   * @author The eFaps Team
42   * @version $Id$
43   */
44  public class QClassEqual
45      extends AbstractQAttrCompare
46  {
47  
48     /**
49      * The values the given attribute must be equal to.
50      */
51      private final List<QClassValue> values = new ArrayList<QClassValue>();
52  
53      /**
54       * Constructor setting attribute and value.
55       * @param _values    values as criteria
56       */
57      public QClassEqual(final QClassValue... _values)
58      {
59          super(new QAttribute("ID"), null);
60          for (final QClassValue 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<QClassValue> 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 QClassValue _value)
92      {
93          this.values.add(_value);
94          return this;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public QClassEqual prepare(final AbstractObjectQuery<?> _query,
102                                  final AbstractQPart _part)
103         throws EFapsException
104     {
105         getAttribute().prepare(_query, this);
106         for (final QClassValue value : this.values) {
107             value.prepare(_query, this);
108         }
109         return this;
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public QClassEqual appendSQL(final SQLSelect _sql)
117         throws EFapsException
118     {
119         getAttribute().appendSQL(_sql);
120         final Classification clazz = this.values.get(0).getClassification();
121         final Type relType = clazz.getClassifyRelationType();
122         _sql.addPart(SQLPart.IN).addPart(SQLPart.PARENTHESIS_OPEN).addPart(SQLPart.SELECT)
123             .addColumnPart(null, relType.getAttribute(clazz.getRelLinkAttributeName()).getSqlColNames().get(0))
124             .addPart(SQLPart.FROM)
125             .addTablePart(relType.getMainTable().getSqlTable(), null)
126             .addPart(SQLPart.WHERE)
127             .addColumnPart(null, relType.getAttribute(clazz.getRelTypeAttributeName()).getSqlColNames().get(0));
128 
129         if (this.values.size() > 1) {
130             _sql.addPart(SQLPart.IN).addPart(SQLPart.PARENTHESIS_OPEN);
131             boolean first = true;
132             for (final QClassValue value : this.values) {
133                 if (first) {
134                     first = false;
135                 } else {
136                     _sql.addPart(SQLPart.COMMA);
137                 }
138                 value.appendSQL(_sql);
139             }
140             _sql.addPart(SQLPart.PARENTHESIS_CLOSE);
141         } else {
142             _sql.addPart(SQLPart.EQUAL);
143             getValue().appendSQL(_sql);
144         }
145         _sql.addPart(SQLPart.PARENTHESIS_CLOSE);
146         return this;
147     }
148 }