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

The following examples show how to use org.pentaho.di.core.row.ValueMetaInterface#isNumeric() . 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: RowOutputConverter.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private Object getValue( ValueMetaInterface targetMeta, ValueMetaInterface strConvertMeta, Object value )
    throws KettleValueException {
  if ( targetMeta.isNumeric() ) {
    try {
      // attempt direct conversion
      return targetMeta.getNativeDataType( value );
    } catch ( KettleValueException e ) {
      if ( log.isDebug() ) {
        log.logDebug( e.getLocalizedMessage(), e );
      }
    }
  }
  // convert from string
  String strValue = getStringValue( value );
  return targetMeta.convertDataFromString( strValue, strConvertMeta, null, null, targetMeta.getTrimType() );
}
 
Example 2
Source File: Validator.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
KettleValidatorException assertNumeric( ValueMetaInterface valueMeta,
                                        Object valueData,
                                        Validation field ) throws KettleValueException {
  if ( valueMeta.isNumeric() || containsOnlyDigits( valueMeta.getString( valueData ) ) ) {
    return null;
  }
  return new KettleValidatorException( this, field, KettleValidatorException.ERROR_NON_NUMERIC_DATA,
    BaseMessages.getString( PKG, "Validator.Exception.NonNumericDataNotAllowed", field.getFieldName(),
      valueMeta.toStringMeta(), valueMeta.getString( valueData ) ), field.getFieldName() );
}
 
Example 3
Source File: SelectRowDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Copy information from the input buffer to the dialog fields.
 */
private void getData() {
  for ( int i = 0; i < buffer.size(); i++ ) {
    RowMetaAndData rowMetaAndData = buffer.get( i );
    RowMetaInterface rowMeta = rowMetaAndData.getRowMeta();
    Object[] rowData = rowMetaAndData.getData();

    for ( int c = 0; c < rowMeta.size(); c++ ) {
      ValueMetaInterface v = rowMeta.getValueMeta( c );
      String show;

      try {
        if ( v.isNumeric() ) {
          show = v.getString( rowData[c] );
        } else {
          show = v.getString( rowData[c] );
        }
      } catch ( KettleValueException e ) {
        show = "<conversion error>";
      }
      if ( show != null ) {
        wFields.table.getItem( i ).setText( c + 1, show );
      }
    }
  }
  wFields.optWidth( true );
}
 
Example 4
Source File: IngresVectorwiseLoader.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws KettleException {
  meta = (IngresVectorwiseLoaderMeta) smi;
  data = (IngresVectorwiseLoaderData) sdi;

  try {
    Object[] r = getRow(); // Get row from input rowset & set row busy!
    // no more input to be expected...
    if ( r == null ) {
      // only close output after the first row was processed
      // to prevent error (NPE) on empty rows set
      if ( !first ) {
        closeOutput();
      }

      if ( logWriter != null ) {
        logWriteThread.join();
        if ( logWriter.isErrorsOccured() ) {
          throw new SQLException( "The error was gotten from ingres sql process" );
        }
      }

      if ( vwLoadMonitorThread != null ) {
        vwLoadMonitorThread.join();
      }

      setOutputDone();
      return false;
    }

    if ( first ) {
      first = false;

      // Cache field indexes.
      //
      data.keynrs = new int[meta.getFieldStream().length];
      for ( int i = 0; i < data.keynrs.length; i++ ) {
        data.keynrs[i] = getInputRowMeta().indexOfValue( meta.getFieldStream()[i] );
      }
      data.bulkRowMeta = getInputRowMeta().clone();
      if ( meta.isUseStandardConversion() ) {
        for ( int i = 0; i < data.bulkRowMeta.size(); i++ ) {
          ValueMetaInterface valueMeta = data.bulkRowMeta.getValueMeta( i );
          if ( valueMeta.isStorageNormal() ) {
            if ( valueMeta.isDate() ) {
              valueMeta.setConversionMask( "yyyy-MM-dd HH:mm:ss" );
            } else if ( valueMeta.isNumeric() ) {
              valueMeta.setDecimalSymbol( "." );
              valueMeta.setGroupingSymbol( "" );
            }
          }
        }
      }

      // execute the client statement...
      //
      execute( meta );

      // Allocate a buffer
      //
      data.fileChannel = data.fifoOpener.getFileChannel();
      data.byteBuffer = ByteBuffer.allocate( data.bufferSize );
    }

    // check if SQL process is still running before processing row
    if ( !checkSqlProcessRunning( data.sqlProcess ) ) {
      throw new Exception( "Ingres SQL process has stopped" );
    }

    writeRowToBulk( data.bulkRowMeta, r );
    putRow( getInputRowMeta(), r );
    incrementLinesOutput();

    if ( checkFeedback( getLinesOutput() ) ) {
      logBasic( BaseMessages.getString( PKG, "IngresVectorwiseLoader.Log.LineNumber" ) + getLinesOutput() );
    }

    return true;

  } catch ( Exception e ) {
    logError( BaseMessages.getString( PKG, "IngresVectorwiseLoader.Log.ErrorInStep" ), e );
    setErrors( 1 );
    stopAll();
    setOutputDone(); // signal end to receiver(s)
    return false;
  }
}
 
