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.print;
22  
23  import org.apache.commons.lang3.builder.ToStringBuilder;
24  import org.efaps.admin.datamodel.Attribute;
25  import org.efaps.admin.datamodel.Classification;
26  import org.efaps.admin.datamodel.Type;
27  import org.efaps.db.wrapper.SQLPart;
28  import org.efaps.db.wrapper.SQLSelect;
29  import org.efaps.util.EFapsException;
30  import org.efaps.util.cache.CacheReloadException;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * Select Part for <code>class[CLASSIFICATIONNAME]</code>.
36   *
37   * @author The eFaps Team
38   * @version $Id$
39   */
40  public class ClassSelectPart
41      extends AbstractSelectPart
42  {
43      /**
44       * Logging instance used in this class.
45       */
46      private static final Logger LOG = LoggerFactory.getLogger(OneSelect.class);
47  
48      /**
49       * Classification this select part belongs to.
50       */
51      private final Classification classification;
52  
53      /**
54       * index of the table.
55       */
56      private int tableIdx;
57  
58      /**
59       * @param _classification   classification
60       * @throws CacheReloadException on error
61       */
62      public ClassSelectPart(final String _classification)
63          throws CacheReloadException
64      {
65          this.classification = (Classification) Type.get(_classification);
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      @Override
72      public int join(final OneSelect _oneSelect,
73                      final SQLSelect _select,
74                      final int _relIndex)
75          throws EFapsException
76      {
77          Integer ret;
78          final String tableName = this.classification.getMainTable().getSqlTable();
79          final Attribute attr = this.classification.getAttribute(this.classification.getLinkAttributeName());
80          if (attr == null) {
81              ClassSelectPart.LOG.error("Could not find attribute: '{}'", this.classification.getLinkAttributeName());
82              throw new EFapsException(ClassSelectPart.class, "joinNoAttribute");
83          }
84          final String column = attr.getSqlColNames().get(0);
85          ret = _oneSelect.getTableIndex(tableName, column, _relIndex, this.classification.getId());
86          if (ret == null) {
87              ret = _oneSelect.getNewTableIndex(tableName, column, _relIndex, this.classification.getId());
88              _select.leftJoin(tableName, ret, column, _relIndex, "ID");
89          }
90          this.tableIdx = ret;
91          return ret;
92      }
93  
94      @Override
95      public void add2Where(final OneSelect _oneselect,
96                            final SQLSelect _select)
97      {
98          if (this.classification.getMainTable().getSqlColType() != null) {
99              _select.addPart(SQLPart.AND)
100                     .addColumnPart(this.tableIdx, this.classification.getMainTable().getSqlColType())
101                     .addPart(SQLPart.EQUAL)
102                     .addValuePart(this.classification.getId());
103         }
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     @Override
110     public Type getType()
111     {
112         return this.classification;
113     }
114 
115     @Override
116     public String toString()
117     {
118         return ToStringBuilder.reflectionToString(this);
119     }
120 }