Example usage for org.apache.commons.vfs FileObject getType

List of usage examples for org.apache.commons.vfs FileObject getType

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileObject getType.

Prototype

public FileType getType() throws FileSystemException;

Source Link

Document

Returns this file's type.

Usage

From source file:org.pentaho.di.job.entries.xmlwellformed.JobEntryXMLWellFormed.java

private boolean processFileFolder(String sourcefilefoldername, String wildcard, Job parentJob, Result result) {
    boolean entrystatus = false;
    FileObject sourcefilefolder = null;
    FileObject CurrentFile = null;

    // Get real source file and wilcard
    String realSourceFilefoldername = environmentSubstitute(sourcefilefoldername);
    if (Const.isEmpty(realSourceFilefoldername)) {
        logError(BaseMessages.getString(PKG, "JobXMLWellFormed.log.FileFolderEmpty", sourcefilefoldername));
        // Update Errors
        updateErrors();/*  w ww.  ja v a  2 s. co  m*/

        return entrystatus;
    }
    String realWildcard = environmentSubstitute(wildcard);

    try {
        sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername, this);

        if (sourcefilefolder.exists()) {
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobXMLWellFormed.Log.FileExists",
                        sourcefilefolder.toString()));
            }
            if (sourcefilefolder.getType() == FileType.FILE) {
                entrystatus = checkOneFile(sourcefilefolder, result, parentJob);

            } else if (sourcefilefolder.getType() == FileType.FOLDER) {
                FileObject[] fileObjects = sourcefilefolder.findFiles(new AllFileSelector() {
                    public boolean traverseDescendents(FileSelectInfo info) {
                        return true;
                    }

                    public boolean includeFile(FileSelectInfo info) {

                        FileObject fileObject = info.getFile();
                        try {
                            if (fileObject == null) {
                                return false;
                            }
                            if (fileObject.getType() != FileType.FILE) {
                                return false;
                            }
                        } catch (Exception ex) {
                            // Upon error don't process the file.
                            return false;
                        } finally {
                            if (fileObject != null) {
                                try {
                                    fileObject.close();
                                } catch (IOException ex) {
                                    /* Ignore */
                                }
                            }

                        }
                        return true;
                    }
                });

                if (fileObjects != null) {
                    for (int j = 0; j < fileObjects.length && !parentJob.isStopped(); j++) {
                        if (successConditionBroken) {
                            if (!successConditionBrokenExit) {
                                logError(BaseMessages.getString(PKG,
                                        "JobXMLWellFormed.Error.SuccessConditionbroken", "" + NrAllErrors));
                                successConditionBrokenExit = true;
                            }
                            return false;
                        }
                        // Fetch files in list one after one ...
                        CurrentFile = fileObjects[j];

                        if (!CurrentFile.getParent().toString().equals(sourcefilefolder.toString())) {
                            // Not in the Base Folder..Only if include sub folders
                            if (include_subfolders) {
                                if (GetFileWildcard(CurrentFile.toString(), realWildcard)) {
                                    checkOneFile(CurrentFile, result, parentJob);
                                }
                            }

                        } else {
                            // In the base folder
                            if (GetFileWildcard(CurrentFile.toString(), realWildcard)) {
                                checkOneFile(CurrentFile, result, parentJob);
                            }
                        }
                    }
                }
            } else {
                logError(BaseMessages.getString(PKG, "JobXMLWellFormed.Error.UnknowFileFormat",
                        sourcefilefolder.toString()));
                // Update Errors
                updateErrors();
            }
        } else {
            logError(BaseMessages.getString(PKG, "JobXMLWellFormed.Error.SourceFileNotExists",
                    realSourceFilefoldername));
            // Update Errors
            updateErrors();
        }
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobXMLWellFormed.Error.Exception.Processing",
                realSourceFilefoldername.toString(), e));
        // Update Errors
        updateErrors();
    } finally {
        if (sourcefilefolder != null) {
            try {
                sourcefilefolder.close();
            } catch (IOException ex) {
                /* Ignore */
            }

        }
        if (CurrentFile != null) {
            try {
                CurrentFile.close();
            } catch (IOException ex) {
                /* Ignore */
            }
        }
    }
    return entrystatus;
}

From source file:org.pentaho.di.job.entries.zipfile.JobEntryZipFile.java

