Search in sources :

Example 96 with KettleStepException

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

the class SQLFileOutput method init.

public boolean init(StepMetaInterface smi, StepDataInterface sdi) {
    meta = (SQLFileOutputMeta) smi;
    data = (SQLFileOutputData) sdi;
    if (super.init(smi, sdi)) {
        try {
            if (meta.getDatabaseMeta() == null) {
                throw new KettleStepException("The connection is not defined (empty)");
            }
            if (meta.getDatabaseMeta() == null) {
                logError(BaseMessages.getString(PKG, "SQLFileOutput.Init.ConnectionMissing", getStepname()));
                return false;
            }
            data.db = new Database(this, meta.getDatabaseMeta());
            data.db.shareVariablesWith(this);
            logBasic("Connected to database [" + meta.getDatabaseMeta() + "]");
            if (meta.isCreateParentFolder()) {
                // Check for parent folder
                FileObject parentfolder = null;
                try {
                    // Get parent folder
                    String filename = environmentSubstitute(meta.getFileName());
                    parentfolder = KettleVFS.getFileObject(filename, getTransMeta()).getParent();
                    if (!parentfolder.exists()) {
                        log.logBasic("Folder parent", "Folder parent " + parentfolder.getName() + " does not exist !");
                        parentfolder.createFolder();
                        log.logBasic("Folder parent", "Folder parent was created.");
                    }
                } catch (Exception e) {
                    logError("Couldn't created parent folder " + parentfolder.getName());
                    setErrors(1L);
                    stopAll();
                } finally {
                    if (parentfolder != null) {
                        try {
                            parentfolder.close();
                        } catch (Exception ex) {
                        /* Ignore */
                        }
                    }
                }
            }
            if (!meta.isDoNotOpenNewFileInit()) {
                if (!openNewFile()) {
                    logError("Couldn't open file [" + buildFilename() + "]");
                    setErrors(1L);
                    stopAll();
                }
            }
            tableName = environmentSubstitute(meta.getTablename());
            schemaName = environmentSubstitute(meta.getSchemaName());
            if (Utils.isEmpty(tableName)) {
                throw new KettleStepException("The tablename is not defined (empty)");
            }
            schemaTable = data.db.getDatabaseMeta().getQuotedSchemaTableCombination(schemaName, tableName);
        } catch (Exception e) {
            logError("An error occurred intialising this step: " + e.getMessage());
            stopAll();
            setErrors(1);
        }
        return true;
    }
    return false;
}
Also used : KettleStepException(org.pentaho.di.core.exception.KettleStepException) Database(org.pentaho.di.core.database.Database) FileObject(org.apache.commons.vfs2.FileObject) KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException)

Example 97 with KettleStepException

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

the class TableInputMeta method getFields.

