/*
 * Copyright 2003 - 2018 The eFaps Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.efaps.db.stmt.selection.elements;

import java.util.Collections;

import org.apache.commons.lang3.ArrayUtils;
import org.efaps.admin.datamodel.Attribute;
import org.efaps.admin.datamodel.SQLTable;
import org.efaps.db.wrapper.SQLSelect;
import org.efaps.db.wrapper.TableIndexer.TableIdx;
import org.efaps.util.EFapsException;

/**
 * The Class AttributeSelect.
 *
 * @author The eFaps Team
 */
public class AttributeElement
    extends AbstractAttributeElement<AttributeElement>
{

    /** The col idxs. */
    private int[] colIdxs;

    /** The attribute. */
    private Attribute attribute;

    /**
     * Gets the attribute.
     *
     * @return the attribute
     */
    public Attribute getAttribute()
    {
        return this.attribute;
    }

    /**
     * Sets the attribute.
     *
     * @param _attribute the attribute
     * @return the attribute element
     */
    public AttributeElement setAttribute(final Attribute _attribute)
    {
        this.attribute = _attribute;
        setDBTable(this.attribute.getTable());
        return this;
    }

    @Override
    public AttributeElement getThis()
    {
        return this;
    }

    @Override
    public void append2SQLSelect(final SQLSelect _sqlSelect)
        throws EFapsException
    {
        if (getTable() instanceof SQLTable) {
            final TableIdx tableIdx =getTableIdx(_sqlSelect);
            for (final String colName : this.attribute.getSqlColNames()) {
                this.colIdxs = ArrayUtils.add(this.colIdxs, _sqlSelect.columnIndex(tableIdx.getIdx(), colName));
            }

            // in case of dependencies for the attribute they must be selected also
            for (final Attribute attr : this.attribute.getDependencies().values()) {
                for (final String colName : attr.getSqlColNames()) {
                    final int colidx = _sqlSelect.column(tableIdx.getIdx(), colName).getColumnIdx();
                    ArrayUtils.add(this.colIdxs, colidx);
                }
            }
        }
    }

    @Override
    public Object getObject(final Object[] _row)
        throws EFapsException
    {
        final Object ret;
        if (this.colIdxs.length == 1) {
            ret = _row[this.colIdxs[0]];
        } else {
            ret = new Object[this.colIdxs.length];
            for (int i = 0; i < this.colIdxs.length; i++) {
                ((Object[]) ret)[i] = this.colIdxs[i];
            }
        }
        return callAuxillary(this.attribute.readDBValue(Collections.singletonList(ret)));
    }
}

@@ -23,9 +23,9 @@
  201
-8
+9
  The
@@ -1343,21 +1343,16 @@
  return 
-this.
 attribut
@@ -1570,21 +1570,16 @@
         
-this.
 attribut
@@ -1613,21 +1613,16 @@
 DBTable(
-this.
 attribut
@@ -1944,16 +1944,17 @@
 bleIdx =
+ 
 getTable
@@ -2010,21 +2010,16 @@
 lName : 
-this.
 attribut
@@ -2056,21 +2056,16 @@
         
-this.
 colIdxs 
@@ -2073,37 +2073,32 @@
  ArrayUtils.add(
-this.
 colIdxs, _sqlSel
@@ -2282,21 +2282,16 @@
  attr : 
-this.
 attribut
@@ -2511,16 +2511,26 @@
         
+ colIdxs =
  ArrayUt
@@ -2537,21 +2537,16 @@
 ils.add(
-this.
 colIdxs,
@@ -2741,21 +2741,16 @@
     if (
-this.
 colIdxs.
@@ -2787,21 +2787,16 @@
  = _row%5B
-this.
 colIdxs%5B
@@ -2846,21 +2846,16 @@
  Object%5B
-this.
 colIdxs.
@@ -2895,21 +2895,16 @@
  0; i %3C 
-this.
 colIdxs.
@@ -2956,21 +2956,21 @@
 t)%5Bi%5D = 
-this.
+_row%5B
 colIdxs%5B
@@ -2971,16 +2971,17 @@
 lIdxs%5Bi%5D
+%5D
 ;%0A      
@@ -3031,13 +3031,8 @@
 ary(
-this.
 attr

Update of AttributeSelect to select correct columns