public boolean processRowFile(Job parentJob, Result result, String realZipfilename, String realWildcard,
        String realWildcardExclude, String realSourceDirectoryOrFile, String realMovetodirectory,
        boolean createparentfolder) {
    boolean Fileexists = false;
    File tempFile = null;/*  w  w  w . j a v a  2s.  co m*/
    File fileZip = null;
    boolean resultat = false;
    boolean renameOk = false;
    boolean orginExist = false;

    // Check if target file/folder exists!
    FileObject originFile = null;
    ZipInputStream zin = null;
    byte[] buffer = null;
    OutputStream dest = null;
    BufferedOutputStream buff = null;
    ZipOutputStream out = null;
    ZipEntry entry = null;
    String localSourceFilename = realSourceDirectoryOrFile;

    try {
        originFile = KettleVFS.getFileObject(realSourceDirectoryOrFile, this);
        localSourceFilename = KettleVFS.getFilename(originFile);
        orginExist = originFile.exists();
    } catch (Exception e) {
        // Ignore errors
    } finally {
        if (originFile != null) {
            try {
                originFile.close();
            } catch (IOException ex) {
                logError("Error closing file '" + originFile.toString() + "'", ex);
            }
        }
    }

    String localrealZipfilename = realZipfilename;
    if (realZipfilename != null && orginExist) {

        FileObject fileObject = null;
        try {
            fileObject = KettleVFS.getFileObject(localrealZipfilename, this);
            localrealZipfilename = KettleVFS.getFilename(fileObject);
            // Check if Zip File exists
            if (fileObject.exists()) {
                Fileexists = true;
                if (log.isDebug()) {
                    logDebug(BaseMessages.getString(PKG, "JobZipFiles.Zip_FileExists1.Label")
                            + localrealZipfilename
                            + BaseMessages.getString(PKG, "JobZipFiles.Zip_FileExists2.Label"));
                }
            }
            // Let's see if we need to create parent folder of destination zip filename
            if (createparentfolder) {
                createParentFolder(localrealZipfilename);
            }

            // Let's start the process now
            if (ifZipFileExists == 3 && Fileexists) {
                // the zip file exists and user want to Fail
                resultat = false;
            } else if (ifZipFileExists == 2 && Fileexists) {
                // the zip file exists and user want to do nothing
                if (addFileToResult) {
                    // Add file to result files name
                    ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, fileObject,
                            parentJob.getJobname(), toString());
                    result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                }
                resultat = true;
            } else if (afterZip == 2 && realMovetodirectory == null) {
                // After Zip, Move files..User must give a destination Folder
                resultat = false;
                logError(
                        BaseMessages.getString(PKG, "JobZipFiles.AfterZip_No_DestinationFolder_Defined.Label"));
            } else {
                // After Zip, Move files..User must give a destination Folder

                // Let's see if we deal with file or folder
                FileObject[] fileList = null;

                FileObject sourceFileOrFolder = KettleVFS.getFileObject(localSourceFilename);
                boolean isSourceDirectory = sourceFileOrFolder.getType().equals(FileType.FOLDER);
                final Pattern pattern;
                final Pattern patternexclude;

                if (isSourceDirectory) {
                    // Let's prepare the pattern matcher for performance reasons.
                    // We only do this if the target is a folder !
                    //
                    if (!Const.isEmpty(realWildcard)) {
                        pattern = Pattern.compile(realWildcard);
                    } else {
                        pattern = null;
                    }
                    if (!Const.isEmpty(realWildcardExclude)) {
                        patternexclude = Pattern.compile(realWildcardExclude);
                    } else {
                        patternexclude = null;
                    }

                    // Target is a directory
                    // Get all the files in the directory...
                    //
                    if (includingSubFolders) {
                        fileList = sourceFileOrFolder.findFiles(new FileSelector() {

                            public boolean traverseDescendents(FileSelectInfo fileInfo) throws Exception {
                                return true;
                            }

                            public boolean includeFile(FileSelectInfo fileInfo) throws Exception {
                                boolean include;

                                // Only include files in the sub-folders...
                                // When we include sub-folders we match the whole filename, not just the base-name
                                //
                                if (fileInfo.getFile().getType().equals(FileType.FILE)) {
                                    include = true;
                                    if (pattern != null) {
                                        String name = fileInfo.getFile().getName().getPath();
                                        include = pattern.matcher(name).matches();
                                    }
                                    if (include && patternexclude != null) {
                                        String name = fileInfo.getFile().getName().getPath();
                                        include = !pattern.matcher(name).matches();
                                    }
                                } else {
                                    include = false;
                                }
                                return include;
                            }
                        });
                    } else {
                        fileList = sourceFileOrFolder.getChildren();
                    }
                } else {
                    pattern = null;
                    patternexclude = null;

                    // Target is a file
                    fileList = new FileObject[] { sourceFileOrFolder };
                }

                if (fileList.length == 0) {
                    resultat = false;
                    logError(BaseMessages.getString(PKG, "JobZipFiles.Log.FolderIsEmpty", localSourceFilename));
                } else if (!checkContainsFile(localSourceFilename, fileList, isSourceDirectory)) {
                    resultat = false;
                    logError(BaseMessages.getString(PKG, "JobZipFiles.Log.NoFilesInFolder",
                            localSourceFilename));
                } else {
                    if (ifZipFileExists == 0 && Fileexists) {
                        // the zip file exists and user want to create new one with unique name
                        // Format Date

                        // do we have already a .zip at the end?
                        if (localrealZipfilename.toLowerCase().endsWith(".zip")) {
                            // strip this off
                            localrealZipfilename = localrealZipfilename.substring(0,
                                    localrealZipfilename.length() - 4);
                        }

                        localrealZipfilename += "_" + StringUtil.getFormattedDateTimeNow(true) + ".zip";
                        if (log.isDebug()) {
                            logDebug(BaseMessages.getString(PKG, "JobZipFiles.Zip_FileNameChange1.Label")
                                    + localrealZipfilename
                                    + BaseMessages.getString(PKG, "JobZipFiles.Zip_FileNameChange1.Label"));
                        }
                    } else if (ifZipFileExists == 1 && Fileexists) {
                        // the zip file exists and user want to append
                        // get a temp file
                        fileZip = getFile(localrealZipfilename);
                        tempFile = File.createTempFile(fileZip.getName(), null);

                        // delete it, otherwise we cannot rename existing zip to it.
                        tempFile.delete();

                        renameOk = fileZip.renameTo(tempFile);

                        if (!renameOk) {
                            logError(BaseMessages.getString(PKG, "JobZipFiles.Cant_Rename_Temp1.Label")
                                    + fileZip.getAbsolutePath()
                                    + BaseMessages.getString(PKG, "JobZipFiles.Cant_Rename_Temp2.Label")
                                    + tempFile.getAbsolutePath()
                                    + BaseMessages.getString(PKG, "JobZipFiles.Cant_Rename_Temp3.Label"));
                        }
                        if (log.isDebug()) {
                            logDebug(BaseMessages.getString(PKG, "JobZipFiles.Zip_FileAppend1.Label")
                                    + localrealZipfilename
                                    + BaseMessages.getString(PKG, "JobZipFiles.Zip_FileAppend2.Label"));
                        }
                    }

                    if (log.isDetailed()) {
                        logDetailed(
                                BaseMessages.getString(PKG, "JobZipFiles.Files_Found1.Label") + fileList.length
                                        + BaseMessages.getString(PKG, "JobZipFiles.Files_Found2.Label")
                                        + localSourceFilename
                                        + BaseMessages.getString(PKG, "JobZipFiles.Files_Found3.Label"));
                    }

                    // Prepare Zip File
                    buffer = new byte[18024];
                    dest = KettleVFS.getOutputStream(localrealZipfilename, false);
                    buff = new BufferedOutputStream(dest);
                    out = new ZipOutputStream(buff);

                    HashSet<String> fileSet = new HashSet<String>();

                    if (renameOk) {
                        // User want to append files to existing Zip file
                        // The idea is to rename the existing zip file to a temporary file
                        // and then adds all entries in the existing zip along with the new files,
                        // excluding the zip entries that have the same name as one of the new files.

                        zin = new ZipInputStream(new FileInputStream(tempFile));
                        entry = zin.getNextEntry();

                        while (entry != null) {
                            String name = entry.getName();

                            if (!fileSet.contains(name)) {

                                // Add ZIP entry to output stream.
                                out.putNextEntry(new ZipEntry(name));
                                // Transfer bytes from the ZIP file to the output file
                                int len;
                                while ((len = zin.read(buffer)) > 0) {
                                    out.write(buffer, 0, len);
                                }

                                fileSet.add(name);
                            }
                            entry = zin.getNextEntry();
                        }
                        // Close the streams
                        zin.close();
                    }

                    // Set the method
                    out.setMethod(ZipOutputStream.DEFLATED);
                    // Set the compression level
                    if (compressionRate == 0) {
                        out.setLevel(Deflater.NO_COMPRESSION);
                    } else if (compressionRate == 1) {
                        out.setLevel(Deflater.DEFAULT_COMPRESSION);
                    }
                    if (compressionRate == 2) {
                        out.setLevel(Deflater.BEST_COMPRESSION);
                    }
                    if (compressionRate == 3) {
                        out.setLevel(Deflater.BEST_SPEED);
                    }
                    // Specify Zipped files (After that we will move,delete them...)
                    FileObject[] zippedFiles = new FileObject[fileList.length];
                    int fileNum = 0;

                    // Get the files in the list...
                    for (int i = 0; i < fileList.length && !parentJob.isStopped(); i++) {
                        boolean getIt = true;
                        boolean getItexclude = false;

                        // First see if the file matches the regular expression!
                        // ..only if target is a folder !
                        if (isSourceDirectory) {
                            // If we include sub-folders, we match on the whole name, not just the basename
                            //
                            String filename;
                            if (includingSubFolders) {
                                filename = fileList[i].getName().getPath();
                            } else {
                                filename = fileList[i].getName().getBaseName();
                            }
                            if (pattern != null) {
                                // Matches the base name of the file (backward compatible!)
                                //
                                Matcher matcher = pattern.matcher(filename);
                                getIt = matcher.matches();
                            }

                            if (patternexclude != null) {
                                Matcher matcherexclude = patternexclude.matcher(filename);
                                getItexclude = matcherexclude.matches();
                            }
                        }

                        // Get processing File
                        String targetFilename = KettleVFS.getFilename(fileList[i]);
                        if (sourceFileOrFolder.getType().equals(FileType.FILE)) {
                            targetFilename = localSourceFilename;
                        }

                        FileObject file = KettleVFS.getFileObject(targetFilename);
                        boolean isTargetDirectory = file.exists() && file.getType().equals(FileType.FOLDER);

                        if (getIt && !getItexclude && !isTargetDirectory && !fileSet.contains(targetFilename)) {
                            // We can add the file to the Zip Archive
                            if (log.isDebug()) {
                                logDebug(BaseMessages.getString(PKG, "JobZipFiles.Add_FilesToZip1.Label")
                                        + fileList[i]
                                        + BaseMessages.getString(PKG, "JobZipFiles.Add_FilesToZip2.Label")
                                        + localSourceFilename
                                        + BaseMessages.getString(PKG, "JobZipFiles.Add_FilesToZip3.Label"));
                            }

                            // Associate a file input stream for the current file
                            InputStream in = KettleVFS.getInputStream(file);

                            // Add ZIP entry to output stream.
                            //
                            String relativeName;
                            String fullName = fileList[i].getName().getPath();
                            String basePath = sourceFileOrFolder.getName().getPath();
                            if (isSourceDirectory) {
                                if (fullName.startsWith(basePath)) {
                                    relativeName = fullName.substring(basePath.length() + 1);
                                } else {
                                    relativeName = fullName;
                                }
                            } else if (isFromPrevious) {
                                int depth = determineDepth(environmentSubstitute(storedSourcePathDepth));
                                relativeName = determineZipfilenameForDepth(fullName, depth);
                            } else {
                                relativeName = fileList[i].getName().getBaseName();
                            }
                            out.putNextEntry(new ZipEntry(relativeName));

                            int len;
                            while ((len = in.read(buffer)) > 0) {
                                out.write(buffer, 0, len);
                            }
                            out.flush();
                            out.closeEntry();

                            // Close the current file input stream
                            in.close();

                            // Get Zipped File
                            zippedFiles[fileNum] = fileList[i];
                            fileNum = fileNum + 1;
                        }
                    }
                    // Close the ZipOutPutStream
                    out.close();
                    buff.close();
                    dest.close();

                    if (log.isBasic()) {
                        logBasic(BaseMessages.getString(PKG, "JobZipFiles.Log.TotalZippedFiles",
                                "" + zippedFiles.length));
                    }
                    // Delete Temp File
                    if (tempFile != null) {
                        tempFile.delete();
                    }

                    // -----Get the list of Zipped Files and Move or Delete Them
                    if (afterZip == 1 || afterZip == 2) {
                        // iterate through the array of Zipped files
                        for (int i = 0; i < zippedFiles.length; i++) {
                            if (zippedFiles[i] != null) {
                                // Delete, Move File
                                FileObject fileObjectd = zippedFiles[i];
                                if (!isSourceDirectory) {
                                    fileObjectd = KettleVFS.getFileObject(localSourceFilename);
                                }

                                // Here we can move, delete files
                                if (afterZip == 1) {
                                    // Delete File
                                    boolean deleted = fileObjectd.delete();
                                    if (!deleted) {
                                        resultat = false;
                                        logError(BaseMessages.getString(PKG,
                                                "JobZipFiles.Cant_Delete_File1.Label") + localSourceFilename
                                                + Const.FILE_SEPARATOR + zippedFiles[i] + BaseMessages
                                                        .getString(PKG, "JobZipFiles.Cant_Delete_File2.Label"));

                                    }
                                    // File deleted
                                    if (log.isDebug()) {
                                        logDebug(BaseMessages.getString(PKG, "JobZipFiles.File_Deleted1.Label")
                                                + localSourceFilename + Const.FILE_SEPARATOR + zippedFiles[i]
                                                + BaseMessages.getString(PKG,
                                                        "JobZipFiles.File_Deleted2.Label"));
                                    }
                                } else if (afterZip == 2) {
                                    // Move File
                                    FileObject fileObjectm = null;
                                    try {
                                        fileObjectm = KettleVFS.getFileObject(realMovetodirectory
                                                + Const.FILE_SEPARATOR + fileObjectd.getName().getBaseName());
                                        fileObjectd.moveTo(fileObjectm);
                                    } catch (IOException e) {
                                        logError(
                                                BaseMessages.getString(PKG, "JobZipFiles.Cant_Move_File1.Label")
                                                        + zippedFiles[i]
                                                        + BaseMessages.getString(PKG,
                                                                "JobZipFiles.Cant_Move_File2.Label")
                                                        + e.getMessage());
                                        resultat = false;
                                    } finally {
                                        try {
                                            if (fileObjectm != null) {
                                                fileObjectm.close();
                                            }
                                        } catch (Exception e) {
                                            if (fileObjectm != null) {
                                                logError("Error closing file '" + fileObjectm.toString() + "'",
                                                        e);
                                            }
                                        }
                                    }
                                    // File moved
                                    if (log.isDebug()) {
                                        logDebug(BaseMessages.getString(PKG, "JobZipFiles.File_Moved1.Label")
                                                + zippedFiles[i]
                                                + BaseMessages.getString(PKG, "JobZipFiles.File_Moved2.Label"));
                                    }
                                }
                            }
                        }
                    }

                    if (addFileToResult) {
                        // Add file to result files name
                        ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, fileObject,
                                parentJob.getJobname(), toString());
                        result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                    }

                    resultat = true;
                }
            }
        } catch (Exception e) {
            logError(BaseMessages.getString(PKG, "JobZipFiles.Cant_CreateZipFile1.Label") + localrealZipfilename
                    + BaseMessages.getString(PKG, "JobZipFiles.Cant_CreateZipFile2.Label"), e);
            resultat = false;
        } finally {
            if (fileObject != null) {
                try {
                    fileObject.close();
                    fileObject = null;
                } catch (IOException ex) {
                    logError("Error closing file '" + fileObject.toString() + "'", ex);
                }
            }

            try {
                if (out != null) {
                    out.close();
                }
                if (buff != null) {
                    buff.close();
                }
                if (dest != null) {
                    dest.close();
                }
                if (zin != null) {
                    zin.close();
                }
                if (entry != null) {
                    entry = null;
                }

            } catch (IOException ex) {
                logError("Error closing zip file entry for file '" + originFile.toString() + "'", ex);
            }
        }
    } else {
        resultat = true;
        if (localrealZipfilename == null) {
            logError(BaseMessages.getString(PKG, "JobZipFiles.No_ZipFile_Defined.Label"));
        }
        if (!orginExist) {
            logError(BaseMessages.getString(PKG, "JobZipFiles.No_FolderCible_Defined.Label",
                    localSourceFilename));
        }
    }
    // return a verifier
    return resultat;
}

