use of org.pentaho.di.core.exception.KettleStepException in project pentaho-kettle by pentaho.
the class Calculator method processRow.
@Override
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
meta = (CalculatorMeta) smi;
data = (CalculatorData) sdi;
// get row, set busy!
Object[] r = getRow();
if (r == null) {
// no more input to be expected...
setOutputDone();
data.clearValuesMetaMapping();
return false;
}
if (first) {
first = false;
data.setOutputRowMeta(getInputRowMeta().clone());
meta.getFields(data.getOutputRowMeta(), getStepname(), null, null, this, repository, metaStore);
// get all metadata, including source rows and temporary fields.
data.setCalcRowMeta(meta.getAllFields(getInputRowMeta()));
data.setFieldIndexes(new FieldIndexes[meta.getCalculation().length]);
List<Integer> tempIndexes = new ArrayList<Integer>();
// CHECKSTYLE:Indentation:OFF
for (int i = 0; i < meta.getCalculation().length; i++) {
CalculatorMetaFunction function = meta.getCalculation()[i];
data.getFieldIndexes()[i] = new FieldIndexes();
if (!Utils.isEmpty(function.getFieldName())) {
data.getFieldIndexes()[i].indexName = data.getCalcRowMeta().indexOfValue(function.getFieldName());
if (data.getFieldIndexes()[i].indexName < 0) {
// Nope: throw an exception
throw new KettleStepException(BaseMessages.getString(PKG, "Calculator.Error.UnableFindField", function.getFieldName(), "" + (i + 1)));
}
} else {
throw new KettleStepException(BaseMessages.getString(PKG, "Calculator.Error.NoNameField", "" + (i + 1)));
}
if (!Utils.isEmpty(function.getFieldA())) {
if (function.getCalcType() != CalculatorMetaFunction.CALC_CONSTANT) {
data.getFieldIndexes()[i].indexA = data.getCalcRowMeta().indexOfValue(function.getFieldA());
if (data.getFieldIndexes()[i].indexA < 0) {
// Nope: throw an exception
throw new KettleStepException("Unable to find the first argument field '" + function.getFieldName() + " for calculation #" + (i + 1));
}
} else {
data.getFieldIndexes()[i].indexA = -1;
}
} else {
throw new KettleStepException("There is no first argument specified for calculated field #" + (i + 1));
}
if (!Utils.isEmpty(function.getFieldB())) {
data.getFieldIndexes()[i].indexB = data.getCalcRowMeta().indexOfValue(function.getFieldB());
if (data.getFieldIndexes()[i].indexB < 0) {
// Nope: throw an exception
throw new KettleStepException("Unable to find the second argument field '" + function.getFieldName() + " for calculation #" + (i + 1));
}
}
data.getFieldIndexes()[i].indexC = -1;
if (!Utils.isEmpty(function.getFieldC())) {
data.getFieldIndexes()[i].indexC = data.getCalcRowMeta().indexOfValue(function.getFieldC());
if (data.getFieldIndexes()[i].indexC < 0) {
// Nope: throw an exception
throw new KettleStepException("Unable to find the third argument field '" + function.getFieldName() + " for calculation #" + (i + 1));
}
}
if (function.isRemovedFromResult()) {
tempIndexes.add(getInputRowMeta().size() + i);
}
}
// Convert temp indexes to int[]
data.setTempIndexes(new int[tempIndexes.size()]);
for (int i = 0; i < data.getTempIndexes().length; i++) {
data.getTempIndexes()[i] = tempIndexes.get(i);
}
}
if (log.isRowLevel()) {
logRowlevel(BaseMessages.getString(PKG, "Calculator.Log.ReadRow") + getLinesRead() + " : " + getInputRowMeta().getString(r));
}
try {
Object[] row = calcFields(getInputRowMeta(), r);
// copy row to possible alternate rowset(s).
putRow(data.getOutputRowMeta(), row);
if (log.isRowLevel()) {
logRowlevel("Wrote row #" + getLinesWritten() + " : " + getInputRowMeta().getString(r));
}
if (checkFeedback(getLinesRead())) {
if (log.isBasic()) {
logBasic(BaseMessages.getString(PKG, "Calculator.Log.Linenr", "" + getLinesRead()));
}
}
} catch (KettleException e) {
if (getStepMeta().isDoingErrorHandling()) {
putError(getInputRowMeta(), r, 1, e.toString(), null, "CALC001");
} else {
logError(BaseMessages.getString(PKG, "Calculator.ErrorInStepRunning" + " : " + e.getMessage()));
throw new KettleStepException(BaseMessages.getString(PKG, "Calculator.ErrorInStepRunning"), e);
}
}
return true;
}
use of org.pentaho.di.core.exception.KettleStepException in project pentaho-kettle by pentaho.
the class ConcatFields method putRowFromStream.
// reads the row from the stream, flushs, add target field and call putRow()
Object[] putRowFromStream(Object[] r) throws KettleStepException {
byte[] targetBinary = ((ConcatFieldsOutputStream) data.writer).read();
if (r == null && targetBinary == null) {
// special condition of header/footer/split
return null;
}
Object[] outputRowData = prepareOutputRow(r);
// add target field
if (outputRowData == null) {
// special condition of header/footer/split
outputRowData = new Object[data.outputRowMeta.size()];
}
if (targetBinary != null) {
if (!data.hasEncoding) {
outputRowData[data.posTargetField] = new String(targetBinary);
} else {
// handle encoding
try {
outputRowData[data.posTargetField] = new String(targetBinary, meta.getEncoding());
} catch (UnsupportedEncodingException e) {
throw new KettleStepException(BaseMessages.getString(PKG, "ConcatFields.Error.UnsupportedEncoding", "" + meta.getEncoding()));
}
}
} else {
outputRowData[data.posTargetField] = null;
}
putRow(data.outputRowMeta, outputRowData);
return outputRowData;
}
use of org.pentaho.di.core.exception.KettleStepException in project pentaho-kettle by pentaho.
the class ConcatFieldsMeta method getFields.
@Override
public void getFields(RowMetaInterface row, String name, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space, Repository repository, IMetaStore metaStore) throws KettleStepException {
// remove selected fields from the stream when true
if (removeSelectedFields) {
if (getOutputFields().length > 0) {
for (int i = 0; i < getOutputFields().length; i++) {
TextFileField field = getOutputFields()[i];
try {
row.removeValueMeta(field.getName());
} catch (KettleValueException e) {
// just ignore exceptions since missing fields are handled in the ConcatFields class
}
}
} else {
// no output fields selected, take them all, remove them all
row.clear();
}
}
// Check Target Field Name
if (Utils.isEmpty(targetFieldName)) {
throw new KettleStepException(BaseMessages.getString(PKG, "ConcatFieldsMeta.CheckResult.TargetFieldNameMissing"));
}
// add targetFieldName
ValueMetaInterface vValue = new ValueMetaString(targetFieldName);
vValue.setLength(targetFieldLength, 0);
vValue.setOrigin(name);
if (!Utils.isEmpty(getEncoding())) {
vValue.setStringEncoding(getEncoding());
}
row.addValueMeta(vValue);
}
use of org.pentaho.di.core.exception.KettleStepException in project pentaho-kettle by pentaho.
the class ConstantMeta method getFields.
public void getFields(RowMetaInterface rowMeta, String name, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space, Repository repository, IMetaStore metaStore) throws KettleStepException {
for (int i = 0; i < fieldName.length; i++) {
if (fieldName[i] != null && fieldName[i].length() != 0) {
int type = ValueMetaFactory.getIdForValueMeta(fieldType[i]);
if (type == ValueMetaInterface.TYPE_NONE) {
type = ValueMetaInterface.TYPE_STRING;
}
try {
ValueMetaInterface v = ValueMetaFactory.createValueMeta(fieldName[i], type);
v.setLength(fieldLength[i]);
v.setPrecision(fieldPrecision[i]);
v.setOrigin(name);
rowMeta.addValueMeta(v);
} catch (Exception e) {
throw new KettleStepException(e);
}
}
}
}
use of org.pentaho.di.core.exception.KettleStepException in project pentaho-kettle by pentaho.
the class CsvInputMeta method getFields.
@Override
public void getFields(RowMetaInterface rowMeta, String origin, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space, Repository repository, IMetaStore metaStore) throws KettleStepException {
try {
// Start with a clean slate, eats the input
rowMeta.clear();
for (int i = 0; i < inputFields.length; i++) {
TextFileInputField field = inputFields[i];
ValueMetaInterface valueMeta = ValueMetaFactory.createValueMeta(field.getName(), field.getType());
valueMeta.setConversionMask(field.getFormat());
valueMeta.setLength(field.getLength());
valueMeta.setPrecision(field.getPrecision());
valueMeta.setConversionMask(field.getFormat());
valueMeta.setDecimalSymbol(field.getDecimalSymbol());
valueMeta.setGroupingSymbol(field.getGroupSymbol());
valueMeta.setCurrencySymbol(field.getCurrencySymbol());
valueMeta.setTrimType(field.getTrimType());
if (lazyConversionActive) {
valueMeta.setStorageType(ValueMetaInterface.STORAGE_TYPE_BINARY_STRING);
}
valueMeta.setStringEncoding(space.environmentSubstitute(encoding));
// In case we want to convert Strings...
// Using a copy of the valueMeta object means that the inner and outer representation format is the same.
// Preview will show the data the same way as we read it.
// This layout is then taken further down the road by the metadata through the transformation.
//
ValueMetaInterface storageMetadata = ValueMetaFactory.cloneValueMeta(valueMeta, ValueMetaInterface.TYPE_STRING);
storageMetadata.setStorageType(ValueMetaInterface.STORAGE_TYPE_NORMAL);
// we don't really know the lengths of the strings read in advance.
storageMetadata.setLength(-1, -1);
valueMeta.setStorageMetadata(storageMetadata);
valueMeta.setOrigin(origin);
rowMeta.addValueMeta(valueMeta);
}
if (!Utils.isEmpty(filenameField) && includingFilename) {
ValueMetaInterface filenameMeta = new ValueMetaString(filenameField);
filenameMeta.setOrigin(origin);
if (lazyConversionActive) {
filenameMeta.setStorageType(ValueMetaInterface.STORAGE_TYPE_BINARY_STRING);
filenameMeta.setStorageMetadata(new ValueMetaString(filenameField));
}
rowMeta.addValueMeta(filenameMeta);
}
if (!Utils.isEmpty(rowNumField)) {
ValueMetaInterface rowNumMeta = new ValueMetaInteger(rowNumField);
rowNumMeta.setLength(10);
rowNumMeta.setOrigin(origin);
rowMeta.addValueMeta(rowNumMeta);
}
} catch (Exception e) {
throw new KettleStepException(e);
}
}
Aggregations