Java Code Examples for org.pentaho.di.core.row.ValueMetaInterface#convertData()

The following examples show how to use org.pentaho.di.core.row.ValueMetaInterface#convertData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SetValueConstant.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private void updateField( Object[] r ) throws Exception {
  // Loop through fields
  for ( int i = 0; i < data.getFieldnr(); i++ ) {
    // DO CONVERSION OF THE DEFAULT VALUE ...
    // Entered by user
    ValueMetaInterface targetValueMeta = data.getOutputRowMeta().getValueMeta( data.getFieldnrs()[i] );
    ValueMetaInterface sourceValueMeta = data.getConvertRowMeta().getValueMeta( data.getFieldnrs()[i] );

    if ( !Utils.isEmpty( meta.getField( i ).getReplaceMask() ) ) {
      sourceValueMeta.setConversionMask( meta.getField( i ).getReplaceMask() );
    }

    sourceValueMeta.setStorageType( ValueMetaInterface.STORAGE_TYPE_NORMAL );
    r[data.getFieldnrs()[i]] = targetValueMeta.convertData( sourceValueMeta, data.getRealReplaceByValues()[i] );
    targetValueMeta.setStorageType( ValueMetaInterface.STORAGE_TYPE_NORMAL );
  }
}
 
Example 2
Source File: DataSetCsvGroup.java    From pentaho-pdi-dataset with Apache License 2.0 5 votes vote down vote up
public static final List<Object[]> getAllRows( LogChannelInterface log, DataSetGroup group, DataSet dataSet ) throws KettleException {
  RowMetaInterface setRowMeta = dataSet.getSetRowMeta( true );
  setValueFormats( setRowMeta );
  String dataSetFilename = getDataSetFilename( group, dataSet.getTableName() );
  List<Object[]> rows = new ArrayList<>();
  final ValueMetaString constantValueMeta = new ValueMetaString( "constant" );

  try {
    FileObject file = KettleVFS.getFileObject( dataSetFilename );
    if ( !file.exists() ) {
      // This is fine.  We haven't put rows in yet.
      //
      return rows;
    }

    try (
      Reader reader = new InputStreamReader( new BufferedInputStream( KettleVFS.getInputStream( file ) ) );
      CSVParser csvParser = new CSVParser( reader, getCsvFormat( setRowMeta ) );
    ) {
      for ( CSVRecord csvRecord : csvParser ) {
        if ( csvRecord.getRecordNumber() > 1 ) {
          Object[] row = RowDataUtil.allocateRowData( setRowMeta.size() );
          for ( int i = 0; i < setRowMeta.size(); i++ ) {
            ValueMetaInterface valueMeta = setRowMeta.getValueMeta( i ).clone();
            constantValueMeta.setConversionMetadata( valueMeta );
            String value = csvRecord.get( i );
            row[ i ] = valueMeta.convertData( constantValueMeta, value );
          }
          rows.add( row );
        }
      }
    }
    return rows;
  } catch ( Exception e ) {
    throw new KettleException( "Unable to get all rows for CSV data set '" + dataSet.getName() + "'", e );
  }
}
 
Example 3
Source File: DatabaseLookup.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private void initNullIf() throws KettleException {
  final String[] returnFields = meta.getReturnValueField();

  data.nullif = new Object[ returnFields.length ];

  for ( int i = 0; i < returnFields.length; i++ ) {
    if ( !Utils.isEmpty( meta.getReturnValueDefault()[ i ] ) ) {
      ValueMetaInterface stringMeta = new ValueMetaString( "string" );
      ValueMetaInterface returnMeta = data.outputRowMeta.getValueMeta( i + getInputRowMeta().size() );
      data.nullif[ i ] = returnMeta.convertData( stringMeta, meta.getReturnValueDefault()[ i ] );
    } else {
      data.nullif[ i ] = null;
    }
  }
}
 
Example 4
Source File: Formula.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private Object convertDataToTargetValueMeta( int i, Object formulaResult ) throws KettleException {
  if ( formulaResult == null ) {
    return formulaResult;
  }
  ValueMetaInterface target = data.outputRowMeta.getValueMeta( i );
  ValueMetaInterface actual = ValueMetaFactory.guessValueMetaInterface( formulaResult );
  Object value = target.convertData( actual, formulaResult );
  return value;
}
 
