Search in sources :

Example 11 with Result

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

the class JobEntryPGPEncryptFiles method execute.

public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    List<RowMetaAndData> rows = result.getRows();
    RowMetaAndData resultRow = null;
    result.setNrErrors(1);
    result.setResult(false);
    try {
        NrErrors = 0;
        NrSuccess = 0;
        successConditionBroken = false;
        successConditionBrokenExit = false;
        limitFiles = Const.toInt(environmentSubstitute(getNrErrorsLessThan()), 10);
        if (include_subfolders) {
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.IncludeSubFoldersOn"));
            }
        }
        String MoveToFolder = environmentSubstitute(destinationFolder);
        // Get source and destination files, also wildcard
        String[] vsourcefilefolder = source_filefolder;
        String[] vuserid = userid;
        String[] vdestinationfilefolder = destination_filefolder;
        String[] vwildcard = wildcard;
        if (iffileexists.equals("move_file")) {
            if (Utils.isEmpty(MoveToFolder)) {
                logError(toString(), BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.Error.MoveToFolderMissing"));
                return result;
            }
            FileObject folder = null;
            try {
                folder = KettleVFS.getFileObject(MoveToFolder);
                if (!folder.exists()) {
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.Error.FolderMissing", MoveToFolder));
                    }
                    if (create_move_to_folder) {
                        folder.createFolder();
                    } else {
                        logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.Error.FolderMissing", MoveToFolder));
                        return result;
                    }
                }
                if (!folder.getType().equals(FileType.FOLDER)) {
                    logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.Error.NotFolder", MoveToFolder));
                    return result;
                }
            } catch (Exception e) {
                logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.Error.GettingMoveToFolder", MoveToFolder, e.getMessage()));
                return result;
            } finally {
                if (folder != null) {
                    try {
                        folder.close();
                    } catch (IOException ex) {
                    /* Ignore */
                    }
                }
            }
        }
        gpg = new GPG(environmentSubstitute(gpglocation), log);
        if (arg_from_previous) {
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.ArgFromPrevious.Found", (rows != null ? rows.size() : 0) + ""));
            }
        }
        if (arg_from_previous && rows != null) {
            for (int iteration = 0; iteration < rows.size(); iteration++) {
                // Success condition broken?
                if (successConditionBroken) {
                    if (!successConditionBrokenExit) {
                        logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Error.SuccessConditionbroken", "" + NrErrors));
                        successConditionBrokenExit = true;
                    }
                    result.setNrErrors(NrErrors);
                    displayResults();
                    return result;
                }
                resultRow = rows.get(iteration);
                // Get source and destination file names, also wildcard
                int vactionType_previous = getActionTypeByCode(resultRow.getString(0, null));
                String vsourcefilefolder_previous = resultRow.getString(1, null);
                String vwildcard_previous = environmentSubstitute(resultRow.getString(2, null));
                String vuserid_previous = resultRow.getString(3, null);
                String vdestinationfilefolder_previous = resultRow.getString(4, null);
                if (!Utils.isEmpty(vsourcefilefolder_previous) && !Utils.isEmpty(vdestinationfilefolder_previous)) {
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.ProcessingRow", vsourcefilefolder_previous, vdestinationfilefolder_previous, vwildcard_previous));
                    }
                    if (!ProcessFileFolder(vactionType_previous, vsourcefilefolder_previous, vuserid_previous, vdestinationfilefolder_previous, vwildcard_previous, parentJob, result, MoveToFolder)) {
                        // The process fail
                        // Update Errors
                        updateErrors();
                    }
                } else {
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.IgnoringRow", vsourcefilefolder[iteration], vdestinationfilefolder[iteration], vwildcard[iteration]));
                    }
                }
            }
        } else if (vsourcefilefolder != null && vdestinationfilefolder != null) {
            for (int i = 0; i < vsourcefilefolder.length && !parentJob.isStopped(); i++) {
                // Success condition broken?
                if (successConditionBroken) {
                    if (!successConditionBrokenExit) {
                        logError(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Error.SuccessConditionbroken", "" + NrErrors));
                        successConditionBrokenExit = true;
                    }
                    result.setNrErrors(NrErrors);
                    displayResults();
                    return result;
                }
                if (!Utils.isEmpty(vsourcefilefolder[i]) && !Utils.isEmpty(vdestinationfilefolder[i])) {
                    // ok we can process this file/folder
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.ProcessingRow", vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i]));
                    }
                    if (!ProcessFileFolder(action_type[i], vsourcefilefolder[i], vuserid[i], vdestinationfilefolder[i], vwildcard[i], parentJob, result, MoveToFolder)) {
                        // Update Errors
                        updateErrors();
                    }
                } else {
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobPGPEncryptFiles.Log.IgnoringRow", vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i]));
                    }
                }
            }
        }
    } catch (Exception e) {
        updateErrors();
        logError(BaseMessages.getString("JobPGPEncryptFiles.Error", e.getMessage()));
    } finally {
        if (source_filefolder != null) {
            source_filefolder = null;
        }
        if (destination_filefolder != null) {
            destination_filefolder = null;
        }
    }
    // Success Condition
    result.setNrErrors(NrErrors);
    result.setNrLinesWritten(NrSuccess);
    if (getSuccessStatus()) {
        result.setResult(true);
    }
    displayResults();
    return result;
}
Also used : RowMetaAndData(org.pentaho.di.core.RowMetaAndData) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) IOException(java.io.IOException) Result(org.pentaho.di.core.Result)

