Search in sources :

Example 26 with IFSFile

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

the class Copy_IBMi_IBMi method copyToSourceFile.

/**
 * Copy IFS directory to Source File (all members).
 *
 * @param remoteServer
 * @param sourcePathString
 * @param targetPathString
 * @return
 */
protected String copyToSourceFile(AS400 remoteServer, String sourcePathString, String targetPathString) {
    // Extract individual names (library, file, member) from the AS400 IFS
    // path
    extractNamesFromIfsPath(targetPathString);
    // Path to the output source file
    String outSourceFilePath = "/QSYS.LIB/" + libraryName + ".LIB/" + fileName + ".FILE";
    try {
        IFSFile inIfsDirFile = new IFSFile(remoteServer, sourcePathString);
        IFSFile outIfsDirectory = new IFSFile(remoteServer, outSourceFilePath);
        // Create Source Physical File.
        if (!outIfsDirectory.exists()) {
            outIfsDirectory.mkdir();
        }
        boolean atLeastOneErrorInMembers = false;
        // Copy IFS files to Source Members
        if (inIfsDirFile.isDirectory()) {
            // From IFS directory to Source file
            // ---------------------------------
            IFSFile[] ifsFiles = inIfsDirFile.listFiles();
            // Copy all objects of the IFS directory
            for (IFSFile ifsFile : ifsFiles) {
                // Correct the source type
                String ifsFileName = ifsFile.getName();
                String mbrName = ifsFileName;
                if (ifsFileName.lastIndexOf(".") >= 0) {
                    mbrName = ifsFileName.substring(0, ifsFileName.lastIndexOf("."));
                }
                // Directories, are not copied
                if (ifsFile.isDirectory()) {
                    row = "Info: IFS directory  " + ifsFile + "  was NOT copied to source physical file  " + libraryName + "/" + fileName + ".";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages();
                    continue;
                // Save files are not copied
                } else if (ifsFileName.endsWith(".savf")) {
                    row = "Info: IFS save file  " + ifsFile + "  was NOT copied to source physical file  " + libraryName + "/" + fileName + ".";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages();
                    continue;
                }
                // Copy IFS file to Source member
                String msgTextMbr = copyToSourceMember(ifsFile.toString(), targetPathString + "/" + mbrName + ".MBR");
                // If at least one message is not empty - note error
                if (!msgTextMbr.isEmpty()) {
                    atLeastOneErrorInMembers = true;
                }
            }
            if (!atLeastOneErrorInMembers) {
                row = "Comp: IFS directory  " + sourcePathString + "  was copied to source physical file  " + libraryName + "/" + fileName + ".";
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
            } else {
                row = "Comp: IFS directory  " + sourcePathString + "  was NOT completely copied to source physical file  " + libraryName + "/" + fileName + ".";
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
            }
            msgText = atLeastOneErrorInMembers ? "ERROR" : "";
            return msgText;
        } else {
            // From IFS file to Source file
            // ----------------------------
            // Correct source type
            String ifsFileName = inIfsDirFile.getName();
            String mbrName = ifsFileName;
            if (ifsFileName.lastIndexOf(".") >= 0) {
                mbrName = ifsFileName.substring(0, ifsFileName.lastIndexOf("."));
            }
            // Copy IFS object to Source file
            String msgTextMbr = copyToSourceMember(sourcePathString, targetPathString + "/" + mbrName + ".MBR");
            // If at least one message is not empty - note error
            if (!msgTextMbr.isEmpty()) {
                atLeastOneErrorInMembers = true;
            }
            if (!atLeastOneErrorInMembers) {
                row = "Comp: IFS file  " + sourcePathString + "  was copied to Source physical file  " + libraryName + "/" + fileName + ".";
                mainWindow.msgVector.add(row);
                mainWindow.showMessages(nodes);
            } else {
                row = "Comp: IFS file  " + sourcePathString + "  was NOT copied to Source physical file  " + libraryName + "/" + fileName + ".";
                mainWindow.msgVector.add(row);
                mainWindow.showMessages(nodes);
            }
            msgText = atLeastOneErrorInMembers ? "ERROR" : "";
            return msgText;
        }
    } catch (Exception exc) {
        exc.printStackTrace();
        row = "Error: Copying from IFS object  " + sourcePathString + "  -  " + exc.toString();
        mainWindow.msgVector.add(row);
        mainWindow.showMessages(nodes);
        return "ERROR";
    }
}
Also used : IFSFile(com.ibm.as400.access.IFSFile)