Example 5
Source File: SasInput.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
protected void convertData( RowMetaInterface source, Object[] sourceData, RowMetaInterface target ) throws KettleException {
  int targetIndex = getInputRowMeta().size();
  for ( int i = 0; i < data.fieldIndexes.size(); i++ ) {
    int fieldIndex = data.fieldIndexes.get( i );
    ValueMetaInterface sourceValueMeta = source.getValueMeta( fieldIndex );
    ValueMetaInterface targetValueMeta = target.getValueMeta( targetIndex );
    sourceData[targetIndex] = targetValueMeta.convertData( sourceValueMeta, sourceData[targetIndex] );

    targetIndex++;
  }
}
 
Example 6
Source File: IfNull.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public void replaceNull( Object[] row, ValueMetaInterface sourceValueMeta, int i, String realReplaceByValue,
  String realconversionMask, boolean setEmptystring ) throws Exception {
  if ( setEmptystring ) {
    row[i] = StringUtil.EMPTY_STRING;
  } else {
    // DO CONVERSION OF THE DEFAULT VALUE ...
    // Entered by user
    ValueMetaInterface targetValueMeta = data.outputRowMeta.getValueMeta( i );
    if ( !Utils.isEmpty( realconversionMask ) ) {
      sourceValueMeta.setConversionMask( realconversionMask );
    }
    row[i] = targetValueMeta.convertData( sourceValueMeta, realReplaceByValue );
  }
}
 
Example 7
Source File: AccessInput.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private Object convert( Object obj, AccessInputField field, int index ) throws Exception {
  // Get column
  Column c = data.t.getColumn( field.getColumn() );
  // Find out field type
  ValueMetaAndData sourceValueMetaAndData = AccessInputMeta.getValueMetaAndData( c, field.getName(), obj );

  // DO CONVERSIONS...
  //
  ValueMetaInterface targetValueMeta = data.outputRowMeta.getValueMeta( data.totalpreviousfields + index );
  return targetValueMeta.convertData( sourceValueMetaAndData.getValueMeta(), sourceValueMetaAndData
    .getValueData() );
}
 
Example 8
Source File: XMLInputSaxDataRetriever.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private void setValueToRow( String value, int fieldnr ) throws KettleValueException {

    XMLInputSaxField xmlInputField = fields.get( fieldnr );

    switch ( xmlInputField.getTrimType() ) {
      case XMLInputSaxField.TYPE_TRIM_LEFT:
        value = Const.ltrim( value );
        break;
      case XMLInputSaxField.TYPE_TRIM_RIGHT:
        value = Const.rtrim( value );
        break;
      case XMLInputSaxField.TYPE_TRIM_BOTH:
        value = Const.trim( value );
        break;
      default:
        break;
    }

    // DO CONVERSIONS...
    ValueMetaInterface targetValueMeta = data.outputRowMeta.getValueMeta( fieldnr );
    ValueMetaInterface sourceValueMeta = data.convertRowMeta.getValueMeta( fieldnr );
    row[fieldnr] = targetValueMeta.convertData( sourceValueMeta, value );

    // Do we need to repeat this field if it is null?
    if ( xmlInputField.isRepeated() ) {
      if ( row[fieldnr] == null && data.previousRow != null ) {
        Object previous = data.previousRow[fieldnr];
        row[fieldnr] = previous;
      }
    }
  }
 
Example 9
Source File: SalesforceInput.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
void doConversions( Object[] outputRowData, int i, String value ) throws KettleValueException {
  ValueMetaInterface targetValueMeta = data.outputRowMeta.getValueMeta( i );
  ValueMetaInterface sourceValueMeta = data.convertRowMeta.getValueMeta( i );

  if ( ValueMetaInterface.TYPE_BINARY != targetValueMeta.getType() ) {
    outputRowData[i] = targetValueMeta.convertData( sourceValueMeta, value );
  } else {
    // binary type of salesforce requires specific conversion
    if ( value != null ) {
      outputRowData[ i ] = Base64.decode( value );
    } else {
      outputRowData[ i ] = null;
    }
  }
}
 