Example 12 with Result

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

the class JobEntrySendNagiosPassiveCheck method execute.

public Result execute(Result previousResult, int nr) {
    log.logBasic(BaseMessages.getString(PKG, "JobEntrySendNagiosPassiveCheck.Started", serverName));
    Result result = previousResult;
    result.setNrErrors(1);
    result.setResult(false);
    // Target
    String realServername = environmentSubstitute(serverName);
    String realPassword = Utils.resolvePassword(variables, password);
    int realPort = Const.toInt(environmentSubstitute(port), DEFAULT_PORT);
    int realResponseTimeOut = Const.toInt(environmentSubstitute(responseTimeOut), DEFAULT_RESPONSE_TIME_OUT);
    int realConnectionTimeOut = Const.toInt(environmentSubstitute(connectionTimeOut), DEFAULT_CONNECTION_TIME_OUT);
    // Sender
    String realSenderServerName = environmentSubstitute(senderServerName);
    String realSenderServiceName = environmentSubstitute(senderServiceName);
    try {
        if (Utils.isEmpty(realServername)) {
            throw new KettleException(BaseMessages.getString(PKG, "JobSendNagiosPassiveCheck.Error.TargetServerMissing"));
        }
        String realMessageString = environmentSubstitute(message);
        if (Utils.isEmpty(realMessageString)) {
            throw new KettleException(BaseMessages.getString(PKG, "JobSendNagiosPassiveCheck.Error.MessageMissing"));
        }
        Level level = Level.UNKNOWN;
        switch(getLevel()) {
            case LEVEL_TYPE_OK:
                level = Level.OK;
                break;
            case LEVEL_TYPE_CRITICAL:
                level = Level.CRITICAL;
                break;
            case LEVEL_TYPE_WARNING:
                level = Level.WARNING;
                break;
            default:
                break;
        }
        Encryption encr = Encryption.NONE;
        switch(getEncryptionMode()) {
            case ENCRYPTION_MODE_TRIPLEDES:
                encr = Encryption.TRIPLE_DES;
                break;
            case ENCRYPTION_MODE_XOR:
                encr = Encryption.XOR;
                break;
            default:
                break;
        }
        // settings
        NagiosSettingsBuilder ns = new NagiosSettingsBuilder();
        ns.withNagiosHost(realServername);
        ns.withPort(realPort);
        ns.withConnectionTimeout(realConnectionTimeOut);
        ns.withResponseTimeout(realResponseTimeOut);
        ns.withEncryption(encr);
        if (!Utils.isEmpty(realPassword)) {
            ns.withPassword(realPassword);
        } else {
            ns.withNoPassword();
        }
        // target nagios host
        NagiosSettings settings = ns.create();
        // sender
        MessagePayloadBuilder pb = new MessagePayloadBuilder();
        if (!Utils.isEmpty(realSenderServerName)) {
            pb.withHostname(realSenderServerName);
        }
        pb.withLevel(level);
        if (!Utils.isEmpty(realSenderServiceName)) {
            pb.withServiceName(realSenderServiceName);
        }
        pb.withMessage(realMessageString);
        MessagePayload payload = pb.create();
        NagiosPassiveCheckSender sender = new NagiosPassiveCheckSender(settings);
        sender.send(payload);
        result.setNrErrors(0);
        result.setResult(true);
    } catch (Exception e) {
        log.logError(BaseMessages.getString(PKG, "JobEntrySendNagiosPassiveCheck.ErrorGetting", e.toString()));
    }
    return result;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) NagiosSettingsBuilder(com.googlecode.jsendnsca.builders.NagiosSettingsBuilder) NagiosPassiveCheckSender(com.googlecode.jsendnsca.NagiosPassiveCheckSender) Level(com.googlecode.jsendnsca.Level) Encryption(com.googlecode.jsendnsca.encryption.Encryption) NagiosSettings(com.googlecode.jsendnsca.NagiosSettings) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) Result(org.pentaho.di.core.Result) MessagePayloadBuilder(com.googlecode.jsendnsca.builders.MessagePayloadBuilder) MessagePayload(com.googlecode.jsendnsca.MessagePayload)

