Search in sources :

Example 6 with VFS

use of org.apache.commons.vfs2.VFS in project pentaho-kettle by pentaho.

the class JobEntryCopyMoveResultFilenames method execute.

public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    result.setNrErrors(1);
    result.setResult(false);
    boolean deleteFile = getAction().equals("delete");
    // Set Embedded NamedCluter MetatStore Provider Key so that it can be passed to VFS
    if (parentJobMeta.getNamedClusterEmbedManager() != null) {
        parentJobMeta.getNamedClusterEmbedManager().passEmbeddedMetastoreKey(this, parentJobMeta.getEmbeddedMetastoreProviderKey());
    }
    String realdestinationFolder = null;
    if (!deleteFile) {
        realdestinationFolder = environmentSubstitute(getDestinationFolder());
        if (!CreateDestinationFolder(realdestinationFolder)) {
            return result;
        }
    }
    if (!Utils.isEmpty(wildcard)) {
        wildcardPattern = Pattern.compile(environmentSubstitute(wildcard));
    }
    if (!Utils.isEmpty(wildcardexclude)) {
        wildcardExcludePattern = Pattern.compile(environmentSubstitute(wildcardexclude));
    }
    if (previousResult != null) {
        NrErrors = 0;
        limitFiles = Const.toInt(environmentSubstitute(getNrErrorsLessThan()), 10);
        NrErrors = 0;
        NrSuccess = 0;
        successConditionBroken = false;
        successConditionBrokenExit = false;
        FileObject file = null;
        try {
            int size = result.getResultFiles().size();
            if (log.isBasic()) {
                logBasic(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.log.FilesFound", "" + size));
            }
            List<ResultFile> resultFiles = result.getResultFilesList();
            if (resultFiles != null && resultFiles.size() > 0) {
                for (Iterator<ResultFile> it = resultFiles.iterator(); it.hasNext() && !parentJob.isStopped(); ) {
                    if (successConditionBroken) {
                        logError(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.Error.SuccessConditionbroken", "" + NrErrors));
                        throw new Exception(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.Error.SuccessConditionbroken", "" + NrErrors));
                    }
                    ResultFile resultFile = it.next();
                    file = resultFile.getFile();
                    if (file != null && file.exists()) {
                        if (!specifywildcard || (CheckFileWildcard(file.getName().getBaseName(), wildcardPattern, true) && !CheckFileWildcard(file.getName().getBaseName(), wildcardExcludePattern, false) && specifywildcard)) {
                            // Copy or Move file
                            if (!processFile(file, realdestinationFolder, result, parentJob, deleteFile)) {
                                // Update Errors
                                updateErrors();
                            }
                        }
                    } else {
                        logError(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.log.ErrorCanNotFindFile", file.toString()));
                        // Update Errors
                        updateErrors();
                    }
                }
            // end for
            }
        } catch (Exception e) {
            logError(BaseMessages.getString(PKG, "JobEntryCopyMoveResultFilenames.Error", e.toString()));
        } finally {
            if (file != null) {
                try {
                    file.close();
                    file = null;
                } catch (Exception ex) {
                /* Ignore */
                }
            }
        }
    }
    // Success Condition
    result.setNrErrors(NrErrors);
    result.setNrLinesWritten(NrSuccess);
    if (getSuccessStatus()) {
        result.setResult(true);
    }
    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 7 with VFS

use of org.apache.commons.vfs2.VFS 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 8 with VFS

use of org.apache.commons.vfs2.VFS 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)

Example 9 with VFS

use of org.apache.commons.vfs2.VFS in project pentaho-kettle by pentaho.

the class JobEntryFileExists method execute.

public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    result.setResult(false);
    result.setNrErrors(0);
    if (filename != 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());
        }
        String realFilename = getRealFilename();
        try {
            FileObject file = KettleVFS.getFileObject(realFilename, this);
            if (file.exists() && file.isReadable()) {
                logDetailed(BaseMessages.getString(PKG, "JobEntryFileExists.File_Exists", realFilename));
                result.setResult(true);
            } else {
                logDetailed(BaseMessages.getString(PKG, "JobEntryFileExists.File_Does_Not_Exist", realFilename));
            }
        } catch (Exception e) {
            result.setNrErrors(1);
            logError(BaseMessages.getString(PKG, "JobEntryFileExists.ERROR_0004_IO_Exception", e.getMessage()), e);
        }
    } else {
        result.setNrErrors(1);
        logError(BaseMessages.getString(PKG, "JobEntryFileExists.ERROR_0005_No_Filename_Defined"));
    }
    return result;
}
Also used : 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)

Example 10 with VFS

use of org.apache.commons.vfs2.VFS in project pentaho-kettle by pentaho.

the class JobEntryMoveFiles method execute.