From source file:org.pentaho.di.job.entries.zipfile.JobEntryZipFile.java

private boolean checkContainsFile(String realSourceDirectoryOrFile, FileObject[] filelist, boolean isDirectory)
        throws FileSystemException {
    boolean retval = false;
    for (int i = 0; i < filelist.length; i++) {
        FileObject file = filelist[i];
        if ((file.exists() && file.getType().equals(FileType.FILE))) {
            retval = true;/*from w  w  w . j a va2  s  . c o  m*/
        }
    }
    return retval;
}

From source file:org.pentaho.di.job.entries.zipfile.JobEntryZipFile.java

public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    List<RowMetaAndData> rows = result.getRows();

    // reset values
    String realZipfilename = null;
    String realWildcard = null;//from  w  w  w.j  av a2s . c om
    String realWildcardExclude = null;
    String realTargetdirectory = null;
    String realMovetodirectory = environmentSubstitute(movetoDirectory);

    // Sanity check
    boolean SanityControlOK = true;

    if (afterZip == 2) {
        if (Const.isEmpty(realMovetodirectory)) {
            SanityControlOK = false;
            logError(BaseMessages.getString(PKG, "JobZipFiles.AfterZip_No_DestinationFolder_Defined.Label"));
        } else {
            FileObject moveToDirectory = null;
            try {
                moveToDirectory = KettleVFS.getFileObject(realMovetodirectory, this);
                if (moveToDirectory.exists()) {
                    if (moveToDirectory.getType() == FileType.FOLDER) {
                        if (log.isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobZipFiles.Log.MoveToFolderExist",
                                    realMovetodirectory));
                        }
                    } else {
                        SanityControlOK = false;
                        logError(BaseMessages.getString(PKG, "JobZipFiles.Log.MoveToFolderNotFolder",
                                realMovetodirectory));
                    }
                } else {
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobZipFiles.Log.MoveToFolderNotNotExist",
                                realMovetodirectory));
                    }
                    if (createMoveToDirectory) {
                        moveToDirectory.createFolder();
                        if (log.isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobZipFiles.Log.MoveToFolderCreaterd",
                                    realMovetodirectory));
                        }
                    } else {
                        SanityControlOK = false;
                        logError(BaseMessages.getString(PKG, "JobZipFiles.Log.MoveToFolderNotNotExist",
                                realMovetodirectory));
                    }
                }
            } catch (Exception e) {
                SanityControlOK = false;
                logError(BaseMessages.getString(PKG, "JobZipFiles.ErrorGettingMoveToFolder.Label",
                        realMovetodirectory), e);
            } finally {
                if (moveToDirectory != null) {
                    realMovetodirectory = KettleVFS.getFilename(moveToDirectory);
                    try {
                        moveToDirectory.close();
                        moveToDirectory = null;
                    } catch (Exception e) {
                        logError("Error moving to directory", e);
                        result.setResult(false);
                        result.setNrErrors(1);
                    }
                }
            }
        }
    }

    if (!SanityControlOK) {
        result.setNrErrors(1);
        result.setResult(false);
        return result;
    }

    // arguments from previous

    if (isFromPrevious) {
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobZipFiles.ArgFromPrevious.Found",
                    (rows != null ? rows.size() : 0) + ""));
        }
    }
    if (isFromPrevious && rows != null) {
        try {
            for (int iteration = 0; iteration < rows.size() && !parentJob.isStopped(); iteration++) {
                // get arguments from previous job entry
                RowMetaAndData resultRow = rows.get(iteration);
                // get target directory
                realTargetdirectory = resultRow.getString(0, null);
                if (!Const.isEmpty(realTargetdirectory)) {
                    // get wildcard to include
                    if (!Const.isEmpty(resultRow.getString(1, null))) {
                        realWildcard = resultRow.getString(1, null);
                    }
                    // get wildcard to exclude
                    if (!Const.isEmpty(resultRow.getString(2, null))) {
                        realWildcardExclude = resultRow.getString(2, null);
                    }

                    // get destination zip file
                    realZipfilename = resultRow.getString(3, null);
                    if (!Const.isEmpty(realZipfilename)) {
                        if (!processRowFile(parentJob, result, realZipfilename, realWildcard,
                                realWildcardExclude, realTargetdirectory, realMovetodirectory,
                                createParentFolder)) {
                            result.setResult(false);
                            return result;
                        }
                    } else {
                        logError("destination zip filename is empty! Ignoring row...");
                    }
                } else {
                    logError("Target directory is empty! Ignoring row...");
                }
            }
        } catch (Exception e) {
            logError("Erreur during process!", e);
            result.setResult(false);
            result.setNrErrors(1);
        }
    } else if (!isFromPrevious) {
        if (!Const.isEmpty(sourceDirectory)) {
            // get values from job entry
            realZipfilename = getFullFilename(environmentSubstitute(zipFilename), addDate, addTime,
                    specifyFormat, dateTimeFormat);
            realWildcard = environmentSubstitute(wildCard);
            realWildcardExclude = environmentSubstitute(excludeWildCard);
            realTargetdirectory = environmentSubstitute(sourceDirectory);

            result.setResult(processRowFile(parentJob, result, realZipfilename, realWildcard,
                    realWildcardExclude, realTargetdirectory, realMovetodirectory, createParentFolder));
        } else {
            logError("Source folder/file is empty! Ignoring row...");
        }
    }

    // End
    return result;
}