Example 13 with Result

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

the class JobEntrySetVariables method setParentJob.

@Override
public void setParentJob(Job parentJob) {
    super.setParentJob(parentJob);
    // PDI-16387 Add a listener for recovering changed values of variables
    JobEntryListener jobListener = new JobEntryListener() {

        @Override
        public void beforeExecution(Job job, JobEntryCopy jobEntryCopy, JobEntryInterface jobEntryInterface) {
            for (String key : changedInitialVariables.keySet()) {
                setVariable(key, changedInitialVariables.get(key));
                parentJob.setVariable(key, changedInitialVariables.get(key));
            }
            changedInitialVariables.clear();
        }

        @Override
        public void afterExecution(Job job, JobEntryCopy jobEntryCopy, JobEntryInterface jobEntryInterface, Result result) {
        }
    };
    parentJob.addJobEntryListener(jobListener);
}
Also used : JobEntryCopy(org.pentaho.di.job.entry.JobEntryCopy) JobEntryInterface(org.pentaho.di.job.entry.JobEntryInterface) JobEntryListener(org.pentaho.di.job.JobEntryListener) Job(org.pentaho.di.job.Job) Result(org.pentaho.di.core.Result)

Example 14 with Result

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

the class JobEntrySimpleEval method execute.

@Override
public Result execute(Result previousResult, int nr) throws KettleException {
    Result result = previousResult;
    result.setNrErrors(1);
    result.setResult(false);
    String sourcevalue = null;
    switch(valuetype) {
        case VALUE_TYPE_FIELD:
            List<RowMetaAndData> rows = result.getRows();
            RowMetaAndData resultRow = null;
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobEntrySimpleEval.Log.ArgFromPrevious.Found", (rows != null ? rows.size() : 0) + ""));
            }
            if (rows.size() == 0) {
                rows = null;
                logError(BaseMessages.getString(PKG, "JobEntrySimpleEval.Error.NoRows"));
                return result;
            }
            // get first row
            resultRow = rows.get(0);
            String realfieldname = environmentSubstitute(fieldname);
            int indexOfField = -1;
            indexOfField = resultRow.getRowMeta().indexOfValue(realfieldname);
            if (indexOfField == -1) {
                logError(BaseMessages.getString(PKG, "JobEntrySimpleEval.Error.FieldNotExist", realfieldname));
                resultRow = null;
                rows = null;
                return result;
            }
            sourcevalue = resultRow.getString(indexOfField, null);
            if (sourcevalue == null) {
                sourcevalue = "";
            }
            resultRow = null;
            rows = null;
            break;
        case VALUE_TYPE_VARIABLE:
            if (Utils.isEmpty(variablename)) {
                logError(BaseMessages.getString(PKG, "JobEntrySimpleEval.Error.VariableMissing"));
                return result;
            }
            if (isSuccessWhenVarSet()) {
                // return variable name
                // remove specifications if needed
                String variableName = StringUtil.getVariableName(Const.NVL(getVariableName(), ""));
                // Get value, if the variable is not set, Null will be returned
                String value = getVariable(variableName);
                if (value != null) {
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobEntrySimpleEval.VariableSet", variableName));
                    }
                    result.setResult(true);
                    result.setNrErrors(0);
                    return result;
                } else {
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobEntrySimpleEval.VariableNotSet", variableName));
                    }
                    // PDI-6943: this job entry does not set errors upon evaluation, independently of the outcome of the check
                    result.setNrErrors(0);
                    return result;
                }
            }
            sourcevalue = environmentSubstitute(getVariableWithSpec());
            break;
        default:
            break;
    }
    if (isDetailed()) {
        logDetailed(BaseMessages.getString(PKG, "JobSimpleEval.Log.ValueToevaluate", sourcevalue));
    }
    boolean success = false;
    String realCompareValue = environmentSubstitute(comparevalue);
    if (realCompareValue == null) {
        realCompareValue = "";
    }
    String realMinValue = environmentSubstitute(minvalue);
    String realMaxValue = environmentSubstitute(maxvalue);
    switch(fieldtype) {
        case FIELD_TYPE_STRING:
            switch(successcondition) {
                case // equal
                SUCCESS_CONDITION_EQUAL:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    success = (sourcevalue.equals(realCompareValue));
                    if (valuetype == VALUE_TYPE_VARIABLE && !success) {
                        // make the empty value evaluate to true when compared to a not set variable
                        if (Utils.isEmpty(realCompareValue)) {
                            String variableName = StringUtil.getVariableName(variablename);
                            if (System.getProperty(variableName) == null) {
                                success = true;
                            }
                        }
                    }
                    break;
                case // different
                SUCCESS_CONDITION_DIFFERENT:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    success = (!sourcevalue.equals(realCompareValue));
                    break;
                case // contains
                SUCCESS_CONDITION_CONTAINS:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    success = (sourcevalue.contains(realCompareValue));
                    break;
                case // not contains
                SUCCESS_CONDITION_NOT_CONTAINS:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    success = (!sourcevalue.contains(realCompareValue));
                    break;
                case // starts with
                SUCCESS_CONDITION_START_WITH:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    success = (sourcevalue.startsWith(realCompareValue));
                    break;
                case // not start with
                SUCCESS_CONDITION_NOT_START_WITH:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    success = (!sourcevalue.startsWith(realCompareValue));
                    break;
                case // ends with
                SUCCESS_CONDITION_END_WITH:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    success = (sourcevalue.endsWith(realCompareValue));
                    break;
                case // not ends with
                SUCCESS_CONDITION_NOT_END_WITH:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    success = (!sourcevalue.endsWith(realCompareValue));
                    break;
                case // regexp
                SUCCESS_CONDITION_REGEX:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    success = (Pattern.compile(realCompareValue).matcher(sourcevalue).matches());
                    break;
                case // in list
                SUCCESS_CONDITION_IN_LIST:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    realCompareValue = Const.NVL(realCompareValue, "");
                    String[] parts = realCompareValue.split(",");
                    for (int i = 0; i < parts.length && !success; i++) {
                        success = (sourcevalue.equals(parts[i].trim()));
                    }
                    break;
                case // not in list
                SUCCESS_CONDITION_NOT_IN_LIST:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    realCompareValue = Const.NVL(realCompareValue, "");
                    parts = realCompareValue.split(",");
                    success = true;
                    for (int i = 0; i < parts.length && success; i++) {
                        success = !(sourcevalue.equals(parts[i].trim()));
                    }
                    break;
                default:
                    break;
            }
            break;
        case FIELD_TYPE_NUMBER:
            double valuenumber;
            try {
                valuenumber = Double.parseDouble(sourcevalue);
            } catch (Exception e) {
                logError(BaseMessages.getString(PKG, "JobEntrySimpleEval.Error.UnparsableNumber", sourcevalue, e.getMessage()));
                return result;
            }
            double valuecompare;
            switch(successnumbercondition) {
                case // equal
                SUCCESS_NUMBER_CONDITION_EQUAL:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    try {
                        valuecompare = Double.parseDouble(realCompareValue);
                    } catch (Exception e) {
                        logError(BaseMessages.getString(PKG, "JobEntrySimpleEval.Error.UnparsableNumber", realCompareValue, e.getMessage()));
                        return result;
                    }
                    success = (valuenumber == valuecompare);
                    break;
                case // different
                SUCCESS_NUMBER_CONDITION_DIFFERENT:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    try {
                        valuecompare = Double.parseDouble(realCompareValue);
                    } catch (Exception e) {
                        logError(BaseMessages.getString(PKG, "JobEntrySimpleEval.Error.UnparsableNumber", realCompareValue, e.getMessage()));
                        return result;
                    }
                    success = (valuenumber != valuecompare);
                    break;
                case // smaller
                SUCCESS_NUMBER_CONDITION_SMALLER:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    try {
                        valuecompare = Double.parseDouble(realCompareValue);
                    } catch (Exception e) {
                        logError(BaseMessages.getString(PKG, "JobEntrySimpleEval.Error.UnparsableNumber", realCompareValue, e.getMessage()));
                        return result;
                    }
                    success = (valuenumber < valuecompare);
                    break;
                case // smaller or equal
                SUCCESS_NUMBER_CONDITION_SMALLER_EQUAL:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    try {
                        valuecompare = Double.parseDouble(realCompareValue);
                    } catch (Exception e) {
                        logError(BaseMessages.getString(PKG, "JobEntrySimpleEval.Error.UnparsableNumber", realCompareValue, e.getMessage()));
                        return result;
                    }
                    success = (valuenumber <= valuecompare);
                    break;
                case // greater
                SUCCESS_NUMBER_CONDITION_GREATER:
                    try {
                        valuecompare = Double.parseDouble(realCompareValue);
                    } catch (Exception e) {
                        logError(BaseMessages.getString(PKG, "JobEntrySimpleEval.Error.UnparsableNumber", realCompareValue, e.getMessage()));
                        return result;
                    }
                    success = (valuenumber > valuecompare);
                    break;
                case // greater or equal
                SUCCESS_NUMBER_CONDITION_GREATER_EQUAL:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    try {
                        valuecompare = Double.parseDouble(realCompareValue);
                    } catch (Exception e) {
                        logError(BaseMessages.getString(PKG, "JobEntrySimpleEval.Error.UnparsableNumber", realCompareValue, e.getMessage()));
                        return result;
                    }
                    success = (valuenumber >= valuecompare);
                    break;
                case // between min and max
                SUCCESS_NUMBER_CONDITION_BETWEEN:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValues", realMinValue, realMaxValue));
                    }
                    double valuemin;
                    try {
                        valuemin = Double.parseDouble(realMinValue);
                    } catch (Exception e) {
                        logError(BaseMessages.getString(PKG, "JobEntrySimpleEval.Error.UnparsableNumber", realMinValue, e.getMessage()));
                        return result;
                    }
                    double valuemax;
                    try {
                        valuemax = Double.parseDouble(realMaxValue);
                    } catch (Exception e) {
                        logError(BaseMessages.getString(PKG, "JobEntrySimpleEval.Error.UnparsableNumber", realMaxValue, e.getMessage()));
                        return result;
                    }
                    if (valuemin >= valuemax) {
                        logError(BaseMessages.getString(PKG, "JobEntrySimpleEval.Error.IncorrectNumbers", realMinValue, realMaxValue));
                        return result;
                    }
                    success = (valuenumber >= valuemin && valuenumber <= valuemax);
                    break;
                case // in list
                SUCCESS_NUMBER_CONDITION_IN_LIST:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    String[] parts = realCompareValue.split(",");
                    for (int i = 0; i < parts.length && !success; i++) {
                        try {
                            valuecompare = Double.parseDouble(parts[i]);
                        } catch (Exception e) {
                            logError(toString(), BaseMessages.getString(PKG, "JobEntrySimpleEval.Error.UnparsableNumber", parts[i], e.getMessage()));
                            return result;
                        }
                        success = (valuenumber == valuecompare);
                    }
                    break;
                case // not in list
                SUCCESS_NUMBER_CONDITION_NOT_IN_LIST:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    realCompareValue = Const.NVL(realCompareValue, "");
                    parts = realCompareValue.split(",");
                    success = true;
                    for (int i = 0; i < parts.length && success; i++) {
                        try {
                            valuecompare = Double.parseDouble(parts[i]);
                        } catch (Exception e) {
                            logError(toString(), BaseMessages.getString(PKG, "JobEntrySimpleEval.Error.UnparsableNumber", parts[i], e.getMessage()));
                            return result;
                        }
                        success = (valuenumber != valuecompare);
                    }
                    break;
                default:
                    break;
            }
            break;
        case FIELD_TYPE_DATE_TIME:
            String realMask = environmentSubstitute(mask);
            SimpleDateFormat df = new SimpleDateFormat();
            if (!Utils.isEmpty(realMask)) {
                df.applyPattern(realMask);
            }
            Date datevalue = null;
            try {
                datevalue = convertToDate(sourcevalue, realMask, df);
            } catch (Exception e) {
                logError(e.getMessage());
                return result;
            }
            Date datecompare;
            switch(successnumbercondition) {
                case // equal
                SUCCESS_NUMBER_CONDITION_EQUAL:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    try {
                        datecompare = convertToDate(realCompareValue, realMask, df);
                    } catch (Exception e) {
                        logError(e.getMessage());
                        return result;
                    }
                    success = (datevalue.equals(datecompare));
                    break;
                case // different
                SUCCESS_NUMBER_CONDITION_DIFFERENT:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    try {
                        datecompare = convertToDate(realCompareValue, realMask, df);
                    } catch (Exception e) {
                        logError(e.getMessage());
                        return result;
                    }
                    success = (!datevalue.equals(datecompare));
                    break;
                case // smaller
                SUCCESS_NUMBER_CONDITION_SMALLER:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    try {
                        datecompare = convertToDate(realCompareValue, realMask, df);
                    } catch (Exception e) {
                        logError(e.getMessage());
                        return result;
                    }
                    success = (datevalue.before(datecompare));
                    break;
                case // smaller or equal
                SUCCESS_NUMBER_CONDITION_SMALLER_EQUAL:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    try {
                        datecompare = convertToDate(realCompareValue, realMask, df);
                    } catch (Exception e) {
                        logError(e.getMessage());
                        return result;
                    }
                    success = (datevalue.before(datecompare) || datevalue.equals(datecompare));
                    break;
                case // greater
                SUCCESS_NUMBER_CONDITION_GREATER:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    try {
                        datecompare = convertToDate(realCompareValue, realMask, df);
                    } catch (Exception e) {
                        logError(e.getMessage());
                        return result;
                    }
                    success = (datevalue.after(datecompare));
                    break;
                case // greater or equal
                SUCCESS_NUMBER_CONDITION_GREATER_EQUAL:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    try {
                        datecompare = convertToDate(realCompareValue, realMask, df);
                    } catch (Exception e) {
                        logError(e.getMessage());
                        return result;
                    }
                    success = (datevalue.after(datecompare) || datevalue.equals(datecompare));
                    break;
                case // between min and max
                SUCCESS_NUMBER_CONDITION_BETWEEN:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValues", realMinValue, realMaxValue));
                    }
                    Date datemin;
                    try {
                        datemin = convertToDate(realMinValue, realMask, df);
                    } catch (Exception e) {
                        logError(e.getMessage());
                        return result;
                    }
                    Date datemax;
                    try {
                        datemax = convertToDate(realMaxValue, realMask, df);
                    } catch (Exception e) {
                        logError(e.getMessage());
                        return result;
                    }
                    if (datemin.after(datemax) || datemin.equals(datemax)) {
                        logError(BaseMessages.getString(PKG, "JobEntrySimpleEval.Error.IncorrectDates", realMinValue, realMaxValue));
                        return result;
                    }
                    success = ((datevalue.after(datemin) || datevalue.equals(datemin)) && (datevalue.before(datemax) || datevalue.equals(datemax)));
                    break;
                case // in list
                SUCCESS_NUMBER_CONDITION_IN_LIST:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    String[] parts = realCompareValue.split(",");
                    for (int i = 0; i < parts.length && !success; i++) {
                        try {
                            datecompare = convertToDate(realCompareValue, realMask, df);
                        } catch (Exception e) {
                            logError(toString(), e.getMessage());
                            return result;
                        }
                        success = (datevalue.equals(datecompare));
                    }
                    break;
                case // not in list
                SUCCESS_NUMBER_CONDITION_NOT_IN_LIST:
                    if (isDebug()) {
                        logDebug(BaseMessages.getString(PKG, "JobSimpleEval.Log.CompareWithValue", sourcevalue, realCompareValue));
                    }
                    realCompareValue = Const.NVL(realCompareValue, "");
                    parts = realCompareValue.split(",");
                    success = true;
                    for (int i = 0; i < parts.length && success; i++) {
                        try {
                            datecompare = convertToDate(realCompareValue, realMask, df);
                        } catch (Exception e) {
                            logError(toString(), e.getMessage());
                            return result;
                        }
                        success = (!datevalue.equals(datecompare));
                    }
                    break;
                default:
                    break;
            }
            df = null;
            break;
        case FIELD_TYPE_BOOLEAN:
            boolean valuebool;
            try {
                valuebool = ValueMetaString.convertStringToBoolean(sourcevalue);
            } catch (Exception e) {
                logError(BaseMessages.getString(PKG, "JobEntrySimpleEval.Error.UnparsableBoolean", sourcevalue, e.getMessage()));
                return result;
            }
            switch(successbooleancondition) {
                case // false
                SUCCESS_BOOLEAN_CONDITION_FALSE:
                    success = (!valuebool);
                    break;
                case // true
                SUCCESS_BOOLEAN_CONDITION_TRUE:
                    success = (valuebool);
                    break;
                default:
                    break;
            }
            break;
        default:
            break;
    }
    result.setResult(success);
    // PDI-6943: this job entry does not set errors upon evaluation, independently of the outcome of the check
    result.setNrErrors(0);
    return result;
}
Also used : RowMetaAndData(org.pentaho.di.core.RowMetaAndData) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) SimpleDateFormat(java.text.SimpleDateFormat) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) Date(java.util.Date) Result(org.pentaho.di.core.Result)

