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.print;
23  
24  import org.efaps.admin.datamodel.Type;
25  import org.efaps.db.GeneralInstance;
26  import org.efaps.db.wrapper.SQLPart;
27  import org.efaps.db.wrapper.SQLSelect;
28  import org.efaps.util.cache.CacheReloadException;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  
33  /**
34   * TODO comment!
35   *
36   * @author The eFaps Team
37   * @version $Id$
38   */
39  public class GenInstSelectPart
40      extends AbstractSelectPart
41  {
42      /**
43       * Logging instance used in this class.
44       */
45      private static final Logger LOG = LoggerFactory.getLogger(GenInstSelectPart.class);
46  
47      /**
48       * Type the General Instance belongs to.
49       */
50      private final Type type;
51  
52      /**
53       * Index of the GernalInstance table.
54       */
55      private Integer tableIdx;
56  
57      /**
58       * Add the type clause.
59       */
60      private boolean addTypeClause = false;
61  
62      /**
63       * @param _type Type the General Instance belongs to
64       */
65      public GenInstSelectPart(final Type _type)
66      {
67          this.type = _type;
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      public int join(final OneSelect _oneSelect,
75                      final SQLSelect _select,
76                      final int _relIndex)
77      {
78          this.tableIdx = _oneSelect.getTableIndex(GeneralInstance.TABLENAME,
79                          GeneralInstance.ISIDCOLUMN + "_" + GeneralInstance.ISTYPECOLUMN, _relIndex, null);
80          if (this.tableIdx == null) {
81              this.tableIdx = _oneSelect.getNewTableIndex(GeneralInstance.TABLENAME,
82                              GeneralInstance.ISIDCOLUMN + "_" + GeneralInstance.ISTYPECOLUMN, _relIndex, null);
83              if (getType().getMainTable().getSqlColType() != null) {
84                  _select.leftJoin(GeneralInstance.TABLENAME, this.tableIdx,
85                                  new String[] {GeneralInstance.ISIDCOLUMN, GeneralInstance.ISTYPECOLUMN},
86                                  _relIndex, new String[] {"ID", getType().getMainTable().getSqlColType()});
87              } else {
88                  _select.leftJoin(GeneralInstance.TABLENAME, this.tableIdx, GeneralInstance.ISIDCOLUMN, _relIndex, "ID");
89                  this.addTypeClause = true;
90              }
91          }
92          _select.column(this.tableIdx, "ID");
93          return this.tableIdx;
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public Type getType()
101     {
102         return this.type;
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public void add2Where(final OneSelect _oneselect,
110                           final SQLSelect _select)
111     {
112         try {
113             if (this.addTypeClause) {
114                 _select.addPart(SQLPart.AND).addColumnPart(this.tableIdx, GeneralInstance.ISTYPECOLUMN)
115                     .addPart(SQLPart.IN).addPart(SQLPart.PARENTHESIS_OPEN);
116                 _select.addValuePart(this.type.getId());
117                 for (final Type childType : this.type.getChildTypes()) {
118                     _select.addPart(SQLPart.COMMA);
119                     _select.addValuePart(childType.getId());
120                 }
121                 _select.addPart(SQLPart.PARENTHESIS_CLOSE);
122             }
123         } catch (final CacheReloadException e) {
124             GenInstSelectPart.LOG.error("Could not build where part for General Instance: {}", this);
125         }
126     }
127 }