Search in sources :

Example 1 with CommandCall

use of com.ibm.as400.access.CommandCall 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 CommandCall

use of com.ibm.as400.access.CommandCall 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 3 with CommandCall

use of com.ibm.as400.access.CommandCall 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)

Example 4 with CommandCall

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

the class Copy_IBMi_IBMi method copyToSaveFile.

/**
 * @param sourcePathString
 * @return
 */
protected String copyToSaveFile(String sourcePathString, String targetPathString) {
    // Get object names from IFS path string: qsyslib, library, file, member.
    extractNamesFromIfsPath(targetPathString);
    try {
        // Create target file path
        IFSFile sourcePath = new IFSFile(remoteServer, sourcePathString);
        // Create target file path
        IFSFile targetPath = new IFSFile(remoteServer, targetPathString);
        if (targetPathString.endsWith(".LIB")) {
            // Add file name to directory path
            targetPathString = targetPathString + "/" + sourcePath.getName();
        }
        // Replace .savf suffix to .FILE for the save file
        targetPathString = targetPathString.replace(".savf", ".FILE");
        targetPath = new IFSFile(remoteServer, targetPathString);
        if (targetPath.exists() && !overwriteAllowed) {
            // IFS file exists and overwriting data is NOT allowed
            row = "Error: IFS file  " + sourcePathString + "  cannot be copied to save file  " + libraryName + "/" + fileName + "(" + memberName + ") . Overwriting files is not allowed.";
            mainWindow.msgVector.add(row);
            mainWindow.showMessages(nodes);
            return "ERROR";
        }
        // Copy IFS file to Save file
        String command_CPYFRMSTMF = "CPYFRMSTMF FROMSTMF('" + sourcePathString + "') TOMBR('" + targetPathString + "') MBROPT(*REPLACE) CVTDTA(*AUTO) STMFCCSID(*STMF)";
        // Enable calling CL commands
        CommandCall cmdCall = new CommandCall(remoteServer);
        cmdCall.run(command_CPYFRMSTMF);
        // 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 IFS file to save file with command CPYFRMSTMF  -  " + as400Message.getID() + " " + as400Message.getText();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
                return "ERROR";
            } else {
                row = "Info: Copy IFS file to save file with command CPYFRMSTMF  -  " + as400Message.getID() + " " + as400Message.getText();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
            }
        }
        row = "Comp: IFS file  " + sourcePathString + "  was copied to save file  " + targetPathString + ".";
        mainWindow.msgVector.add(row);
        mainWindow.showMessages();
    } catch (Exception exc) {
        exc.printStackTrace();
        row = "Error: Copy IFS file to save file with command CPYFRMSTMF  -  " + exc.toString();
        mainWindow.msgVector.add(row);
        mainWindow.showMessages();
    }
    msgText = "";
    return msgText;
}
Also used : CommandCall(com.ibm.as400.access.CommandCall) AS400Message(com.ibm.as400.access.AS400Message) IFSFile(com.ibm.as400.access.IFSFile)

Example 5 with CommandCall

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

the class Copy_IBMi_IBMi method copyToSourceMember.

/**
 * @param sourcePathString
 * @param targetPathString
 * @return
 */
