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

The following examples show how to use org.pentaho.di.core.row.ValueMetaInterface#isDate() . 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: SalesforceStep.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * normalize object for future sent in Salesforce
 *
 * @param valueMeta value meta
 * @param value pentaho internal value object
 * @return object for sending in Salesforce
 * @throws KettleValueException
 */
public Object normalizeValue( ValueMetaInterface valueMeta, Object value ) throws KettleValueException {
  if ( valueMeta.isDate() ) {
    // Pass date field converted to UTC, see PDI-10836
    Calendar cal = Calendar.getInstance( valueMeta.getDateFormatTimeZone() );
    cal.setTime( valueMeta.getDate( value ) );
    Calendar utc = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) );
    // Reset time-related fields
    utc.clear();
    utc.set( cal.get( Calendar.YEAR ), cal.get( Calendar.MONTH ), cal.get( Calendar.DATE ),
      cal.get( Calendar.HOUR_OF_DAY ), cal.get( Calendar.MINUTE ), cal.get( Calendar.SECOND ) );
    value = utc;
  } else if ( valueMeta.isStorageBinaryString() ) {
    value = valueMeta.convertToNormalStorageType( value );
  }

  if ( ValueMetaInterface.TYPE_INTEGER == valueMeta.getType() ) {
    // Salesforce integer values can be only http://www.w3.org/2001/XMLSchema:int
    // see org.pentaho.di.ui.trans.steps.salesforceinput.SalesforceInputDialog#addFieldToTable
    // So we need convert Hitachi Vantara integer (real java Long value) to real int.
    // It will be sent correct as http://www.w3.org/2001/XMLSchema:int

    // use checked cast for prevent losing data
    value = Ints.checkedCast( (Long) value );
  }
  return value;
}
 
Example 2
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 3
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 4
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;
}