From source file:org.pentaho.di.repository.filerep.KettleFileRepository.java

public String[] getJobNames(ObjectId id_directory, boolean includeDeleted) throws KettleException {
    try {/*  w  w  w.j a  v  a2s. c  o  m*/
        List<String> list = new ArrayList<String>();

        RepositoryDirectoryInterface tree = loadRepositoryDirectoryTree();
        RepositoryDirectoryInterface directory = tree.findDirectory(id_directory);

        String folderName = calcDirectoryName(directory);
        FileObject folder = KettleVFS.getFileObject(folderName);

        for (FileObject child : folder.getChildren()) {
            if (child.getType().equals(FileType.FILE)) {
                if (!child.isHidden() || !repositoryMeta.isHidingHiddenFiles()) {
                    String name = child.getName().getBaseName();

                    if (name.endsWith(EXT_JOB)) {

                        String jobName = name.substring(0, name.length() - 4);
                        list.add(jobName);
                    }
                }
            }
        }

        return list.toArray(new String[list.size()]);
    } catch (Exception e) {
        throw new KettleException(
                "Unable to get list of transformations names in folder with id : " + id_directory, e);
    }
}

From source file:org.pentaho.di.repository.filerep.KettleFileRepository.java

private ObjectId[] getRootObjectIDs(String extension) throws KettleException {
    try {/*from www.  j  a  va2s.  com*/
        // Get all the files in the root directory with a certain extension...
        //
        List<ObjectId> list = new ArrayList<ObjectId>();

        String folderName = repositoryMeta.getBaseDirectory();
        FileObject folder = KettleVFS.getFileObject(folderName);

        for (FileObject child : folder.getChildren()) {
            if (child.getType().equals(FileType.FILE)) {
                if (!child.isHidden() || !repositoryMeta.isHidingHiddenFiles()) {
                    String name = child.getName().getBaseName();

                    if (name.endsWith(extension)) {
                        list.add(new StringObjectId(name));
                    }
                }
            }
        }

        return list.toArray(new ObjectId[list.size()]);
    } catch (Exception e) {
        throw new KettleException("Unable to get root object ids for extension [" + extension + "]", e);
    }
}

