Search in sources :

Example 6 with CommandCall

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

the class Copy_PC_IBMi method copyFromPcDirectory.

/**
 * Copying a PC directory to IFS directory or to Library/Source File.
 *
 * @param sourcePathString
 * @param targetPathString
 * @param sourcePathStringPrefix
 */
protected void copyFromPcDirectory(String sourcePathString, String targetPathString, String sourcePathStringPrefix) {
    // Get object and member names (libraryName, fileName, memberName
    extractNamesFromIfsPath(targetPathString);
    if (!sourcePathString.contains(pcFileSep + ".")) {
        IFSFile targetPath = new IFSFile(remoteServer, targetPathString + "/" + sourcePathString.substring(sourcePathStringPrefix.length()));
        // --------------------------------
        if (!targetPathString.startsWith("/QSYS.LIB")) {
            // Create the first shadow IFS directory
            try {
                if (!targetPath.exists()) {
                    // Create new directory in target IFS directory with PC file ending name
                    targetPath.mkdir();
                    // Add message text to the message table and show it
                    row = "Info: Directory  " + sourcePathString + "  was created in IFS directory  " + targetPathString + ".";
                    mainWindow.msgVector.add(row);
                }
            } catch (Exception exc) {
                exc.printStackTrace();
                row = exc.toString();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
                return;
            }
            // Create shadow directories in the first shadow IFS directory created above
            msgText = walkPcDirectory_CreateNestedIfsDirectories(remoteServer, sourcePathString, targetPath);
            // Copy PC files to appropriate IFS shadow directories
            msgText = copyPcFilesToIfsDirectories(sourcePathString, targetPath, sourcePathStringPrefix);
            // Construct a message in the message table and show it
            if (msgText.isEmpty()) {
                row = "Comp: PC directory  " + sourcePathString + "  was copied to IFS directory  " + targetPathString + ".";
            } else {
                row = "Comp: PC directory  " + sourcePathString + "  was NOT completely copied to IFS directory  " + targetPathString + ".";
            }
            mainWindow.msgVector.add(row);
            mainWindow.showMessages();
        }
        // ------------------
        if (targetPathString.startsWith("/QSYS.LIB")) {
            if (ibmCcsid.equals("*DEFAULT")) {
                ibmCcsid = "500";
            }
            // Create new source physical file in the library and call copyToSourceFile() method
            if (targetPathString.endsWith(".LIB")) {
                // Extract individual names (libraryName, fileName, memberName) from the AS400 IFS path
                extractNamesFromIfsPath(targetPathString);
                // Extract a new file name from the last component of the PC path string.
                // The file name extracted by the method before was null!
                fileName = sourcePathString.substring(sourcePathString.lastIndexOf(pcFileSep) + 1);
                // Create new source physical file
                String sourceRecordLength = (String) properties.get("SOURCE_RECORD_LENGTH");
                // The property contains all digits. It was made certain when the user entered the value.
                int sourceRecordLengthInt = Integer.parseInt(sourceRecordLength);
                // Source record must contain 12 byte prefix to data line: sequence number (6) and date (6)
                sourceRecordLengthInt += 12;
                // Build command CRTSRCPF to create a source physical file with certain CCSID in the library
                String commandText = "CRTSRCPF FILE(" + libraryName + "/" + fileName + ") " + "RCDLEN(" + sourceRecordLengthInt + ") CCSID(" + ibmCcsid + ")";
                // Enable calling CL commands
                CommandCall cmdCall = new CommandCall(remoteServer);
                try {
                    // Run the command
                    cmdCall.run(commandText);
                    // 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: Create source physical file  " + libraryName + "/" + fileName + " using CRTSRCPF command  -  " + as400Message.getID() + " " + as400Message.getText();
                            mainWindow.msgVector.add(row);
                            mainWindow.showMessages();
                            return;
                        } else {
                            row = "Info: Create source physical file  " + libraryName + "/" + fileName + " using CRTSRCPF command  -  " + as400Message.getID() + " " + as400Message.getText();
                            mainWindow.msgVector.add(row);
                            mainWindow.showMessages();
                        }
                    }
                } catch (Exception exc) {
                    exc.printStackTrace();
                    row = "Error: Copying PC directory  " + sourcePathString + "  to source physical File  " + libraryName + "/" + fileName + "  -  " + exc.toString();
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages();
                    // Must return! Could be fatal error (e. g. lock of the source file)!
                    return;
                }
                // Copy members to the new source physical file in a library
                msgText = copyToSourceFile(sourcePathString, targetPathString + "/" + fileName + ".FILE");
                if (msgText.isEmpty()) {
                    row = "Comp: PC directory  " + sourcePathString + "  was copied to source physical file  " + libraryName + "/" + fileName + ".";
                } else {
                    row = "Comp: PC directory  " + sourcePathString + "  was NOT completely copied to source physical file  " + libraryName + "/" + fileName + ".";
                }
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
            } else // ---------------------------------------------------------
            if (targetPathString.endsWith(".FILE")) {
                // Copy single PC file to source file to existing member or as a new member
                msgText = copyToSourceFile(sourcePathString, targetPathString);
                if (msgText.isEmpty()) {
                    row = "Comp: PC directory  " + sourcePathString + "  was copied to source physical file  " + libraryName + "/" + fileName + ".";
                } else {
                    row = "Comp: PC directory  " + sourcePathString + "  was NOT completely copied to source physical file  " + libraryName + "/" + fileName + ".";
                }
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
            } else // ----------------------------------------
            if (targetPathString.endsWith(".MBR")) {
                row = "Error: PC directory  " + sourcePathString + "  cannot be copied to a source physical file member  " + libraryName + "/" + fileName + "(" + memberName + ").";
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
            }
        }
    }
}
Also used : CommandCall(com.ibm.as400.access.CommandCall) AS400Message(com.ibm.as400.access.AS400Message) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) IFSFile(com.ibm.as400.access.IFSFile)

