Search in sources :

Example 81 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class Value method trunc.

// implement the TRUNC function, version without arguments
public Value trunc() throws KettleValueException {
    if (isNull()) {
        // don't do anything, leave it at NULL!
        return this;
    }
    if (isInteger()) {
        // Nothing
        return this;
    }
    if (isBigNumber()) {
        getBigNumber().setScale(0, BigDecimal.ROUND_FLOOR);
    } else if (isNumber()) {
        setValue(Math.floor(getNumber()));
    } else if (isDate()) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDate());
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        setValue(cal.getTime());
    } else {
        throw new KettleValueException("Function TRUNC only works on numbers and dates");
    }
    return this;
}
Also used : Calendar(java.util.Calendar) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 82 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class Value method last_day.

// implement the LAST_DAY function, arguments in args[]
public Value last_day() throws KettleValueException {
    if (getType() == VALUE_TYPE_DATE) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDate());
        int last_day = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, last_day);
        setValue(cal.getTime());
    } else {
        throw new KettleValueException("Function last_day only works on a date");
    }
    return this;
}
Also used : Calendar(java.util.Calendar) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 83 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class Value method first_day.

public Value first_day() throws KettleValueException {
    if (getType() == VALUE_TYPE_DATE) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDate());
        cal.set(Calendar.DAY_OF_MONTH, 1);
        setValue(cal.getTime());
    } else {
        throw new KettleValueException("Function first_day only works on a date");
    }
    return this;
}
Also used : Calendar(java.util.Calendar) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 84 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class Value method add_months.

// implement the ADD_MONTHS function, one argument
public Value add_months(int months) throws KettleValueException {
    if (getType() == VALUE_TYPE_DATE) {
        if (!isNull() && getDate() != null) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(getDate());
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH);
            int day = cal.get(Calendar.DAY_OF_MONTH);
            month += months;
            int newyear = year + (int) Math.floor(month / 12);
            int newmonth = month % 12;
            cal.set(newyear, newmonth, 1);
            int newday = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            if (newday < day) {
                cal.set(Calendar.DAY_OF_MONTH, newday);
            } else {
                cal.set(Calendar.DAY_OF_MONTH, day);
            }
            setValue(cal.getTime());
        }
    } else {
        throw new KettleValueException("Function add_months only works on a date!");
    }
    return this;
}
Also used : Calendar(java.util.Calendar) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 85 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class Value method round.

/**
 * Rounds the Number value to a certain number decimal places.
 *
 * @param decimalPlaces
 * @return The rounded Number Value
 * @throws KettleValueException
 *           in case it's not a number (or other problem).
 */
public Value round(int decimalPlaces) throws KettleValueException {
    if (isNull()) {
        return this;
    }
    if (isNumeric()) {
        if (isBigNumber()) {
            // Multiply by 10^decimalPlaces
            // For example 123.458343938437, Decimalplaces = 2
            // 
            BigDecimal bigDec = getBigNumber();
            // System.out.println("ROUND decimalPlaces : "+decimalPlaces+", bigNumber = "+bigDec);
            bigDec = bigDec.setScale(decimalPlaces, BigDecimal.ROUND_HALF_EVEN);
            // System.out.println("ROUND finished result         : "+bigDec);
            setValue(bigDec);
        } else {
            setValue(Const.round(getNumber(), decimalPlaces));
        }
    } else {
        throw new KettleValueException("Function ROUND only works with a number");
    }
    return this;
}
Also used : KettleValueException(org.pentaho.di.core.exception.KettleValueException) BigDecimal(java.math.BigDecimal)

Aggregations

KettleValueException (org.pentaho.di.core.exception.KettleValueException)127 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)35 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)35 KettleException (org.pentaho.di.core.exception.KettleException)25 KettleStepException (org.pentaho.di.core.exception.KettleStepException)17 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)16 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)13 IOException (java.io.IOException)12 Test (org.junit.Test)11 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)11 KettleFileException (org.pentaho.di.core.exception.KettleFileException)11 ParseException (java.text.ParseException)10 Date (java.util.Date)9 EOFException (java.io.EOFException)8 SQLException (java.sql.SQLException)8 ArrayList (java.util.ArrayList)8 Calendar (java.util.Calendar)8 KettleEOFException (org.pentaho.di.core.exception.KettleEOFException)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 BigDecimal (java.math.BigDecimal)6