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.section;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.efaps.db.AbstractObjectQuery;
28  import org.efaps.db.search.AbstractQPart;
29  import org.efaps.db.wrapper.SQLPart;
30  import org.efaps.db.wrapper.SQLSelect;
31  import org.efaps.util.EFapsException;
32  
33  
34  /**
35   * Comparison for max value.
36   *
37   * @author The eFaps Team
38   * @version $Id$
39   */
40  public class QOrderBySection
41       extends AbstractQSection
42  {
43  
44      /**
45       * List of attributes this will be ordered.
46       */
47      private final List<AbstractQPart> parts = new ArrayList<AbstractQPart>();
48  
49      /**
50       * Constructor adding Attributes.
51       * @param _parts Array of AbstractQPart
52       */
53      public QOrderBySection(final AbstractQPart... _parts)
54      {
55          for (final AbstractQPart part : _parts) {
56              this.parts.add(part);
57          }
58      }
59  
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public QOrderBySection appendSQL(final SQLSelect _select)
65          throws EFapsException
66      {
67          _select.addPart(SQLPart.ORDERBY);
68          boolean first = true;
69          for (final AbstractQPart part : this.parts) {
70              if (first) {
71                  first = false;
72              } else {
73                  _select.addPart(SQLPart.COMMA);
74              }
75              part.appendSQL(_select);
76          }
77          return this;
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public QOrderBySection prepare(final AbstractObjectQuery<?> _query)
85          throws EFapsException
86      {
87          for (final AbstractQPart part : this.parts) {
88              part.prepare(_query, null);
89          }
90          return this;
91      }
92  
93      /**
94       * Add an attribute to the list of attributes the query will be ordered by.
95       *
96       * @param _part AbstractQPart to be added
97       * @return this
98       */
99      public QOrderBySection addPart(final AbstractQPart _part)
100     {
101         this.parts.add(_part);
102         return this;
103     }
104 }