Search in sources :

Example 1 with ResultFile

use of org.apache.hop.core.ResultFile in project hop by apache.

the class AbstractFileErrorHandler method getWriter.

/**
 * returns the OutputWiter if exists. Otherwhise it will create a new one.
 *
 * @return
 * @throws HopException
 */
Writer getWriter(Object source) throws HopException {
    try {
        Writer outputStreamWriter = writers.get(source);
        if (outputStreamWriter != null) {
            return outputStreamWriter;
        }
        FileObject file = getReplayFilename(destinationDirectory, processingFilename, dateString, fileExtension, source);
        ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, file, baseTransform.getPipelineMeta().getName(), baseTransform.getTransformName());
        baseTransform.addResultFile(resultFile);
        try {
            if (encoding == null) {
                outputStreamWriter = new OutputStreamWriter(HopVfs.getOutputStream(file, false));
            } else {
                outputStreamWriter = new OutputStreamWriter(HopVfs.getOutputStream(file, false), encoding);
            }
        } catch (Exception e) {
            throw new HopException(BaseMessages.getString(PKG, "AbstractFileErrorHandler.Exception.CouldNotCreateFileErrorHandlerForFile") + file.getName().getURI(), e);
        }
        writers.put(source, outputStreamWriter);
        return outputStreamWriter;
    } catch (HopFileException e) {
        throw new HopException(BaseMessages.getString(PKG, "AbstractFileErrorHandler.Exception.CouldNotCreateFileErrorHandlerForFile"), e);
    }
}
Also used : HopFileException(org.apache.hop.core.exception.HopFileException) HopException(org.apache.hop.core.exception.HopException) OutputStreamWriter(java.io.OutputStreamWriter) FileObject(org.apache.commons.vfs2.FileObject) ResultFile(org.apache.hop.core.ResultFile) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) HopException(org.apache.hop.core.exception.HopException) IOException(java.io.IOException) HopFileException(org.apache.hop.core.exception.HopFileException)

Example 2 with ResultFile

use of org.apache.hop.core.ResultFile in project hop by apache.

the class BaseFileInputTransform method init.

/**
 * Initialize transform before execute.
 */
@Override
public boolean init() {
    initErrorHandling();
    meta.additionalOutputFields.normalize();
    data.files = meta.getFileInputList(this);
    data.currentFileIndex = 0;
    // If there are missing files,
    // fail if we don't ignore errors
    // 
    Result previousResult = getPipeline().getPreviousResult();
    Map<String, ResultFile> resultFiles = (previousResult != null) ? previousResult.getResultFiles() : null;
    if ((previousResult == null || resultFiles == null || resultFiles.size() == 0) && data.files.nrOfMissingFiles() > 0 && !meta.inputFiles.acceptingFilenames && !meta.errorHandling.errorIgnored) {
        logError(BaseMessages.getString(PKG, "BaseFileInputTransform.Log.Error.NoFilesSpecified"));
        return false;
    }
    return super.init();
}
Also used : ResultFile(org.apache.hop.core.ResultFile) Result(org.apache.hop.core.Result)

Example 3 with ResultFile

use of org.apache.hop.core.ResultFile in project hop by apache.

the class ActionFileCompare method execute.