Example 27 with IFSFile

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

the class Copy_IBMi_IBMi method copyToIfsFile.

/**
 * Copy IFS file or Source member or Save file to an IFS file; If the target
 * IFS file does not exist, one is created.
 *
 * @param targetPathString
 * @param sourcePathString
 * @param fromWalk
 * @return
 */
protected String copyToIfsFile(String sourcePathString, String targetPathString, boolean fromWalk) {
    if (targetPathString.equals(sourcePathString)) {
        return "ERROR";
    }
    // 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/")) {
            extractNamesFromIfsPath(sourcePathString);
            if (sourcePathString.endsWith(".FILE") && inputDirFile.isSourcePhysicalFile()) {
                if (targetPathString.endsWith(".savf")) {
                    row = "Error: Source file  " + libraryName + "/" + fileName + "  cannot be copied to IFS file  " + targetPathString + "  ending with .savf.";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(nodes);
                    return "ERROR";
                }
                msgText = copyFromSourceFile(remoteServer, sourcePathString, targetPathString);
                if (msgText.isEmpty()) {
                    row = "Comp: Source file  " + libraryName + "/" + fileName + "  was copied to IFS file  " + targetPathString + ".";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(nodes);
                    return "";
                } else {
                    row = "Comp File: Source file  " + libraryName + "/" + fileName + "  was NOT copied to IFS file.";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(nodes);
                    return "ERROR";
                }
            }
            if (sourcePathString.endsWith(".MBR")) {
                if (targetPathString.endsWith(".savf")) {
                    row = "Error: Source member  " + libraryName + "/" + fileName + "(" + memberName + ")  cannot be copied to IFS file  " + targetPathString + "  ending with .savf.";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(nodes);
                    return "ERROR";
                }
                msgText = copyFromSourceMember(remoteServer, sourcePathString, targetPathString);
                if (msgText.isEmpty()) {
                    row = "Comp: Source member  " + libraryName + "/" + fileName + "(" + memberName + ")  was copied to IFS file  " + targetPathString + ".";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(nodes);
                    return "";
                } else {
                    row = "Comp File: Source member  " + libraryName + "/" + fileName + "(" + memberName + ")  was NOT copied to IFS file.";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(nodes);
                    return "ERROR";
                }
            } else // ---------
            if (inputDirFile.toString().contains(".LIB") && inputDirFile.toString().endsWith(".SAVF")) {
                msgText = copyFromSaveFile(remoteServer, sourcePathString, targetPathString);
                return msgText;
            }
        } else {
            if (outputDirFile.isDirectory()) {
                // When IFS directory, add source file name
                // ------------------
                // IFS file path string = target IFS directory + source IFS file
                // name
                targetPathString = sourcePathStringPrefix + "/" + inputDirFile.getName();
                outputDirFile = new IFSFile(remoteServer, targetPathString);
            }
            // =======================================
            try {
                byte[] inputByteArray = new byte[2000000];
                int bytesRead;
                // ---------------------------------
                if (outputDirFile.exists() && !overwriteAllowed) {
                    row = "Error: IFS file  " + inputDirFile + "  was NOT copied to the existing file  " + targetPathString + ". Overwriting files is not allowed.";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(noNodes);
                    return "ERROR";
                }
                // Get input file CCSID attribute
                int inputFileCcsid = inputDirFile.getCCSID();
                // Target file CCSID attribute - not yet ready
                int outputFileCcsid;
                // -------------------------------------
                if (!outputDirFile.exists()) {
                    // If target file does not exist, create one and set its CCSID
                    // from the input file
                    outputDirFile.createNewFile();
                    outputDirFile.setCCSID(inputFileCcsid);
                }
                outputFileCcsid = outputDirFile.getCCSID();
                // Open input IFS file
                ifsInStream = new IFSFileInputStream(remoteServer, sourcePathString);
                // 
                if (ibmCcsid.equals("*DEFAULT") || outputFileCcsid == inputFileCcsid) {
                    ifsOutStream = new IFSFileOutputStream(remoteServer, targetPathString);
                    // Copy IFS file to IFS file reading input stream to byte
                    // array and using output stream for output
                    // Read first portion of bytes
                    bytesRead = ifsInStream.read(inputByteArray);
                    // Repeat if at least one byte was read
                    while (bytesRead > 0) {
                        // Write out bytes read before
                        ifsOutStream.write(inputByteArray, 0, bytesRead);
                        // Read next portion of bytes
                        bytesRead = ifsInStream.read(inputByteArray);
                    }
                    // Close files
                    ifsOutStream.close();
                    ifsInStream.close();
                    if (fromWalk) {
                        row = "Info: IFS file  " + sourcePathString + "  was copied unchanged (binary) to IFS file  " + targetPathString + ". " + outputFileCcsid + " -> " + outputFileCcsid + ".";
                    } else {
                        row = "Comp: IFS file  " + sourcePathString + "  was copied unchanged (binary) to IFS file  " + targetPathString + ". " + outputFileCcsid + " -> " + outputFileCcsid + ".";
                    }
                // 
                } else {
                    // 
                    // Conversion from source IFS file's CCSID to target IFS file's CCSID
                    // ----------
                    // Open output IFS file
                    ifsOutStream = new IFSFileOutputStream(remoteServer, targetPathString, outputFileCcsid);
                    // Copy IFS file to IFS file reading input stream to byte
                    // array and using byte array for output
                    // Read first portion of bytes
                    bytesRead = ifsInStream.read(inputByteArray);
                    // Repeat if at least one byte was read
                    while (bytesRead > 0) {
                        // Convert input byte array with input CCSID to String
                        // (UTF-16)
                        AS400Text textConverter = new AS400Text(bytesRead, inputFileCcsid, remoteServer);
                        String text = (String) textConverter.toObject(inputByteArray);
                        // Convert data from String (UTF-16) to outpu byte array
                        // with output CCSID
                        AS400Text textConverter2 = new AS400Text(bytesRead, outputFileCcsid, remoteServer);
                        byte[] outputByteArray = (byte[]) textConverter2.toBytes(text);
                        // Write converted text in second byte array to output
                        // stream
                        ifsOutStream.write(outputByteArray, 0, outputByteArray.length);
                        // Read next byte array
                        bytesRead = ifsInStream.read(inputByteArray);
                        // Close files
                        ifsOutStream.close();
                        ifsInStream.close();
                        if (fromWalk) {
                            row = "Info: IFS file  " + sourcePathString + "  was copied to IFS file  " + targetPathString + ", Convert  " + inputFileCcsid + " -> " + outputFileCcsid;
                        } else {
                            row = "Comp: IFS file  " + sourcePathString + "  was copied to IFS file  " + targetPathString + ", Convert  " + inputFileCcsid + " -> " + outputFileCcsid;
                        }
                    }
                }
                mainWindow.msgVector.add(row);
                mainWindow.showMessages(nodes);
                return "";
            } catch (Exception exc) {
                exc.printStackTrace();
                row = "Error: Copying to IFS file " + targetPathString + "  -  " + exc.toString();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages(nodes);
                return "ERROR";
            }
        }
    } catch (Exception exc) {
        exc.printStackTrace();
        row = "Error: Copying to IFS file " + targetPathString + "  -  " + exc.toString();
        mainWindow.msgVector.add(row);
        mainWindow.showMessages(nodes);
        return "ERROR";
    }
    return "";
}
Also used : IFSFileOutputStream(com.ibm.as400.access.IFSFileOutputStream) AS400Text(com.ibm.as400.access.AS400Text) IFSFile(com.ibm.as400.access.IFSFile) IFSFileInputStream(com.ibm.as400.access.IFSFileInputStream)

