Search in sources :

Example 11 with ResultFile

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

the class JobEntryFTP method addFilenameToResultFilenames.

private void addFilenameToResultFilenames(Result result, Job parentJob, String filename) throws KettleException {
    if (isaddresult) {
        FileObject targetFile = null;
        try {
            targetFile = KettleVFS.getFileObject(filename, this);
            // Add to the result files...
            ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, targetFile, parentJob.getJobname(), toString());
            resultFile.setComment(BaseMessages.getString(PKG, "JobEntryFTP.Downloaded", serverName));
            result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.FileAddedToResult", filename));
            }
        } catch (Exception e) {
            throw new KettleException(e);
        } finally {
            try {
                targetFile.close();
                targetFile = null;
            } catch (Exception e) {
            // Ignore close errors
            }
        }
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) FileObject(org.apache.commons.vfs2.FileObject) ResultFile(org.pentaho.di.core.ResultFile) FTPException(com.enterprisedt.net.ftp.FTPException) 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) UnknownHostException(java.net.UnknownHostException)

Example 12 with ResultFile

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

the class JobEntryMail method execute.

public Result execute(Result result, int nr) {
    File masterZipfile = null;
    // Send an e-mail...
    // create some properties and get the default Session
    Properties props = new Properties();
    if (Utils.isEmpty(server)) {
        logError(BaseMessages.getString(PKG, "JobMail.Error.HostNotSpecified"));
        result.setNrErrors(1L);
        result.setResult(false);
        return result;
    }
    String protocol = "smtp";
    if (usingSecureAuthentication) {
        if (secureConnectionType.equals("TLS")) {
            // Allow TLS authentication
            props.put("mail.smtp.starttls.enable", "true");
        } else {
            protocol = "smtps";
            // required to get rid of a SSL exception :
            // nested exception is:
            // javax.net.ssl.SSLException: Unsupported record version Unknown
            props.put("mail.smtps.quitwait", "false");
        }
    }
    props.put("mail." + protocol + ".host", environmentSubstitute(server));
    if (!Utils.isEmpty(port)) {
        props.put("mail." + protocol + ".port", environmentSubstitute(port));
    }
    if (log.isDebug()) {
        props.put("mail.debug", "true");
    }
    if (usingAuthentication) {
        props.put("mail." + protocol + ".auth", "true");
    /*
       * authenticator = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new
       * PasswordAuthentication( StringUtil.environmentSubstitute(Const.NVL(authenticationUser, "")),
       * StringUtil.environmentSubstitute(Const.NVL(authenticationPassword, "")) ); } };
       */
    }
    Session session = Session.getInstance(props);
    session.setDebug(log.isDebug());
    try {
        // create a message
        Message msg = new MimeMessage(session);
        // set message priority
        if (usePriority) {
            String priority_int = "1";
            if (priority.equals("low")) {
                priority_int = "3";
            }
            if (priority.equals("normal")) {
                priority_int = "2";
            }
            // (String)int between 1= high and 3 = low.
            msg.setHeader("X-Priority", priority_int);
            msg.setHeader("Importance", importance);
            // seems to be needed for MS Outlook.
            // where it returns a string of high /normal /low.
            msg.setHeader("Sensitivity", sensitivity);
        // Possible values are normal, personal, private, company-confidential
        }
        // Set Mail sender (From)
        String sender_address = environmentSubstitute(replyAddress);
        if (!Utils.isEmpty(sender_address)) {
            String sender_name = environmentSubstitute(replyName);
            if (!Utils.isEmpty(sender_name)) {
                sender_address = sender_name + '<' + sender_address + '>';
            }
            msg.setFrom(new InternetAddress(sender_address));
        } else {
            throw new MessagingException(BaseMessages.getString(PKG, "JobMail.Error.ReplyEmailNotFilled"));
        }
        // set Reply to addresses
        String reply_to_address = environmentSubstitute(replyToAddresses);
        if (!Utils.isEmpty(reply_to_address)) {
            // Split the mail-address: space separated
            String[] reply_Address_List = environmentSubstitute(reply_to_address).split(" ");
            InternetAddress[] address = new InternetAddress[reply_Address_List.length];
            for (int i = 0; i < reply_Address_List.length; i++) {
                address[i] = new InternetAddress(reply_Address_List[i]);
            }
            msg.setReplyTo(address);
        }
        // Split the mail-address: space separated
        String[] destinations = environmentSubstitute(destination).split(" ");
        InternetAddress[] address = new InternetAddress[destinations.length];
        for (int i = 0; i < destinations.length; i++) {
            address[i] = new InternetAddress(destinations[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, address);
        String realCC = environmentSubstitute(getDestinationCc());
        if (!Utils.isEmpty(realCC)) {
            // Split the mail-address Cc: space separated
            String[] destinationsCc = realCC.split(" ");
            InternetAddress[] addressCc = new InternetAddress[destinationsCc.length];
            for (int i = 0; i < destinationsCc.length; i++) {
                addressCc[i] = new InternetAddress(destinationsCc[i]);
            }
            msg.setRecipients(Message.RecipientType.CC, addressCc);
        }
        String realBCc = environmentSubstitute(getDestinationBCc());
        if (!Utils.isEmpty(realBCc)) {
            // Split the mail-address BCc: space separated
            String[] destinationsBCc = realBCc.split(" ");
            InternetAddress[] addressBCc = new InternetAddress[destinationsBCc.length];
            for (int i = 0; i < destinationsBCc.length; i++) {
                addressBCc[i] = new InternetAddress(destinationsBCc[i]);
            }
            msg.setRecipients(Message.RecipientType.BCC, addressBCc);
        }
        String realSubject = environmentSubstitute(subject);
        if (!Utils.isEmpty(realSubject)) {
            msg.setSubject(realSubject);
        }
        msg.setSentDate(new Date());
        StringBuilder messageText = new StringBuilder();
        String endRow = isUseHTML() ? "<br>" : Const.CR;
        String realComment = environmentSubstitute(comment);
        if (!Utils.isEmpty(realComment)) {
            messageText.append(realComment).append(Const.CR).append(Const.CR);
        }
        if (!onlySendComment) {
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.Job")).append(endRow);
            messageText.append("-----").append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.JobName") + "    : ").append(parentJob.getJobMeta().getName()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.JobDirectory") + "  : ").append(parentJob.getJobMeta().getRepositoryDirectory()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.JobEntry") + "   : ").append(getName()).append(endRow);
            messageText.append(Const.CR);
        }
        if (includeDate) {
            messageText.append(endRow).append(BaseMessages.getString(PKG, "JobMail.Log.Comment.MsgDate") + ": ").append(XMLHandler.date2string(new Date())).append(endRow).append(endRow);
        }
        if (!onlySendComment && result != null) {
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.PreviousResult") + ":").append(endRow);
            messageText.append("-----------------").append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.JobEntryNr") + "         : ").append(result.getEntryNr()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.Errors") + "               : ").append(result.getNrErrors()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.LinesRead") + "           : ").append(result.getNrLinesRead()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.LinesWritten") + "        : ").append(result.getNrLinesWritten()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.LinesInput") + "          : ").append(result.getNrLinesInput()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.LinesOutput") + "         : ").append(result.getNrLinesOutput()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.LinesUpdated") + "        : ").append(result.getNrLinesUpdated()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.LinesRejected") + "       : ").append(result.getNrLinesRejected()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.Status") + "  : ").append(result.getExitStatus()).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.Result") + "               : ").append(result.getResult()).append(endRow);
            messageText.append(endRow);
        }
        if (!onlySendComment && (!Utils.isEmpty(environmentSubstitute(contactPerson)) || !Utils.isEmpty(environmentSubstitute(contactPhone)))) {
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.ContactInfo") + " :").append(endRow);
            messageText.append("---------------------").append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.PersonToContact") + " : ").append(environmentSubstitute(contactPerson)).append(endRow);
            messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.Tel") + "  : ").append(environmentSubstitute(contactPhone)).append(endRow);
            messageText.append(endRow);
        }
        // Include the path to this job entry...
        if (!onlySendComment) {
            JobTracker jobTracker = parentJob.getJobTracker();
            if (jobTracker != null) {
                messageText.append(BaseMessages.getString(PKG, "JobMail.Log.Comment.PathToJobentry") + ":").append(endRow);
                messageText.append("------------------------").append(endRow);
                addBacktracking(jobTracker, messageText);
                if (isUseHTML()) {
                    messageText.replace(0, messageText.length(), messageText.toString().replace(Const.CR, endRow));
                }
            }
        }
        MimeMultipart parts = new MimeMultipart();
        // put the text in the
        MimeBodyPart part1 = new MimeBodyPart();
        // Attached files counter
        int nrattachedFiles = 0;
        if (useHTML) {
            if (!Utils.isEmpty(getEncoding())) {
                part1.setContent(messageText.toString(), "text/html; " + "charset=" + getEncoding());
            } else {
                part1.setContent(messageText.toString(), "text/html; " + "charset=ISO-8859-1");
            }
        } else {
            part1.setText(messageText.toString());
        }
        parts.addBodyPart(part1);
        if (includingFiles && result != null) {
            List<ResultFile> resultFiles = result.getResultFilesList();
            if (resultFiles != null && !resultFiles.isEmpty()) {
                if (!zipFiles) {
                    // 
                    for (ResultFile resultFile : resultFiles) {
                        FileObject file = resultFile.getFile();
                        if (file != null && file.exists()) {
                            boolean found = false;
                            for (int i = 0; i < fileType.length; i++) {
                                if (fileType[i] == resultFile.getType()) {
                                    found = true;
                                }
                            }
                            if (found) {
                                // create a data source
                                MimeBodyPart files = new MimeBodyPart();
                                URLDataSource fds = new URLDataSource(file.getURL());
                                // get a data Handler to manipulate this file type;
                                files.setDataHandler(new DataHandler(fds));
                                // include the file in the data source
                                files.setFileName(file.getName().getBaseName());
                                // insist on base64 to preserve line endings
                                files.addHeader("Content-Transfer-Encoding", "base64");
                                // add the part with the file in the BodyPart();
                                parts.addBodyPart(files);
                                nrattachedFiles++;
                                logBasic("Added file '" + fds.getName() + "' to the mail message.");
                            }
                        }
                    }
                } else {
                    // create a single ZIP archive of all files
                    masterZipfile = new File(System.getProperty("java.io.tmpdir") + Const.FILE_SEPARATOR + environmentSubstitute(zipFilename));
                    ZipOutputStream zipOutputStream = null;
                    try {
                        zipOutputStream = new ZipOutputStream(new FileOutputStream(masterZipfile));
                        for (ResultFile resultFile : resultFiles) {
                            boolean found = false;
                            for (int i = 0; i < fileType.length; i++) {
                                if (fileType[i] == resultFile.getType()) {
                                    found = true;
                                }
                            }
                            if (found) {
                                FileObject file = resultFile.getFile();
                                ZipEntry zipEntry = new ZipEntry(file.getName().getBaseName());
                                zipOutputStream.putNextEntry(zipEntry);
                                // Now put the content of this file into this archive...
                                BufferedInputStream inputStream = new BufferedInputStream(KettleVFS.getInputStream(file));
                                try {
                                    int c;
                                    while ((c = inputStream.read()) >= 0) {
                                        zipOutputStream.write(c);
                                    }
                                } finally {
                                    inputStream.close();
                                }
                                zipOutputStream.closeEntry();
                                nrattachedFiles++;
                                logBasic("Added file '" + file.getName().getURI() + "' to the mail message in a zip archive.");
                            }
                        }
                    } catch (Exception e) {
                        logError("Error zipping attachement files into file [" + masterZipfile.getPath() + "] : " + e.toString());
                        logError(Const.getStackTracker(e));
                        result.setNrErrors(1);
                    } finally {
                        if (zipOutputStream != null) {
                            try {
                                zipOutputStream.finish();
                                zipOutputStream.close();
                            } catch (IOException e) {
                                logError("Unable to close attachement zip file archive : " + e.toString());
                                logError(Const.getStackTracker(e));
                                result.setNrErrors(1);
                            }
                        }
                    }
                    // Now attach the master zip file to the message.
                    if (result.getNrErrors() == 0) {
                        // create a data source
                        MimeBodyPart files = new MimeBodyPart();
                        FileDataSource fds = new FileDataSource(masterZipfile);
                        // get a data Handler to manipulate this file type;
                        files.setDataHandler(new DataHandler(fds));
                        // include the file in the data source
                        files.setFileName(fds.getName());
                        // add the part with the file in the BodyPart();
                        parts.addBodyPart(files);
                    }
                }
            }
        }
        int nrEmbeddedImages = 0;
        if (embeddedimages != null && embeddedimages.length > 0) {
            FileObject imageFile = null;
            for (int i = 0; i < embeddedimages.length; i++) {
                String realImageFile = environmentSubstitute(embeddedimages[i]);
                String realcontenID = environmentSubstitute(contentids[i]);
                if (messageText.indexOf("cid:" + realcontenID) < 0) {
                    if (log.isDebug()) {
                        log.logDebug("Image [" + realImageFile + "] is not used in message body!");
                    }
                } else {
                    try {
                        boolean found = false;
                        imageFile = KettleVFS.getFileObject(realImageFile, this);
                        if (imageFile.exists() && imageFile.getType() == FileType.FILE) {
                            found = true;
                        } else {
                            log.logError("We can not find [" + realImageFile + "] or it is not a file");
                        }
                        if (found) {
                            // Create part for the image
                            MimeBodyPart messageBodyPart = new MimeBodyPart();
                            // Load the image
                            URLDataSource fds = new URLDataSource(imageFile.getURL());
                            messageBodyPart.setDataHandler(new DataHandler(fds));
                            // Setting the header
                            messageBodyPart.setHeader("Content-ID", "<" + realcontenID + ">");
                            // Add part to multi-part
                            parts.addBodyPart(messageBodyPart);
                            nrEmbeddedImages++;
                            log.logBasic("Image '" + fds.getName() + "' was embedded in message.");
                        }
                    } catch (Exception e) {
                        log.logError("Error embedding image [" + realImageFile + "] in message : " + e.toString());
                        log.logError(Const.getStackTracker(e));
                        result.setNrErrors(1);
                    } finally {
                        if (imageFile != null) {
                            try {
                                imageFile.close();
                            } catch (Exception e) {
                            /* Ignore */
                            }
                        }
                    }
                }
            }
        }
        if (nrEmbeddedImages > 0 && nrattachedFiles == 0) {
            // If we need to embedd images...
            // We need to create a "multipart/related" message.
            // otherwise image will appear as attached file
            parts.setSubType("related");
        }
        // put all parts together
        msg.setContent(parts);
        Transport transport = null;
        try {
            transport = session.getTransport(protocol);
            String authPass = getPassword(authenticationPassword);
            if (usingAuthentication) {
                if (!Utils.isEmpty(port)) {
                    transport.connect(environmentSubstitute(Const.NVL(server, "")), Integer.parseInt(environmentSubstitute(Const.NVL(port, ""))), environmentSubstitute(Const.NVL(authenticationUser, "")), authPass);
                } else {
                    transport.connect(environmentSubstitute(Const.NVL(server, "")), environmentSubstitute(Const.NVL(authenticationUser, "")), authPass);
                }
            } else {
                transport.connect();
            }
            transport.sendMessage(msg, msg.getAllRecipients());
        } finally {
            if (transport != null) {
                transport.close();
            }
        }
    } catch (IOException e) {
        logError("Problem while sending message: " + e.toString());
        result.setNrErrors(1);
    } catch (MessagingException mex) {
        logError("Problem while sending message: " + mex.toString());
        result.setNrErrors(1);
        Exception ex = mex;
        do {
            if (ex instanceof SendFailedException) {
                SendFailedException sfex = (SendFailedException) ex;
                Address[] invalid = sfex.getInvalidAddresses();
                if (invalid != null) {
                    logError("    ** Invalid Addresses");
                    for (int i = 0; i < invalid.length; i++) {
                        logError("         " + invalid[i]);
                        result.setNrErrors(1);
                    }
                }
                Address[] validUnsent = sfex.getValidUnsentAddresses();
                if (validUnsent != null) {
                    logError("    ** ValidUnsent Addresses");
                    for (int i = 0; i < validUnsent.length; i++) {
                        logError("         " + validUnsent[i]);
                        result.setNrErrors(1);
                    }
                }
                Address[] validSent = sfex.getValidSentAddresses();
                if (validSent != null) {
                    // System.out.println("    ** ValidSent Addresses");
                    for (int i = 0; i < validSent.length; i++) {
                        logError("         " + validSent[i]);
                        result.setNrErrors(1);
                    }
                }
            }
            if (ex instanceof MessagingException) {
                ex = ((MessagingException) ex).getNextException();
            } else {
                ex = null;
            }
        } while (ex != null);
    } finally {
        if (masterZipfile != null && masterZipfile.exists()) {
            masterZipfile.delete();
        }
    }
    if (result.getNrErrors() > 0) {
        result.setResult(false);
    } else {
        result.setResult(true);
    }
    return result;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) ZipEntry(java.util.zip.ZipEntry) DataHandler(javax.activation.DataHandler) Properties(java.util.Properties) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) BufferedInputStream(java.io.BufferedInputStream) FileDataSource(javax.activation.FileDataSource) FileObject(org.apache.commons.vfs2.FileObject) SendFailedException(javax.mail.SendFailedException) URLDataSource(javax.activation.URLDataSource) MessagingException(javax.mail.MessagingException) JobTracker(org.pentaho.di.core.gui.JobTracker) IOException(java.io.IOException) ResultFile(org.pentaho.di.core.ResultFile) Date(java.util.Date) MessagingException(javax.mail.MessagingException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) SendFailedException(javax.mail.SendFailedException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) IOException(java.io.IOException) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) Transport(javax.mail.Transport) ResultFile(org.pentaho.di.core.ResultFile) File(java.io.File) Session(javax.mail.Session)

