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.util.ArrayList;
24  import java.util.List;
25  
26  import org.efaps.admin.datamodel.Attribute;
27  import org.efaps.admin.datamodel.ui.UIInterface;
28  import org.efaps.db.print.OneSelect;
29  import org.efaps.util.EFapsException;
30  
31  /**
32   * TODO comment!
33   *
34   * @author The eFaps Team
35   * @version $Id$
36   */
37  public class FormatValueSelect
38      extends AbstractValueSelect
39      implements IAttributeChildValueSelect
40  {
41  
42      /**
43       * Pattern the formatter will use.
44       */
45      private final String pattern;
46  
47      /**
48       * @param _oneSelect OneSelect
49       * @param _pattern pattern the formatter will use
50       */
51      public FormatValueSelect(final OneSelect _oneSelect,
52                               final String _pattern)
53      {
54          super(_oneSelect);
55          this.pattern = _pattern;
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      @Override
62      public String getValueType()
63      {
64          return "format";
65      }
66  
67      /**
68       * Method to format a given object.
69       *
70       * @param _attribute Attribute the object belongs to
71       * @param _object Object to be formated
72       * @return formated object
73       * @throws EFapsException on error
74       */
75      public Object get(final Attribute _attribute,
76                           final Object _object)
77          throws EFapsException
78      {
79          Object ret = null;
80          if (_object != null) {
81              final UIInterface ui = _attribute.getAttributeType().getUI();
82              if (_object instanceof List<?>) {
83                  final List<?> objectList = (List<?>) _object;
84                  final List<Object> temp = new ArrayList<Object>();
85                  for (final Object object : objectList) {
86                      temp.add(ui.format(object, this.pattern));
87                  }
88                  ret = temp;
89              } else {
90                  ret = ui.format(_object, this.pattern);
91              }
92          }
93          return ret;
94      }
95  }