Example 10
Source File: SelectValues.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
/**
 * Change the meta-data of certain fields.
 * <p/>
 * This, we can do VERY fast.
 * <p/>
 *
 * @param row The row to manipulate
 * @return the altered RowData array
 * @throws KettleValueException
 */
@VisibleForTesting
synchronized Object[] metadataValues( RowMetaInterface rowMeta, Object[] rowData ) throws KettleException {
  if ( data.firstmetadata ) {
    data.firstmetadata = false;

    data.metanrs = new int[ meta.getMeta().length ];
    for ( int i = 0; i < data.metanrs.length; i++ ) {
      data.metanrs[ i ] = rowMeta.indexOfValue( meta.getMeta()[ i ].getName() );
      if ( data.metanrs[ i ] < 0 ) {
        logError( BaseMessages
          .getString( PKG, "SelectValues.Log.CouldNotFindField", meta.getMeta()[ i ].getName() ) );
        setErrors( 1 );
        stopAll();
        return null;
      }
    }

    // Check for doubles in the selected fields...
    int[] cnt = new int[ meta.getMeta().length ];
    for ( int i = 0; i < meta.getMeta().length; i++ ) {
      cnt[ i ] = 0;
      for ( int j = 0; j < meta.getMeta().length; j++ ) {
        if ( meta.getMeta()[ i ].getName().equals( meta.getMeta()[ j ].getName() ) ) {
          cnt[ i ]++;
        }

        if ( cnt[ i ] > 1 ) {
          logError( BaseMessages.getString( PKG, "SelectValues.Log.FieldCouldNotSpecifiedMoreThanTwice2", meta
            .getMeta()[ i ].getName() ) );
          setErrors( 1 );
          stopAll();
          return null;
        }
      }
    }

    // Also apply the metadata on the row meta to allow us to convert the data correctly, with the correct mask.
    //
    for ( int i = 0; i < data.metanrs.length; i++ ) {
      SelectMetadataChange change = meta.getMeta()[ i ];
      ValueMetaInterface valueMeta = rowMeta.getValueMeta( data.metanrs[ i ] );
      if ( !Utils.isEmpty( change.getConversionMask() ) ) {
        valueMeta.setConversionMask( change.getConversionMask() );
      }

      valueMeta.setDateFormatLenient( change.isDateFormatLenient() );
      valueMeta.setDateFormatLocale( EnvUtil.createLocale( change.getDateFormatLocale() ) );
      valueMeta.setDateFormatTimeZone( EnvUtil.createTimeZone( change.getDateFormatTimeZone() ) );
      valueMeta.setLenientStringToNumber( change.isLenientStringToNumber() );

      if ( !Utils.isEmpty( change.getEncoding() ) ) {
        valueMeta.setStringEncoding( change.getEncoding() );
      }
      if ( !Utils.isEmpty( change.getDecimalSymbol() ) ) {
        valueMeta.setDecimalSymbol( change.getDecimalSymbol() );
      }
      if ( !Utils.isEmpty( change.getGroupingSymbol() ) ) {
        valueMeta.setGroupingSymbol( change.getGroupingSymbol() );
      }
      if ( !Utils.isEmpty( change.getCurrencySymbol() ) ) {
        valueMeta.setCurrencySymbol( change.getCurrencySymbol() );
      }
    }
  }

  //
  // Change the data too
  //
  for ( int i = 0; i < data.metanrs.length; i++ ) {
    int index = data.metanrs[ i ];
    ValueMetaInterface fromMeta = rowMeta.getValueMeta( index );
    ValueMetaInterface toMeta = data.metadataRowMeta.getValueMeta( index );

    // If we need to change from BINARY_STRING storage type to NORMAL...
    //
    try {
      if ( fromMeta.isStorageBinaryString()
        && meta.getMeta()[ i ].getStorageType() == ValueMetaInterface.STORAGE_TYPE_NORMAL ) {
        rowData[ index ] = fromMeta.convertBinaryStringToNativeType( (byte[]) rowData[ index ] );
      }
      if ( meta.getMeta()[ i ].getType() != ValueMetaInterface.TYPE_NONE && fromMeta.getType() != toMeta.getType() ) {
        rowData[ index ] = toMeta.convertData( fromMeta, rowData[ index ] );
      }
    } catch ( KettleValueException e ) {
      throw new KettleConversionException( e.getMessage(), Collections.<Exception>singletonList( e ),
        Collections.singletonList( toMeta ), rowData );
    }
  }

  return rowData;
}
 