Example 13 with ResultFile

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

the class JobEntryMoveFiles method addFileToResultFilenames.

private void addFileToResultFilenames(String fileaddentry, Result result, Job parentJob) {
    try {
        ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject(fileaddentry, this), parentJob.getJobname(), toString());
        result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
        if (log.isDebug()) {
            logDebug(" ------ ");
            logDebug(BaseMessages.getString(PKG, "JobMoveFiles.Log.FileAddedToResultFilesName", fileaddentry));
        }
    } catch (Exception e) {
        log.logError(BaseMessages.getString(PKG, "JobMoveFiles.Error.AddingToFilenameResult"), fileaddentry + "" + 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 14 with ResultFile

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

the class JobEntryMysqlBulkFile method execute.

public Result execute(Result previousResult, int nr) {
    String LimitNbrLignes = "";
    String ListOfColumn = "*";
    String strHighPriority = "";
    String OutDumpText = "";
    String OptionEnclosed = "";
    String FieldSeparator = "";
    String LinesTerminated = "";
    Result result = previousResult;
    result.setResult(false);
    // Let's check the filename ...
    if (filename != null) {
        // User has specified a file, We can continue ...
        String realFilename = getRealFilename();
        File file = new File(realFilename);
        if (file.exists() && iffileexists == 2) {
            // the file exists and user want to Fail
            result.setResult(false);
            result.setNrErrors(1);
            logError(BaseMessages.getString(PKG, "JobMysqlBulkFile.FileExists1.Label") + realFilename + BaseMessages.getString(PKG, "JobMysqlBulkFile.FileExists2.Label"));
        } else if (file.exists() && iffileexists == 1) {
            // the file exists and user want to do nothing
            result.setResult(true);
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobMysqlBulkFile.FileExists1.Label") + realFilename + BaseMessages.getString(PKG, "JobMysqlBulkFile.FileExists2.Label"));
            }
        } else {
            if (file.exists() && iffileexists == 0) {
                // File exists and user want to renamme it with unique name
                // Format Date
                // Try to clean filename (without wildcard)
                String wildcard = realFilename.substring(realFilename.length() - 4, realFilename.length());
                if (wildcard.substring(0, 1).equals(".")) {
                    // Find wildcard
                    realFilename = realFilename.substring(0, realFilename.length() - 4) + "_" + StringUtil.getFormattedDateTimeNow(true) + wildcard;
                } else {
                    // did not find wildcard
                    realFilename = realFilename + "_" + StringUtil.getFormattedDateTimeNow(true);
                }
                logDebug(BaseMessages.getString(PKG, "JobMysqlBulkFile.FileNameChange1.Label") + realFilename + BaseMessages.getString(PKG, "JobMysqlBulkFile.FileNameChange1.Label"));
            }
            // User has specified an existing file, We can continue ...
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobMysqlBulkFile.FileExists1.Label") + realFilename + BaseMessages.getString(PKG, "JobMysqlBulkFile.FileExists2.Label"));
            }
            if (connection != null) {
                // User has specified a connection, We can continue ...
                Database db = new Database(this, connection);
                db.shareVariablesWith(this);
                try {
                    db.connect(parentJob.getTransactionId(), null);
                    // Get schemaname
                    String realSchemaname = environmentSubstitute(schemaname);
                    // Get tablename
                    String realTablename = environmentSubstitute(tablename);
                    if (db.checkTableExists(realTablename)) {
                        // The table existe, We can continue ...
                        if (log.isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobMysqlBulkFile.TableExists1.Label") + realTablename + BaseMessages.getString(PKG, "JobMysqlBulkFile.TableExists2.Label"));
                        }
                        // Add schemaname (Most the time Schemaname.Tablename)
                        if (schemaname != null) {
                            realTablename = realSchemaname + "." + realTablename;
                        }
                        // Set the Limit lines
                        if (Const.toInt(getRealLimitlines(), 0) > 0) {
                            LimitNbrLignes = "LIMIT " + getRealLimitlines();
                        }
                        // Set list of Column, if null get all columns (*)
                        if (getRealListColumn() != null) {
                            ListOfColumn = MysqlString(getRealListColumn());
                        }
                        // Fields separator
                        if (getRealSeparator() != null && outdumpvalue == 0) {
                            FieldSeparator = "FIELDS TERMINATED BY '" + Const.replace(getRealSeparator(), "'", "''") + "'";
                        }
                        // Lines Terminated by
                        if (getRealLineterminated() != null && outdumpvalue == 0) {
                            LinesTerminated = "LINES TERMINATED BY '" + Const.replace(getRealLineterminated(), "'", "''") + "'";
                        }
                        // High Priority ?
                        if (isHighPriority()) {
                            strHighPriority = "HIGH_PRIORITY";
                        }
                        if (getRealEnclosed() != null && outdumpvalue == 0) {
                            if (isOptionEnclosed()) {
                                OptionEnclosed = "OPTIONALLY ";
                            }
                            OptionEnclosed = OptionEnclosed + "ENCLOSED BY '" + Const.replace(getRealEnclosed(), "'", "''") + "'";
                        }
                        // OutFile or Dumpfile
                        if (outdumpvalue == 0) {
                            OutDumpText = "INTO OUTFILE";
                        } else {
                            OutDumpText = "INTO DUMPFILE";
                        }
                        String FILEBulkFile = "SELECT " + strHighPriority + " " + ListOfColumn + " " + OutDumpText + " '" + realFilename + "' " + FieldSeparator + " " + OptionEnclosed + " " + LinesTerminated + " FROM " + realTablename + " " + LimitNbrLignes + " LOCK IN SHARE MODE";
                        try {
                            if (log.isDetailed()) {
                                logDetailed(FILEBulkFile);
                            }
                            // Run the SQL
                            PreparedStatement ps = db.prepareSQL(FILEBulkFile);
                            ps.execute();
                            // Everything is OK...we can disconnect now
                            db.disconnect();
                            if (isAddFileToResult()) {
                                // Add filename to output files
                                ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject(realFilename, this), parentJob.getJobname(), toString());
                                result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                            }
                            result.setResult(true);
                        } catch (SQLException je) {
                            db.disconnect();
                            result.setNrErrors(1);
                            logError(BaseMessages.getString(PKG, "JobMysqlBulkFile.Error.Label") + " " + je.getMessage());
                        } catch (KettleFileException e) {
                            logError(BaseMessages.getString(PKG, "JobMysqlBulkFile.Error.Label") + e.getMessage());
                            result.setNrErrors(1);
                        }
                    } else {
                        // Of course, the table should have been created already before the bulk load operation
                        db.disconnect();
                        result.setNrErrors(1);
                        if (log.isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobMysqlBulkFile.TableNotExists1.Label") + realTablename + BaseMessages.getString(PKG, "JobMysqlBulkFile.TableNotExists2.Label"));
                        }
                    }
                } catch (KettleDatabaseException dbe) {
                    db.disconnect();
                    result.setNrErrors(1);
                    logError(BaseMessages.getString(PKG, "JobMysqlBulkFile.Error.Label") + " " + dbe.getMessage());
                }
            } else {
                // No database connection is defined
                result.setNrErrors(1);
                logError(BaseMessages.getString(PKG, "JobMysqlBulkFile.Nodatabase.Label"));
            }
        }
    } else {
        // No file was specified
        result.setNrErrors(1);
        logError(BaseMessages.getString(PKG, "JobMysqlBulkFile.Nofilename.Label"));
    }
    return result;
}
Also used : KettleFileException(org.pentaho.di.core.exception.KettleFileException) SQLException(java.sql.SQLException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) Database(org.pentaho.di.core.database.Database) PreparedStatement(java.sql.PreparedStatement) ResultFile(org.pentaho.di.core.ResultFile) File(java.io.File) ResultFile(org.pentaho.di.core.ResultFile) Result(org.pentaho.di.core.Result)

Example 15 with ResultFile

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

the class JobEntryPGPDecryptFiles method addFileToResultFilenames.

private void addFileToResultFilenames(String fileaddentry, Result result, Job parentJob) {
    try {
        ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject(fileaddentry), parentJob.getJobname(), toString());
        result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
        if (isDebug()) {
            logDebug(" ------ ");
            logDebug(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Log.FileAddedToResultFilesName", fileaddentry));
        }
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobPGPDecryptFiles.Error.AddingToFilenameResult"), fileaddentry + "" + 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)

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