public void getFields(RowMetaInterface row, String origin, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space, Repository repository, IMetaStore metaStore) throws KettleStepException {
    if (databaseMeta == null) {
        // TODO: throw an exception here
        return;
    }
    boolean param = false;
    Database db = getDatabase();
    // keep track of it for canceling purposes...
    super.databases = new Database[] { db };
    // First try without connecting to the database... (can be S L O W)
    String sNewSQL = sql;
    if (isVariableReplacementActive()) {
        sNewSQL = db.environmentSubstitute(sql);
        if (space != null) {
            sNewSQL = space.environmentSubstitute(sNewSQL);
        }
    }
    RowMetaInterface add = null;
    try {
        add = db.getQueryFields(sNewSQL, param);
    } catch (KettleDatabaseException dbe) {
        throw new KettleStepException("Unable to get queryfields for SQL: " + Const.CR + sNewSQL, dbe);
    }
    if (add != null) {
        for (int i = 0; i < add.size(); i++) {
            ValueMetaInterface v = add.getValueMeta(i);
            v.setOrigin(origin);
        }
        row.addRowMeta(add);
    } else {
        try {
            db.connect();
            RowMetaInterface paramRowMeta = null;
            Object[] paramData = null;
            StreamInterface infoStream = getStepIOMeta().getInfoStreams().get(0);
            if (!Utils.isEmpty(infoStream.getStepname())) {
                param = true;
                if (info.length >= 0 && info[0] != null) {
                    paramRowMeta = info[0];
                    paramData = RowDataUtil.allocateRowData(paramRowMeta.size());
                }
            }
            add = db.getQueryFields(sNewSQL, param, paramRowMeta, paramData);
            if (add == null) {
                return;
            }
            for (int i = 0; i < add.size(); i++) {
                ValueMetaInterface v = add.getValueMeta(i);
                v.setOrigin(origin);
            }
            row.addRowMeta(add);
        } catch (KettleException ke) {
            throw new KettleStepException("Unable to get queryfields for SQL: " + Const.CR + sNewSQL, ke);
        } finally {
            db.disconnect();
        }
    }
    if (isLazyConversionActive()) {
        for (int i = 0; i < row.size(); i++) {
            ValueMetaInterface v = row.getValueMeta(i);
            try {
                if (v.getType() == ValueMetaInterface.TYPE_STRING) {
                    ValueMetaInterface storageMeta = ValueMetaFactory.cloneValueMeta(v);
                    storageMeta.setStorageType(ValueMetaInterface.STORAGE_TYPE_NORMAL);
                    v.setStorageMetadata(storageMeta);
                    v.setStorageType(ValueMetaInterface.STORAGE_TYPE_BINARY_STRING);
                }
            } catch (KettlePluginException e) {
                throw new KettleStepException("Unable to clone meta for lazy conversion: " + Const.CR + v, e);
            }
        }
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) KettlePluginException(org.pentaho.di.core.exception.KettlePluginException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) Database(org.pentaho.di.core.database.Database) StreamInterface(org.pentaho.di.trans.step.errorhandling.StreamInterface)

Example 98 with KettleStepException

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

the class TableOutput method processRow.

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    meta = (TableOutputMeta) smi;
    data = (TableOutputData) sdi;
    // this also waits for a previous step to be finished.
    Object[] r = getRow();
    if (r == null) {
        // truncate the table if there are no rows at all coming into this step
        if (first && meta.truncateTable()) {
            truncateTable();
        }
        return false;
    }
    if (first) {
        first = false;
        if (meta.truncateTable()) {
            truncateTable();
        }
        data.outputRowMeta = getInputRowMeta().clone();
        meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
        if (!meta.specifyFields()) {
            // Just take the input row
            data.insertRowMeta = getInputRowMeta().clone();
        } else {
            data.insertRowMeta = new RowMeta();
            // 
            // Cache the position of the compare fields in Row row
            // 
            data.valuenrs = new int[meta.getFieldDatabase().length];
            for (int i = 0; i < meta.getFieldDatabase().length; i++) {
                data.valuenrs[i] = getInputRowMeta().indexOfValue(meta.getFieldStream()[i]);
                if (data.valuenrs[i] < 0) {
                    throw new KettleStepException(BaseMessages.getString(PKG, "TableOutput.Exception.FieldRequired", meta.getFieldStream()[i]));
                }
            }
            for (int i = 0; i < meta.getFieldDatabase().length; i++) {
                ValueMetaInterface insValue = getInputRowMeta().searchValueMeta(meta.getFieldStream()[i]);
                if (insValue != null) {
                    ValueMetaInterface insertValue = insValue.clone();
                    insertValue.setName(meta.getFieldDatabase()[i]);
                    data.insertRowMeta.addValueMeta(insertValue);
                } else {
                    throw new KettleStepException(BaseMessages.getString(PKG, "TableOutput.Exception.FailedToFindField", meta.getFieldStream()[i]));
                }
            }
        }
    }
    try {
        Object[] outputRowData = writeToTable(getInputRowMeta(), r);
        if (outputRowData != null) {
            // in case we want it go further...
            putRow(data.outputRowMeta, outputRowData);
            incrementLinesOutput();
        }
        if (checkFeedback(getLinesRead())) {
            if (log.isBasic()) {
                logBasic("linenr " + getLinesRead());
            }
        }
    } catch (KettleException e) {
        logError("Because of an error, this step can't continue: ", e);
        setErrors(1);
        stopAll();
        // signal end to receiver(s)
        setOutputDone();
        return false;
    }
    return true;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) RowMeta(org.pentaho.di.core.row.RowMeta) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 99 with KettleStepException

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

the class TextFileInputMeta method getFields.