Example 7 with CommandCall

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

the class Copy_PC_IBMi method copyToSourceMember.

/**
 * Copy PC text file to the IBM i source member.
 *
 * @param sourcePathString
 * @param targetPathString
 * @param fromDirectory
 * @return
 */
protected String copyToSourceMember(String sourcePathString, String targetPathString, boolean fromDirectory) {
    if (ibmCcsid.equals("*DEFAULT")) {
        ibmCcsid = "500";
        ibmCcsidInt = 500;
    }
    // Extract individual names (libraryName, fileName, memberName) from the AS400 IFS path
    extractNamesFromIfsPath(targetPathString);
    if (sourcePathString.endsWith(pcFileSep)) {
        sourcePathString = sourcePathString.substring(0, sourcePathString.lastIndexOf(pcFileSep));
    }
    // Get source type from the PC file name
    String sourceType;
    if (sourcePathString.lastIndexOf(".") > 0) {
        // If file name has postfix with a dot, the posfix will be the source type
        sourceType = sourcePathString.substring(sourcePathString.lastIndexOf(".") + 1);
    } else {
        // If file name does not have a postfix (there is no dot), the source type will be MBR
        sourceType = "MBR";
    }
    // Path to input PC text file
    Path inTextFile = Paths.get(sourcePathString);
    // Path to the output source member
    String outMemberPathString = "/QSYS.LIB/" + libraryName + ".LIB/" + fileName + ".FILE" + "/" + memberName + ".MBR";
    IFSFile ifsMbr = new IFSFile(remoteServer, outMemberPathString);
    String clrPfmCommand;
    String chgPfmCommand;
    // Enable calling CL commands
    CommandCall cmdCall = new CommandCall(remoteServer);
    msgText = "";
    try {
        // Open PC input file regarded as a text file.
        if (pcCharset.equals("*DEFAULT")) {
            // Decode input using its encoding. Ignore PC charset parameter.
            inFile = Files.newBufferedReader(inTextFile);
        } else {
            // Decode input using PC charset parameter.
            inFile = Files.newBufferedReader(inTextFile, Charset.forName(pcCharset));
        }
        try {
            // Read the first text line
            textLine = inFile.readLine();
        // If an error is found in the first line of the file,
        // processing is broken, the error is caught (see catch blocks), a message is reported,
        // and no member is created.
        } catch (Exception exc) {
            exc.printStackTrace();
            row = "Error: Data of the PC file  " + sourcePathString + "  cannot be copied to the source physical file  " + libraryName + "/" + fileName + "  -  " + exc.toString();
            mainWindow.msgVector.add(row);
            mainWindow.showMessages();
            msgText = "ERROR";
            return msgText;
        }
        // -----------------------------------
        if (ifsMbr.exists() && !properties.getProperty("OVERWRITE_FILE").equals("Y")) {
            row = "Info: PC file  " + sourcePathString + "  cannot be copied to the existing source physical member  " + libraryName + "/" + fileName + "(" + memberName + "). Overwriting files is not allowed.";
            mainWindow.msgVector.add(row);
            mainWindow.showMessages();
            return "ERROR";
        }
        // SEQUENCE NUMBER and DATE information fields in the first 6 + 6 positions.
        try {
            // Non-empty member can be checked for information fields if the line is longer than 12
            if (textLine != null && textLine.length() > 12) {
                int seqNbr = Integer.parseInt(textLine.substring(0, 6));
                int date = Integer.parseInt(textLine.substring(6, 12));
                seqAndDatePresent = true;
            } else {
                // Otherwise the line cannot have information fields
                seqAndDatePresent = false;
            }
        } catch (NumberFormatException nfe) {
            seqAndDatePresent = false;
        }
        if (seqAndDatePresent) {
            // Input records contain sequence and data fields
            // ----------------------------------------------
            // Copying is done directly using a sequential file preserving sequence and data field values.
            // Clear physical file member
            clrPfmCommand = "CLRPFM FILE(" + libraryName + "/" + fileName + ") MBR(" + memberName + ")";
            cmdCall.run(clrPfmCommand);
            // Obtain output database file record description
            AS400FileRecordDescription outRecDesc = new AS400FileRecordDescription(remoteServer, outMemberPathString);
            // Retrieve record format from the record description
            RecordFormat[] format = outRecDesc.retrieveRecordFormat();
            // Obtain output record object
            Record outRecord = new Record(format[0]);
            // 
            // Note: Now create the member if no error was found in the first text line.
            // -----
            // 
            // Create the member (SequentialFile object)
            outSeqFile = new SequentialFile(remoteServer, outMemberPathString);
            // Set the record format (the only one)
            outSeqFile.setRecordFormat(format[0]);
            try {
                // Open the member
                outSeqFile.open();
            } catch (com.ibm.as400.access.AS400Exception as400exc) {
                // as400exc.printStackTrace();
                // Add new member if open could not be performed (when the member does not exist)
                // (the second parameter is a text description)
                // The new member inherits the CCSID from its parent Source physical file
                outSeqFile.addPhysicalFileMember(memberName, "Source member " + memberName);
                // Change member to set its Source Type
                chgPfmCommand = "CHGPFM FILE(" + libraryName + "/" + fileName + ") MBR(" + memberName + ") SRCTYPE(" + sourceType + ")";
                // Perform the CL command
                cmdCall.run(chgPfmCommand);
                // Open the new member
                outSeqFile.open();
            }
            // Process all lines
            while (textLine != null) {
                // Get lengths of three fields of the source record
                int lenSEQ = format[0].getFieldDescription("SRCSEQ").getLength();
                int lenDAT = format[0].getFieldDescription("SRCDAT").getLength();
                // Set the three field values from the input text line according to their lengths
                outRecord.setField("SRCSEQ", new BigDecimal(new BigInteger(textLine.substring(0, lenSEQ)), 2));
                outRecord.setField("SRCDAT", new BigDecimal(textLine.substring(lenSEQ, lenSEQ + lenDAT)));
                outRecord.setField("SRCDTA", textLine.substring(lenSEQ + lenDAT));
                try {
                    // Write source record
                    outSeqFile.write(outRecord);
                    // Read next text line
                    textLine = inFile.readLine();
                } catch (Exception exc) {
                    exc.printStackTrace();
                    row = "Error: 1 Data of the PC file  " + sourcePathString + "  cannot be copied to the source physical file  " + libraryName + "/" + fileName + "  -  " + exc.toString();
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages();
                    msgText = "ERROR";
                    break;
                }
            }
            // Close files
            inFile.close();
            outSeqFile.close();
        } else {
            // Input records DO NOT contain sequence and data fields
            // -----------------------------------------------------
            // Copying is done indirectly using a temporary IFS file in the /home/userName directory.
            // First create an IFS '/home/userName directory if it does not exist
            String home_userName = "/home/" + userName;
            IFSFile ifsDir = new IFSFile(remoteServer, home_userName);
            // Create new directory
            ifsDir.mkdir();
            // String for command CHGATR to set CCSID attribute of the new directory
            String command_CHGATR = "CHGATR OBJ('" + home_userName + ") ATR(*CCSID) VALUE(" + ibmCcsid + ") SUBTREE(*ALL)";
            // Perform the command
            cmdCall.run(command_CHGATR);
            // Create hidden temporary file (with leading dot) in the directory
            String tmp_File = home_userName + "/.tmp" + Timestamp.valueOf(LocalDateTime.now()).toString();
            IFSFile ifsTmpFile = new IFSFile(remoteServer, tmp_File);
            ifsTmpFile.createNewFile();
            // Copy PC file to temporary IFS file
            copyFromPcFile(sourcePathString, tmp_File, notFromWalk);
            // Copy data from temporary IFS file to the member. If the member does not exist it is created.
            String commandCpyFrmStmfString = "CPYFRMSTMF FROMSTMF('" + tmp_File + "') TOMBR('" + targetPathString + "') MBROPT(*REPLACE) CVTDTA(*AUTO) STMFCCSID(*STMF) DBFCCSID(*FILE)";
            // Perform the command
            cmdCall.run(commandCpyFrmStmfString);
            // Delete the temporary file
            ifsTmpFile.delete();
        }
        row = "Info: PC file  " + sourcePathString + "  was copied to source physical file member  " + libraryName + "/" + fileName + "(" + memberName + "). Convert " + pcCharset + " -> " + ibmCcsid + ".";
        mainWindow.msgVector.add(row);
        mainWindow.showMessages();
        if (!msgText.isEmpty()) {
            return "ERROR";
        }
        if (msgText.isEmpty() && !fromDirectory) {
            row = "Comp: PC file  " + sourcePathString + "  was copied to source physical file member  " + libraryName + "/" + fileName + "(" + memberName + "). Convert " + pcCharset + " -> " + ibmCcsid + ".";
            mainWindow.msgVector.add(row);
            mainWindow.showMessages();
            return "";
        }
        if (!msgText.isEmpty() && !fromDirectory) {
            row = "Comp: PC file  " + sourcePathString + "  was NOT completely copied to source physical file member  " + libraryName + "/" + fileName + "(" + memberName + "). Convert " + pcCharset + " -> " + ibmCcsid + ".";
            mainWindow.msgVector.add(row);
            mainWindow.showMessages();
            return "ERROR";
        }
    } catch (Exception exc) {
        try {
            inFile.close();
            outSeqFile.close();
        } catch (Exception exce) {
            exce.printStackTrace();
        }
        exc.printStackTrace();
        row = "Error: 3 PC file  " + sourcePathString + "  cannot be copied to the source physical file  " + libraryName + "/" + fileName + "  -  " + exc.toString();
        mainWindow.msgVector.add(row);
        mainWindow.showMessages();
        // Must not continue in order not to lock an object
        return "ERROR";
    }
    return "";
}
Also used : Path(java.nio.file.Path) CommandCall(com.ibm.as400.access.CommandCall) SequentialFile(com.ibm.as400.access.SequentialFile) RecordFormat(com.ibm.as400.access.RecordFormat) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) BigDecimal(java.math.BigDecimal) AS400FileRecordDescription(com.ibm.as400.access.AS400FileRecordDescription) BigInteger(java.math.BigInteger) Record(com.ibm.as400.access.Record) IFSFile(com.ibm.as400.access.IFSFile)

