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.Type;
28  import org.efaps.db.print.OneSelect;
29  import org.efaps.db.wrapper.SQLSelect;
30  import org.efaps.util.EFapsException;
31  
32  /**
33   * Value Select to Select the OID of an instance.
34   *
35   * @author The eFaps Team
36   * @version $Id$
37   */
38  public class OIDValueSelect
39      extends AbstractValueSelect
40  {
41      /**
42       * Attribute belonging to this IDValueSelect.
43       */
44      private Attribute attribute;
45  
46      /**
47       * Type belonging to this OIDValueSelect.
48       */
49      private Type type;
50  
51      /**
52       * @param _oneSelect    OneSelect
53       */
54      public OIDValueSelect(final OneSelect _oneSelect)
55      {
56          super(_oneSelect);
57      }
58  
59      /**
60       * {@inheritDoc}
61       */
62      @Override
63      public int append2SQLSelect(final Type _type,
64                                  final SQLSelect _select,
65                                  final int _tableIndex,
66                                  final int _colIndex)
67      {
68          int ret = 0;
69          if (getParent() == null || !"type".equals(getParent().getValueType())) {
70              this.type = _type;
71              _select.column(_tableIndex, "ID");
72              getColIndexs().add(_colIndex);
73              ret++;
74          }
75          // in case that the type has a column for type it must be added
76          if (this.type != null) {
77              if (this.type.getMainTable().getSqlColType() != null) {
78                  _select.column(_tableIndex, this.type.getMainTable().getSqlColType());
79                  getColIndexs().add(_colIndex + ret);
80                  ret++;
81              }
82          }
83          this.attribute = _type.getAttribute("OID");
84          return ret;
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      public Object getValue(final Object _object)
92          throws EFapsException
93      {
94          final StringBuilder bldr = new StringBuilder();
95          boolean retNull = false;
96          if (_object instanceof Object[]) {
97              final Object[] object = (Object[]) _object;
98              if (object[0] == null || object[1] == null) {
99                  retNull = true;
100             } else {
101                 bldr.append(object[1]).append(".").append(object[0]);
102             }
103         } else {
104             if (_object == null) {
105                 retNull = true;
106             } else {
107                 bldr.append(this.type.getId()).append(".").append(_object);
108             }
109         }
110         return retNull ? null : bldr.toString();
111     }
112 
113     /**
114      * {@inheritDoc}
115      */
116     @Override
117     public Object getValue(final List<Object> _objectList)
118         throws EFapsException
119     {
120         final List<Object> ret = new ArrayList<Object>();
121         for (final Object object : _objectList) {
122             ret.add(getValue(object));
123         }
124         return _objectList.size() > 0 ? (ret.size() > 1 ? ret : ret.get(0)) : null;
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public String getValueType()
132     {
133         return "oid";
134     }
135 
136     /**
137      * {@inheritDoc}
138      */
139     @Override
140     public Attribute getAttribute()
141     {
142         return this.attribute;
143     }
144 
145     /**
146      * Getter method for the instance variable {@link #type}.
147      *
148      * @return value of instance variable {@link #type}
149      */
150     public Type getType()
151     {
152         return this.type;
153     }
154 
155     /**
156      * Setter method for instance variable {@link #type}.
157      *
158      * @param _type value for instance variable {@link #type}
159      */
160 
161     public void setType(final Type _type)
162     {
163         this.type = _type;
164     }
165 }