@Override
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) {
            file1 = HopVfs.getFileObject(realFilename1);
            file2 = HopVfs.getFileObject(realFilename2);
            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, parentWorkflow.getWorkflowName(), toString());
                    resultFile.setComment(BaseMessages.getString(PKG, "ActionWaitForFile.FilenameAdded"));
                    result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                    resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, file2, parentWorkflow.getWorkflowName(), toString());
                    resultFile.setComment(BaseMessages.getString(PKG, "ActionWaitForFile.FilenameAdded"));
                    result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                }
            } else {
                if (!file1.exists()) {
                    logError(BaseMessages.getString(PKG, "ActionFileCompare.ERROR_0004_File1_Does_Not_Exist", realFilename1));
                }
                if (!file2.exists()) {
                    logError(BaseMessages.getString(PKG, "ActionFileCompare.ERROR_0005_File2_Does_Not_Exist", realFilename2));
                }
                result.setResult(false);
                result.setNrErrors(1);
            }
        } else {
            logError(BaseMessages.getString(PKG, "ActionFileCompare.ERROR_0006_Need_Two_Filenames"));
        }
    } catch (Exception e) {
        result.setResult(false);
        result.setNrErrors(1);
        logError(BaseMessages.getString(PKG, "ActionFileCompare.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.apache.hop.core.ResultFile) HopFileException(org.apache.hop.core.exception.HopFileException) HopXmlException(org.apache.hop.core.exception.HopXmlException) IOException(java.io.IOException) ICheckResult(org.apache.hop.core.ICheckResult) Result(org.apache.hop.core.Result)

Example 4 with ResultFile

use of org.apache.hop.core.ResultFile in project hop by apache.

the class ActionFtp method addFilenameToResultFilenames.

private void addFilenameToResultFilenames(Result result, IWorkflowEngine<WorkflowMeta> parentWorkflow, String filename) throws HopException {
    if (isAddResult) {
        FileObject targetFile = null;
        try {
            targetFile = HopVfs.getFileObject(filename);
            // Add to the result files...
            ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, targetFile, parentWorkflow.getWorkflowName(), toString());
            resultFile.setComment(BaseMessages.getString(PKG, "ActionFTP.Downloaded", serverName));
            result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "ActionFTP.FileAddedToResult", filename));
            }
        } catch (Exception e) {
            throw new HopException(e);
        } finally {
            try {
                targetFile.close();
                targetFile = null;
            } catch (Exception e) {
            // Ignore close errors
            }
        }
    }
}
Also used : HopException(org.apache.hop.core.exception.HopException) FileObject(org.apache.commons.vfs2.FileObject) ResultFile(org.apache.hop.core.ResultFile) HopException(org.apache.hop.core.exception.HopException) HopXmlException(org.apache.hop.core.exception.HopXmlException) UnknownHostException(java.net.UnknownHostException)

Example 5 with ResultFile

use of org.apache.hop.core.ResultFile in project hop by apache.

the class PropertyInput method openNextFile.