Example 8 with CommandCall

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

the class CreateAndDeleteInIBMi method createSourcePhysicalFile.

/**
 * Create Source Physical File.
 */
protected void createSourcePhysicalFile() {
    // Get library name from the IFS path string
    extractNamesFromIfsPath(mainWindow.rightPathString);
    // "true" stands for changing result to upper case
    String sourceFileName = new GetTextFromDialog("CREATE NEW SOURCE PHYSICAL FILE").getTextFromDialog("Library", "Source physical file name", libraryName, "", true, currentX, currentY);
    if (sourceFileName == null) {
        return;
    }
    if (sourceFileName.isEmpty()) {
        sourceFileName = "QRPGLESRC";
    }
    // Get and adjust source record length
    sourceRecordLength = (String) properties.get("SOURCE_RECORD_LENGTH");
    // The property contains all digits. It was made certain when the user entered the value.
    int sourceRecordLengthInt = Integer.parseInt(sourceRecordLength);
    // Source record must contain 12 byte prefix to data line: sequence number (6) and date (6)
    sourceRecordLengthInt += 12;
    // Build command CRTSRCPF to create a source physical file with certain CCSID in the library
    String commandText = "CRTSRCPF FILE(" + libraryName + "/" + sourceFileName + ") " + "RCDLEN(" + sourceRecordLengthInt + ") CCSID(" + ibmCcsid + ")";
    // Enable calling CL commands
    CommandCall command = new CommandCall(remoteServer);
    try {
        // Run the command
        command.run(commandText);
        // Get messages from the command if any
        AS400Message[] as400MessageList = command.getMessageList();
        String msgType;
        // Send all messages from the command. After ESCAPE message - return.
        for (AS400Message as400Message : as400MessageList) {
            if (as400Message.getType() == AS400Message.ESCAPE) {
                msgType = "Error";
                row = msgType + ": message from the CRTSRCPF command is " + as400Message.getID() + " " + as400Message.getText();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
                return;
            } else {
                msgType = "Info";
                row = msgType + ": message from the CRTSRCPF command is " + as400Message.getID() + " " + as400Message.getText();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
            }
        }
    } catch (Exception exc) {
        exc.printStackTrace();
        row = "Error: Creating source physical file  " + ifsFile.toString() + " - " + exc.toString() + ".";
        mainWindow.msgVector.add(row);
        mainWindow.showMessages();
        return;
    }
    row = "Comp: Source physical file  " + sourceFileName + "  was created in  " + libraryName + ".";
    mainWindow.msgVector.add(row);
    mainWindow.showMessages();
}
Also used : CommandCall(com.ibm.as400.access.CommandCall) AS400Message(com.ibm.as400.access.AS400Message)

