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.Type;
26  import org.efaps.ci.CIAdminDataModel;
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 TypeValueSelect
38      extends AbstractValueSelect
39  {
40      /**
41       * Type this TypeValueSelect belongs to.
42       */
43      private Type type;
44  
45      /**
46       * @param _oneSelect OneSelect
47       */
48      public TypeValueSelect(final OneSelect _oneSelect)
49      {
50          super(_oneSelect);
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      @Override
57      public int append2SQLSelect(final Type _type,
58                                  final SQLSelect _select,
59                                  final int _tableIndex,
60                                  final int _colIndex)
61      {
62          int ret = 0;
63          if (_type.getMainTable().getSqlColType() != null) {
64              _select.column(_tableIndex, _type.getMainTable().getSqlColType());
65              getColIndexs().add(_colIndex + ret);
66              ret++;
67          }
68          this.type = _type;
69          return ret;
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public Object getValue(final Object _currentObject)
77          throws EFapsException
78      {
79          Type tempType;
80          if (this.type.getMainTable().getSqlColType() == null) {
81              tempType = this.type;
82          } else if (_currentObject != null) {
83              // check is necessary because Oracle JDBC returns for getObject always a BigDecimal
84              if (_currentObject instanceof BigDecimal) {
85                  tempType = Type.get(((BigDecimal) _currentObject).longValue());
86              } else {
87                  tempType = Type.get((Long) _currentObject);
88              }
89          } else {
90              tempType = null;
91          }
92          return analyzeChildValue(this, tempType);
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public String getValueType()
100     {
101         return "type";
102     }
103 
104     /**
105      * @param _currentSelect select to be analyzed
106      * @param _tempType current type
107      * @return the object
108      */
109     protected Object analyzeChildValue(final AbstractValueSelect _currentSelect,
110                                        final Type _tempType)
111     {
112         Object ret;
113         if (_tempType != null && _currentSelect.getChildValueSelect() != null) {
114             switch (_currentSelect.getChildValueSelect().getValueType()) {
115                 case "label":
116                     ret = _tempType.getLabel();
117                     break;
118                 case "oid":
119                     ret = new StringBuilder().append(CIAdminDataModel.Type.getType().getId()).append(".").append(
120                                     _tempType.getId()).toString();
121                     break;
122                 case "UUID":
123                     ret = _tempType.getUUID();
124                     break;
125                 case "name":
126                     ret = _tempType.getName();
127                     break;
128                 default:
129                     ret = _tempType;
130                     break;
131             }
132         } else {
133             ret = _tempType;
134         }
135         return ret;
136     }
137 }