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.admin.datamodel.attributetype;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.efaps.admin.datamodel.Attribute;
27  import org.efaps.admin.datamodel.Dimension;
28  import org.efaps.admin.datamodel.Dimension.UoM;
29  
30  
31  /**
32   * Abstract class for an attribute type with an UoM.
33   *
34   * @author The eFaps Team
35   * @version $Id$
36   */
37  public abstract class AbstractWithUoMType
38      extends AbstractType
39  {
40      /**
41       * Needed for serialization.
42       */
43      private static final long serialVersionUID = 1L;
44  
45      /**
46       * {@inheritDoc}
47       */
48      @Override
49      public Object readValue(final Attribute _attribute,
50                              final List<Object> _objectList)
51      {
52          final List<Object[]> ret = new ArrayList<Object[]>();
53          for (final Object object : _objectList) {
54              final Object[] temp = (Object[]) object;
55              final Object value = readValue(temp[0]);
56              final UoM uom = temp[1] == null ? _attribute.getDimension().getBaseUoM() : Dimension.getUoM((Long) temp[1]);
57              if (temp.length > 2) {
58                  final Object obj = temp[2];
59                  double dbl = 0;
60                  if (obj instanceof Number) {
61                      dbl = ((Number) obj).doubleValue();
62                  } else if (obj != null) {
63                      dbl = Double.parseDouble(obj.toString());
64                  }
65                  ret.add(new Object[]{value, uom, dbl});
66              } else {
67                  ret.add(new Object[]{value, uom});
68              }
69  
70          }
71          return _objectList.size() > 0 ? ret.size() > 1 ? ret : ret.get(0) : null;
72      }
73  
74      /**
75       * Method to read the value from the cached result.
76       * @see #readValue(CachedResult, List)
77       *
78       * @param _object   Object to read
79       * @return value as object
80       */
81      protected abstract Object readValue(final Object _object);
82  }