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 org.efaps.admin.datamodel.Attribute;
24  import org.efaps.admin.datamodel.Dimension.UoM;
25  import org.efaps.admin.datamodel.Type;
26  import org.efaps.admin.datamodel.attributetype.AbstractWithUoMType;
27  import org.efaps.admin.datamodel.attributetype.RateType;
28  import org.efaps.admin.datamodel.ui.FieldValue;
29  import org.efaps.admin.ui.AbstractUserInterfaceObject.TargetMode;
30  import org.efaps.db.print.FileSelectPart;
31  import org.efaps.db.print.OneSelect;
32  import org.efaps.db.store.AbstractStoreResource;
33  import org.efaps.db.wrapper.SQLSelect;
34  import org.efaps.util.EFapsException;
35  
36  /**
37   * TODO comment!
38   *
39   * @author The eFaps Team
40   * @version $Id$
41   */
42  public class LabelValueSelect
43      extends AbstractValueSelect
44      implements IAttributeChildValueSelect
45  {
46  
47      /**
48       * @param _oneSelect OneSelect
49       */
50      public LabelValueSelect(final OneSelect _oneSelect)
51      {
52          super(_oneSelect);
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public String getValueType()
60      {
61          return "label";
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public Object getValue(final Object _object)
69      {
70          Object ret = _object;
71          if (_object != null && _object instanceof String) {
72              ret = ((String) _object).trim();
73          }
74          return ret;
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public int append2SQLSelect(final Type _type,
82                                  final SQLSelect _select,
83                                  final int _tableIndex,
84                                  final int _colIndex)
85      {
86          int ret = 0;
87          if (getParent() == null && getParentSelectPart() != null
88                          && getParentSelectPart() instanceof FileSelectPart) {
89              _select.column(_tableIndex, AbstractStoreResource.COLNAME_FILENAME);
90              getColIndexs().add(_colIndex);
91              ret = 1;
92          }
93          return ret;
94      }
95  
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public Object get(final Attribute _attribute,
102                       final Object _object)
103         throws EFapsException
104     {
105         final Object ret;
106         if (_attribute.getAttributeType().getDbAttrType() instanceof RateType) {
107             ret = getRate(_attribute, _object);
108         } else if (_attribute.getAttributeType().getDbAttrType() instanceof AbstractWithUoMType) {
109             ret = getValueUOM(_object);
110         }  else {
111             ret = _object;
112         }
113         return ret;
114     }
115 
116     /**
117      * @param _object object the value is wanted for
118      * @return Object
119      */
120     protected Object getValueUOM(final Object _object)
121     {
122         Object ret = null;
123         if (_object instanceof Object[]) {
124             final Object[] values = (Object[]) _object;
125             ret = ((UoM) values[1]).getName();
126         }
127         return ret;
128     }
129 
130     /**
131      * @param _attribute Attribute this value is wanted for
132      * @param _object object the rate is wanted for
133      * @return Object
134      * @throws EFapsException on error
135      */
136     protected Object getRate(final Attribute _attribute,
137                              final Object _object)
138         throws EFapsException
139     {
140         Object ret = null;
141         if (_object instanceof Object[]) {
142             final FieldValue fieldValue = new FieldValue(null, _attribute, _object, null, null);
143             fieldValue.setTargetMode(TargetMode.VIEW);
144             ret = fieldValue.getObject4Compare();
145         }
146         return ret;
147     }
148 }