private boolean openNextFile() {
    InputStream fis = null;
    try {
        if (!meta.isFileField()) {
            if (data.filenr >= data.files.nrOfFiles()) {
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "PropertyInput.Log.FinishedProcessing"));
                }
                return false;
            }
            // Is this the last file?
            data.last_file = (data.filenr == data.files.nrOfFiles() - 1);
            data.file = data.files.getFile(data.filenr);
            // Move file pointer ahead!
            data.filenr++;
        } else {
            // Get row from input rowset & set row busy!
            data.readrow = getRow();
            if (data.readrow == null) {
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "PropertyInput.Log.FinishedProcessing"));
                }
                return false;
            }
            if (first) {
                first = false;
                data.inputRowMeta = getInputRowMeta();
                data.outputRowMeta = data.inputRowMeta.clone();
                meta.getFields(data.outputRowMeta, getTransformName(), null, null, this, metadataProvider);
                // Get total previous fields
                data.totalpreviousfields = data.inputRowMeta.size();
                // Create convert meta-data objects that will contain Date & Number formatters
                data.convertRowMeta = data.outputRowMeta.cloneToType(IValueMeta.TYPE_STRING);
                // Check is filename field is provided
                if (Utils.isEmpty(meta.getDynamicFilenameField())) {
                    logError(BaseMessages.getString(PKG, "PropertyInput.Log.NoField"));
                    throw new HopException(BaseMessages.getString(PKG, "PropertyInput.Log.NoField"));
                }
                // cache the position of the field
                if (data.indexOfFilenameField < 0) {
                    data.indexOfFilenameField = getInputRowMeta().indexOfValue(meta.getDynamicFilenameField());
                    if (data.indexOfFilenameField < 0) {
                        // The field is unreachable !
                        logError(BaseMessages.getString(PKG, "PropertyInput.Log.ErrorFindingField") + "[" + meta.getDynamicFilenameField() + "]");
                        throw new HopException(BaseMessages.getString(PKG, "PropertyInput.Exception.CouldnotFindField", meta.getDynamicFilenameField()));
                    }
                }
            }
            // End if first
            String filename = getInputRowMeta().getString(data.readrow, data.indexOfFilenameField);
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "PropertyInput.Log.FilenameInStream", meta.getDynamicFilenameField(), filename));
            }
            data.file = HopVfs.getFileObject(filename);
        // Check if file exists!
        }
        // Check if file is empty
        data.filename = HopVfs.getFilename(data.file);
        // Add additional fields?
        if (meta.getShortFileNameField() != null && meta.getShortFileNameField().length() > 0) {
            data.shortFilename = data.file.getName().getBaseName();
        }
        if (meta.getPathField() != null && meta.getPathField().length() > 0) {
            data.path = HopVfs.getFilename(data.file.getParent());
        }
        if (meta.isHiddenField() != null && meta.isHiddenField().length() > 0) {
            data.hidden = data.file.isHidden();
        }
        if (meta.getExtensionField() != null && meta.getExtensionField().length() > 0) {
            data.extension = data.file.getName().getExtension();
        }
        if (meta.getLastModificationDateField() != null && meta.getLastModificationDateField().length() > 0) {
            data.lastModificationDateTime = new Date(data.file.getContent().getLastModifiedTime());
        }
        if (meta.getUriField() != null && meta.getUriField().length() > 0) {
            data.uriName = data.file.getName().getURI();
        }
        if (meta.getRootUriField() != null && meta.getRootUriField().length() > 0) {
            data.rootUriName = data.file.getName().getRootURI();
        }
        if (meta.getSizeField() != null && meta.getSizeField().length() > 0) {
            data.size = data.file.getContent().getSize();
        }
        if (meta.resetRowNumber()) {
            data.rownr = 0;
        }
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "PropertyInput.Log.OpeningFile", data.file.toString()));
        }
        if (meta.isAddResultFile()) {
            // Add this to the result file names...
            ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, data.file, getPipelineMeta().getName(), getTransformName());
            resultFile.setComment(BaseMessages.getString(PKG, "PropertyInput.Log.FileAddedResult"));
            addResultFile(resultFile);
        }
        fis = data.file.getContent().getInputStream();
        if (data.propfiles) {
            // load properties file
            data.pro = new Properties();
            data.pro.load(fis);
            data.it = data.pro.keySet().iterator();
        } else {
            // create wini object
            data.wini = new Wini();
            if (!Utils.isEmpty(data.realEncoding)) {
                data.wini.getConfig().setFileEncoding(Charset.forName(data.realEncoding));
            }
            // load INI file
            data.wini.load(fis);
            if (data.realSection != null) {
                // just one section
                data.iniSection = data.wini.get(data.realSection);
                if (data.iniSection == null) {
                    throw new HopException(BaseMessages.getString(PKG, "PropertyInput.Error.CanNotFindSection", data.realSection, "" + data.file.getName()));
                }
            } else {
                // We need to fetch all sections
                data.itSection = data.wini.keySet().iterator();
                data.iniSection = data.wini.get(data.itSection.next().toString());
            }
            data.iniIt = data.iniSection.keySet().iterator();
        }
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "PropertyInput.Log.FileOpened", data.file.toString()));
            logDetailed(BaseMessages.getString(PKG, "PropertyInput.log.TotalKey", "" + (data.propfiles ? data.pro.size() : data.iniSection.size()), HopVfs.getFilename(data.file)));
        }
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "PropertyInput.Log.UnableToOpenFile", "" + data.filenr, data.file.toString(), e.toString()));
        stopAll();
        setErrors(1);
        return false;
    } finally {
        BaseTransform.closeQuietly(fis);
    }
    return true;
}
Also used : Wini(org.ini4j.Wini) HopException(org.apache.hop.core.exception.HopException) InputStream(java.io.InputStream) ResultFile(org.apache.hop.core.ResultFile) Properties(java.util.Properties) Date(java.util.Date) HopException(org.apache.hop.core.exception.HopException)

Aggregations

ResultFile (org.apache.hop.core.ResultFile)51 HopException (org.apache.hop.core.exception.HopException)35 FileObject (org.apache.commons.vfs2.FileObject)18 HopFileException (org.apache.hop.core.exception.HopFileException)14 IOException (java.io.IOException)12 Result (org.apache.hop.core.Result)11 HopXmlException (org.apache.hop.core.exception.HopXmlException)11 HopTransformException (org.apache.hop.core.exception.HopTransformException)8 ICheckResult (org.apache.hop.core.ICheckResult)7 OutputStream (java.io.OutputStream)6 Date (java.util.Date)6 File (java.io.File)5 HopValueException (org.apache.hop.core.exception.HopValueException)5 ValueMetaString (org.apache.hop.core.row.value.ValueMetaString)5 BufferedOutputStream (java.io.BufferedOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4 RowMetaAndData (org.apache.hop.core.RowMetaAndData)4 LocalFile (org.apache.commons.vfs2.provider.local.LocalFile)3 RowMeta (org.apache.hop.core.row.RowMeta)3 BufferedInputStream (java.io.BufferedInputStream)2