Search in sources :

Example 1 with IFSFile

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

the class Copy_IBMi_IBMi method copyFromSourceFile.

/**
 * Copy Source File (all members) to IFS directory.
 *
 * @param remoteServer
 * @param sourcePathString
 * @param targetPathString
 * @return
 */
protected String copyFromSourceFile(AS400 remoteServer, String sourcePathString, String targetPathString) {
    // Extract individual names (library, file, member) from the path of the source physical file
    extractNamesFromIfsPath(sourcePathString);
    String inLibName = libraryName;
    String inFileName = fileName;
    // Path to the input source file
    String inSourceFilePath = "/QSYS.LIB/" + libraryName + ".LIB/" + fileName + ".FILE";
    // Enable calling CL commands
    CommandCall cmdCall = new CommandCall(remoteServer);
    IFSFile inIfsDirectory = new IFSFile(remoteServer, inSourceFilePath);
    IFSFile outIfsDirectory = new IFSFile(remoteServer, targetPathString);
    try {
        // Check if target is a Source Physical File
        if (targetPathString.endsWith(".FILE") && outIfsDirectory.isSourcePhysicalFile()) {
            // Target is another source file
            // -----------------------------
            // Command CPYSRCF will be used.
            // Get object names from target path string for command "to"
            // parameters
            extractNamesFromIfsPath(targetPathString);
            // parameter TOFILE
            String toFile = libraryName + "/" + fileName;
            if (!overwriteAllowed) {
                // Member is not overwtitten but printed.
                // parameter TOFILE
                toFile = "*PRINT";
                row = "Info: Existing members of the source file  " + libraryName + "/" + fileName + " will not be overwritten but printed. Overwriting files is not allowed.";
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
            }
            // Build text of the command to copy all members
            String command_CPYSRCF = "CPYSRCF FROMFILE(" + inLibName + "/" + inFileName + ") TOFILE(" + toFile + ") FROMMBR(*ALL) TOMBR(*FROMMBR) MBROPT(*REPLACE)";
            // Perform the command
            cmdCall.run(command_CPYSRCF);
            // Get messages from the command if any
            AS400Message[] as400MessageList = cmdCall.getMessageList();
            // return.
            for (AS400Message as400Message : as400MessageList) {
                if (as400Message.getType() == AS400Message.ESCAPE) {
                    row = "Error: Command CPYSRCF   -  " + as400Message.getID() + " " + as400Message.getText();
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages();
                    return "ERROR";
                } else {
                    row = "Info: Command CPYSRCF  -  " + as400Message.getID() + " " + as400Message.getText();
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages();
                }
            }
            if (!overwriteAllowed) {
                row = "Comp: Members of source file " + inLibName + "/" + inFileName + "  of matching names were copied to printer file QSYSPRT.";
            } else {
                row = "Comp: Source file " + inLibName + "/" + inFileName + "  was copied to source file  " + libraryName + "/" + fileName + ".";
            }
            mainWindow.msgVector.add(row);
            mainWindow.showMessages(nodes);
            return "";
        } else {
            // Check if target is a Library or IFS directory
            if (targetPathString.startsWith("/QSYS.LIB")) {
                // Target is a Library
                // -------------------
                // Command CPYF will be used.
                // Get input Library name from the path string of the input Source Physical File
                extractNamesFromIfsPath(inSourceFilePath);
                inLibName = libraryName;
                inFileName = fileName;
                extractNamesFromIfsPath(targetPathString);
                String outLibName = libraryName;
                String commandText = "CPYF FROMFILE(" + inLibName + "/" + inFileName + ")  TOFILE(" + outLibName + "/" + inFileName + ") FROMMBR(*ALL) TOMBR(*FROMMBR) MBROPT(*REPLACE) CRTFILE(*YES) FMTOPT(*MAP)";
                // Enable calling CL commands
                CommandCall command = new CommandCall(remoteServer);
                try {
                    // Run the command CPYF
                    command.run(commandText);
                    // Get messages from the command if any
                    AS400Message[] as400MessageList = command.getMessageList();
                    String msgType;
                    // return.
                    for (AS400Message as400Message : as400MessageList) {
                        if (as400Message.getType() == AS400Message.ESCAPE) {
                            msgType = "Error";
                            row = msgType + ": message from the CPYF command is " + as400Message.getID() + " " + as400Message.getText();
                            mainWindow.msgVector.add(row);
                            mainWindow.showMessages();
                            return "";
                        } else {
                            msgType = "Info";
                            row = msgType + ": message from the CPYF command is " + as400Message.getID() + " " + as400Message.getText();
                            mainWindow.msgVector.add(row);
                            mainWindow.showMessages();
                        }
                    }
                } catch (Exception exc) {
                    exc.printStackTrace();
                    row = "Error: Copying source physical file  " + inSourceFilePath + " - " + exc.toString() + ".";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages();
                    return "ERROR";
                }
                row = "Comp: Source physical file  " + inLibName + "/" + inFileName + "  was copied to library  " + outLibName + ".";
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
                return "";
            } else {
                // Target is an IFS directory
                // --------------------------
                // If the IFS directory name differs from the Source Physical File name
                // ---------------------------------
                // Create NEW directory with the Source Physical File name.
                String ifsSrcName = inIfsDirectory.getName();
                ifsSrcName = ifsSrcName.substring(0, ifsSrcName.indexOf("."));
                String outDirEndName = targetPathString.substring(targetPathString.lastIndexOf("/") + 1);
                if (!ifsSrcName.equals(outDirEndName)) {
                    targetPathString = targetPathString + "/" + ifsSrcName;
                    outIfsDirectory = new IFSFile(remoteServer, targetPathString);
                    outIfsDirectory.mkdir();
                }
                // Continue as if IFS directory had the same name
                // If the IFS directory has the same name as Source Physical File
                // --------------------------------------
                // Copy all members from Source file to the IFS directory
                boolean atLeastOneErrorInMembers = false;
                IFSFile[] ifsFiles = inIfsDirectory.listFiles();
                for (IFSFile ifsFile : ifsFiles) {
                    // Insert new IFS file or rewrite existing IFS file
                    String msgTextMbr = copyFromSourceMember(remoteServer, ifsFile.toString(), targetPathString + "/" + ifsFile.getName());
                    // If at least one message is not empty - note error
                    if (!msgTextMbr.isEmpty()) {
                        atLeastOneErrorInMembers = true;
                    }
                }
                if (!atLeastOneErrorInMembers) {
                    row = "Comp: Source physical file  " + libraryName + "/" + fileName + "  was copied to IFS directory  " + targetPathString + ".";
                    mainWindow.msgVector.add(row);
                } else {
                    row = "Error: Source physical file  " + libraryName + "/" + fileName + "  was NOT completely copied to IFS directory  " + targetPathString + ".";
                    mainWindow.msgVector.add(row);
                }
                mainWindow.showMessages();
                msgText = atLeastOneErrorInMembers ? "ERROR" : "";
                return msgText;
            }
        }
    } catch (Exception exc) {
        exc.printStackTrace();
        row = "Error: Copying from source physical file  " + libraryName + "/" + fileName + "  -  " + exc.toString();
        mainWindow.msgVector.add(row);
        mainWindow.showMessages();
        return "ERROR";
    }
}
Also used : CommandCall(com.ibm.as400.access.CommandCall) AS400Message(com.ibm.as400.access.AS400Message) IFSFile(com.ibm.as400.access.IFSFile)