From source file:org.pentaho.di.repository.filerep.KettleFileRepository.java

public String[] getTransformationNames(ObjectId id_directory, boolean includeDeleted) throws KettleException {
    try {/*from   w w w .jav  a 2  s  .  com*/
        List<String> list = new ArrayList<String>();

        RepositoryDirectoryInterface tree = loadRepositoryDirectoryTree();
        RepositoryDirectoryInterface directory = tree.findDirectory(id_directory);

        String folderName = calcDirectoryName(directory);
        FileObject folder = KettleVFS.getFileObject(folderName);

        for (FileObject child : folder.getChildren()) {
            if (child.getType().equals(FileType.FILE)) {
                if (!child.isHidden() || !repositoryMeta.isHidingHiddenFiles()) {
                    String name = child.getName().getBaseName();

                    if (name.endsWith(EXT_TRANSFORMATION)) {

                        String transName = name.substring(0, name.length() - 4);
                        list.add(transName);
                    }
                }
            }
        }

        return list.toArray(new String[list.size()]);
    } catch (Exception e) {
        throw new KettleException(
                "Unable to get list of transformations names in folder with id : " + id_directory, e);
    }
}

From source file:org.pentaho.di.repository.filerep.KettleFileRepository.java

public RepositoryDirectoryInterface loadRepositoryDirectoryTree(RepositoryDirectoryInterface dir)
        throws KettleException {
    try {/*w  ww .jav a 2  s.  c  o m*/
        String folderName = calcDirectoryName(dir);
        FileObject folder = KettleVFS.getFileObject(folderName);

        for (FileObject child : folder.getChildren()) {
            if (child.getType().equals(FileType.FOLDER)) {
                if (!child.isHidden() || !repositoryMeta.isHidingHiddenFiles()) {
                    if (!".meta".equals(child.getName().getBaseName())) {
                        RepositoryDirectory subDir = new RepositoryDirectory(dir,
                                child.getName().getBaseName());
                        subDir.setObjectId(new StringObjectId(calcObjectId(subDir)));
                        dir.addSubdirectory(subDir);

                        loadRepositoryDirectoryTree(subDir);
                    }
                }
            }
        }

        return dir;
    } catch (Exception e) {
        throw new KettleException("Unable to load the directory tree from this file repository", e);
    }
}

