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;
22  
23  import org.apache.commons.lang3.builder.ToStringBuilder;
24  import org.efaps.admin.datamodel.Attribute;
25  import org.efaps.db.AbstractObjectQuery;
26  import org.efaps.db.search.compare.AbstractQAttrCompare;
27  import org.efaps.db.wrapper.SQLPart;
28  import org.efaps.db.wrapper.SQLSelect;
29  import org.efaps.util.EFapsException;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * Represents the Attribute in the Query.
35   *
36   * @author The eFaps Team
37   * @version $Id$
38   */
39  public class QAttribute
40      extends AbstractQPart
41  {
42  
43      /**
44       * Logging instance used in this class.
45       */
46      private static final Logger LOG = LoggerFactory.getLogger(QAttribute.class);
47  
48      /**
49       * Attribute this QueryAttribute is based on.
50       */
51      private Attribute attribute;
52  
53      /**
54       * Name of the attribute this QueryAttribute is based on.
55       */
56      private final String attributeName;
57  
58      /**
59       * Index of the table the attribute belongs to.
60       */
61      private Integer tableIndex;
62  
63      /**
64       * Is this attribute used in a compare applying ignore case.
65       */
66      private boolean ignoreCase = false;
67  
68      /**
69       * @param _attribute Attribute
70       */
71      public QAttribute(final Attribute _attribute)
72      {
73          this.attribute = _attribute;
74          this.attributeName = this.attribute.getName();
75      }
76  
77      /**
78       * @param _attributeName Name of the attribute
79       */
80      public QAttribute(final String _attributeName)
81      {
82          this.attributeName = _attributeName;
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      @Override
89      public AbstractQPart prepare(final AbstractObjectQuery<?> _query,
90                                   final AbstractQPart _part)
91          throws EFapsException
92      {
93          if (_part instanceof AbstractQAttrCompare) {
94              this.ignoreCase = ((AbstractQAttrCompare) _part).isIgnoreCase();
95          }
96          if (this.attribute == null) {
97              if (_query.getBaseType().getAttributes().containsKey(this.attributeName)) {
98                  this.attribute = _query.getBaseType().getAttribute(this.attributeName);
99              } else {
100                 QAttribute.LOG.error("Could not get attribute with Name '{}' for type: '{}'", this.attributeName,
101                                 _query.getBaseType());
102                 throw new EFapsException(getClass(), "prepare", this.attributeName);
103             }
104         }
105         this.tableIndex = _query.getIndex4SqlTable(this.attribute.getTable());
106         return this;
107     }
108 
109     /**
110      * {@inheritDoc}
111      */
112     @Override
113     public AbstractQPart appendSQL(final SQLSelect _sql)
114     {
115         if (this.ignoreCase) {
116             _sql.addPart(SQLPart.UPPER).addPart(SQLPart.PARENTHESIS_OPEN);
117         }
118         _sql.addColumnPart(this.tableIndex, this.attribute.getSqlColNames().get(0));
119         if (this.ignoreCase) {
120             _sql.addPart(SQLPart.PARENTHESIS_CLOSE);
121         }
122         return this;
123     }
124 
125     /**
126      * Getter method for the instance variable {@link #attribute}.
127      *
128      * @return value of instance variable {@link #attribute}
129      */
130     public Attribute getAttribute()
131     {
132         return this.attribute;
133     }
134 
135     @Override
136     public String toString()
137     {
138         return new ToStringBuilder(this)
139                         .append("attributeName", this.attributeName)
140                         .append("attribute", this.attribute).toString();
141     }
142 }