Example 11
Source File: LDAPInput.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
private Object getAttributeValue( LDAPInputField field, Attribute attr, int i, Object outputRowData ) throws Exception {

    if ( field.getType() == ValueMetaInterface.TYPE_BINARY ) {
      // It's a binary field
      // no need to convert, just return the value as it
      try {
        return attr.get();
      } catch ( java.lang.ClassCastException e ) {
        return attr.get().toString().getBytes();
      }
    }

    String retval = null;
    if ( field.getReturnType() == LDAPInputField.FETCH_ATTRIBUTE_AS_BINARY
      && field.getType() == ValueMetaInterface.TYPE_STRING ) {
      // Convert byte[] to string
      return LDAPConnection.extractBytesAndConvertToString( attr, field.isObjectSid() );
    }

    // extract as string
    retval = extractString( attr );

    // DO Trimming!
    switch ( field.getTrimType() ) {
      case LDAPInputField.TYPE_TRIM_LEFT:
        retval = Const.ltrim( retval );
        break;
      case LDAPInputField.TYPE_TRIM_RIGHT:
        retval = Const.rtrim( retval );
        break;
      case LDAPInputField.TYPE_TRIM_BOTH:
        retval = Const.trim( retval );
        break;
      default:
        break;
    }

    // DO CONVERSIONS...
    //
    ValueMetaInterface targetValueMeta = data.outputRowMeta.getValueMeta( i );
    ValueMetaInterface sourceValueMeta = data.convertRowMeta.getValueMeta( i );
    return targetValueMeta.convertData( sourceValueMeta, retval );

  }
 
Example 12
Source File: GetVariable.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws KettleException {
  Object[] rowData;

  if ( data.readsRows ) {
    rowData = getRow();
    if ( rowData == null ) {
      setOutputDone();
      return false;
    }
  } else {
    rowData = RowDataUtil.allocateRowData( 0 );
    incrementLinesRead();
  }

  // initialize
  if ( first && rowData != null ) {
    first = false;

    // Make output meta data
    //
    if ( data.readsRows ) {
      data.inputRowMeta = getInputRowMeta();
    } else {
      data.inputRowMeta = new RowMeta();
    }
    data.outputRowMeta = data.inputRowMeta.clone();
    meta.getFields( data.outputRowMeta, getStepname(), null, null, this, repository, metaStore );

    // Create a copy of the output row metadata to do the data conversion...
    //
    data.conversionMeta = data.outputRowMeta.cloneToType( ValueMetaInterface.TYPE_STRING );

    // Add the variables to the row...
    //
    // Keep the Object[] for speed. Although this step will always be used in "small" amounts, there's always going to
    // be those cases where performance is required.
    //
    int fieldsLength = meta.getFieldDefinitions().length;
    data.extraData = new Object[fieldsLength];
    for ( int i = 0; i < fieldsLength; i++ ) {
      String newValue = environmentSubstitute( meta.getFieldDefinitions()[i].getVariableString() );
      if ( log.isDetailed() ) {
        logDetailed( "field [" + meta.getFieldDefinitions()[i].getFieldName() + "] has value [" + newValue + "]" );
      }

      // Convert the data to the desired data type...
      //
      ValueMetaInterface targetMeta = data.outputRowMeta.getValueMeta( data.inputRowMeta.size() + i );
      ValueMetaInterface sourceMeta = data.conversionMeta.getValueMeta( data.inputRowMeta.size() + i ); // String type
                                                                                                        // +
                                                                                                        // conversion
                                                                                                        // masks,
                                                                                                        // symbols,
                                                                                                        // trim type,
                                                                                                        // etc
      data.extraData[i] = targetMeta.convertData( sourceMeta, newValue );
    }
  }

  rowData = RowDataUtil.addRowData( rowData, data.inputRowMeta.size(), data.extraData );

  putRow( data.outputRowMeta, rowData );

  if ( !data.readsRows ) { // Just one row and then stop!

    setOutputDone();
    return false;
  }

  return true;
}