protected String copyToSourceMember(String sourcePathString, String targetPathString) {
    // Get object names from IFS path string: qsyslib, library, file, member.
    extractNamesFromIfsPath(targetPathString);
    try {
        // Create source file path
        IFSFile sourcePath = new IFSFile(remoteServer, sourcePathString);
        // Create target file path
        IFSFile targetPath = new IFSFile(remoteServer, targetPathString);
        // Check if member name is correct
        try {
            targetPath.exists();
        } catch (Exception exc) {
            row = "Error: IFS file  " + sourcePathString + "  cannot be copied to source member  " + libraryName + "/" + fileName + "(" + memberName + ") . Member name is invalid.";
            mainWindow.msgVector.add(row);
            mainWindow.showMessages(nodes);
            return "ERROR";
        }
        // Check if overwriting data is allowed
        if (targetPath.exists() && !overwriteAllowed) {
            // IFS file exists and overwriting data is NOT allowed
            row = "Error: IFS file  " + sourcePathString + "  cannot be copied to source member  " + libraryName + "/" + fileName + "(" + memberName + ") . Overwriting files is not allowed.";
            mainWindow.msgVector.add(row);
            mainWindow.showMessages(nodes);
            return "ERROR";
        }
        // Copy IFS file to Member
        String command_CPYFRMSTMF = "CPYFRMSTMF FROMSTMF('" + sourcePathString + "') TOMBR('" + targetPathString + "') MBROPT(*REPLACE) CVTDTA(*AUTO) STMFCCSID(*STMF) DBFCCSID(*FILE)";
        // Enable calling CL commands
        CommandCall cmdCall = new CommandCall(remoteServer);
        cmdCall.run(command_CPYFRMSTMF);
        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 IFS file  " + sourcePathString + "  to source member  " + targetPathString + "  using command CPYFRMSTMF  -  " + as400Message.getID() + " " + as400Message.getText();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
                return "ERROR";
            } else {
                row = "Info: Copy IFS file  " + sourcePathString + "  to source member  " + targetPathString + "  using command CPYFRMSTMF  -  " + as400Message.getID() + " " + as400Message.getText();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
            }
        }
        // Get source type from application paameters
        String sourceType = (String) properties.get("SOURCE_TYPE");
        // parameter
        if (sourceType.equals("*DEFAULT")) {
            // Get default source type for standard source physical file name
            // (QCLSRC, QRPGLESRC, ...)
            sourceType = getDefaultSourceType(fileName);
        }
        String command_CHGPFM = "CHGPFM FILE(" + libraryName + "/" + fileName + ") MBR(" + memberName + ") SRCTYPE(" + sourceType + ")";
        // Perform command CGHPFM to correct source type of the member
        cmdCall.run(command_CHGPFM);
        // Get messages from the command if any
        as400MessageList = cmdCall.getMessageList();
        // Send all messages from the command. After ESCAPE message - return.
        for (AS400Message as400Message : as400MessageList) {
            if (as400Message.getType() == AS400Message.ESCAPE) {
                row = "Error: Command CHGPFM   -  " + as400Message.getID() + " " + as400Message.getText();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
                return "ERROR";
            } else {
                row = "Info: Command CHGPFM  -  " + as400Message.getID() + " " + as400Message.getText();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
            }
        }
        row = "Comp: IFS file  " + sourcePathString + "  was copied to source member  " + targetPathString + ".";
        mainWindow.msgVector.add(row);
        mainWindow.showMessages();
    } catch (Exception exc) {
        exc.printStackTrace();
        row = "Error: Copy IFS file  " + sourcePathString + "  to source member  " + targetPathString + "  using command CPYFRMSTMF  -  " + exc.toString();
        mainWindow.msgVector.add(row);
        mainWindow.showMessages();
    }
    msgText = "";
    return msgText;
}
Also used : CommandCall(com.ibm.as400.access.CommandCall) AS400Message(com.ibm.as400.access.AS400Message) IFSFile(com.ibm.as400.access.IFSFile)

Aggregations

CommandCall (com.ibm.as400.access.CommandCall)17 AS400Message (com.ibm.as400.access.AS400Message)14 IFSFile (com.ibm.as400.access.IFSFile)9 Job (com.ibm.as400.access.Job)2 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)2 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)2 AS400FileRecordDescription (com.ibm.as400.access.AS400FileRecordDescription)1 AS400Text (com.ibm.as400.access.AS400Text)1 IFSFileOutputStream (com.ibm.as400.access.IFSFileOutputStream)1 Record (com.ibm.as400.access.Record)1 RecordFormat (com.ibm.as400.access.RecordFormat)1 SequentialFile (com.ibm.as400.access.SequentialFile)1 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Path (java.nio.file.Path)1 BadLocationException (javax.swing.text.BadLocationException)1 CannotRedoException (javax.swing.undo.CannotRedoException)1 CannotUndoException (javax.swing.undo.CannotUndoException)1