@Override
public void getFields(RowMetaInterface row, String name, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space, Repository repository, IMetaStore metaStore) throws KettleStepException {
    if (!isPassingThruFields()) {
        // all incoming fields are not transmitted !
        row.clear();
    } else {
        if (info != null) {
            boolean found = false;
            for (int i = 0; i < info.length && !found; i++) {
                if (info[i] != null) {
                    row.mergeRowMeta(info[i]);
                    found = true;
                }
            }
        }
    }
    for (int i = 0; i < inputFields.length; i++) {
        TextFileInputField field = inputFields[i];
        int type = field.getType();
        if (type == ValueMetaInterface.TYPE_NONE) {
            type = ValueMetaInterface.TYPE_STRING;
        }
        try {
            ValueMetaInterface v = ValueMetaFactory.createValueMeta(field.getName(), type);
            v.setLength(field.getLength());
            v.setPrecision(field.getPrecision());
            v.setOrigin(name);
            v.setConversionMask(field.getFormat());
            v.setDecimalSymbol(field.getDecimalSymbol());
            v.setGroupingSymbol(field.getGroupSymbol());
            v.setCurrencySymbol(field.getCurrencySymbol());
            v.setDateFormatLenient(dateFormatLenient);
            v.setDateFormatLocale(dateFormatLocale);
            v.setTrimType(field.getTrimType());
            row.addValueMeta(v);
        } catch (Exception e) {
            throw new KettleStepException(e);
        }
    }
    if (errorIgnored) {
        if (errorCountField != null && errorCountField.length() > 0) {
            ValueMetaInterface v = new ValueMetaInteger(errorCountField);
            v.setLength(ValueMetaInterface.DEFAULT_INTEGER_LENGTH, 0);
            v.setOrigin(name);
            row.addValueMeta(v);
        }
        if (errorFieldsField != null && errorFieldsField.length() > 0) {
            ValueMetaInterface v = new ValueMetaString(errorFieldsField);
            v.setOrigin(name);
            row.addValueMeta(v);
        }
        if (errorTextField != null && errorTextField.length() > 0) {
            ValueMetaInterface v = new ValueMetaString(errorTextField);
            v.setOrigin(name);
            row.addValueMeta(v);
        }
    }
    if (includeFilename) {
        ValueMetaInterface v = new ValueMetaString(filenameField);
        v.setLength(100);
        v.setOrigin(name);
        row.addValueMeta(v);
    }
    if (includeRowNumber) {
        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(getRootUriField());
        v.setLength(100, -1);
        v.setOrigin(name);
        row.addValueMeta(v);
    }
}
Also used : ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) KettleStepException(org.pentaho.di.core.exception.KettleStepException) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger) ValueMetaBoolean(org.pentaho.di.core.row.value.ValueMetaBoolean) ValueMetaDate(org.pentaho.di.core.row.value.ValueMetaDate) KettleException(org.pentaho.di.core.exception.KettleException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 100 with KettleStepException

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

the class TextFileOutput method writeRow.

public void writeRow(RowMetaInterface rowMeta, Object[] r) throws KettleStepException {
    try {
        if (meta.getOutputFields() == null || meta.getOutputFields().length == 0) {
            /*
         * Write all values in stream to text file.
         */
            for (int i = 0; i < rowMeta.size(); i++) {
                if (i > 0 && data.binarySeparator.length > 0) {
                    data.writer.write(data.binarySeparator);
                }
                ValueMetaInterface v = rowMeta.getValueMeta(i);
                Object valueData = r[i];
                // no special null value default was specified since no fields are specified at all
                // As such, we pass null
                // 
                writeField(v, valueData, null);
            }
            data.writer.write(data.binaryNewline);
        } else {
            /*
         * Only write the fields specified!
         */
            for (int i = 0; i < meta.getOutputFields().length; i++) {
                if (i > 0 && data.binarySeparator.length > 0) {
                    data.writer.write(data.binarySeparator);
                }
                ValueMetaInterface v = rowMeta.getValueMeta(data.fieldnrs[i]);
                Object valueData = r[data.fieldnrs[i]];
                writeField(v, valueData, data.binaryNullValue[i]);
            }
            data.writer.write(data.binaryNewline);
        }
        incrementLinesOutput();
    } catch (Exception e) {
        throw new KettleStepException("Error writing line", e);
    }
}
Also used : KettleStepException(org.pentaho.di.core.exception.KettleStepException) FileObject(org.apache.commons.vfs2.FileObject) KettleException(org.pentaho.di.core.exception.KettleException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) IOException(java.io.IOException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Aggregations

KettleStepException (org.pentaho.di.core.exception.KettleStepException)235 KettleException (org.pentaho.di.core.exception.KettleException)139 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)103 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)97 RowMeta (org.pentaho.di.core.row.RowMeta)51 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)44 ValueMetaInteger (org.pentaho.di.core.row.value.ValueMetaInteger)27 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)27 KettleValueException (org.pentaho.di.core.exception.KettleValueException)26 IOException (java.io.IOException)23 RowAdapter (org.pentaho.di.trans.step.RowAdapter)21 ArrayList (java.util.ArrayList)20 StepMeta (org.pentaho.di.trans.step.StepMeta)19 RowSet (org.pentaho.di.core.RowSet)18 Database (org.pentaho.di.core.database.Database)15 KettlePluginException (org.pentaho.di.core.exception.KettlePluginException)13 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)13 FormAttachment (org.eclipse.swt.layout.FormAttachment)12 FormData (org.eclipse.swt.layout.FormData)12 Button (org.eclipse.swt.widgets.Button)12