Example 2 with IFSFile

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

the class Copy_IBMi_IBMi method copy_IBMi_IBMi.

/**
 * Initial method calling further methods for copying from IBM i to IBMi.
 */
protected void copy_IBMi_IBMi() {
    if (remoteServer == null) {
        return;
    }
    inputDirFile = new IFSFile(remoteServer, sourcePathString);
    outputDirFile = new IFSFile(remoteServer, targetPathString);
    // From Save File
    if (sourcePathString.startsWith("/QSYS.LIB") && sourcePathString.toUpperCase().endsWith(".SAVF")) {
        inputDirFile = new IFSFile(remoteServer, sourcePathString.replace(".SAVF", ".FILE"));
    }
    // To Save File
    if (targetPathString.startsWith("/QSYS.LIB") && targetPathString.toUpperCase().endsWith(".SAVF")) {
        outputDirFile = new IFSFile(remoteServer, targetPathString.replace(".savf", ".FILE"));
    }
    // Path prefix is the leading part of the path up to and including the last slash:
    // e.g. /home/vzupka/ILESRC -> /home/vzupka/
    sourcePathStringPrefix = sourcePathString.substring(0, sourcePathString.lastIndexOf("/") + 1);
    // ---------------------------------------
    try {
        if (inputDirFile.isDirectory() && outputDirFile.isDirectory()) {
            // ---------------------------
            if (sourcePathString.startsWith("/QSYS.LIB") && targetPathString.startsWith("/QSYS.LIB")) {
                // From Source file to Source file or Library
                copyFromSourceFile(remoteServer, sourcePathString, targetPathString);
            } else if (sourcePathString.startsWith("/QSYS.LIB") && !targetPathString.startsWith("/QSYS.LIB")) {
                // From Source file to IFS directory
                copyFromSourceFile(remoteServer, sourcePathString, targetPathString);
            } else if (!sourcePathString.startsWith("/QSYS.LIB") && targetPathString.startsWith("/QSYS.LIB")) {
                // From IFS directory to Source file
                copyFromIfsDirectory(sourcePathString, targetPathString, sourcePathStringPrefix);
            } else if (!sourcePathString.startsWith("/QSYS.LIB") && !targetPathString.startsWith("/QSYS.LIB")) {
                // From IFS directory to IFS directory
                copyToIfsDirectory(sourcePathString, targetPathString, sourcePathStringPrefix);
            }
        } else if (inputDirFile.isFile() && outputDirFile.isDirectory()) {
            // ----------------------
            if (sourcePathString.startsWith("/QSYS.LIB") && targetPathString.startsWith("/QSYS.LIB")) {
                // From Member to Source file
                if (sourcePathString.endsWith(".MBR")) {
                    copyFromSourceMember(remoteServer, sourcePathString, targetPathString);
                } else if (sourcePathString.endsWith(".SAVF")) {
                    // From Save file to Save file in different Library
                    copyFromSaveFile(remoteServer, sourcePathString, targetPathString);
                }
            } else if (sourcePathString.startsWith("/QSYS.LIB") && !targetPathString.startsWith("/QSYS.LIB")) {
                if (sourcePathString.endsWith(".MBR")) {
                    // From Member to IFS directory
                    copyFromSourceMember(remoteServer, sourcePathString, targetPathString);
                } else if (sourcePathString.endsWith(".SAVF")) {
                    // From Save file to file in IFS directory
                    copyFromSaveFile(remoteServer, sourcePathString, targetPathString);
                }
            } else if (!sourcePathString.startsWith("/QSYS.LIB") && targetPathString.startsWith("/QSYS.LIB")) {
                if (sourcePathString.endsWith(".savf") && targetPathString.endsWith(".LIB")) {
                    // From IFS file to Save file in a Library
                    copyFromIfsFile(sourcePathString, targetPathString);
                } else if (targetPathString.endsWith(".FILE")) {
                    // From IFS file to Source file
                    copyToSourceFile(remoteServer, sourcePathString, targetPathString);
                }
            } else if (!sourcePathString.startsWith("/QSYS.LIB") && !targetPathString.startsWith("/QSYS.LIB")) {
                // From IFS directory to IFS directory
                copyToIfsDirectory(sourcePathString, targetPathString, sourcePathStringPrefix);
            }
        } else // -----------------
        if (inputDirFile.isFile() && outputDirFile.isFile()) {
            // From file to file
            if (sourcePathString.startsWith("/QSYS.LIB") && targetPathString.startsWith("/QSYS.LIB")) {
                if (sourcePathString.endsWith(".MBR")) {
                    // From Member to Member
                    copyFromSourceMember(remoteServer, sourcePathString, targetPathString);
                } else if (sourcePathString.endsWith(".SAVF")) {
                    // From Save file to Save file in another Library
                    copyFromSaveFile(remoteServer, sourcePathString, targetPathString);
                }
            } else if (sourcePathString.startsWith("/QSYS.LIB") && !targetPathString.startsWith("/QSYS.LIB")) {
                if (sourcePathString.endsWith(".MBR")) {
                    // From Member to IFS file
                    copyFromSourceMember(remoteServer, sourcePathString, targetPathString);
                } else {
                    // From Save file to IFS file
                    copyFromSaveFile(remoteServer, sourcePathString, targetPathString);
                }
            } else if (!sourcePathString.startsWith("/QSYS.LIB") && targetPathString.startsWith("/QSYS.LIB")) {
                if (targetPathString.endsWith(".MBR")) {
                    // From IFS file to Member
                    copyToSourceMember(sourcePathString, targetPathString);
                } else {
                    // From IFS file to Save file
                    copyToSaveFile(sourcePathString, targetPathString);
                }
            } else if (!sourcePathString.startsWith("/QSYS.LIB") && !targetPathString.startsWith("/QSYS.LIB")) {
                // From IFS file to IFS file
                copyToIfsFile(sourcePathString, targetPathString, fromWalk);
            }
        }
    } catch (Exception exc) {
        exc.printStackTrace();
    }
    // Remove message scroll listener (cancel scrolling to the last message)
    mainWindow.scrollMessagePane.getVerticalScrollBar().removeAdjustmentListener(mainWindow.messageScrollPaneAdjustmentListenerMax);
}
Also used : IFSFile(com.ibm.as400.access.IFSFile)

