Search in sources :

Example 6 with ResultFile

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

the class JobEntryCopyMoveResultFilenames method processFile.

private boolean processFile(FileObject sourcefile, String destinationFolder, Result result, Job parentJob, boolean deleteFile) {
    boolean retval = false;
    try {
        if (deleteFile) {
            // delete file
            if (sourcefile.delete()) {
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.log.DeletedFile", sourcefile.toString()));
                }
                // Remove source file from result files list
                result.getResultFiles().remove(sourcefile.toString());
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.RemovedFileFromResult", sourcefile.toString()));
                }
            } else {
                logError(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.CanNotDeletedFile", sourcefile.toString()));
            }
        } else {
            // return destination short filename
            String shortfilename = getDestinationFilename(sourcefile.getName().getBaseName());
            // build full destination filename
            String destinationFilename = destinationFolder + Const.FILE_SEPARATOR + shortfilename;
            FileObject destinationfile = KettleVFS.getFileObject(destinationFilename, this);
            boolean filexists = destinationfile.exists();
            if (filexists) {
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.Log.FileExists", destinationFilename));
                }
            }
            if ((!filexists) || (filexists && isOverwriteFile())) {
                if (getAction().equals("copy")) {
                    // Copy file
                    FileUtil.copyContent(sourcefile, destinationfile);
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.log.CopiedFile", sourcefile.toString(), destinationFolder));
                    }
                } else {
                    // Move file
                    sourcefile.moveTo(destinationfile);
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.log.MovedFile", sourcefile.toString(), destinationFolder));
                    }
                }
                if (isRemovedSourceFilename()) {
                    // Remove source file from result files list
                    result.getResultFiles().remove(sourcefile.toString());
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.RemovedFileFromResult", sourcefile.toString()));
                    }
                }
                if (isAddDestinationFilename()) {
                    // Add destination filename to Resultfilenames ...
                    ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject(destinationfile.toString(), this), parentJob.getJobname(), toString());
                    result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.AddedFileToResult", destinationfile.toString()));
                    }
                }
            }
        }
        retval = true;
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.Log.ErrorProcessing", e.toString()));
    }
    return retval;
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) ResultFile(org.pentaho.di.core.ResultFile) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException)

Example 7 with ResultFile

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

the class JobEntryDeleteResultFilenames method execute.

public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    result.setResult(false);
    if (previousResult != null) {
        try {
            int size = previousResult.getResultFiles().size();
            if (log.isBasic()) {
                logBasic(BaseMessages.getString(PKG, "JobEntryDeleteResultFilenames.log.FilesFound", "" + size));
            }
            if (!specifywildcard) {
                // Delete all files
                previousResult.getResultFiles().clear();
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobEntryDeleteResultFilenames.log.DeletedFiles", "" + size));
                }
            } else {
                List<ResultFile> resultFiles = result.getResultFilesList();
                if (resultFiles != null && resultFiles.size() > 0) {
                    for (Iterator<ResultFile> it = resultFiles.iterator(); it.hasNext() && !parentJob.isStopped(); ) {
                        ResultFile resultFile = it.next();
                        FileObject file = resultFile.getFile();
                        if (file != null && file.exists()) {
                            if (CheckFileWildcard(file.getName().getBaseName(), environmentSubstitute(wildcard), true) && !CheckFileWildcard(file.getName().getBaseName(), environmentSubstitute(wildcardexclude), false)) {
                                // Remove file from result files list
                                result.getResultFiles().remove(resultFile.getFile().toString());
                                if (log.isDetailed()) {
                                    logDetailed(BaseMessages.getString(PKG, "JobEntryDeleteResultFilenames.log.DeletedFile", file.toString()));
                                }
                            }
                        }
                    }
                }
            }
            result.setResult(true);
        } catch (Exception e) {
            logError(BaseMessages.getString(PKG, "JobEntryDeleteResultFilenames.Error", e.toString()));
        }
    }
    return result;
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) ResultFile(org.pentaho.di.core.ResultFile) 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)

Example 8 with ResultFile

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

the class JobEntryDosToUnix method addFileToResultFilenames.

private void addFileToResultFilenames(FileObject fileaddentry, Result result, Job parentJob) {
    try {
        ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, fileaddentry, parentJob.getJobname(), toString());
        result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
        if (isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobDosToUnix.Log.FileAddedToResultFilesName", fileaddentry));
        }
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobDosToUnix.Error.AddingToFilenameResult", fileaddentry.toString(), e.getMessage()));
    }
}
Also used : ResultFile(org.pentaho.di.core.ResultFile) 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)

Example 9 with ResultFile

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

the class JobEntryEvalFilesMetrics method execute.

