Search in sources :

Example 1 with AS400FTP

use of com.ibm.as400.access.AS400FTP in project IBMiProgTool by vzupka.

the class Copy_PC_IBMi method copyToSaveFile.

/**
 * Copy PC file to a new or existing Save File
 *
 * @param targetPathString
 * @param sourcePathString
 * @param toLibrary
 * @return
 */
@SuppressWarnings("UseSpecificCatch")
protected String copyToSaveFile(String sourcePathString, String targetPathString, boolean toLibrary) {
    // Extract individual names (libraryName, fileName, memberName) from the AS400 IFS path
    extractNamesFromIfsPath(targetPathString);
    String saveFilePathString;
    // Copy to LIBRARY
    if (toLibrary) {
        // Save file name is derived from PC file name excluding suffix .savf
        saveFileName = sourcePathString.substring(sourcePathString.lastIndexOf(pcFileSep) + 1, sourcePathString.lastIndexOf(".savf"));
        // Save file path string is derived from the path string of the library by adding the PC file name with suffix
        // .SAVF in upper case
        saveFilePathString = targetPathString + "/" + sourcePathString.substring(sourcePathString.lastIndexOf(pcFileSep) + 1).toUpperCase();
        // Create a new Save File if it does not exist
        try {
            SaveFile saveFile = new SaveFile(remoteServer, libraryName, saveFileName);
            if (!saveFile.exists()) {
                saveFile.create();
            }
        } catch (Exception exc) {
            exc.printStackTrace();
            row = "Error1: " + exc.toString();
            mainWindow.msgVector.add(row);
            mainWindow.showMessages();
            return "ERROR";
        }
        // Copy the PC file to Save file using FTP (File Transfer Protocol)
        AS400FTP ftp = new AS400FTP(remoteServer);
        try {
            // FTP Binary data transfer
            // ftp.setDataTransferType(AS400FTP.BINARY); // not necessary when suffix is .savf
            // FTP Put command
            ftp.put(sourcePathString, saveFilePathString);
            ftp.disconnect();
            row = "Comp: PC file  " + sourcePathString + "  was copied to save file  " + libraryName + "/" + saveFileName + ".";
            mainWindow.msgVector.add(row);
            mainWindow.showMessages();
            return "";
        } catch (Exception exc) {
            exc.printStackTrace();
            row = "Error: Copying PC file  " + sourcePathString + "  to save file  " + libraryName + "/" + saveFileName + "  failed:  " + exc.toString();
            mainWindow.msgVector.add(row);
            mainWindow.showMessages();
            return "ERROR";
        }
    } else // 
    // Copy to IFS FILE
    {
        // Save file name is derived from PC file name excluding suffix .savf
        saveFileName = sourcePathString.substring(sourcePathString.lastIndexOf(pcFileSep) + 1);
        if (!sourcePathString.endsWith(".savf")) {
            row = "Error: Copying PC save file  " + sourcePathString + "  ending with suffix \".savf\" cannot be copied to the existing file  " + targetPathString + "  with a different suffix.";
            mainWindow.msgVector.add(row);
            mainWindow.showMessages();
            return "ERROR";
        } else {
            // Copy the PC file to Save file using FTP (File Transfer Protocol)
            AS400FTP ftp = new AS400FTP(remoteServer);
            try {
                // FTP Binary data transfer
                // ftp.setDataTransferType(AS400FTP.BINARY); // not necessary when suffix is .savf
                // FTP Put command
                ftp.put(sourcePathString, targetPathString);
                ftp.disconnect();
                row = "Comp: PC save file  " + sourcePathString + "  was copied to IFS save file  " + targetPathString + ".";
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
                return "";
            } catch (Exception exc) {
                exc.printStackTrace();
                row = "Error: Copying PC save file  " + sourcePathString + "  to IFS save file  " + targetPathString + "  failed:  " + exc.toString();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
                return "ERROR";
            }
        }
    }
}
Also used : SaveFile(com.ibm.as400.access.SaveFile) AS400FTP(com.ibm.as400.access.AS400FTP) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException)