From source file:org.pentaho.di.repository.filerep.KettleFileRepository.java

public List<RepositoryElementMetaInterface> getTransformationObjects(ObjectId idDirectory,
        boolean includeDeleted) throws KettleException {

    try {//from w w w.j  a v  a  2  s . co m
        List<RepositoryElementMetaInterface> list = new ArrayList<RepositoryElementMetaInterface>();

        RepositoryDirectoryInterface tree = loadRepositoryDirectoryTree();
        RepositoryDirectoryInterface directory = tree.findDirectory(idDirectory);

        String folderName = calcDirectoryName(directory);
        FileObject folder = KettleVFS.getFileObject(folderName);

        for (FileObject child : folder.getChildren()) {
            if (child.getType().equals(FileType.FILE)) {
                if (!child.isHidden() || !repositoryMeta.isHidingHiddenFiles()) {

                    String name = child.getName().getBaseName();

                    if (name.endsWith(EXT_TRANSFORMATION)) {

                        String transName = name.substring(0, name.length() - 4);

                        ObjectId id = new StringObjectId(
                                calcObjectId(directory, transName, EXT_TRANSFORMATION));
                        Date date = new Date(child.getContent().getLastModifiedTime());
                        list.add(new RepositoryObject(id, transName, directory, "-", date,
                                RepositoryObjectType.TRANSFORMATION, "", false));
                    }
                }
            }
        }

        return list;
    } catch (Exception e) {
        throw new KettleException("Unable to get list of transformations in folder with id : " + idDirectory,
                e);
    }
}