Example 28 with IFSFile

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

the class Copy_PC_IBMi method walkPcDirectory_CreateNestedIfsDirectories.

/**
 * Walk through PC directory recursively to create shadow directories in IFS target directory. Shadow directories are
 * named by last names of the PC directory paths so that only one-level directories are inserted to the IFS target
 * directory.
 *
 * @param remoteServer
 * @param sourcePathString
 * @param ifsPath
 * @return
 */
protected String walkPcDirectory_CreateNestedIfsDirectories(AS400 remoteServer, String sourcePathString, IFSFile ifsPath) {
    try {
        Stream<Path> stream = Files.list(Paths.get(sourcePathString));
        stream.forEach(inPath -> {
            String pcPathName = inPath.toString();
            // Path to the new shadow directory to be created in IFS
            String newDirPathString = ifsPath.toString() + "/" + pcPathName.substring(pcPathName.lastIndexOf(pcFileSep) + 1);
            IFSFile ifsNewPath = new IFSFile(remoteServer, newDirPathString);
            // Only directories in PC are processed (not single files)
            if (Files.isDirectory(inPath)) {
                try {
                    if (!ifsNewPath.toString().contains(pcFileSep + ".")) {
                        // Create new shadow IFS directory
                        ifsNewPath.mkdir();
                        // Add message text to the message table and show it
                        row = "Info: Directory  " + pcPathName + "  was created in IFS directory  " + ifsPath.toString() + ".";
                        mainWindow.msgVector.add(row);
                        mainWindow.showMessages();
                    }
                    // Recursive call with different PC path
                    walkPcDirectory_CreateNestedIfsDirectories(remoteServer, pcPathName, ifsNewPath);
                } catch (Exception exc) {
                    exc.printStackTrace();
                }
            }
        });
    } catch (Exception exc) {
        exc.printStackTrace();
    }
    return "";
}
Also used : Path(java.nio.file.Path) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) IFSFile(com.ibm.as400.access.IFSFile)