Example 5
Source File: MySQLBulkLoader.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override
public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws KettleException {
  meta = (MySQLBulkLoaderMeta) smi;
  data = (MySQLBulkLoaderData) sdi;

  try {
    Object[] r = getRow(); // Get row from input rowset & set row busy!
    if ( r == null ) { // no more input to be expected...

      setOutputDone();

      closeOutput();

      return false;
    }

    if ( first ) {
      first = false;

      // Cache field indexes.
      //
      data.keynrs = new int[meta.getFieldStream().length];
      for ( int i = 0; i < data.keynrs.length; i++ ) {
        data.keynrs[i] = getInputRowMeta().indexOfValue( meta.getFieldStream()[i] );
      }

      data.bulkFormatMeta = new ValueMetaInterface[data.keynrs.length];
      for ( int i = 0; i < data.keynrs.length; i++ ) {
        ValueMetaInterface sourceMeta = getInputRowMeta().getValueMeta( data.keynrs[i] );
        if ( sourceMeta.isDate() ) {
          if ( meta.getFieldFormatType()[i] == MySQLBulkLoaderMeta.FIELD_FORMAT_TYPE_DATE ) {
            data.bulkFormatMeta[i] = data.bulkDateMeta.clone();
          } else if ( meta.getFieldFormatType()[i] == MySQLBulkLoaderMeta.FIELD_FORMAT_TYPE_TIMESTAMP ) {
            data.bulkFormatMeta[i] = data.bulkTimestampMeta.clone(); // default to timestamp
          }
        } else if ( sourceMeta.isNumeric()
            && meta.getFieldFormatType()[i] == MySQLBulkLoaderMeta.FIELD_FORMAT_TYPE_NUMBER ) {
          data.bulkFormatMeta[i] = data.bulkNumberMeta.clone();
        }

        if ( data.bulkFormatMeta[i] == null && !sourceMeta.isStorageBinaryString() ) {
          data.bulkFormatMeta[i] = sourceMeta.clone();
        }
      }

      // execute the client statement...
      //
      execute( meta );
    }

    // Every nr of rows we re-start the bulk load process to allow indexes etc to fit into the MySQL server memory
    // Performance could degrade if we don't do this.
    //
    if ( data.bulkSize > 0 && getLinesOutput() > 0 && ( getLinesOutput() % data.bulkSize ) == 0 ) {
      closeOutput();
      executeLoadCommand();
    }

    writeRowToBulk( getInputRowMeta(), r );
    putRow( getInputRowMeta(), r );
    incrementLinesOutput();

    return true;
  } catch ( Exception e ) {
    logError( BaseMessages.getString( PKG, "MySQLBulkLoader.Log.ErrorInStep" ), e );
    setErrors( 1 );
    stopAll();
    setOutputDone(); // signal end to receiver(s)
    return false;
  }
}
 
Example 6
Source File: TextFileOutputDialog.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
private void get() {
  try {
    RowMetaInterface r = transMeta.getPrevStepFields( stepname );
    if ( r != null ) {
      TableItemInsertListener listener = new TableItemInsertListener() {
        public boolean tableItemInserted( TableItem tableItem, ValueMetaInterface v ) {

          if ( v.isNumeric() ) {
            // currency symbol
            tableItem.setText( 6, Const.NVL( v.getCurrencySymbol(), "" ) );

            // decimal and grouping
            tableItem.setText( 7, Const.NVL( v.getDecimalSymbol(), "" ) );
            tableItem.setText( 8, Const.NVL( v.getGroupingSymbol(), "" ) );
          }

          // trim type
          tableItem.setText( 9, Const.NVL( ValueMetaString.getTrimTypeDesc( v.getTrimType() ), "" ) );

          // conversion mask
          if ( !Utils.isEmpty( v.getConversionMask() ) ) {
            tableItem.setText( 3, v.getConversionMask() );
          } else {
            if ( v.isNumber() ) {
              if ( v.getLength() > 0 ) {
                int le = v.getLength();
                int pr = v.getPrecision();

                if ( v.getPrecision() <= 0 ) {
                  pr = 0;
                }

                String mask = "";
                for ( int m = 0; m < le - pr; m++ ) {
                  mask += "0";
                }
                if ( pr > 0 ) {
                  mask += ".";
                }
                for ( int m = 0; m < pr; m++ ) {
                  mask += "0";
                }
                tableItem.setText( 3, mask );
              }
            }
          }

          return true;
        }
      };
      BaseStepDialog.getFieldsFromPrevious( r, wFields, 1, new int[] { 1 }, new int[] { 2 }, 4, 5, listener );
    }
  } catch ( KettleException ke ) {
    new ErrorDialog( shell, BaseMessages.getString( PKG, "System.Dialog.GetFieldsFailed.Title" ), BaseMessages
      .getString( PKG, "System.Dialog.GetFieldsFailed.Message" ), ke );
  }

}
 
