Search in sources :

Example 1 with SAPRowIterator

use of org.pentaho.di.trans.steps.sapinput.sap.impl.SAPRowIterator in project pentaho-kettle by pentaho.

the class SapInput method processRow.

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    Object[] r = getRow();
    if (r == null) {
        // no more input to be expected...
        setOutputDone();
        return false;
    }
    if (first) {
        first = false;
        // Determine the output row metadata of this step
        // 
        data.outputRowMeta = new RowMeta();
        meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
        // Pre-calculate the indexes of the parameters for performance reasons...
        // 
        data.parameterIndexes = new ArrayList<Integer>();
        for (SapParameter parameter : meta.getParameters()) {
            int index = getInputRowMeta().indexOfValue(parameter.getFieldName());
            if (index < 0) {
                throw new KettleException("Unable to find field '" + parameter.getFieldName() + "'");
            }
            data.parameterIndexes.add(index);
        }
        // Pre-calculate the output fields
        // 
        data.output = new ArrayList<SAPField>();
        for (SapOutputField outputField : meta.getOutputFields()) {
            SAPField field = new SAPField(outputField.getSapFieldName(), outputField.getTableName(), "output_" + outputField.getSapType().getDescription());
            data.output.add(field);
        }
    }
    // Assemble the list of input fields for the SAP function execution...
    // 
    ArrayList<SAPField> input = new ArrayList<SAPField>();
    for (int i = 0; i < meta.getParameters().size(); i++) {
        SapParameter parameter = meta.getParameters().get(i);
        int fieldIndex = data.parameterIndexes.get(i);
        ValueMetaInterface valueMeta = getInputRowMeta().getValueMeta(fieldIndex);
        Object value = valueMeta.convertToNormalStorageType(r[fieldIndex]);
        // TODO: figure out if the executeFunction needs the data to be in a specific data type or format!!
        // If so, value needs to be converted to the appropriate data type here.
        SAPField field = new SAPField(parameter.getParameterName(), parameter.getTableName(), "input_" + parameter.getSapType().getDescription(), value);
        input.add(field);
    }
    // Get the output...
    // 
    SAPRowIterator resultSet;
    try {
        resultSet = data.sapConnection.executeFunctionCursored(meta.getFunction(), input, data.output);
    } catch (SAPException e) {
        throw new KettleException(e);
    }
    while (resultSet.hasNext()) {
        SAPRow sapRow = resultSet.next();
        Object[] outputRowData = RowDataUtil.allocateRowData(data.outputRowMeta.size());
        // Makes it easier to add all sorts of fields later on, like row number, input fields, etc.
        int outputIndex = 0;
        for (SAPField field : sapRow.getFields()) {
            // TODO: Here we should check as well whether or not the correct data types are delivered from SAP.
            // Make sure that we don't pass the appropriate data types : String, long, double, Date, BigDecimal, Boolean,
            // byte[] ONLY!!
            // 
            outputRowData[outputIndex++] = field.getValue();
        }
        // Pass the row along: row metadata and data need to correspond!!
        // 
        putRow(data.outputRowMeta, outputRowData);
        if (getTrans().isStopped()) {
            break;
        }
    }
    return true;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) SAPRow(org.pentaho.di.trans.steps.sapinput.sap.SAPRow) RowMeta(org.pentaho.di.core.row.RowMeta) ArrayList(java.util.ArrayList) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) SAPException(org.pentaho.di.trans.steps.sapinput.sap.SAPException) SAPRowIterator(org.pentaho.di.trans.steps.sapinput.sap.impl.SAPRowIterator) SAPField(org.pentaho.di.trans.steps.sapinput.sap.SAPField)

Aggregations

ArrayList (java.util.ArrayList)1 KettleException (org.pentaho.di.core.exception.KettleException)1 RowMeta (org.pentaho.di.core.row.RowMeta)1 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)1 SAPException (org.pentaho.di.trans.steps.sapinput.sap.SAPException)1 SAPField (org.pentaho.di.trans.steps.sapinput.sap.SAPField)1 SAPRow (org.pentaho.di.trans.steps.sapinput.sap.SAPRow)1 SAPRowIterator (org.pentaho.di.trans.steps.sapinput.sap.impl.SAPRowIterator)1