Search in sources :

Example 6 with RecordFormat

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

the class Copy_IBMi_PC method copyDataFromMemberToFile.

/**
 * Copy data from source member to PC file;
 * If the output PC file does not exist, one is created.
 *
 * @param remoteServer
 * @param as400PathString
 * @param pcPathString
 * @return
 */
@SuppressWarnings("UseSpecificCatch")
protected String copyDataFromMemberToFile(AS400 remoteServer, String as400PathString, String pcPathString) {
    IFSFile ifsFile = new IFSFile(remoteServer, as400PathString);
    // Create an AS400FileRecordDescription object that represents the file
    AS400FileRecordDescription inRecDesc = new AS400FileRecordDescription(remoteServer, as400PathString);
    Path pcFilePath = Paths.get(pcPathString);
    try {
        int ccsidAttribute = ifsFile.getCCSID();
        int ccsidForDisplay = ccsidAttribute;
        // In this case, the universal CCSID 65535 is assumed.
        if (ccsidAttribute == 1208) {
            ccsidForDisplay = 65535;
        }
        if (ibmCcsidInt == 1208) {
            ibmCcsidInt = 65535;
        }
        // Get list of record formats of the database file
        RecordFormat[] format = inRecDesc.retrieveRecordFormat();
        // Create an AS400File object that represents the file
        SequentialFile as400seqFile = new SequentialFile(remoteServer, as400PathString);
        // Set the record format (the only one)
        as400seqFile.setRecordFormat(format[0]);
        // Open the source physical file member as a sequential file
        as400seqFile.open(AS400File.READ_ONLY, 0, AS400File.COMMIT_LOCK_LEVEL_NONE);
        // 
        if (Files.exists(pcFilePath) && !properties.getProperty("OVERWRITE_FILE").equals("Y")) {
            row = "Info: Source member " + libraryName + "/" + fileName + "(" + memberName + ") cannot be copied to PC file " + pcPathString + ". Overwriting files is not allowed.";
            mainWindow.msgVector.add(row);
            mainWindow.showMessages(noNodes);
            return "ERROR";
        }
        // If the file does not exist, create it
        if (!Files.exists(pcFilePath)) {
            Files.createFile(pcFilePath);
        }
        // Rewrite the PC file with records from the source member
        // -------------------
        // 
        // Open the output PC text file - with specified character set
        // ----------------------------
        // Characters read from input are translated to the specified character set if possible.
        // If input characters are incapable to be translated, an error message is reported (UnmappableCharacterException).
        BufferedWriter pcOutFile;
        if (pcCharset.equals("*DEFAULT")) {
            // Ignore PC charset parameter for binary transfer.
            pcOutFile = Files.newBufferedWriter(pcFilePath, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
        } else {
            // Use PC charset parameter for conversion.
            pcOutFile = Files.newBufferedWriter(pcFilePath, Charset.forName(pcCharset), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
        }
        // Read the first source member record
        Record inRecord = as400seqFile.readNext();
        // --------------------
        while (inRecord != null) {
            StringBuilder textLine = new StringBuilder();
            if (sourceRecordPrefixPresent) {
                // Source record is composed of three source record fields: seq. number, date, source data.
                DecimalFormat df1 = new DecimalFormat("0000.00");
                DecimalFormat df2 = new DecimalFormat("000000");
                String seq = df1.format((Number) inRecord.getField("SRCSEQ"));
                String seq2 = seq.substring(0, 4) + seq.substring(5);
                textLine.append(seq2);
                String srcDat = df2.format((Number) inRecord.getField("SRCDAT"));
                textLine.append(srcDat);
            }
            // Otherwise, source record consists of source data only
            // Convert source data into byte array
            byte[] byteArray = inRecord.getFieldAsBytes("SRCDTA");
            String translatedData;
            // Translate member data using parameter "IBM i CCSID"
            AS400Text textConverter = new AS400Text(byteArray.length, ibmCcsidInt, remoteServer);
            if (ibmCcsid.equals("*DEFAULT")) {
                // Translate member data using its CCSID attribute (possibly modified)
                textConverter = new AS400Text(byteArray.length, ccsidForDisplay, remoteServer);
            }
            // Convert byte array buffer to String text line (UTF-16)
            String str = (String) textConverter.toObject(byteArray);
            if (pcCharset.equals("*DEFAULT")) {
                // Assign "ISO-8859-1" as default charset
                pcCharset = "ISO-8859-1";
            }
            // Translate the String into PC charset
            translatedData = new String(str.getBytes(pcCharset), pcCharset);
            textLine.append(translatedData).append(NEW_LINE);
            // Write the translated text line to the PC file
            pcOutFile.write(textLine.toString());
            // Read next source member record
            inRecord = as400seqFile.readNext();
        }
        // Close the files
        pcOutFile.close();
        as400seqFile.close();
        row = "Info: Source member " + libraryName + "/" + fileName + "(" + memberName + ")  was copied to PC file  " + pcPathString + " using charset " + pcCharset + ".";
        mainWindow.msgVector.add(row);
        mainWindow.showMessages(noNodes);
        return "";
    } catch (Exception exc) {
        exc.printStackTrace();
        row = "Error: Copying member  " + libraryName + "/" + fileName + "(" + memberName + ")  -  " + exc.toString();
        mainWindow.msgVector.add(row);
        mainWindow.showMessages(noNodes);
        return "ERROR";
    }
}
Also used : Path(java.nio.file.Path) SequentialFile(com.ibm.as400.access.SequentialFile) RecordFormat(com.ibm.as400.access.RecordFormat) DecimalFormat(java.text.DecimalFormat) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) AS400FileRecordDescription(com.ibm.as400.access.AS400FileRecordDescription) BufferedWriter(java.io.BufferedWriter) AS400Text(com.ibm.as400.access.AS400Text) Record(com.ibm.as400.access.Record) IFSFile(com.ibm.as400.access.IFSFile)

Aggregations

AS400FileRecordDescription (com.ibm.as400.access.AS400FileRecordDescription)6 Record (com.ibm.as400.access.Record)6 RecordFormat (com.ibm.as400.access.RecordFormat)6 SequentialFile (com.ibm.as400.access.SequentialFile)6 IFSFile (com.ibm.as400.access.IFSFile)5 AS400Text (com.ibm.as400.access.AS400Text)4 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)3 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)3 DecimalFormat (java.text.DecimalFormat)3 BadLocationException (javax.swing.text.BadLocationException)3 Point (java.awt.Point)2 BigDecimal (java.math.BigDecimal)2 Path (java.nio.file.Path)2 PatternSyntaxException (java.util.regex.PatternSyntaxException)2 CannotRedoException (javax.swing.undo.CannotRedoException)2 CannotUndoException (javax.swing.undo.CannotUndoException)2 CommandCall (com.ibm.as400.access.CommandCall)1 BufferedWriter (java.io.BufferedWriter)1 BigInteger (java.math.BigInteger)1 LocalDate (java.time.LocalDate)1