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.value;
22  
23  import java.math.BigDecimal;
24  
25  import org.efaps.admin.datamodel.Status;
26  import org.efaps.admin.datamodel.Type;
27  import org.efaps.db.print.OneSelect;
28  import org.efaps.db.wrapper.SQLSelect;
29  import org.efaps.util.EFapsException;
30  
31  /**
32   * TODO comment!
33   *
34   * @author The eFaps Team
35   * @version $Id$
36   */
37  public class StatusValueSelect
38      extends AbstractValueSelect
39  {
40      /**
41       * @param _oneSelect OneSelect
42       */
43      public StatusValueSelect(final OneSelect _oneSelect)
44      {
45          super(_oneSelect);
46      }
47  
48      /**
49       * {@inheritDoc}
50       */
51      @Override
52      public int append2SQLSelect(final Type _type,
53                                  final SQLSelect _select,
54                                  final int _tableIndex,
55                                  final int _colIndex)
56      {
57          int ret = 0;
58          Type tmpType = _type;
59          // not all types inside a hierarchy have the typattribute assigned so it must be searched for
60          while (!tmpType.isCheckStatus() && tmpType.getParentType() != null) {
61              tmpType = tmpType.getParentType();
62          }
63          if (tmpType.isCheckStatus()) {
64              for (final String colName : tmpType.getStatusAttribute().getSqlColNames()) {
65                  _select.column(_tableIndex, colName);
66                  getColIndexs().add(_colIndex + ret);
67                  ret++;
68              }
69          }
70          return ret;
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public Object getValue(final Object _currentObject)
78          throws EFapsException
79      {
80          Object ret = null;
81          Status tempStatus;
82          if (_currentObject != null) {
83              // check is necessary because Oracle JDBC returns for getObject always a BigDecimal
84              if (_currentObject instanceof BigDecimal) {
85                  tempStatus = Status.get(((BigDecimal) _currentObject).longValue());
86              } else {
87                  tempStatus = Status.get((Long) _currentObject);
88              }
89          } else {
90              tempStatus = null;
91          }
92          if (tempStatus != null && getChildValueSelect() != null) {
93              switch (getChildValueSelect().getValueType()) {
94                  case "label":
95                      ret = tempStatus.getLabel();
96                      break;
97                  case "oid":
98                      ret = new StringBuilder().append(Type.get(tempStatus.getStatusGroup().getUUID()).getId())
99                          .append(".").append(tempStatus.getId()).toString();
100                     break;
101                 case "key":
102                     ret = tempStatus.getKey();
103                     break;
104                 case "type":
105                     ret = ((TypeValueSelect) getChildValueSelect()).analyzeChildValue(getChildValueSelect(),
106                                     Type.get(tempStatus.getStatusGroup().getUUID()));
107                     break;
108                 default:
109                     ret = tempStatus;
110                     break;
111             }
112         } else {
113             ret = tempStatus;
114         }
115         return ret;
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public String getValueType()
123     {
124         return "status";
125     }
126 }