public Result execute(Result previousResult, int nr) throws KettleException {
    Result result = previousResult;
    result.setNrErrors(1);
    result.setResult(false);
    List<RowMetaAndData> rows = result.getRows();
    RowMetaAndData resultRow = null;
    // Set Embedded NamedCluter MetatStore Provider Key so that it can be passed to VFS
    if (parentJobMeta.getNamedClusterEmbedManager() != null) {
        parentJobMeta.getNamedClusterEmbedManager().passEmbeddedMetastoreKey(this, parentJobMeta.getEmbeddedMetastoreProviderKey());
    }
    try {
        initMetrics();
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Error.Init", e.toString()));
        return result;
    }
    // Get source and destination files, also wildcard
    String[] vsourcefilefolder = sourceFileFolder;
    String[] vwildcard = sourceWildcard;
    String[] vincludeSubFolders = sourceIncludeSubfolders;
    switch(getSourceFiles()) {
        case SOURCE_FILES_PREVIOUS_RESULT:
            // Filenames are retrieved from previous result rows
            String realResultFieldFile = environmentSubstitute(getResultFieldFile());
            String realResultFieldWildcard = environmentSubstitute(getResultFieldWildcard());
            String realResultFieldIncluseSubfolders = environmentSubstitute(getResultFieldIncludeSubfolders());
            int indexOfResultFieldFile = -1;
            if (Utils.isEmpty(realResultFieldFile)) {
                logError(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Error.ResultFieldsFileMissing"));
                return result;
            }
            int indexOfResultFieldWildcard = -1;
            int indexOfResultFieldIncludeSubfolders = -1;
            // as such we must get rows
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Log.ArgFromPrevious.Found", (rows != null ? rows.size() : 0) + ""));
            }
            if (rows != null && rows.size() > 0) {
                // We get rows
                RowMetaAndData firstRow = rows.get(0);
                indexOfResultFieldFile = firstRow.getRowMeta().indexOfValue(realResultFieldFile);
                if (indexOfResultFieldFile == -1) {
                    logError(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Error.CanNotFindField", realResultFieldFile));
                    return result;
                }
                if (!Utils.isEmpty(realResultFieldWildcard)) {
                    indexOfResultFieldWildcard = firstRow.getRowMeta().indexOfValue(realResultFieldWildcard);
                    if (indexOfResultFieldWildcard == -1) {
                        logError(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Error.CanNotFindField", realResultFieldWildcard));
                        return result;
                    }
                }
                if (!Utils.isEmpty(realResultFieldIncluseSubfolders)) {
                    indexOfResultFieldIncludeSubfolders = firstRow.getRowMeta().indexOfValue(realResultFieldIncluseSubfolders);
                    if (indexOfResultFieldIncludeSubfolders == -1) {
                        logError(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Error.CanNotFindField", realResultFieldIncluseSubfolders));
                        return result;
                    }
                }
                for (int iteration = 0; iteration < rows.size() && !parentJob.isStopped(); iteration++) {
                    resultRow = rows.get(iteration);
                    // Get source and destination file names, also wildcard
                    String vsourcefilefolder_previous = resultRow.getString(indexOfResultFieldFile, null);
                    String vwildcard_previous = null;
                    if (indexOfResultFieldWildcard > -1) {
                        vwildcard_previous = resultRow.getString(indexOfResultFieldWildcard, null);
                    }
                    String vincludeSubFolders_previous = NO;
                    if (indexOfResultFieldIncludeSubfolders > -1) {
                        vincludeSubFolders_previous = resultRow.getString(indexOfResultFieldIncludeSubfolders, NO);
                    }
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Log.ProcessingRow", vsourcefilefolder_previous, vwildcard_previous));
                    }
                    ProcessFileFolder(vsourcefilefolder_previous, vwildcard_previous, vincludeSubFolders_previous, parentJob, result);
                }
            }
            break;
        case SOURCE_FILES_FILENAMES_RESULT:
            List<ResultFile> resultFiles = result.getResultFilesList();
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Log.ResultFilenames.Found", (resultFiles != null ? resultFiles.size() : 0) + ""));
            }
            if (resultFiles != null && resultFiles.size() > 0) {
                // Let's check wildcard
                Pattern pattern = null;
                String realPattern = environmentSubstitute(getResultFilenamesWildcard());
                if (!Utils.isEmpty(realPattern)) {
                    pattern = Pattern.compile(realPattern);
                }
                for (Iterator<ResultFile> it = resultFiles.iterator(); it.hasNext() && !parentJob.isStopped(); ) {
                    ResultFile resultFile = it.next();
                    FileObject file = resultFile.getFile();
                    try {
                        if (file != null && file.exists()) {
                            boolean getIt = true;
                            if (pattern != null) {
                                Matcher matcher = pattern.matcher(file.getName().getBaseName());
                                getIt = matcher.matches();
                            }
                            if (getIt) {
                                getFileSize(file, result, parentJob);
                            }
                        }
                    } catch (Exception e) {
                        incrementErrors();
                        logError(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Error.GettingFileFromResultFilenames", file.toString(), e.toString()));
                    } finally {
                        if (file != null) {
                            try {
                                file.close();
                            } catch (Exception e) {
                            /* Ignore */
                            }
                        }
                    }
                }
            }
            break;
        default:
            // from grid entered by user
            if (vsourcefilefolder != null && vsourcefilefolder.length > 0) {
                for (int i = 0; i < vsourcefilefolder.length && !parentJob.isStopped(); i++) {
                    if (isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Log.ProcessingRow", vsourcefilefolder[i], vwildcard[i]));
                    }
                    ProcessFileFolder(vsourcefilefolder[i], vwildcard[i], vincludeSubFolders[i], parentJob, result);
                }
            } else {
                logError(BaseMessages.getString(PKG, "JobEvalFilesMetrics.Error.FilesGridEmpty"));
                return result;
            }
            break;
    }
    result.setResult(isSuccess());
    result.setNrErrors(getNrError());
    displayResults();
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ResultFile(org.pentaho.di.core.ResultFile) 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) RowMetaAndData(org.pentaho.di.core.RowMetaAndData) FileObject(org.apache.commons.vfs2.FileObject)

Example 10 with ResultFile

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

the class JobEntryFileCompare method execute.

public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    result.setResult(false);
    String realFilename1 = getRealFilename1();
    String realFilename2 = getRealFilename2();
    FileObject file1 = null;
    FileObject file2 = null;
    try {
        if (filename1 != null && filename2 != null) {
            // Set Embedded NamedCluter MetatStore Provider Key so that it can be passed to VFS
            if (parentJobMeta.getNamedClusterEmbedManager() != null) {
                parentJobMeta.getNamedClusterEmbedManager().passEmbeddedMetastoreKey(this, parentJobMeta.getEmbeddedMetastoreProviderKey());
            }
            file1 = KettleVFS.getFileObject(realFilename1, this);
            file2 = KettleVFS.getFileObject(realFilename2, this);
            if (file1.exists() && file2.exists()) {
                if (equalFileContents(file1, file2)) {
                    result.setResult(true);
                } else {
                    result.setResult(false);
                }
                // add filename to result filenames
                if (addFilenameToResult && file1.getType() == FileType.FILE && file2.getType() == FileType.FILE) {
                    ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, file1, parentJob.getJobname(), toString());
                    resultFile.setComment(BaseMessages.getString(PKG, "JobWaitForFile.FilenameAdded"));
                    result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                    resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, file2, parentJob.getJobname(), toString());
                    resultFile.setComment(BaseMessages.getString(PKG, "JobWaitForFile.FilenameAdded"));
                    result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                }
            } else {
                if (!file1.exists()) {
                    logError(BaseMessages.getString(PKG, "JobEntryFileCompare.ERROR_0004_File1_Does_Not_Exist", realFilename1));
                }
                if (!file2.exists()) {
                    logError(BaseMessages.getString(PKG, "JobEntryFileCompare.ERROR_0005_File2_Does_Not_Exist", realFilename2));
                }
                result.setResult(false);
                result.setNrErrors(1);
            }
        } else {
            logError(BaseMessages.getString(PKG, "JobEntryFileCompare.ERROR_0006_Need_Two_Filenames"));
        }
    } catch (Exception e) {
        result.setResult(false);
        result.setNrErrors(1);
        logError(BaseMessages.getString(PKG, "JobEntryFileCompare.ERROR_0007_Comparing_Files", realFilename2, realFilename2, e.getMessage()));
    } finally {
        try {
            if (file1 != null) {
                file1.close();
                file1 = null;
            }
            if (file2 != null) {
                file2.close();
                file2 = null;
            }
        } catch (IOException e) {
        // Ignore errors
        }
    }
    return result;
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) ResultFile(org.pentaho.di.core.ResultFile) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) IOException(java.io.IOException) Result(org.pentaho.di.core.Result)

Aggregations

ResultFile (org.pentaho.di.core.ResultFile)83 KettleException (org.pentaho.di.core.exception.KettleException)65 FileObject (org.apache.commons.vfs2.FileObject)32 IOException (java.io.IOException)29 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)29 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)28 Result (org.pentaho.di.core.Result)20 KettleFileException (org.pentaho.di.core.exception.KettleFileException)16 KettleStepException (org.pentaho.di.core.exception.KettleStepException)12 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)11 File (java.io.File)10 OutputStream (java.io.OutputStream)10 Date (java.util.Date)9 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)6 FileInputStream (java.io.FileInputStream)5 KettleValueException (org.pentaho.di.core.exception.KettleValueException)5 ArrayList (java.util.ArrayList)4 Matcher (java.util.regex.Matcher)4 Pattern (java.util.regex.Pattern)4 KettleExtensionPoint (org.pentaho.di.core.extension.KettleExtensionPoint)4