Example 2 with AS400FTP

use of com.ibm.as400.access.AS400FTP in project IBMiProgTool by vzupka.

the class Copy_IBMi_PC method copyToPcFile.

/**
 * Copying IBM i IFS file or Source member or Save file to PC file;
 * If the PC file does not exist, one is created.
 *
 * @param pcPathString
 * @param as400PathString
 * @param fromWalk
 * @return
 */
protected String copyToPcFile(String as400PathString, String pcPathString, boolean fromWalk) {
    // Path to PC file
    Path pcFilePath = Paths.get(pcPathString);
    IFSFile ifsDirFile = new IFSFile(remoteServer, as400PathString);
    try {
        // Source physical file is a directory and cannot be copied to PC file
        if (ifsDirFile.isSourcePhysicalFile()) {
            row = "Error: Source physical file  " + as400PathString + "  cannot be copied to file  " + pcPathString + ".";
            mainWindow.msgVector.add(row);
            mainWindow.showMessages(noNodes);
            return "ERROR";
        }
        // IFS directory cannot be copied to PC file
        if (ifsDirFile.isDirectory()) {
            row = "Error: IFS directory  " + as400PathString + "  cannot be copied to PC file  " + pcPathString + ".";
            mainWindow.msgVector.add(row);
            mainWindow.showMessages(noNodes);
            return "ERROR";
        }
        // Source physical file MEMBER:
        if (sourcePathString.startsWith("/QSYS.LIB/")) {
            extractNamesFromIfsPath(as400PathString);
            if (sourcePathString.endsWith(".MBR")) {
                // 
                if (pcPathString.endsWith(".savf")) {
                    row = "Error: Source member  " + libraryName + "/" + fileName + "(" + memberName + ")  cannot be copied to PC file  " + pcPathString + "  ending with .savf.";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(noNodes);
                    return "ERROR";
                }
                // 
                // Member to PC file
                // ------
                msgText = copyFromSourceMember(remoteServer, sourcePathString, pcPathString);
                if (msgText.isEmpty()) {
                    row = "Comp: Source member  " + libraryName + "/" + fileName + "(" + memberName + ")  was copied to PC file  " + pcPathString + " using charset " + pcCharset + ".";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(nodes);
                    return "";
                } else {
                    row = "Comp File: Source member  " + libraryName + "/" + fileName + "(" + memberName + ")  was NOT copied to PC file  " + pcPathString + ".";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(noNodes);
                    return "ERROR";
                }
            } else // ---------
            if (ifsDirFile.toString().contains(".LIB") && ifsDirFile.toString().endsWith(".SAVF") && ifsDirFile.getSubtype().equals("SAVF")) {
                msgText = copyFromSaveFile(remoteServer, sourcePathString, pcPathString);
                return msgText;
            }
        } else {
            // From IFS stream file to PC file (no directories are involved)
            try {
                byte[] byteArray = new byte[2000000];
                int bytesRead;
                // ---------------------------------
                if (Files.exists(pcFilePath) && !properties.getProperty("OVERWRITE_FILE").equals("Y")) {
                    row = "Error: IFS file  " + ifsDirFile + "  was NOT copied to the existing file  " + pcPathString + ". Overwriting files is not allowed.";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(noNodes);
                    return "ERROR";
                }
                // ------------------------------------
                if (Files.notExists(pcFilePath)) {
                    Files.createFile(pcFilePath);
                }
                // IFS file with suffix .savf cannot be copied to PC file with different suffix
                if (sourcePathString.endsWith(".savf") && !pcPathString.endsWith(".savf")) {
                    row = "Error: IFS file  " + ifsDirFile + "  ending with suffix \".savf\" cannot be copied to the existing file  " + pcPathString + "  with a different suffix.";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(noNodes);
                    return "ERROR";
                }
                // Copy "save" file from IFS to PC file
                if (sourcePathString.endsWith(".savf") && pcPathString.endsWith(".savf")) {
                    // Copy the PC file to Save file using FTP (File Transfer Protocol)
                    AS400FTP ftp = new AS400FTP(remoteServer);
                    try {
                        // FTP Binary data transfer
                        // ftp.setDataTransferType(AS400FTP.BINARY); // not necessary when suffix is .savf
                        // FTP Get command
                        ftp.get(sourcePathString, pcPathString);
                        ftp.disconnect();
                        row = "Comp: IFS save file  " + sourcePathString + "  was copied to PC  save file  " + pcPathString + ".";
                        mainWindow.msgVector.add(row);
                        mainWindow.showMessages(noNodes);
                        return "";
                    } catch (Exception exc) {
                        exc.printStackTrace();
                        row = "Error: Copying IFS save file  " + sourcePathString + "  to PC  save file  " + pcPathString + "  failed:  " + exc.toString();
                        mainWindow.msgVector.add(row);
                        mainWindow.showMessages(noNodes);
                        return "ERROR";
                    }
                }
                // 
                if (pcCharset.equals("*DEFAULT") && ibmCcsid.equals("*DEFAULT")) {
                    // Open input IFS file
                    IFSFileInputStream ifsInStream = new IFSFileInputStream(remoteServer, as400PathString);
                    // Open the output PC file as buffered output stream
                    OutputStream os = Files.newOutputStream(pcFilePath, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
                    BufferedOutputStream bos = new BufferedOutputStream(os);
                    // Copy IFS file to PC file reading input stream to byte array and using byte buffer for output
                    // Read first portion of bytes
                    bytesRead = ifsInStream.read(byteArray);
                    // Repeat if at least one byte was read
                    while (bytesRead > 0) {
                        // Write out bytes read before
                        bos.write(byteArray, 0, bytesRead);
                        // Read next portion of bytes
                        bytesRead = ifsInStream.read(byteArray);
                    }
                    // Close files
                    bos.close();
                    ifsInStream.close();
                    if (fromWalk) {
                        row = "Info: IFS file  " + as400PathString + "  was copied unchanged (binary) to PC file  " + pcPathString + ".";
                        mainWindow.msgVector.add(row);
                        mainWindow.showMessages(noNodes);
                    } else {
                        row = "Comp: IFS file  " + as400PathString + "  was copied unchanged (binary) to PC file  " + pcPathString + ".";
                        mainWindow.msgVector.add(row);
                        mainWindow.showMessages(noNodes);
                    }
                // 
                // Data conversion is done
                // -----------------------
                } else {
                    // 
                    IFSFile ifsFile = new IFSFile(remoteServer, as400PathString);
                    if (ibmCcsid.equals("*DEFAULT")) {
                        // CCSID attribute of the input file
                        ibmCcsidInt = ifsFile.getCCSID();
                    }
                    // Open input IFS file
                    IFSFileInputStream ifsInStream = new IFSFileInputStream(remoteServer, as400PathString);
                    // Open output text file
                    if (pcCharset.equals("*DEFAULT")) {
                        pcCharset = "ISO-8859-1";
                    }
                    BufferedWriter outfileText = Files.newBufferedWriter(pcFilePath, Charset.forName(pcCharset), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
                    // Copy IFS file to PC file reading input stream to byte array and using byte buffer for output
                    // Read first portion of bytes
                    bytesRead = ifsInStream.read(byteArray);
                    // Repeat if at least one byte was read
                    while (bytesRead > 0) {
                        // Convert input file data using "IBM i CCSID" parameter
                        AS400Text textConverter = new AS400Text(bytesRead, ibmCcsidInt, remoteServer);
                        // Convert byte array buffer to translated data text
                        String str = (String) textConverter.toObject(byteArray);
                        String translatedData = new String(str.getBytes(pcCharset), pcCharset);
                        // Write translated data to the text file
                        outfileText.write(translatedData);
                        // Read next byte array
                        bytesRead = ifsInStream.read(byteArray);
                    }
                    // Close files
                    outfileText.close();
                    ifsInStream.close();
                    if (fromWalk) {
                        row = "Info: IFS file  " + as400PathString + "  was copied to PC file  " + pcPathString + ",  " + ibmCcsid + " -> " + pcCharset + ".";
                        mainWindow.msgVector.add(row);
                        mainWindow.showMessages(noNodes);
                    } else {
                        row = "Comp: IFS file  " + as400PathString + "  was copied to PC file  " + pcPathString + ",  " + ibmCcsid + " -> " + pcCharset + ".";
                        mainWindow.msgVector.add(row);
                        mainWindow.showMessages(noNodes);
                    }
                }
                return "";
            } catch (Exception exc) {
                exc.printStackTrace();
                row = "Error: Copying to PC file " + pcPathString + "  failed.  -  " + exc.toString();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages(noNodes);
                return "ERROR";
            }
        }
    } catch (Exception exc) {
        exc.printStackTrace();
        row = "Error: Copying to PC file " + pcPathString + "  -  " + exc.toString();
        mainWindow.msgVector.add(row);
        mainWindow.showMessages(noNodes);
        return "ERROR";
    }
    return "";
}
Also used : Path(java.nio.file.Path) AS400Text(com.ibm.as400.access.AS400Text) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) AS400FTP(com.ibm.as400.access.AS400FTP) BufferedOutputStream(java.io.BufferedOutputStream) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) IFSFile(com.ibm.as400.access.IFSFile) IFSFileInputStream(com.ibm.as400.access.IFSFileInputStream) BufferedWriter(java.io.BufferedWriter)