Example 9 with CommandCall

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

the class CreateAndDeleteInIBMi method deleteLibrary.

/**
 * Delete library
 */
protected void deleteLibrary() {
    extractNamesFromIfsPath(mainWindow.rightPathString);
    // Enable calling CL commands
    CommandCall command = new CommandCall(remoteServer);
    // Build command DLTLIB to delete the library
    String commandText = "DLTLIB LIB(" + libraryName + ")";
    try {
        // Run the command
        command.run(commandText);
        // Get messages from the command if any
        AS400Message[] as400MessageList = command.getMessageList();
        String msgType;
        // Send all messages from the command. After ESCAPE message - return.
        for (AS400Message as400Message : as400MessageList) {
            if (as400Message.getType() == AS400Message.ESCAPE) {
                msgType = "Error";
                row = msgType + ": message from the DLTLIB command is  " + as400Message.getID() + " " + as400Message.getText();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
                return;
            } else {
                msgType = "Info";
                row = msgType + ": message from the DLTLIB command is  " + as400Message.getID() + " " + as400Message.getText();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
            }
        }
    } catch (Exception exc) {
        exc.printStackTrace();
        row = "Error: Deleting library  " + libraryName + "  -  " + exc.toString() + ".";
        mainWindow.msgVector.add(row);
        mainWindow.showMessages();
        return;
    }
    row = "Comp: Library  " + libraryName + "  was deleted.";
    mainWindow.msgVector.add(row);
    mainWindow.showMessages();
}
Also used : CommandCall(com.ibm.as400.access.CommandCall) AS400Message(com.ibm.as400.access.AS400Message)

