use of com.ibm.as400.access.RecordFormat 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 "";
}
use of com.ibm.as400.access.RecordFormat in project IBMiProgTool by vzupka.
the class SearchWindow method readSourceMember.
/**
* Read Source Member data into text area.
*
* @return
*/
protected JTextArea readSourceMember() {
IFSFile ifsFile = new IFSFile(remoteServer, mainWindow.sourcePathString);
// Create an AS400FileRecordDescription object that represents the file
AS400FileRecordDescription inRecDesc = new AS400FileRecordDescription(remoteServer, mainWindow.sourcePathString);
try {
// Decide what CCSID is appropriate for displaying the member
int ccsidAttribute = ifsFile.getCCSID();
// 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, mainWindow.sourcePathString);
// 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);
// Read the first source member record
Record inRecord = as400seqFile.readNext();
// --------------------
while (inRecord != null) {
StringBuilder textLine = new StringBuilder();
// 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");
// Sequence number - 6 bytes
String seq = df1.format((Number) inRecord.getField("SRCSEQ"));
String seq2 = seq.substring(0, 4) + seq.substring(5);
textLine.append(seq2);
// Date - 6 bytes
String srcDat = df2.format((Number) inRecord.getField("SRCDAT"));
// textLine.append(srcDat);
textLine.append(srcDat);
// Data from source record (the source line)
byte[] bytes = inRecord.getFieldAsBytes("SRCDTA");
// Create object for conversion from bytes to characters
// Ignore "IBM i CCSID" parameter - display characters in the
// member.
AS400Text textConverter = new AS400Text(bytes.length, remoteServer);
// Convert byte array buffer to text line (String - UTF-16)
String translatedData = (String) textConverter.toObject(bytes);
// Append translated data to text line
textLine.append(translatedData).append(NEW_LINE);
// Append text line to text area
textArea.append(textLine.toString());
// Read next source member record
inRecord = as400seqFile.readNext();
}
// Close the file
as400seqFile.close();
// Set scroll bar to top
textArea.setCaretPosition(0);
// Display the window.
setVisible(true);
mainWindow.row = "Info: Source member " + mainWindow.sourcePathString + " has CCSID " + ccsidAttribute + ".";
mainWindow.msgVector.add(mainWindow.row);
mainWindow.showMessages(mainWindow.nodes);
} catch (Exception exc) {
exc.printStackTrace();
mainWindow.row = "Error: " + exc.toString();
mainWindow.msgVector.add(mainWindow.row);
mainWindow.showMessages(mainWindow.nodes);
}
// Remove message scroll listener (cancel scrolling to the last message)
mainWindow.scrollMessagePane.getVerticalScrollBar().removeAdjustmentListener(mainWindow.messageScrollPaneAdjustmentListenerMax);
return textArea;
}
use of com.ibm.as400.access.RecordFormat in project IBMiProgTool by vzupka.
the class DisplayFile method displaySourceMember.
/**
* Display source member using its CCSID attribute; Only data part of the source record is translated (to String -
* UTF-16).
*
* @param remoteServer
* @param as400PathString
*/
protected void displaySourceMember(AS400 remoteServer, String as400PathString) {
this.setTitle("Display member '" + as400PathString + "'");
IFSFile ifsFile = new IFSFile(remoteServer, as400PathString);
// Create an AS400FileRecordDescription object that represents the file
AS400FileRecordDescription inRecDesc = new AS400FileRecordDescription(remoteServer, as400PathString);
try {
// Decide what CCSID is appropriate for displaying the member
int ccsidAttribute = ifsFile.getCCSID();
characterSetLabel.setText("CCSID " + ccsidAttribute + " was used for display.");
// 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);
// Read the first source member record
Record inRecord = as400seqFile.readNext();
// --------------------
while (inRecord != null) {
StringBuilder textLine = new StringBuilder();
// 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");
// Sequence number - 6 bytes
String seq = df1.format((Number) inRecord.getField("SRCSEQ"));
String seq2 = seq.substring(0, 4) + seq.substring(5);
textLine.append(seq2);
// Date - 6 bytes
String srcDat = df2.format((Number) inRecord.getField("SRCDAT"));
// textLine.append(srcDat);
textLine.append(srcDat);
// Data from source record (the source line)
byte[] bytes = inRecord.getFieldAsBytes("SRCDTA");
// Create object for conversion from bytes to characters
// Ignore "IBM i CCSID" parameter - display characters in the
// member.
AS400Text textConverter = new AS400Text(bytes.length, remoteServer);
// Convert byte array buffer to text line (String - UTF-16)
String translatedData = (String) textConverter.toObject(bytes);
// Append translated data to text line
textLine.append(translatedData).append(NEW_LINE);
// Append text line to text area
textArea.append(textLine.toString());
// Read next source member record
inRecord = as400seqFile.readNext();
}
// Close the file
as400seqFile.close();
// Set scroll bar to top
textArea.setCaretPosition(0);
// Display the window.
setVisible(true);
row = "Info: Source member " + as400PathString + " has CCSID " + ccsidAttribute + ".";
mainWindow.msgVector.add(row);
mainWindow.showMessages(nodes);
} catch (Exception exc) {
exc.printStackTrace();
row = "Error: " + exc.toString();
mainWindow.msgVector.add(row);
mainWindow.showMessages(nodes);
}
// Remove message scroll listener (cancel scrolling to the last message)
mainWindow.scrollMessagePane.getVerticalScrollBar().removeAdjustmentListener(mainWindow.messageScrollPaneAdjustmentListenerMax);
}
use of com.ibm.as400.access.RecordFormat in project IBMiProgTool by vzupka.
the class EditFile method displaySourceMember.
protected void displaySourceMember() {
this.setTitle("Edit member '" + filePathString + "'");
// Extract individual names (libraryName, fileName, memberName) from the AS400 IFS path
extractNamesFromIfsPath(filePathString);
IFSFile ifsFile = new IFSFile(remoteServer, filePathString);
// Create an AS400FileRecordDescription object that represents the file
AS400FileRecordDescription inRecDesc = new AS400FileRecordDescription(remoteServer, filePathString);
// Set editability
textArea.setEditable(true);
textArea.setText("");
try {
ccsidAttribute = ifsFile.getCCSID();
characterSetLabel.setText("CCSID " + ccsidAttribute + " was used for display.");
// 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, filePathString);
// 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);
// Read the first source member record
Record inRecord = as400seqFile.readNext();
// --------------------
while (inRecord != null) {
StringBuilder textLine = new StringBuilder();
// Prefix is not displayed because it must not be edited!!!
// 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");
// Sequence number - 6 bytes String seq = df1.format((Number)
// --inRecord.getField("SRCSEQ"));
// String seq2 = seq.substring(0, 4) + seq.substring(5);
// Date - 6 bytes
// --String srcDat = df2.format((Number)
// inRecord.getField("SRCDAT"));
// Data from source record (the source line)
byte[] bytes = inRecord.getFieldAsBytes("SRCDTA");
// Create object for conversion from bytes to characters
// Ignore "IBM i CCSID" parameter - display characters in the
// member.
AS400Text textConverter = new AS400Text(bytes.length, remoteServer);
// Convert byte array buffer to text line (String - UTF-16)
String translatedData = (String) textConverter.toObject(bytes);
// Append translated data to text line
textLine.append(translatedData).append(NEW_LINE);
// Append text line to text area
textArea.append(textLine.toString());
// Read next source member record
inRecord = as400seqFile.readNext();
}
// Close the file
as400seqFile.close();
} catch (Exception exc) {
isError = true;
exc.printStackTrace();
row = "Error in displaying source member: " + exc.toString();
mainWindow.msgVector.add(row);
mainWindow.showMessages(nodes);
}
// Remove message scroll listener (cancel scrolling to the last message)
mainWindow.scrollMessagePane.getVerticalScrollBar().removeAdjustmentListener(mainWindow.messageScrollPaneAdjustmentListenerMax);
}
use of com.ibm.as400.access.RecordFormat in project IBMiProgTool by vzupka.
the class EditFile method rewriteSourceMemberDirect.
/**
* Rewrite source member with edited text area directly, without intermediate temporary IFS file; Records are
* written, updated or deleted, with numbers and dates using BigDecimal objects;
*
* THIS METHOD IS NOT USED because it is very slow.
*
* @return
*/
protected String rewriteSourceMemberDirect() {
// Extract individual names (libraryName, fileName, memberName) from the
// AS400 IFS path
extractNamesFromIfsPath(filePathString);
// Path to the output source member
String outMemberPathString = "/QSYS.LIB/" + libraryName + ".LIB/" + fileName + ".FILE" + "/" + memberName + ".MBR";
try {
Files.delete(tmpFilePath);
String[] lines = textArea.getText().split("\n");
// ------------------------------------
if (!properties.getProperty("OVERWRITE_FILE").equals("Y")) {
row = "Info: Member " + libraryName + "/" + fileName + "(" + memberName + ") cannot be overwtitten. " + " Overwriting files is not allowed.";
mainWindow.msgVector.add(row);
mainWindow.showMessages();
return "ERROR";
}
// Overwrite is allowed
// --------------------
// 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]);
msgText = "";
if (lines.length > 0) {
// Create the member (SequentialFile object)
outSeqFile = new SequentialFile(remoteServer, outMemberPathString);
// Set the record format (the only one)
outSeqFile.setRecordFormat(format[0]);
try {
outSeqFile.open(AS400File.READ_WRITE, 100000, AS400File.COMMIT_LOCK_LEVEL_NONE);
} 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);
// Open the new member
outSeqFile.open(AS400File.READ_WRITE, 100000, AS400File.COMMIT_LOCK_LEVEL_NONE);
}
// Member records contain sequence and data fields
// -----------------------------------------------
// Get length of member data field
int lenDTA = format[0].getFieldDescription("SRCDTA").getLength();
// Base sequential number - 6 digits
BigDecimal seqNumber = new BigDecimal("0000.00");
// Increment to the previous sequential number - 6 digits
BigDecimal increment = new BigDecimal("0001.00");
// Get actual date and transform it to YYMMDD
LocalDate date = LocalDate.now();
int intYear = date.getYear();
int intMonth = date.getMonthValue();
int intDay = date.getDayOfMonth();
String strYear = String.valueOf(intYear);
String strMonth = String.valueOf(intMonth);
if (intMonth < 10) {
strMonth = "0" + strMonth;
}
String strDay = String.valueOf(intDay);
if (intDay < 10) {
strDay = "0" + strDay;
}
String strSrcDat = strYear.substring(2) + strMonth + strDay;
String dataLine;
// Process all lines
for (int idx = 0; idx < lines.length; idx++) {
// System.out.println("0'" + lines[idx] + "'");
if (lines[idx].equals("\n")) {
// System.out.println("1'" + lines[idx] + "'");
dataLine = " ";
} else {
// System.out.println("2'" + lines[idx] + "'");
dataLine = lines[idx].replace("\r", "");
}
seqNumber = seqNumber.add(increment);
// Insert sequential number into the source record (zoned
// decimal, 2 d.p.)
outRecord.setField("SRCSEQ", seqNumber);
// Insert today's date YYMMDD into the source record (zoned
// decimal, 0 d.p.)
outRecord.setField("SRCDAT", new BigDecimal(strSrcDat));
// Adjust data line obtained from the text area - truncate
// or pad by spaces if necessary
int dataLength;
if (dataLine.length() >= lenDTA) {
// Shorten the data line to fit the data field in the
// record
dataLength = lenDTA;
} else {
// Pad the data line with spaces to fit the data field
// in the record
char[] chpad = new char[lenDTA - dataLine.length()];
for (int jdx = 0; jdx < chpad.length; jdx++) {
// pad the data line with spaces
chpad[jdx] = ' ';
}
dataLine = dataLine + String.valueOf(chpad);
dataLength = lenDTA;
}
// Insert data to the member data field
outRecord.setField("SRCDTA", dataLine.substring(0, dataLength));
// area
try {
outSeqFile.positionCursor(idx + 1);
outSeqFile.update(outRecord);
} catch (Exception exc) {
exc.printStackTrace();
row = "Error: 1 Data cannot be written to the source member " + libraryName + "/" + fileName + "(" + memberName + ") - " + exc.toString();
mainWindow.msgVector.add(row);
mainWindow.showMessages();
msgText = "ERROR";
break;
}
}
// Close file
outSeqFile.close();
// Set caret at the beginning of the text area
textArea.setCaretPosition(0);
textArea.requestFocus();
row = "Comp: Source member " + libraryName + "/" + fileName + "(" + memberName + ") was saved.";
mainWindow.msgVector.add(row);
mainWindow.showMessages();
return "";
}
} catch (Exception exc) {
try {
outSeqFile.close();
} catch (Exception exce) {
exce.printStackTrace();
}
exc.printStackTrace();
row = "Error: 3 Data cannot be written to the source member " + libraryName + "/" + fileName + "(" + memberName + ") - " + exc.toString();
mainWindow.msgVector.add(row);
mainWindow.showMessages();
// Must not continue in order not to lock an object
return "ERROR";
}
return "";
}
Aggregations