Example 3 with AS400FTP

use of com.ibm.as400.access.AS400FTP in project IBMiProgTool by vzupka.

the class Copy_IBMi_PC method copyFromSaveFile.

/**
 * Copy Save File to PC directory or PC file as .savf type.
 *
 * @param remoteServer
 * @param as400PathString
 * @param pcPathString
 * @return
 */
protected String copyFromSaveFile(AS400 remoteServer, String as400PathString, String pcPathString) {
    System.out.println("as400PathString: " + as400PathString);
    AS400FTP ftp = new AS400FTP(remoteServer);
    String filePathString;
    String directoryOrFile;
    try {
        // Get fileName value from the IFS path string
        extractNamesFromIfsPath(as400PathString);
        // 
        if (Files.isDirectory(Paths.get(pcPathString))) {
            filePathString = pcPathString + pcFileSep + saveFileName + ".savf";
            directoryOrFile = "directory";
        } else {
            filePathString = pcPathString;
            directoryOrFile = "file";
        }
        if (!Files.exists(Paths.get(filePathString))) {
            Files.createFile(Paths.get(filePathString));
        } else {
            if (!filePathString.endsWith(".savf")) {
                row = "Error: Save file  " + libraryName + "/" + saveFileName + "  cannot be coppied to the PC file  " + filePathString + ".  Target file name must end with suffix .savf.";
                mainWindow.msgVector.add(row);
                mainWindow.showMessages(noNodes);
                return "ERROR";
            }
        }
        // Binary data transfer
        ftp.setDataTransferType(AS400FTP.BINARY);
        // Get command
        ftp.get(as400PathString, filePathString);
        ftp.disconnect();
        row = "Comp: Save file  " + libraryName + "/" + saveFileName + "  was copied to PC " + directoryOrFile + " " + pcPathString + ".";
        mainWindow.msgVector.add(row);
        mainWindow.showMessages(nodes);
        return "";
    } catch (Exception exc) {
        exc.printStackTrace();
        row = "Error: Copying save file  " + libraryName + "/" + saveFileName + "  -  " + exc.toString() + ".";
        mainWindow.msgVector.add(row);
        mainWindow.showMessages(noNodes);
        return "ERROR";
    }
}
Also used : AS400FTP(com.ibm.as400.access.AS400FTP) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException)

Aggregations

AS400FTP (com.ibm.as400.access.AS400FTP)3 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)3 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)3 AS400Text (com.ibm.as400.access.AS400Text)1 IFSFile (com.ibm.as400.access.IFSFile)1 IFSFileInputStream (com.ibm.as400.access.IFSFileInputStream)1 SaveFile (com.ibm.as400.access.SaveFile)1 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedWriter (java.io.BufferedWriter)1 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1