Example 15 with Result

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

the class JobEntrySQL method execute.

public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    if (connection != null) {
        Database db = new Database(this, connection);
        FileObject SQLfile = null;
        db.shareVariablesWith(this);
        try {
            String theSQL = null;
            db.connect(parentJob.getTransactionId(), null);
            if (sqlfromfile) {
                if (sqlfilename == null) {
                    throw new KettleDatabaseException(BaseMessages.getString(PKG, "JobSQL.NoSQLFileSpecified"));
                }
                try {
                    String realfilename = environmentSubstitute(sqlfilename);
                    SQLfile = KettleVFS.getFileObject(realfilename, this);
                    if (!SQLfile.exists()) {
                        logError(BaseMessages.getString(PKG, "JobSQL.SQLFileNotExist", realfilename));
                        throw new KettleDatabaseException(BaseMessages.getString(PKG, "JobSQL.SQLFileNotExist", realfilename));
                    }
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobSQL.SQLFileExists", realfilename));
                    }
                    InputStream IS = KettleVFS.getInputStream(SQLfile);
                    try {
                        InputStreamReader BIS = new InputStreamReader(new BufferedInputStream(IS, 500));
                        StringBuilder lineSB = new StringBuilder(256);
                        lineSB.setLength(0);
                        BufferedReader buff = new BufferedReader(BIS);
                        String sLine = null;
                        theSQL = Const.CR;
                        while ((sLine = buff.readLine()) != null) {
                            if (Utils.isEmpty(sLine)) {
                                theSQL = theSQL + Const.CR;
                            } else {
                                theSQL = theSQL + Const.CR + sLine;
                            }
                        }
                    } finally {
                        IS.close();
                    }
                } catch (Exception e) {
                    throw new KettleDatabaseException(BaseMessages.getString(PKG, "JobSQL.ErrorRunningSQLfromFile"), e);
                }
            } else {
                theSQL = sql;
            }
            if (!Utils.isEmpty(theSQL)) {
                // let it run
                if (useVariableSubstitution) {
                    theSQL = environmentSubstitute(theSQL);
                }
                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobSQL.Log.SQlStatement", theSQL));
                }
                if (sendOneStatement) {
                    db.execStatement(theSQL);
                } else {
                    db.execStatements(theSQL);
                }
            }
        } catch (KettleDatabaseException je) {
            result.setNrErrors(1);
            logError(BaseMessages.getString(PKG, "JobSQL.ErrorRunJobEntry", je.getMessage()));
        } finally {
            db.disconnect();
            if (SQLfile != null) {
                try {
                    SQLfile.close();
                } catch (Exception e) {
                // Ignore errors
                }
            }
        }
    } else {
        result.setNrErrors(1);
        logError(BaseMessages.getString(PKG, "JobSQL.NoDatabaseConnection"));
    }
    if (result.getNrErrors() == 0) {
        result.setResult(true);
    } else {
        result.setResult(false);
    }
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Database(org.pentaho.di.core.database.Database) BufferedReader(java.io.BufferedReader) FileObject(org.apache.commons.vfs2.FileObject) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) Result(org.pentaho.di.core.Result)

Aggregations

Result (org.pentaho.di.core.Result)192 Test (org.junit.Test)75 KettleException (org.pentaho.di.core.exception.KettleException)75 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)64 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)57 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)40 FileObject (org.apache.commons.vfs2.FileObject)34 Job (org.pentaho.di.job.Job)32 IOException (java.io.IOException)24 ResultFile (org.pentaho.di.core.ResultFile)20 File (java.io.File)17 ArrayList (java.util.ArrayList)16 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)15 KettleExtensionPoint (org.pentaho.di.core.extension.KettleExtensionPoint)12 Pattern (java.util.regex.Pattern)10 KettleFileException (org.pentaho.di.core.exception.KettleFileException)10 Database (org.pentaho.di.core.database.Database)9 Date (java.util.Date)8 Trans (org.pentaho.di.trans.Trans)8 Matcher (java.util.regex.Matcher)7