Example 3 with IFSFile

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

the class Copy_IBMi_IBMi method copyToIfsDirectory.

/**
 * Copy IFS directory/file or Source file/member or Save file to IFS
 * directory; If the output IFS file does not exist, one is created.
 *
 * @param sourcePathString
 * @param targetPathString
 * @param sourcePathStringPrefix
 * @return
 */
protected String copyToIfsDirectory(String sourcePathString, String targetPathString, String sourcePathStringPrefix) {
    // Cannot copy to itself
    if (targetPathString.equals(sourcePathString)) {
        return null;
    }
    extractNamesFromIfsPath(sourcePathString);
    // Path to input IFS file
    inputDirFile = new IFSFile(remoteServer, sourcePathString);
    // Path to output IFS file
    outputDirFile = new IFSFile(remoteServer, targetPathString);
    try {
        // --------------------
        if (sourcePathString.startsWith("/QSYS.LIB/")) {
            if (inputDirFile.isDirectory()) {
                if (!targetPathString.startsWith("/QSYS.LIB/")) {
                    // --------------------
                    if (inputDirFile.isSourcePhysicalFile()) {
                        // Copy from Source file to Source file
                        msgText = copyFromSourceFile(remoteServer, sourcePathString, targetPathString);
                        if (msgText.isEmpty()) {
                            row = "Comp: Source physical file  " + libraryName + "/" + fileName + "  was copied to IFS directory  " + targetPathString + ".";
                        } else {
                            row = "Comp: Source physical file  " + libraryName + "/" + fileName + "  was NOT completely copied to IFS directory  " + targetPathString + ".";
                        }
                    } else if (sourcePathString.endsWith(".LIB")) {
                        // Library to IFS directory/file is an ERROR!
                        // -------
                        row = "Error: IBM i library  " + libraryName + "  cannot be copied to IFS.";
                        msgText = "ERROR";
                    }
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(nodes);
                    return msgText;
                } else if (sourcePathString.endsWith(".MBR")) {
                    // Member of Source Physical File to IFS file:
                    // ------
                    msgText = copyFromSourceMember(remoteServer, sourcePathString, targetPathString);
                    if (msgText.isEmpty()) {
                        row = "Comp: Source member  " + libraryName + "/" + fileName + "(" + memberName + ")  was copied to IFS directory  " + targetPathString + ".";
                    } else {
                        row = "Comp: Source member  " + libraryName + "/" + fileName + "(" + memberName + ")  was NOT copied to IFS directory  " + targetPathString + ".";
                    }
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(nodes);
                    return msgText;
                } else if (sourcePathString.endsWith(".SAVF")) {
                    // Save File to IFS directory/file:
                    // ---------
                    copyFromSaveFile(remoteServer, sourcePathString, targetPathString);
                    return msgText;
                }
            } else {
                // If target is Library
                // Copy IFS directory to a Library is not allowed.
                row = "Error: Copy IFS directory to a Library is not allowed.";
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
                return "ERROR";
            }
        } else // 
        // IFS (Integrated File System):
        // -----------------------------
        // Path prefix is the leading part of the path up to and including the
        // last slash:
        // ifsPathStringPrefix = sourcePathString.substring(0,
        // sourcePathString.indexOf(ifsPath.getName()));
        {
            if (inputDirFile.isDirectory()) {
                if (!targetPathString.startsWith("/QSYS.LIB/")) {
                    try {
                        // Create the first shadow directory in IFS target directory.
                        // The name of the directory is the target IFS path string plus source file name.
                        // (Source file name is source path string minus prefix.)
                        targetPathString = targetPathString + "/" + inputDirFile.toString().substring(sourcePathStringPrefix.length());
                        outputDirFile = new IFSFile(remoteServer, targetPathString);
                        if (!outputDirFile.exists()) {
                            outputDirFile.mkdir();
                            row = "Info: IFS directory  " + targetPathString + "  was created.";
                            mainWindow.msgVector.add(row);
                            mainWindow.showMessages(nodes);
                            msgText = "";
                        }
                    } catch (Exception exc) {
                        msgText = "ERROR";
                        exc.printStackTrace();
                        row = "Error: Copying IFS directory  " + sourcePathString + "  to IFS directory  " + targetPathString + "  -  " + exc.toString() + ".";
                        mainWindow.msgVector.add(row);
                        mainWindow.showMessages(nodes);
                        // Fatal error - no continuation is possible
                        return "ERROR";
                    }
                    // Create parameter for the next method call
                    inputDirFile = new IFSFile(remoteServer, sourcePathString);
                    // Create nested shadow directories in IFS target directory
                    msgText = walkIfsDirectory_CreateNestedIfsDirectories(inputDirFile, targetPathString);
                    // Copy IFS files to appropriate IFS shadow directories
                    msgText = copyIfsFilesToIfsDirectories(inputDirFile, targetPathString, sourcePathStringPrefix);
                    if (msgText.isEmpty()) {
                        row = "Comp: IFS directory  " + sourcePathString + "  was copied to IFS directory  " + targetPathString + ".";
                        mainWindow.msgVector.add(row);
                        mainWindow.showMessages(nodes);
                    } else {
                        row = "Comp: IFS directory  " + sourcePathString + "  was NOT completely copied to IFS directory  " + targetPathString + ".";
                        mainWindow.msgVector.add(row);
                        mainWindow.showMessages(nodes);
                    }
                    return msgText;
                }
            // 
            } else // 
            // IFS stream file: to IFS directory
            // ----------------
            {
                // inputDirFile = new IFSFile(remoteServer, sourcePathString);
                // If the output IFS file does not exist - create an empty file
                // and continue.
                // -------------------------------------
                // IFS file = IFS direcrory + IFS file
                String newTargetPathString = targetPathString + "/" + inputDirFile.getName();
                outputDirFile = new IFSFile(remoteServer, targetPathString);
                if (!inputDirFile.exists()) {
                    inputDirFile.createNewFile();
                    row = "Info: IFS directory  " + targetPathString + "  was created.";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages();
                    msgText = "";
                }
                // Copy to IFS file
                msgText = copyToIfsFile(inputDirFile.toString(), newTargetPathString, notFromWalk);
                if (msgText.isEmpty()) {
                    row = "Comp: IFS file  " + sourcePathString + "  was copied to IFS directory  " + targetPathString + ".";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(nodes);
                } else {
                    row = "Comp: IFS file  " + sourcePathString + "  was NOT copied to IFS directory  " + targetPathString + ".";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(nodes);
                }
                return msgText;
            }
        // IFS directory to Library is NOT applicable
        // ------------------------------------------
        }
    } catch (Exception exc) {
        exc.printStackTrace();
        row = "Error: Copying  " + sourcePathString + "  to  " + targetPathString + "  -  " + exc.toString() + ".";
        mainWindow.msgVector.add(row);
        mainWindow.showMessages(nodes);
    }
    return "";
}
Also used : IFSFile(com.ibm.as400.access.IFSFile)