public Result execute(Result previousResult, int nr) throws KettleException {
    Result result = previousResult;
    List<RowMetaAndData> rows = result.getRows();
    RowMetaAndData resultRow = null;
    result.setNrErrors(1);
    result.setResult(false);
    NrErrors = 0;
    NrSuccess = 0;
    successConditionBroken = false;
    successConditionBrokenExit = false;
    limitFiles = Const.toInt(environmentSubstitute(getNrErrorsLessThan()), 10);
    if (log.isDetailed()) {
        if (simulate) {
            logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.SimulationOn"));
        }
        if (include_subfolders) {
            logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.IncludeSubFoldersOn"));
        }
    }
    String MoveToFolder = environmentSubstitute(destinationFolder);
    // Get source and destination files, also wildcard
    String[] vsourcefilefolder = source_filefolder;
    String[] vdestinationfilefolder = destination_filefolder;
    String[] vwildcard = wildcard;
    // Set Embedded NamedCluter MetatStore Provider Key so that it can be passed to VFS
    if (parentJobMeta.getNamedClusterEmbedManager() != null) {
        parentJobMeta.getNamedClusterEmbedManager().passEmbeddedMetastoreKey(this, parentJobMeta.getEmbeddedMetastoreProviderKey());
    }
    if (iffileexists.equals("move_file")) {
        if (Utils.isEmpty(MoveToFolder)) {
            logError(BaseMessages.getString(PKG, "JobMoveFiles.Log.Error.MoveToFolderMissing"));
            return result;
        }
        FileObject folder = null;
        try {
            folder = KettleVFS.getFileObject(MoveToFolder, this);
            if (!folder.exists()) {
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.Error.FolderMissing", MoveToFolder));
                }
                if (create_move_to_folder) {
                    folder.createFolder();
                } else {
                    logError(BaseMessages.getString(PKG, "JobMoveFiles.Log.Error.FolderMissing", MoveToFolder));
                    return result;
                }
            }
            if (!folder.getType().equals(FileType.FOLDER)) {
                logError(BaseMessages.getString(PKG, "JobMoveFiles.Log.Error.NotFolder", MoveToFolder));
                return result;
            }
        } catch (Exception e) {
            logError(BaseMessages.getString(PKG, "JobMoveFiles.Log.Error.GettingMoveToFolder", MoveToFolder, e.getMessage()));
            return result;
        } finally {
            if (folder != null) {
                try {
                    folder.close();
                } catch (IOException ex) {
                /* Ignore */
                }
            }
        }
    }
    if (arg_from_previous) {
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.ArgFromPrevious.Found", (rows != null ? rows.size() : 0) + ""));
        }
    }
    if (arg_from_previous && rows != null) {
        for (int iteration = 0; iteration < rows.size() && !parentJob.isStopped(); iteration++) {
            // Success condition broken?
            if (successConditionBroken) {
                if (!successConditionBrokenExit) {
                    logError(BaseMessages.getString(PKG, "JobMoveFiles.Error.SuccessConditionbroken", "" + NrErrors));
                    successConditionBrokenExit = true;
                }
                result.setNrErrors(NrErrors);
                displayResults();
                return result;
            }
            resultRow = rows.get(iteration);
            // Get source and destination file names, also wildcard
            String vsourcefilefolder_previous = resultRow.getString(0, null);
            String vdestinationfilefolder_previous = resultRow.getString(1, null);
            String vwildcard_previous = resultRow.getString(2, null);
            if (!Utils.isEmpty(vsourcefilefolder_previous) && !Utils.isEmpty(vdestinationfilefolder_previous)) {
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.ProcessingRow", vsourcefilefolder_previous, vdestinationfilefolder_previous, vwildcard_previous));
                }
                if (!ProcessFileFolder(vsourcefilefolder_previous, vdestinationfilefolder_previous, vwildcard_previous, parentJob, result, MoveToFolder)) {
                    // The move process fail
                    // Update Errors
                    updateErrors();
                }
            } else {
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.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, "JobMoveFiles.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 (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.ProcessingRow", vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i]));
                }
                if (!ProcessFileFolder(vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i], parentJob, result, MoveToFolder)) {
                    // Update Errors
                    updateErrors();
                }
            } else {
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobMoveFiles.Log.IgnoringRow", vsourcefilefolder[i], vdestinationfilefolder[i], vwildcard[i]));
                }
            }
        }
    }
    // 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)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)50 IOException (java.io.IOException)27 KettleException (org.pentaho.di.core.exception.KettleException)23 FileSystemException (org.apache.commons.vfs2.FileSystemException)22 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)20 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)20 Result (org.pentaho.di.core.Result)19 File (java.io.File)18 FileSystemManager (org.apache.commons.vfs2.FileSystemManager)11 DefaultFileSystemManager (org.apache.commons.vfs2.impl.DefaultFileSystemManager)11 ResultFile (org.pentaho.di.core.ResultFile)11 VFSClassLoader (org.apache.commons.vfs2.impl.VFSClassLoader)10 KettleFileException (org.pentaho.di.core.exception.KettleFileException)10 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)7 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)7 StandardFileSystemManager (org.apache.commons.vfs2.impl.StandardFileSystemManager)6 URL (java.net.URL)4 Matcher (java.util.regex.Matcher)4 Pattern (java.util.regex.Pattern)4