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

The following examples show how to use org.pentaho.di.core.row.ValueMetaInterface#isNumber() . 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: ExcelOutputDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private void get() {
  try {
    RowMetaInterface r = transMeta.getPrevStepFields( stepname );
    if ( r != null ) {
      TableItemInsertListener listener = new TableItemInsertListener() {
        @Override
        public boolean tableItemInserted( TableItem tableItem, ValueMetaInterface v ) {
          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 2
Source File: ExcelWriterStepDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private void get() {
  try {
    RowMetaInterface r = transMeta.getPrevStepFields( stepname );
    if ( r != null ) {
      TableItemInsertListener listener = new TableItemInsertListener() {
        @Override
        public boolean tableItemInserted( TableItem tableItem, ValueMetaInterface v ) {
          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, 5 }, new int[] { 2 }, 0, 0, listener );
    }
  } catch ( KettleException ke ) {
    new ErrorDialog( shell, BaseMessages.getString( PKG, "System.Dialog.GetFieldsFailed.Title" ), BaseMessages
      .getString( PKG, "System.Dialog.GetFieldsFailed.Message" ), ke );
  }

}
 
Example 3
Source File: XMLOutputDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private void get() {
  try {
    RowMetaInterface r = transMeta.getPrevStepFields( stepname );
    if ( r != null && !r.isEmpty() ) {
      TableItemInsertListener listener = new TableItemInsertListener() {
        public boolean tableItemInserted( TableItem tableItem, ValueMetaInterface v ) {
          tableItem.setText( 3, ContentType.Element.name() );
          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( 4, mask );
            }
          }
          return true;
        }
      };
      BaseStepDialog.getFieldsFromPrevious( r, wFields, 1, new int[] { 1 }, new int[] { 4 }, 6, 7, listener );
    }
  } catch ( KettleException ke ) {
    new ErrorDialog( shell, BaseMessages.getString( PKG, "System.Dialog.GetFieldsFailed.Title" ), BaseMessages
        .getString( PKG, "System.Dialog.GetFieldsFailed.Message" ), ke );
  }

}
 
Example 4
Source File: ConcatFieldsDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private void get() {
  try {
    RowMetaInterface r = transMeta.getPrevStepFields( stepname );
    if ( r != null ) {
      TableItemInsertListener listener = new TableItemInsertListener() {
        @Override
        public boolean tableItemInserted( TableItem tableItem, ValueMetaInterface v ) {
          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 5
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 6
Source File: ValueMetaBase.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
/**
 * Compare 2 values of the same data type
 *
 * @param data1
 *          the first value
 * @param meta2
 *          the second value's metadata
 * @param data2
 *          the second value
 * @return 0 if the values are equal, -1 if data1 is smaller than data2 and +1 if it's larger.
 * @throws KettleValueException
 *           In case we get conversion errors
 */
@Override
public int compare( Object data1, ValueMetaInterface meta2, Object data2 ) throws KettleValueException {
  if ( meta2 == null ) {
    throw new KettleValueException( toStringMeta()
        + " : Second meta data (meta2) is null, please check one of the previous steps." );
  }

  try {
    // Before we can compare data1 to data2 we need to make sure they have the
    // same data type etc.
    //
    if ( getType() == meta2.getType() ) {
      if ( getStorageType() == meta2.getStorageType() ) {
        return compare( data1, data2 );
      }

      // Convert the storage type to compare the data.
      //
      switch ( getStorageType() ) {
        case STORAGE_TYPE_NORMAL:
          return compare( data1, meta2.convertToNormalStorageType( data2 ) );
        case STORAGE_TYPE_BINARY_STRING:
          if ( storageMetadata != null && storageMetadata.getConversionMask() != null && !meta2.isNumber() ) {
            // BACKLOG-18754 - if there is a storage conversion mask, we should use
            // it as the mask for meta2 (meta2 can have specific storage type and type, so
            // it can't be used directly to convert data2 to binary string)
            ValueMetaInterface meta2StorageMask = meta2.clone();
            meta2StorageMask.setConversionMask( storageMetadata.getConversionMask() );
            return compare( data1, meta2StorageMask.convertToBinaryStringStorageType( data2 ) );
          } else {
            return compare( data1, meta2.convertToBinaryStringStorageType( data2 ) );
          }
        case STORAGE_TYPE_INDEXED:
          switch ( meta2.getStorageType() ) {
            case STORAGE_TYPE_INDEXED:
              return compare( data1, data2 ); // not accessible, just to make sure.
            case STORAGE_TYPE_NORMAL:
              return -meta2.compare( data2, convertToNormalStorageType( data1 ) );
            case STORAGE_TYPE_BINARY_STRING:
              return -meta2.compare( data2, convertToBinaryStringStorageType( data1 ) );
            default:
              throw new KettleValueException( meta2.toStringMeta() + " : Unknown storage type : "
                  + meta2.getStorageType() );

          }
        default:
          throw new KettleValueException( toStringMeta() + " : Unknown storage type : " + getStorageType() );
      }
    } else if ( ValueMetaInterface.TYPE_INTEGER == getType() && ValueMetaInterface.TYPE_NUMBER == meta2.getType() ) {
      // BACKLOG-18738
      // compare Double to Integer
      return -meta2.compare( data2, meta2.convertData( this, data1 ) );
    }

    // If the data types are not the same, the first one is the driver...
    // The second data type is converted to the first one.
    //
    return compare( data1, convertData( meta2, data2 ) );
  } catch ( Exception e ) {
    throw new KettleValueException(
        toStringMeta() + " : Unable to compare with value [" + meta2.toStringMeta() + "]", e );
  }
}
 
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;
}