Example 4 with IFSFile

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

the class Copy_IBMi_IBMi method copyFromSaveFile.

/**
 * Copy Save File to IFS file.
 *
 * @param remoteServer
 * @param sourcePathString
 * @param targetPathString
 * @return
 */
protected String copyFromSaveFile(AS400 remoteServer, String sourcePathString, String targetPathString) {
    // Enable calling CL commands
    CommandCall cmdCall = new CommandCall(remoteServer);
    IFSFile sourcePath = new IFSFile(remoteServer, sourcePathString);
    IFSFile targetPath = new IFSFile(remoteServer, targetPathString);
    // Extract individual names (library, file, member) from the AS400 IFS path
    extractNamesFromIfsPath(sourcePathString);
    String inLibName = libraryName;
    String inFileName = saveFileName;
    // Extract individual names (library, file, member) from the AS400 IFS
    // path
    extractNamesFromIfsPath(targetPathString);
    try {
        // ----------
        if (targetPathString.startsWith("/QSYS.LIB")) {
            // Build command string
            String command_CRTDUPOBJ = "CRTDUPOBJ OBJ(" + inFileName + ") FROMLIB(" + inLibName + ") OBJTYPE(*FILE) " + "TOLIB(" + libraryName + ") NEWOBJ(*OBJ) DATA(*YES)";
            IFSFile newTargetPath = targetPath;
            // For copy to Library, derive new target file path (by appending source file name)
            if (targetPathString.endsWith(".LIB")) {
                newTargetPath = new IFSFile(remoteServer, targetPathString + "/" + sourcePath.getName());
            }
            // Check if the file to copy already exists and if can be overwritten.
            if (newTargetPath.exists() && !overwriteAllowed) {
                row = "Error: Save file  " + inLibName + "/" + fileName + "  was NOT copied. Overwriting is not allowed.";
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
                return "ERROR";
            }
            // Perform the command. This command does not allow overwriting files with matching name,
            // even if allowed by "Overwrite data" parameter.
            cmdCall.run(command_CRTDUPOBJ);
            // Get messages from the command if any
            AS400Message[] as400MessageList = cmdCall.getMessageList();
            // Send all messages from the command. After ESCAPE message - return.
            for (AS400Message as400Message : as400MessageList) {
                if (as400Message.getType() == AS400Message.ESCAPE) {
                    row = "Error: Copy save file with command CRTDUPOBJ   -  " + as400Message.getID() + " " + as400Message.getText();
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages();
                    return "ERROR";
                } else {
                    row = "Info: Copy save file with command CRTDUPOBJ  -  " + as400Message.getID() + " " + as400Message.getText();
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages();
                }
            }
            row = "Comp: Save file  " + libraryName + "/" + fileName + "  was copied to library  " + targetPathString + ".";
            mainWindow.msgVector.add(row);
            mainWindow.showMessages();
            return "";
        } else // To IFS directory
        // ----------------
        {
            // Extract individual names (library, file, member) from the AS400 IFS path
            extractNamesFromIfsPath(sourcePathString);
            String toStmf = targetPathString;
            if (targetPath.isDirectory()) {
                toStmf = targetPathString + "/" + sourcePath.getName();
            }
            sourcePathString = sourcePathString.replace(".SAVF", ".FILE");
            toStmf = toStmf.replace(".FILE", ".savf");
            IFSFile newTargetPath = new IFSFile(remoteServer, toStmf);
            // Check if the file to copy already exists and if can be overwritten.
            if (newTargetPath.exists() && !overwriteAllowed) {
                row = "Error: Save file  " + inLibName + "/" + inFileName + "  was NOT copied to existing save file  " + newTargetPath.getName().replace(".SAVF", ".savf") + ". Overwriting is not allowed.";
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
                return "ERROR";
            }
            // Copy from save file to IFS file
            String command_CPYTOSTMF = "CPYTOSTMF FROMMBR('" + sourcePathString + "') " + "TOSTMF('" + toStmf + "') STMFOPT(*REPLACE)";
            command_CPYTOSTMF = command_CPYTOSTMF.replace(".SAVF", ".savf");
            // Perform the command
            cmdCall.run(command_CPYTOSTMF);
            // Get messages from the command if any
            AS400Message[] as400MessageList = cmdCall.getMessageList();
            // return.
            for (AS400Message as400Message : as400MessageList) {
                if (as400Message.getType() == AS400Message.ESCAPE) {
                    row = "Error: Copy save file with command CPYTOSTMF   -  " + as400Message.getID() + " " + as400Message.getText();
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages();
                    return "ERROR";
                } else {
                    row = "Info: Copy save file with command CPYTOSTMF  -  " + as400Message.getID() + " " + as400Message.getText();
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages();
                }
            }
            row = "Comp: Save file  " + libraryName + "/" + fileName + "  was copied to IFS directory  " + targetPathString + ".";
            mainWindow.msgVector.add(row);
            mainWindow.showMessages();
            return "";
        }
    } catch (Exception exc) {
        exc.printStackTrace();
        row = "Error: Copying save file  " + libraryName + "/" + fileName + "  -  " + exc.toString() + ".";
        mainWindow.msgVector.add(row);
        mainWindow.showMessages();
        return "ERROR";
    }
}
Also used : CommandCall(com.ibm.as400.access.CommandCall) AS400Message(com.ibm.as400.access.AS400Message) IFSFile(com.ibm.as400.access.IFSFile)

