use of org.pentaho.di.core.exception.KettleStepException in project pentaho-kettle by pentaho.
the class FixedInputMeta method getFields.
public void getFields(RowMetaInterface rowMeta, String origin, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space, Repository repository, IMetaStore metaStore) throws KettleStepException {
try {
for (int i = 0; i < fieldDefinition.length; i++) {
FixedFileInputField field = fieldDefinition[i];
ValueMetaInterface valueMeta = ValueMetaFactory.createValueMeta(field.getName(), field.getType());
valueMeta.setConversionMask(field.getFormat());
valueMeta.setTrimType(field.getTrimType());
valueMeta.setLength(field.getLength());
valueMeta.setPrecision(field.getPrecision());
valueMeta.setConversionMask(field.getFormat());
valueMeta.setDecimalSymbol(field.getDecimal());
valueMeta.setGroupingSymbol(field.getGrouping());
valueMeta.setCurrencySymbol(field.getCurrency());
valueMeta.setStringEncoding(space.environmentSubstitute(encoding));
if (lazyConversionActive) {
valueMeta.setStorageType(ValueMetaInterface.STORAGE_TYPE_BINARY_STRING);
}
// In case we want to convert Strings...
//
ValueMetaInterface storageMetadata = ValueMetaFactory.cloneValueMeta(valueMeta, ValueMetaInterface.TYPE_STRING);
storageMetadata.setStorageType(ValueMetaInterface.STORAGE_TYPE_NORMAL);
valueMeta.setStorageMetadata(storageMetadata);
valueMeta.setOrigin(origin);
rowMeta.addValueMeta(valueMeta);
}
} catch (Exception e) {
throw new KettleStepException(e);
}
}
use of org.pentaho.di.core.exception.KettleStepException in project pentaho-kettle by pentaho.
the class DimensionLookup method processRow.
@Override
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
meta = (DimensionLookupMeta) smi;
data = (DimensionLookupData) sdi;
// Get row from input rowset & set row busy!
Object[] r = getRow();
if (r == null) {
// no more input to be expected...
// signal end to receiver(s)
setOutputDone();
return false;
}
if (first) {
first = false;
data.schemaTable = meta.getDatabaseMeta().getQuotedSchemaTableCombination(data.realSchemaName, data.realTableName);
data.inputRowMeta = getInputRowMeta().clone();
data.outputRowMeta = getInputRowMeta().clone();
meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
// Get the fields that need conversion to normal storage...
// Modify the storage type of the input data...
//
data.lazyList = new ArrayList<Integer>();
for (int i = 0; i < data.inputRowMeta.size(); i++) {
ValueMetaInterface valueMeta = data.inputRowMeta.getValueMeta(i);
if (valueMeta.isStorageBinaryString()) {
data.lazyList.add(i);
valueMeta.setStorageType(ValueMetaInterface.STORAGE_TYPE_NORMAL);
}
}
// The start date value column (if applicable)
//
data.startDateFieldIndex = -1;
if (data.startDateChoice == DimensionLookupMeta.START_DATE_ALTERNATIVE_COLUMN_VALUE) {
data.startDateFieldIndex = data.inputRowMeta.indexOfValue(meta.getStartDateFieldName());
if (data.startDateFieldIndex < 0) {
throw new KettleStepException(BaseMessages.getString(PKG, "DimensionLookup.Exception.StartDateValueColumnNotFound", meta.getStartDateFieldName()));
}
}
// Lookup values
data.keynrs = new int[meta.getKeyStream().length];
for (int i = 0; i < meta.getKeyStream().length; i++) {
// logDetailed("Lookup values key["+i+"] --> "+key[i]+", row==null?"+(row==null));
data.keynrs[i] = data.inputRowMeta.indexOfValue(meta.getKeyStream()[i]);
if (data.keynrs[i] < 0) {
// couldn't find field!
throw new KettleStepException(BaseMessages.getString(PKG, "DimensionLookup.Exception.KeyFieldNotFound", meta.getKeyStream()[i]));
}
}
// Return values
data.fieldnrs = new int[meta.getFieldStream().length];
for (int i = 0; meta.getFieldStream() != null && i < meta.getFieldStream().length; i++) {
if (!DimensionLookupMeta.isUpdateTypeWithoutArgument(meta.isUpdate(), meta.getFieldUpdate()[i])) {
data.fieldnrs[i] = data.outputRowMeta.indexOfValue(meta.getFieldStream()[i]);
if (data.fieldnrs[i] < 0) {
throw new KettleStepException(BaseMessages.getString(PKG, "DimensionLookup.Exception.KeyFieldNotFound", meta.getFieldStream()[i]));
}
} else {
data.fieldnrs[i] = -1;
}
}
if (!meta.isUpdate() && meta.isPreloadingCache()) {
preloadCache();
} else {
//
if (data.cacheKeyRowMeta == null) {
// KEY : the natural key(s)
//
data.cacheKeyRowMeta = new RowMeta();
for (int i = 0; i < data.keynrs.length; i++) {
ValueMetaInterface key = data.inputRowMeta.getValueMeta(data.keynrs[i]);
data.cacheKeyRowMeta.addValueMeta(key.clone());
}
data.cache = new ByteArrayHashMap(meta.getCacheSize() > 0 ? meta.getCacheSize() : 5000, data.cacheKeyRowMeta);
}
}
if (!Utils.isEmpty(meta.getDateField())) {
data.datefieldnr = data.inputRowMeta.indexOfValue(meta.getDateField());
} else {
data.datefieldnr = -1;
}
// Initialize the start date value in case we don't have one in the input rows
//
data.valueDateNow = determineDimensionUpdatedDate(r);
determineTechKeyCreation();
data.notFoundTk = new Long(meta.getDatabaseMeta().getNotFoundTK(isAutoIncrement()));
if (getCopy() == 0) {
checkDimZero();
}
setDimLookup(data.outputRowMeta);
}
//
for (int lazyFieldIndex : data.lazyList) {
ValueMetaInterface valueMeta = getInputRowMeta().getValueMeta(lazyFieldIndex);
r[lazyFieldIndex] = valueMeta.convertToNormalStorageType(r[lazyFieldIndex]);
}
try {
// add new values to the row in rowset[0].
Object[] outputRow = lookupValues(data.inputRowMeta, r);
// copy row to output rowset(s);
putRow(data.outputRowMeta, outputRow);
if (checkFeedback(getLinesRead())) {
if (log.isBasic()) {
logBasic(BaseMessages.getString(PKG, "DimensionLookup.Log.LineNumber") + getLinesRead());
}
}
} catch (KettleException e) {
logError(BaseMessages.getString(PKG, "DimensionLookup.Log.StepCanNotContinueForErrors", e.getMessage()));
logError(Const.getStackTracker(e));
setErrors(1);
stopAll();
// signal end to receiver(s)
setOutputDone();
return false;
}
return true;
}
use of org.pentaho.di.core.exception.KettleStepException in project pentaho-kettle by pentaho.
the class DimensionLookup method dimInsert.
/**
* This inserts new record into dimension Optionally, if the entry already exists, update date range from previous
* version of the entry.
*/
public Long dimInsert(RowMetaInterface inputRowMeta, Object[] row, Long technicalKey, boolean newEntry, Long versionNr, Date dateFrom, Date dateTo) throws KettleException {
DatabaseMeta databaseMeta = meta.getDatabaseMeta();
if (data.prepStatementInsert == null && data.prepStatementUpdate == null) {
// first time: construct prepared statement
RowMetaInterface insertRowMeta = new RowMeta();
/*
* Construct the SQL statement...
*
* INSERT INTO d_customer(keyfield, versionfield, datefrom, dateto, key[], fieldlookup[], last_updated,
* last_inserted, last_version) VALUES (val_key ,val_version , val_datfrom, val_datto, keynrs[], fieldnrs[],
* last_updated, last_inserted, last_version) ;
*/
String sql = "INSERT INTO " + data.schemaTable + "( ";
if (!isAutoIncrement()) {
// NO
sql += databaseMeta.quoteField(meta.getKeyField()) + ", ";
// AUTOINCREMENT
// the first return value
insertRowMeta.addValueMeta(data.outputRowMeta.getValueMeta(inputRowMeta.size()));
// after the input
} else {
if (databaseMeta.needsPlaceHolder()) {
// placeholder on informix!
sql += "0, ";
}
}
sql += databaseMeta.quoteField(meta.getVersionField()) + ", " + databaseMeta.quoteField(meta.getDateFrom()) + ", " + databaseMeta.quoteField(meta.getDateTo());
insertRowMeta.addValueMeta(new ValueMetaInteger(meta.getVersionField()));
insertRowMeta.addValueMeta(new ValueMetaDate(meta.getDateFrom()));
insertRowMeta.addValueMeta(new ValueMetaDate(meta.getDateTo()));
for (int i = 0; i < meta.getKeyLookup().length; i++) {
sql += ", " + databaseMeta.quoteField(meta.getKeyLookup()[i]);
insertRowMeta.addValueMeta(inputRowMeta.getValueMeta(data.keynrs[i]));
}
for (int i = 0; i < meta.getFieldLookup().length; i++) {
//
if (!DimensionLookupMeta.isUpdateTypeWithoutArgument(meta.isUpdate(), meta.getFieldUpdate()[i])) {
sql += ", " + databaseMeta.quoteField(meta.getFieldLookup()[i]);
insertRowMeta.addValueMeta(inputRowMeta.getValueMeta(data.fieldnrs[i]));
}
}
//
for (int i = 0; i < meta.getFieldUpdate().length; i++) {
ValueMetaInterface valueMeta = null;
switch(meta.getFieldUpdate()[i]) {
case DimensionLookupMeta.TYPE_UPDATE_DATE_INSUP:
case DimensionLookupMeta.TYPE_UPDATE_DATE_INSERTED:
valueMeta = new ValueMetaDate(meta.getFieldLookup()[i]);
break;
case DimensionLookupMeta.TYPE_UPDATE_LAST_VERSION:
valueMeta = new ValueMetaBoolean(meta.getFieldLookup()[i]);
break;
default:
break;
}
if (valueMeta != null) {
sql += ", " + databaseMeta.quoteField(valueMeta.getName());
insertRowMeta.addValueMeta(valueMeta);
}
}
sql += ") VALUES (";
if (!isAutoIncrement()) {
sql += "?, ";
}
sql += "?, ?, ?";
for (int i = 0; i < data.keynrs.length; i++) {
sql += ", ?";
}
for (int i = 0; i < meta.getFieldLookup().length; i++) {
//
if (!DimensionLookupMeta.isUpdateTypeWithoutArgument(meta.isUpdate(), meta.getFieldUpdate()[i])) {
sql += ", ?";
}
}
//
for (int i = 0; i < meta.getFieldUpdate().length; i++) {
switch(meta.getFieldUpdate()[i]) {
case DimensionLookupMeta.TYPE_UPDATE_DATE_INSUP:
case DimensionLookupMeta.TYPE_UPDATE_DATE_INSERTED:
case DimensionLookupMeta.TYPE_UPDATE_LAST_VERSION:
sql += ", ?";
break;
default:
break;
}
}
sql += " )";
try {
if (technicalKey == null && databaseMeta.supportsAutoGeneratedKeys()) {
logDetailed("SQL w/ return keys=[" + sql + "]");
data.prepStatementInsert = data.db.getConnection().prepareStatement(databaseMeta.stripCR(sql), Statement.RETURN_GENERATED_KEYS);
} else {
logDetailed("SQL=[" + sql + "]");
data.prepStatementInsert = data.db.getConnection().prepareStatement(databaseMeta.stripCR(sql));
}
// pstmt=con.prepareStatement(sql, new String[] { "klant_tk" } );
} catch (SQLException ex) {
throw new KettleDatabaseException("Unable to prepare dimension insert :" + Const.CR + sql, ex);
}
/*
* UPDATE d_customer SET dateto = val_datnow, last_updated = <now> last_version = false WHERE keylookup[] =
* keynrs[] AND versionfield = val_version - 1 ;
*/
RowMetaInterface updateRowMeta = new RowMeta();
String sql_upd = "UPDATE " + data.schemaTable + Const.CR;
// The end of the date range
//
sql_upd += "SET " + databaseMeta.quoteField(meta.getDateTo()) + " = ?" + Const.CR;
updateRowMeta.addValueMeta(new ValueMetaDate(meta.getDateTo()));
//
for (int i = 0; i < meta.getFieldUpdate().length; i++) {
ValueMetaInterface valueMeta = null;
switch(meta.getFieldUpdate()[i]) {
case DimensionLookupMeta.TYPE_UPDATE_DATE_INSUP:
case DimensionLookupMeta.TYPE_UPDATE_DATE_UPDATED:
valueMeta = new ValueMetaDate(meta.getFieldLookup()[i]);
break;
case DimensionLookupMeta.TYPE_UPDATE_LAST_VERSION:
valueMeta = new ValueMetaBoolean(meta.getFieldLookup()[i]);
break;
default:
break;
}
if (valueMeta != null) {
sql_upd += ", " + databaseMeta.quoteField(valueMeta.getName()) + " = ?" + Const.CR;
updateRowMeta.addValueMeta(valueMeta);
}
}
sql_upd += "WHERE ";
for (int i = 0; i < meta.getKeyLookup().length; i++) {
if (i > 0) {
sql_upd += "AND ";
}
sql_upd += databaseMeta.quoteField(meta.getKeyLookup()[i]) + " = ?" + Const.CR;
updateRowMeta.addValueMeta(inputRowMeta.getValueMeta(data.keynrs[i]));
}
sql_upd += "AND " + databaseMeta.quoteField(meta.getVersionField()) + " = ? ";
updateRowMeta.addValueMeta(new ValueMetaInteger(meta.getVersionField()));
try {
logDetailed("Preparing update: " + Const.CR + sql_upd + Const.CR);
data.prepStatementUpdate = data.db.getConnection().prepareStatement(databaseMeta.stripCR(sql_upd));
} catch (SQLException ex) {
throw new KettleDatabaseException("Unable to prepare dimension update :" + Const.CR + sql_upd, ex);
}
data.insertRowMeta = insertRowMeta;
data.updateRowMeta = updateRowMeta;
}
Object[] insertRow = new Object[data.insertRowMeta.size()];
int insertIndex = 0;
if (!isAutoIncrement()) {
insertRow[insertIndex++] = technicalKey;
}
// Caller is responsible for setting proper version number depending
// on if newEntry == true
insertRow[insertIndex++] = versionNr;
switch(data.startDateChoice) {
case DimensionLookupMeta.START_DATE_ALTERNATIVE_NONE:
insertRow[insertIndex++] = dateFrom;
break;
case DimensionLookupMeta.START_DATE_ALTERNATIVE_SYSDATE:
// use the time the step execution begins as the date from (passed in as dateFrom).
// before, the current system time was used. this caused an exclusion of the row in the
// lookup portion of the step that uses this 'valueDate' and not the current time.
// the result was multiple inserts for what should have been 1 [PDI-4317]
insertRow[insertIndex++] = dateFrom;
break;
case DimensionLookupMeta.START_DATE_ALTERNATIVE_START_OF_TRANS:
insertRow[insertIndex++] = getTrans().getStartDate();
break;
case DimensionLookupMeta.START_DATE_ALTERNATIVE_NULL:
insertRow[insertIndex++] = null;
break;
case DimensionLookupMeta.START_DATE_ALTERNATIVE_COLUMN_VALUE:
insertRow[insertIndex++] = inputRowMeta.getDate(row, data.startDateFieldIndex);
break;
default:
throw new KettleStepException(BaseMessages.getString(PKG, "DimensionLookup.Exception.IllegalStartDateSelection", Integer.toString(data.startDateChoice)));
}
insertRow[insertIndex++] = dateTo;
for (int i = 0; i < data.keynrs.length; i++) {
insertRow[insertIndex++] = row[data.keynrs[i]];
}
for (int i = 0; i < data.fieldnrs.length; i++) {
if (data.fieldnrs[i] >= 0) {
// Ignore last_version, last_updated, etc. These are handled below...
//
insertRow[insertIndex++] = row[data.fieldnrs[i]];
}
}
//
for (int i = 0; i < meta.getFieldUpdate().length; i++) {
switch(meta.getFieldUpdate()[i]) {
case DimensionLookupMeta.TYPE_UPDATE_DATE_INSUP:
case DimensionLookupMeta.TYPE_UPDATE_DATE_INSERTED:
insertRow[insertIndex++] = new Date();
break;
case DimensionLookupMeta.TYPE_UPDATE_LAST_VERSION:
insertRow[insertIndex++] = Boolean.TRUE;
// Always the last version on insert.
break;
default:
break;
}
}
if (isDebug()) {
logDebug("rins, size=" + data.insertRowMeta.size() + ", values=" + data.insertRowMeta.getString(insertRow));
}
// INSERT NEW VALUE!
data.db.setValues(data.insertRowMeta, insertRow, data.prepStatementInsert);
data.db.insertRow(data.prepStatementInsert);
if (isDebug()) {
logDebug("Row inserted!");
}
if (technicalKey == null && databaseMeta.supportsAutoGeneratedKeys()) {
try {
RowMetaAndData keys = data.db.getGeneratedKeys(data.prepStatementInsert);
if (keys.getRowMeta().size() > 0) {
technicalKey = keys.getRowMeta().getInteger(keys.getData(), 0);
} else {
throw new KettleDatabaseException("Unable to retrieve value of auto-generated technical key : no value found!");
}
} catch (Exception e) {
throw new KettleDatabaseException("Unable to retrieve value of auto-generated technical key : unexpected error: ", e);
}
}
if (!newEntry) {
// we have to update the previous version in the dimension!
/*
* UPDATE d_customer SET dateto = val_datfrom , last_updated = <now> , last_version = false WHERE keylookup[] =
* keynrs[] AND versionfield = val_version - 1 ;
*/
Object[] updateRow = new Object[data.updateRowMeta.size()];
int updateIndex = 0;
switch(data.startDateChoice) {
case DimensionLookupMeta.START_DATE_ALTERNATIVE_NONE:
updateRow[updateIndex++] = dateFrom;
break;
case DimensionLookupMeta.START_DATE_ALTERNATIVE_SYSDATE:
updateRow[updateIndex++] = new Date();
break;
case DimensionLookupMeta.START_DATE_ALTERNATIVE_START_OF_TRANS:
updateRow[updateIndex++] = getTrans().getCurrentDate();
break;
case DimensionLookupMeta.START_DATE_ALTERNATIVE_NULL:
updateRow[updateIndex++] = null;
break;
case DimensionLookupMeta.START_DATE_ALTERNATIVE_COLUMN_VALUE:
updateRow[updateIndex++] = inputRowMeta.getDate(row, data.startDateFieldIndex);
break;
default:
throw new KettleStepException(BaseMessages.getString("DimensionLookup.Exception.IllegalStartDateSelection", Integer.toString(data.startDateChoice)));
}
//
for (int i = 0; i < meta.getFieldUpdate().length; i++) {
switch(meta.getFieldUpdate()[i]) {
case DimensionLookupMeta.TYPE_UPDATE_DATE_INSUP:
updateRow[updateIndex++] = new Date();
break;
case DimensionLookupMeta.TYPE_UPDATE_LAST_VERSION:
updateRow[updateIndex++] = Boolean.FALSE;
// Never the last version on this update
break;
case DimensionLookupMeta.TYPE_UPDATE_DATE_UPDATED:
updateRow[updateIndex++] = new Date();
break;
default:
break;
}
}
for (int i = 0; i < data.keynrs.length; i++) {
updateRow[updateIndex++] = row[data.keynrs[i]];
}
updateRow[updateIndex++] = versionNr - 1;
if (isRowLevel()) {
logRowlevel("UPDATE using rupd=" + data.updateRowMeta.getString(updateRow));
}
// UPDATE VALUES
// set values for update
//
data.db.setValues(data.updateRowMeta, updateRow, data.prepStatementUpdate);
if (isDebug()) {
logDebug("Values set for update (" + data.updateRowMeta.size() + ")");
}
// do the actual update
data.db.insertRow(data.prepStatementUpdate);
if (isDebug()) {
logDebug("Row updated!");
}
}
return technicalKey;
}
use of org.pentaho.di.core.exception.KettleStepException in project pentaho-kettle by pentaho.
the class ExcelInputMeta method getFields.
@Override
public void getFields(RowMetaInterface row, String name, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space, Repository repository, IMetaStore metaStore) throws KettleStepException {
for (int i = 0; i < field.length; i++) {
int type = field[i].getType();
if (type == ValueMetaInterface.TYPE_NONE) {
type = ValueMetaInterface.TYPE_STRING;
}
try {
ValueMetaInterface v = ValueMetaFactory.createValueMeta(field[i].getName(), type);
v.setLength(field[i].getLength());
v.setPrecision(field[i].getPrecision());
v.setOrigin(name);
v.setConversionMask(field[i].getFormat());
v.setDecimalSymbol(field[i].getDecimalSymbol());
v.setGroupingSymbol(field[i].getGroupSymbol());
v.setCurrencySymbol(field[i].getCurrencySymbol());
row.addValueMeta(v);
} catch (Exception e) {
throw new KettleStepException(e);
}
}
if (fileField != null && fileField.length() > 0) {
ValueMetaInterface v = new ValueMetaString(fileField);
v.setLength(250);
v.setPrecision(-1);
v.setOrigin(name);
row.addValueMeta(v);
}
if (sheetField != null && sheetField.length() > 0) {
ValueMetaInterface v = new ValueMetaString(sheetField);
v.setLength(250);
v.setPrecision(-1);
v.setOrigin(name);
row.addValueMeta(v);
}
if (sheetRowNumberField != null && sheetRowNumberField.length() > 0) {
ValueMetaInterface v = new ValueMetaInteger(sheetRowNumberField);
v.setLength(ValueMetaInterface.DEFAULT_INTEGER_LENGTH, 0);
v.setOrigin(name);
row.addValueMeta(v);
}
if (rowNumberField != null && rowNumberField.length() > 0) {
ValueMetaInterface v = new ValueMetaInteger(rowNumberField);
v.setLength(ValueMetaInterface.DEFAULT_INTEGER_LENGTH, 0);
v.setOrigin(name);
row.addValueMeta(v);
}
if (getShortFileNameField() != null && getShortFileNameField().length() > 0) {
ValueMetaInterface v = new ValueMetaString(space.environmentSubstitute(getShortFileNameField()));
v.setLength(100, -1);
v.setOrigin(name);
row.addValueMeta(v);
}
if (getExtensionField() != null && getExtensionField().length() > 0) {
ValueMetaInterface v = new ValueMetaString(space.environmentSubstitute(getExtensionField()));
v.setLength(100, -1);
v.setOrigin(name);
row.addValueMeta(v);
}
if (getPathField() != null && getPathField().length() > 0) {
ValueMetaInterface v = new ValueMetaString(space.environmentSubstitute(getPathField()));
v.setLength(100, -1);
v.setOrigin(name);
row.addValueMeta(v);
}
if (getSizeField() != null && getSizeField().length() > 0) {
ValueMetaInterface v = new ValueMetaInteger(space.environmentSubstitute(getSizeField()));
v.setOrigin(name);
v.setLength(9);
row.addValueMeta(v);
}
if (isHiddenField() != null && isHiddenField().length() > 0) {
ValueMetaInterface v = new ValueMetaBoolean(space.environmentSubstitute(isHiddenField()));
v.setOrigin(name);
row.addValueMeta(v);
}
if (getLastModificationDateField() != null && getLastModificationDateField().length() > 0) {
ValueMetaInterface v = new ValueMetaDate(space.environmentSubstitute(getLastModificationDateField()));
v.setOrigin(name);
row.addValueMeta(v);
}
if (getUriField() != null && getUriField().length() > 0) {
ValueMetaInterface v = new ValueMetaString(space.environmentSubstitute(getUriField()));
v.setLength(100, -1);
v.setOrigin(name);
row.addValueMeta(v);
}
if (getRootUriField() != null && getRootUriField().length() > 0) {
ValueMetaInterface v = new ValueMetaString(space.environmentSubstitute(getRootUriField()));
v.setLength(100, -1);
v.setOrigin(name);
row.addValueMeta(v);
}
}
use of org.pentaho.di.core.exception.KettleStepException in project pentaho-kettle by pentaho.
the class HTTPPOST method processRow.
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
meta = (HTTPPOSTMeta) smi;
data = (HTTPPOSTData) sdi;
// Get row from input rowset & set row busy!
Object[] r = getRow();
if (r == null) {
// no more input to be expected...
setOutputDone();
return false;
}
if (first) {
first = false;
data.inputRowMeta = getInputRowMeta();
data.outputRowMeta = getInputRowMeta().clone();
meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
if (meta.isUrlInField()) {
if (Utils.isEmpty(meta.getUrlField())) {
logError(BaseMessages.getString(PKG, "HTTPPOST.Log.NoField"));
throw new KettleException(BaseMessages.getString(PKG, "HTTPPOST.Log.NoField"));
}
// cache the position of the field
if (data.indexOfUrlField < 0) {
String realUrlfieldName = environmentSubstitute(meta.getUrlField());
data.indexOfUrlField = data.inputRowMeta.indexOfValue((realUrlfieldName));
if (data.indexOfUrlField < 0) {
// The field is unreachable !
logError(BaseMessages.getString(PKG, "HTTPPOST.Log.ErrorFindingField", realUrlfieldName));
throw new KettleException(BaseMessages.getString(PKG, "HTTPPOST.Exception.ErrorFindingField", realUrlfieldName));
}
}
} else {
data.realUrl = environmentSubstitute(meta.getUrl());
}
// set body parameters
int nrargs = meta.getArgumentField().length;
if (nrargs > 0) {
data.useBodyParameters = false;
data.useHeaderParameters = false;
data.contentTypeHeaderOverwrite = false;
int nrheader = 0;
int nrbody = 0;
for (int i = 0; i < nrargs; i++) {
// split into body / header
if (meta.getArgumentHeader()[i]) {
// at least one header parameter
data.useHeaderParameters = true;
nrheader++;
} else {
// at least one body parameter
data.useBodyParameters = true;
nrbody++;
}
}
data.header_parameters_nrs = new int[nrheader];
data.headerParameters = new NameValuePair[nrheader];
data.body_parameters_nrs = new int[nrbody];
data.bodyParameters = new NameValuePair[nrbody];
int posHeader = 0;
int posBody = 0;
for (int i = 0; i < nrargs; i++) {
int fieldIndex = data.inputRowMeta.indexOfValue(meta.getArgumentField()[i]);
if (fieldIndex < 0) {
logError(BaseMessages.getString(PKG, "HTTPPOST.Log.ErrorFindingField") + meta.getArgumentField()[i] + "]");
throw new KettleStepException(BaseMessages.getString(PKG, "HTTPPOST.Exception.CouldnotFindField", meta.getArgumentField()[i]));
}
if (meta.getArgumentHeader()[i]) {
data.header_parameters_nrs[posHeader] = fieldIndex;
data.headerParameters[posHeader] = new BasicNameValuePair(environmentSubstitute(meta.getArgumentParameter()[i]), data.outputRowMeta.getString(r, data.header_parameters_nrs[posHeader]));
posHeader++;
if (CONTENT_TYPE.equalsIgnoreCase(meta.getArgumentParameter()[i])) {
// Content-type will be overwritten
data.contentTypeHeaderOverwrite = true;
}
} else {
data.body_parameters_nrs[posBody] = fieldIndex;
data.bodyParameters[posBody] = new BasicNameValuePair(environmentSubstitute(meta.getArgumentParameter()[i]), data.outputRowMeta.getString(r, data.body_parameters_nrs[posBody]));
posBody++;
}
}
}
// set query parameters
int nrQuery = meta.getQueryField().length;
if (nrQuery > 0) {
data.useQueryParameters = true;
data.query_parameters_nrs = new int[nrQuery];
data.queryParameters = new NameValuePair[nrQuery];
for (int i = 0; i < nrQuery; i++) {
data.query_parameters_nrs[i] = data.inputRowMeta.indexOfValue(meta.getQueryField()[i]);
if (data.query_parameters_nrs[i] < 0) {
logError(BaseMessages.getString(PKG, "HTTPPOST.Log.ErrorFindingField") + meta.getQueryField()[i] + "]");
throw new KettleStepException(BaseMessages.getString(PKG, "HTTPPOST.Exception.CouldnotFindField", meta.getQueryField()[i]));
}
data.queryParameters[i] = new BasicNameValuePair(environmentSubstitute(meta.getQueryParameter()[i]), data.outputRowMeta.getString(r, data.query_parameters_nrs[i]));
}
}
// set request entity?
if (!Utils.isEmpty(meta.getRequestEntity())) {
data.indexOfRequestEntity = data.inputRowMeta.indexOfValue(environmentSubstitute(meta.getRequestEntity()));
if (data.indexOfRequestEntity < 0) {
throw new KettleStepException(BaseMessages.getString(PKG, "HTTPPOST.Exception.CouldnotFindRequestEntityField", meta.getRequestEntity()));
}
}
data.realEncoding = environmentSubstitute(meta.getEncoding());
}
try {
Object[] outputRowData = callHTTPPOST(r);
// copy row to output rowset(s);
putRow(data.outputRowMeta, outputRowData);
if (checkFeedback(getLinesRead())) {
if (isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "HTTPPOST.LineNumber") + getLinesRead());
}
}
} catch (KettleException e) {
boolean sendToErrorRow = false;
String errorMessage = null;
if (getStepMeta().isDoingErrorHandling()) {
sendToErrorRow = true;
errorMessage = e.toString();
} else {
logError(BaseMessages.getString(PKG, "HTTPPOST.ErrorInStepRunning") + e.getMessage());
setErrors(1);
logError(Const.getStackTracker(e));
stopAll();
// signal end to receiver(s)
setOutputDone();
return false;
}
if (sendToErrorRow) {
// Simply add this row to the error row
putError(getInputRowMeta(), r, 1, errorMessage, null, "HTTPPOST001");
}
}
return true;
}
Aggregations