Example 29 with IFSFile

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

the class CreateAndDeleteInIBMi method createIfsFile.

/**
 * Create IFS file.
 */
@SuppressWarnings("UseSpecificCatch")
protected void createIfsFile() {
    // "false" stands for not changing result to upper case
    String fileName = new GetTextFromDialog("CREATE NEW FILE").getTextFromDialog("Parent directory", "New file name", mainWindow.rightPathString + "/", "", false, currentX, currentY);
    // User canceled creating the directory
    if (fileName == null) {
        return;
    }
    if (fileName.isEmpty()) {
        fileName = "New file";
    }
    try {
        // Get path to the newly created directory by adding its name to the parent directory path
        IFSFile ifsFile = new IFSFile(remoteServer, mainWindow.rightPathString + "/" + fileName);
        // Create new empty file
        ifsFile.createNewFile();
        // Set CCSID from parameters
        ifsFile.setCCSID(ibmCcsidInt);
        row = "Comp: IFS file  " + ifsFile.toString() + "  created in directory  " + mainWindow.rightPathString + ".";
        mainWindow.msgVector.add(row);
        mainWindow.showMessages();
    } catch (Exception exc) {
        exc.printStackTrace();
        row = "Error:" + exc.toString();
        mainWindow.msgVector.add(row);
        mainWindow.showMessages();
    }
}
Also used : IFSFile(com.ibm.as400.access.IFSFile)

Example 30 with IFSFile

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

the class CreateAndDeleteInIBMi method createIfsDirectory.

/**
 * Create IFS directory.
 */
protected void createIfsDirectory() {
    // Enable calling CL commands
    CommandCall cmdCall = new CommandCall(remoteServer);
    // "false" stands for not changing result to upper case
    String directoryName = new GetTextFromDialog("CREATE NEW DIRECTORY").getTextFromDialog("Parent directory", "New directory name", mainWindow.rightPathString + "/", "", false, currentX, currentY);
    // User canceled creating the directory
    if (directoryName == null) {
        return;
    }
    if (directoryName.isEmpty()) {
        directoryName = "New directory";
    }
    try {
        // Get path to the newly created directory by adding its name to the parent directory path
        IFSFile ifsFile = new IFSFile(remoteServer, mainWindow.rightPathString + "/" + directoryName);
        // Create new directory
        ifsFile.mkdir();
        // String for command CHGATR to set CCSID attribute of the new directory
        String command_CHGATR = "CHGATR OBJ('" + mainWindow.rightPathString + "/" + directoryName + "') ATR(*CCSID) VALUE(" + ibmCcsid + ") SUBTREE(*ALL)";
        System.out.println(command_CHGATR);
        // Perform the command
        cmdCall.run(command_CHGATR);
        // 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: Change CCSID attribute with command CHGATR   -  " + as400Message.getID() + " " + as400Message.getText();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
            } else {
                row = "Info: Change CCSID attribute with command CHGATR  -  " + as400Message.getID() + " " + as400Message.getText();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages();
            }
        }
        row = "Comp: IFS directory  " + ifsFile.toString() + "  created in directory  " + mainWindow.rightPathString + "  -  CCSID " + ibmCcsid + ".";
        mainWindow.msgVector.add(row);
        mainWindow.showMessages();
    } catch (Exception exc) {
        exc.printStackTrace();
        row = "Error:" + exc.toString();
        mainWindow.msgVector.add(row);
        mainWindow.showMessages();
    }
}
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