use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class Value method readObj.
/**
* Read the metadata and data for this Value object from the specified data input stream
*
* @param dis
* @throws IOException
*/
public void readObj(DataInputStream dis) throws IOException {
// type
int theType = dis.readInt();
newValue(theType);
// name-length
int nameLength = dis.readInt();
// name
StringBuilder nameBuffer = new StringBuilder();
for (int i = 0; i < nameLength; i++) {
nameBuffer.append(dis.readChar());
}
setName(new String(nameBuffer));
// length & precision
setLength(dis.readInt(), dis.readInt());
// Null?
setNull(dis.readBoolean());
// Read the values
if (!isNull()) {
switch(getType()) {
case VALUE_TYPE_STRING:
// read the length
int stringLength = dis.readInt();
if (stringLength < 0) {
setValue((String) null);
} else {
byte[] chars = new byte[stringLength];
dis.readFully(chars);
setValue(new String(chars, Const.XML_ENCODING));
}
break;
case VALUE_TYPE_BIGNUMBER:
// read the length
int bnLength = dis.readInt();
if (bnLength < 0) {
setValue((BigDecimal) null);
} else {
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < bnLength; i++) {
buffer.append(dis.readChar());
}
setValue(buffer.toString());
try {
convertString(VALUE_TYPE_BIGNUMBER);
} catch (KettleValueException e) {
throw new IOException("Unable to convert String to BigNumber while reading from data input stream [" + getString() + "]");
}
}
break;
case VALUE_TYPE_DATE:
if (dis.readBoolean()) {
setValue(new Date(dis.readLong()));
}
break;
case VALUE_TYPE_NUMBER:
setValue(dis.readDouble());
break;
case VALUE_TYPE_INTEGER:
setValue(dis.readLong());
break;
case VALUE_TYPE_BOOLEAN:
setValue(dis.readBoolean());
break;
default:
break;
}
}
}
use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class Value method trunc.
@SuppressWarnings("fallthrough")
public Value trunc(int level) throws KettleValueException {
if (isNull()) {
// don't do anything, leave it at NULL!
return this;
}
if (isInteger()) {
// nothing to do.
return this;
}
if (isBigNumber()) {
getBigNumber().setScale(level, BigDecimal.ROUND_FLOOR);
} else if (isNumber()) {
double pow = Math.pow(10, level);
setValue(Math.floor(getNumber() * pow) / pow);
} else if (isDate()) {
Calendar cal = Calendar.getInstance();
cal.setTime(getDate());
switch(level) {
// MONTHS
case 5:
cal.set(Calendar.MONTH, 1);
// DAYS
case 4:
cal.set(Calendar.DAY_OF_MONTH, 1);
// HOURS
case 3:
cal.set(Calendar.HOUR_OF_DAY, 0);
// MINUTES
case 2:
cal.set(Calendar.MINUTE, 0);
// SECONDS
case 1:
cal.set(Calendar.SECOND, 0);
// MILI-SECONDS
case 0:
cal.set(Calendar.MILLISECOND, 0);
break;
default:
throw new KettleValueException("Argument of TRUNC of date has to be between 0 and 5");
}
} else {
throw new KettleValueException("Function TRUNC only works with numbers and dates");
}
return this;
}
use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class Value method multiply.
public Value multiply(Value v) throws KettleValueException {
// a number and a string!
if (isNull() || v.isNull()) {
setNull();
return this;
}
if ((v.isString() && isNumeric()) || (v.isNumeric() && isString())) {
StringBuilder s;
String append = "";
int n;
if (v.isString()) {
s = new StringBuilder(v.getString());
append = v.getString();
n = (int) getInteger();
} else {
s = new StringBuilder(getString());
append = getString();
n = (int) v.getInteger();
}
if (n == 0) {
s.setLength(0);
} else {
for (int i = 1; i < n; i++) {
s.append(append);
}
}
setValue(s);
} else if (isBigNumber() || v.isBigNumber()) {
// big numbers
setValue(ValueDataUtil.multiplyBigDecimals(getBigNumber(), v.getBigNumber(), null));
} else if (isNumber() || v.isNumber()) {
// numbers
setValue(getNumber() * v.getNumber());
} else if (isInteger() || v.isInteger()) {
// integers
setValue(getInteger() * v.getInteger());
} else {
throw new KettleValueException("Multiplication can only be done with numbers or a number and a string!");
}
return this;
}
use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class KettleDatabaseRepositoryConnectionDelegate method getDatabaseAttributes.
public Collection<RowMetaAndData> getDatabaseAttributes(ObjectId id_database) throws KettleDatabaseException, KettleValueException {
String sql = "SELECT * FROM " + quoteTable(KettleDatabaseRepository.TABLE_R_DATABASE_ATTRIBUTE) + " WHERE " + quote(KettleDatabaseRepository.FIELD_DATABASE_ID_DATABASE) + " = ?";
// Get the prepared statement
//
PreparedStatement ps = getPreparedStatement(sql);
// Assemble the parameter (if any)
//
RowMetaInterface parameterMeta = new RowMeta();
parameterMeta.addValueMeta(new ValueMetaInteger("id"));
Object[] parameterData = new Object[] { ((LongObjectId) id_database).longValue() };
List<RowMetaAndData> attrs = new ArrayList<RowMetaAndData>();
return callRead(new Callable<Collection<RowMetaAndData>>() {
@Override
public Collection<RowMetaAndData> call() throws Exception {
ResultSet resultSet = null;
try {
resultSet = database.openQuery(ps, parameterMeta, parameterData);
List<Object[]> rows = database.getRows(resultSet, 0, null);
for (Object[] row : rows) {
RowMetaAndData rowWithMeta = new RowMetaAndData(database.getReturnRowMeta(), row);
long id = rowWithMeta.getInteger(quote(KettleDatabaseRepository.FIELD_DATABASE_ATTRIBUTE_ID_DATABASE_ATTRIBUTE), 0);
if (id > 0) {
attrs.add(rowWithMeta);
}
}
return attrs;
} catch (KettleDatabaseException e) {
throw e;
} finally {
database.closeQuery(resultSet);
}
}
});
}
use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class ValueMetaBaseLenientStringConversionTest method testStrToBigNumberStrict.
@Test
public void testStrToBigNumberStrict() throws Exception {
System.setProperty(Const.KETTLE_LENIENT_STRING_TO_NUMBER_CONVERSION, "N");
String[] values = new String[] { "1b", "1,5", "10,000,000.25" };
ValueMetaBigNumber meta = new ValueMetaBigNumber();
Throwable exc = null;
BigDecimal converted = null;
for (String value : values) {
try {
converted = meta.convertStringToBigNumber(value);
} catch (Exception e) {
exc = e;
} finally {
Assert.assertTrue("Conversion of '" + value + "' didn't fail. Value is " + converted, exc instanceof KettleValueException);
exc = null;
}
}
}
Aggregations