Example 7
Source File: AddXML.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
private String formatField( ValueMetaInterface valueMeta, Object valueData, XMLField field )
  throws KettleValueException {
  String retval = "";
  if ( field == null ) {
    return "";
  }

  if ( valueMeta == null || valueMeta.isNull( valueData ) ) {
    String defaultNullValue = field.getNullString();
    return Utils.isEmpty( defaultNullValue ) ? "" : defaultNullValue;
  }

  if ( valueMeta.isNumeric() ) {
    // Formatting
    if ( !Utils.isEmpty( field.getFormat() ) ) {
      data.df.applyPattern( field.getFormat() );
    } else {
      data.df.applyPattern( data.defaultDecimalFormat.toPattern() );
    }
    // Decimal
    if ( !Utils.isEmpty( field.getDecimalSymbol() ) ) {
      data.dfs.setDecimalSeparator( field.getDecimalSymbol().charAt( 0 ) );
    } else {
      data.dfs.setDecimalSeparator( data.defaultDecimalFormatSymbols.getDecimalSeparator() );
    }
    // Grouping
    if ( !Utils.isEmpty( field.getGroupingSymbol() ) ) {
      data.dfs.setGroupingSeparator( field.getGroupingSymbol().charAt( 0 ) );
    } else {
      data.dfs.setGroupingSeparator( data.defaultDecimalFormatSymbols.getGroupingSeparator() );
    }
    // Currency symbol
    if ( !Utils.isEmpty( field.getCurrencySymbol() ) ) {
      data.dfs.setCurrencySymbol( field.getCurrencySymbol() );
    } else {
      data.dfs.setCurrencySymbol( data.defaultDecimalFormatSymbols.getCurrencySymbol() );
    }

    data.df.setDecimalFormatSymbols( data.dfs );

    if ( valueMeta.isBigNumber() ) {
      retval = data.df.format( valueMeta.getBigNumber( valueData ) );
    } else if ( valueMeta.isNumber() ) {
      retval = data.df.format( valueMeta.getNumber( valueData ) );
    } else {
      // Integer
      retval = data.df.format( valueMeta.getInteger( valueData ) );
    }
  } else if ( valueMeta.isDate() ) {
    if ( field != null && !Utils.isEmpty( field.getFormat() ) && valueMeta.getDate( valueData ) != null ) {
      if ( !Utils.isEmpty( field.getFormat() ) ) {
        data.daf.applyPattern( field.getFormat() );
      } else {
        data.daf.applyPattern( data.defaultDateFormat.toLocalizedPattern() );
      }
      data.daf.setDateFormatSymbols( data.dafs );
      retval = data.daf.format( valueMeta.getDate( valueData ) );
    } else {
      if ( valueMeta.isNull( valueData ) ) {
        if ( field != null && !Utils.isEmpty( field.getNullString() ) ) {
          retval = field.getNullString();
        }
      } else {
        retval = valueMeta.getString( valueData );
      }
    }
  } else if ( valueMeta.isString() ) {
    retval = valueMeta.getString( valueData );
  } else if ( valueMeta.isBinary() ) {
    if ( valueMeta.isNull( valueData ) ) {
      if ( !Utils.isEmpty( field.getNullString() ) ) {
        retval = field.getNullString();
      } else {
        retval = Const.NULL_BINARY;
      }
    } else {
      try {
        retval = new String( valueMeta.getBinary( valueData ), "UTF-8" );
      } catch ( UnsupportedEncodingException e ) {
        // chances are small we'll get here. UTF-8 is
        // mandatory.
        retval = Const.NULL_BINARY;
      }
    }
  } else {
    // Boolean
    retval = valueMeta.getString( valueData );
  }

  return retval;
}