Example 5 with IFSFile

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

the class Copy_IBMi_IBMi method copyFromSourceMember.

/**
 * Copy Source member to Source file or Library.
 *
 * @param remoteServer
 * @param sourcePathString
 * @param targetPathString
 * @return
 */
protected String copyFromSourceMember(AS400 remoteServer, String sourcePathString, String targetPathString) {
    msgText = "";
    // Get object names from source path string: qsyslib, library, file,
    // member.
    extractNamesFromIfsPath(sourcePathString);
    // Save the object names for command "from" parameters
    String inLibName = libraryName;
    String inFileName = fileName;
    String inMbrName = memberName;
    // Build new member name appended by source type
    String typedMemberName = inMbrName;
    String sourceType;
    if (properties.get("SOURCE_TYPE").equals("*DEFAULT")) {
        // Get default source type for standard source physical file name (QCLSRC, QRPGLESRC, ...)
        sourceType = getDefaultSourceType(fileName);
    } else {
        // Get source type from combo box
        sourceType = (String) properties.get("SOURCE_TYPE");
    }
    // Add the source type to member name
    typedMemberName += "." + sourceType;
    // Create source file path
    IFSFile sourcePath = new IFSFile(remoteServer, sourcePathString);
    // Create target file path
    IFSFile targetPath = new IFSFile(remoteServer, targetPathString);
    try {
        // Enable calling CL commands
        CommandCall cmdCall = new CommandCall(remoteServer);
        String newTargetPathString = targetPathString;
        // ------------------------
        if (targetPath.isSourcePhysicalFile()) {
            // For both Source file or member, CPYSRCPF command is used.
            // Get object names from target path string for command "to"
            // parameters
            extractNamesFromIfsPath(targetPathString);
            // Target is a directory (source file)
            if (targetPath.isDirectory()) {
                // If target is IFS directory, the new file path will be:
                // directory + typed member name
                newTargetPathString = targetPathString + "/" + inMbrName + ".MBR";
            // Target is file (member)
            } else {
                // If target is IFS file, the new file path will be:
                // file's directory (prefix up to last "/") + typed member name
                newTargetPathString = targetPathString;
            }
            // Check if member names are equal and overwriting data is allowed
            targetPath = new IFSFile(remoteServer, newTargetPathString);
            if (targetPath.exists() && !overwriteAllowed) {
                // IFS file exists and overwriting data is NOT allowed
                row = "Error: Source member " + inLibName + "/" + inFileName + "(" + inMbrName + ") " + "cannot be copied source member  " + libraryName + "/" + fileName + "(" + memberName + ") . Overwriting files is not allowed.";
                mainWindow.msgVector.add(row);
                mainWindow.showMessages(nodes);
                return "ERROR";
            }
            // 
            // Copy Source Physical File command
            // ---------------------------------
            String command_CPYSRCF = "CPYSRCF FROMFILE(" + inLibName + "/" + inFileName + ") " + "TOFILE(" + libraryName + "/" + fileName + ") FROMMBR(" + inMbrName + ") TOMBR(" + memberName + ") MBROPT(*REPLACE)";
            // Perform the command
            cmdCall.run(command_CPYSRCF);
            int sourceCcsid = sourcePath.getCCSID();
            sourceCcsidStr = String.valueOf(sourceCcsid);
            int targetCcsid = targetPath.getCCSID();
            targetCcsidStr = String.valueOf(targetCcsid);
            // Get messages from the command if any
            AS400Message[] as400MessageList = cmdCall.getMessageList();
            // Send all messages from the command. After ESCAPE message - return.
            for (AS400Message as400Message : as400MessageList) {
                if (as400Message.getType() == AS400Message.ESCAPE) {
                    row = "Error: Copy source member  " + inLibName + "/" + inFileName + "(" + inMbrName + ")  using command CPYSRCF   -  " + as400Message.getID() + " " + as400Message.getText();
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages();
                    return "ERROR";
                } else {
                    row = "Info: Copy source member  " + inLibName + "/" + inFileName + "(" + inMbrName + ")  using command CPYSRCF  -  " + as400Message.getID() + " " + as400Message.getText();
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages();
                }
            }
            row = "Info: Source member " + inLibName + "/" + inFileName + "(" + inMbrName + ") was copied to source file  " + libraryName + "/" + fileName + "(" + memberName + "). Convert " + sourceCcsidStr + " -> " + targetCcsidStr + ".";
            mainWindow.msgVector.add(row);
            mainWindow.showMessages(nodes);
            return "";
        } else {
            if (targetPath.isDirectory()) {
                // To directory
                // ------------
                // If target is IFS directory, the new file path will be:
                // directory + typed member name
                newTargetPathString = targetPathString + "/" + typedMemberName;
            // To file
            // -------
            } else {
                // If target is IFS file, the new file path will be:
                // file's directory (prefix up to last "/") + typed member name
                newTargetPathString = targetPathString.substring(0, targetPathString.lastIndexOf("/")) + "/" + typedMemberName;
            }
        }
        // For both directory and file continue.
        // ---------------------------
        targetPath = new IFSFile(remoteServer, newTargetPathString);
        // Check if overwriting data is allowed
        if (targetPath.exists() && !overwriteAllowed) {
            // IFS file exists and overwriting data is NOT allowed
            row = "Error: Source member " + libraryName + "/" + fileName + "(" + typedMemberName + ") cannot be copied to IFS directory " + targetPathString + ". Overwriting files is not allowed.";
            mainWindow.msgVector.add(row);
            mainWindow.showMessages(nodes);
            return "ERROR";
        }
        // 
        // Copy source member to IFS file command
        // ------------------------------
        String commandText = "CPYTOSTMF FROMMBR('" + sourcePathString + "') TOSTMF('" + newTargetPathString + "') STMFOPT(*REPLACE) " + "CVTDTA(*AUTO) STMFCCSID(*STMF) ENDLINFMT(*LF)";
        // Perform the command
        cmdCall.run(commandText);
        // Obtain CCSID attribute of the target IFS file
        // targetPath = new IFSFile(remoteServer, newTargetPathString);
        int sourceCcsid = sourcePath.getCCSID();
        sourceCcsidStr = String.valueOf(sourceCcsid);
        int targetCcsid = 0;
        if (targetPath.exists()) {
            // CCSID attribute of IFS file
            targetCcsid = targetPath.getCCSID();
        } else {
            // CCSID attribute of source file
            targetCcsid = sourcePath.getCCSID();
        }
        targetCcsidStr = String.valueOf(targetCcsid);
        // Get messages from the command if any
        AS400Message[] as400MessageList = cmdCall.getMessageList();
        // Send all messages from the command. After ESCAPE message - return.
        for (AS400Message as400Message : as400MessageList) {
            if (as400Message.getType() == AS400Message.ESCAPE) {
                row = "Error: Copy source member using command CPYTOSTMF to IFS file with CCSID " + targetCcsidStr + ".  -  " + as400Message.getID() + " " + as400Message.getText();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
                return "ERROR";
            } else {
                row = "Info: Copy source member using command CPYTOSTMF to IFS file with CCSID " + targetCcsidStr + ".  -  " + as400Message.getID() + " " + as400Message.getText();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
            }
        }
        // Positive completion message
        row = "Comp: Source member  " + sourcePathString + "  with CCSID " + sourceCcsidStr + " was copied to IFS file  " + newTargetPathString + "  with CCSID attribute " + targetCcsidStr + ".";
        mainWindow.msgVector.add(row);
        mainWindow.showMessages();
    } catch (Exception exc) {
        exc.printStackTrace();
        row = "Error: Copy source member using command CPYTOSTMF to IFS file with CCSID " + targetCcsidStr + ".  -  " + exc.toString();
        mainWindow.msgVector.add(row);
        mainWindow.showMessages();
    }
    return msgText;
}
Also used : CommandCall(com.ibm.as400.access.CommandCall) AS400Message(com.ibm.as400.access.AS400Message) IFSFile(com.ibm.as400.access.IFSFile)

Aggregations

IFSFile (com.ibm.as400.access.IFSFile)44 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)14 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)14 AS400Text (com.ibm.as400.access.AS400Text)11 CommandCall (com.ibm.as400.access.CommandCall)9 AS400Message (com.ibm.as400.access.AS400Message)8 Path (java.nio.file.Path)7 AS400FileRecordDescription (com.ibm.as400.access.AS400FileRecordDescription)5 IFSFileInputStream (com.ibm.as400.access.IFSFileInputStream)5 Record (com.ibm.as400.access.Record)5 RecordFormat (com.ibm.as400.access.RecordFormat)5 SequentialFile (com.ibm.as400.access.SequentialFile)5 BadLocationException (javax.swing.text.BadLocationException)5 Point (java.awt.Point)4 PatternSyntaxException (java.util.regex.PatternSyntaxException)4 IFSFileOutputStream (com.ibm.as400.access.IFSFileOutputStream)3 DecimalFormat (java.text.DecimalFormat)3 CannotRedoException (javax.swing.undo.CannotRedoException)3 CannotUndoException (javax.swing.undo.CannotUndoException)3 BufferedWriter (java.io.BufferedWriter)2