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.Attribute;
26  import org.efaps.admin.datamodel.attributetype.AbstractWithUoMType;
27  import org.efaps.admin.datamodel.attributetype.DecimalType;
28  import org.efaps.admin.datamodel.attributetype.RateType;
29  import org.efaps.db.print.OneSelect;
30  import org.efaps.util.EFapsException;
31  
32  /**
33   * TODO comment!
34   *
35   * @author The eFaps Team
36   * @version $Id$
37   */
38  public class ValueValueSelect
39      extends AbstractValueSelect
40      implements IAttributeChildValueSelect
41  {
42  
43      /**
44       * @param _oneSelect OneSelect
45       */
46      public ValueValueSelect(final OneSelect _oneSelect)
47      {
48          super(_oneSelect);
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      @Override
55      public String getValueType()
56      {
57          return "value";
58      }
59  
60      /**
61       * @param _attribute Attribute this value is wanted for
62       * @param _object object containing the value for the attribute
63       * @return value
64       * @throws EFapsException on error
65       */
66      public Object get(final Attribute _attribute,
67                        final Object _object)
68          throws EFapsException
69      {
70          final Object ret;
71          if (_attribute.getAttributeType().getDbAttrType() instanceof RateType) {
72              ret = getRate(_attribute, _object);
73          } else if (_attribute.getAttributeType().getDbAttrType() instanceof AbstractWithUoMType) {
74              ret = getValueUOM(_object);
75          } else {
76              ret = _object;
77          }
78          return ret;
79      }
80  
81      /**
82       * @param _attribute    Attribute the rate is wanted for
83       * @param _object       object the rate is wanted for
84       * @return Object
85       * @throws EFapsException on error
86       */
87      protected Object getRate(final Attribute _attribute,
88                               final Object _object)
89          throws EFapsException
90      {
91          Object ret = null;
92          if (_object instanceof Object[]) {
93              final Object[] values = (Object[]) _object;
94  
95              BigDecimal numerator;
96              if (values[0] instanceof BigDecimal) {
97                  numerator = (BigDecimal) values[0];
98              } else {
99                  numerator = DecimalType.parseLocalized(values[0].toString());
100             }
101             BigDecimal denominator;
102             if (values[1] instanceof BigDecimal) {
103                 denominator = (BigDecimal) values[1];
104             } else {
105                 denominator = DecimalType.parseLocalized(values[1].toString());
106             }
107             // the oracle jdbc returns BigDecimal from the database that does not have the scale as
108             // it is set explicitly, therefore it is forced here (before calculation)
109             if (_attribute != null) {
110                 if (numerator.scale() < _attribute.getScale()) {
111                     numerator = numerator.setScale(_attribute.getScale());
112                 }
113                 if (denominator.scale() < _attribute.getScale()) {
114                     denominator = denominator.setScale(_attribute.getScale());
115                 }
116             }
117             ret = numerator.divide(denominator,
118                                 numerator.scale() > denominator.scale() ? numerator.scale() : denominator.scale(),
119                                 BigDecimal.ROUND_UP);
120         }
121         return ret;
122     }
123 
124     /**
125      * @param _object object the value is wanted for
126      * @return Object
127      */
128     protected Object getValueUOM(final Object _object)
129     {
130         Object ret = null;
131         if (_object instanceof Object[]) {
132             final Object[] values = (Object[]) _object;
133             ret = values[0];
134         }
135         return ret;
136     }
137 }