Example 10 with CommandCall

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

the class CreateAndDeleteInIBMi method clearLibrary.

/**
 * Clear library
 */
protected void clearLibrary() {
    extractNamesFromIfsPath(mainWindow.rightPathString);
    // Enable calling CL commands
    CommandCall command = new CommandCall(remoteServer);
    // Build command CLRLIB to clear the library
    String commandText = "CLRLIB LIB(" + libraryName + ")";
    try {
        // Run the command
        command.run(commandText);
        // Get messages from the command if any
        AS400Message[] as400MessageList = command.getMessageList();
        String msgType;
        // Send all messages from the command. After ESCAPE message - return.
        for (AS400Message as400Message : as400MessageList) {
            if (as400Message.getType() == AS400Message.ESCAPE) {
                msgType = "Error";
                row = msgType + ": message from the CLRLIB command is  " + as400Message.getID() + " " + as400Message.getText();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
                return;
            } else {
                msgType = "Info";
                row = msgType + ": message from the CLRLIB command is  " + as400Message.getID() + " " + as400Message.getText();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
            }
        }
    } catch (Exception exc) {
        exc.printStackTrace();
        row = "Error: Clearing library  " + libraryName + "  -  " + exc.toString() + ".";
        mainWindow.msgVector.add(row);
        mainWindow.showMessages();
        return;
    }
    row = "Comp: Library  " + libraryName + "  was cleared.";
    mainWindow.msgVector.add(row);
    mainWindow.showMessages();
}
Also used : CommandCall(com.ibm.as400.access.CommandCall) AS400Message(com.ibm.as400.access.AS400Message)

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