From source file:org.pentaho.di.repository.filerep.KettleFileRepository.java

public List<RepositoryElementMetaInterface> getJobObjects(ObjectId id_directory, boolean includeDeleted)
        throws KettleException {

    try {//from  ww  w .  j  a v  a  2  s.co  m
        List<RepositoryElementMetaInterface> list = new ArrayList<RepositoryElementMetaInterface>();

        RepositoryDirectoryInterface tree = loadRepositoryDirectoryTree();
        RepositoryDirectoryInterface directory = tree.findDirectory(id_directory);

        String folderName = calcDirectoryName(directory);
        FileObject folder = KettleVFS.getFileObject(folderName);

        for (FileObject child : folder.getChildren()) {
            if (child.getType().equals(FileType.FILE)) {
                if (!child.isHidden() || !repositoryMeta.isHidingHiddenFiles()) {
                    String name = child.getName().getBaseName();

                    if (name.endsWith(EXT_JOB)) {

                        String jobName = name.substring(0, name.length() - 4);

                        ObjectId id = new StringObjectId(calcObjectId(directory, jobName, EXT_JOB));
                        Date date = new Date(child.getContent().getLastModifiedTime());
                        list.add(new RepositoryObject(id, jobName, directory, "-", date,
                                RepositoryObjectType.JOB, "", false));
                    }
                }
            }
        }

        return list;
    } catch (Exception e) {
        throw new KettleException("Unable to get list of jobs in folder with id : " + id_directory, e);
    }
}