Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

JAVA CODING EXAMPLES - 4690 Operating System Base

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

JAVA CODING EXAMPLES - 4690 Operating System Base

/* * Class to test access to a 4690 Keyed File from a Java Application * It is written to use an existing file on a shared basis to make sure * the file can be accessed while GSA or SA is also using the file */ /* * Philip Robinson June 1999 * Version 1.0 */ import com.ibm.OS4690.* ; import java.io.* ; class Keyed_File_Access { public static void main(String[] args) { String fileName ; int inchr ; byte[] myRecord = new byte[3000] ; String myKey ; // This try is for ALL the code in the class to catch any errors that may occur try { // Ask for a filename System.out.println("Type a Filename and then press the Enter key") ; // Read user input fileName = "" ; while (true) { inchr = System.in.read() ; if (inchr == 13 || inchr == 10) break ; fileName = fileName + (char)inchr ; } // Show filename user entered System.out.println("\nUsing Filename: " + fileName) ; // Open filename as an existing 4690 Keyed File for shared read access KeyedFile myFile = new KeyedFile(new File(fileName), "r", KeyedFile.SHARED_READ_ACCESS) ; // Display KeyLength and RecordSize System.out.println("\nRecordSize is: " + myFile.getRecordSize()) ; System.out.println("KeyLength is: " + myFile.getKeyLength()) ;

JAVA CODING EXAMPLES - 4690 Operating System Base


// Ask user for keys until user enters 0 (zero) while (true) { // Ask user for a key System.out.println("\nType a Key and press Enter") ; System.out.println("Type 0 (zero) to stop") ; // Read user input and put it into myKey myKey = "" ; while (true) { inchr = System.in.read() ; if (inchr == 13 || inchr == 10) break ; myKey = myKey + (char)inchr ; } // If key is zero then stop if (myKey.equalsIgnoreCase("0")) break ; // Now pack myKey into the record buffer (a byte array) packUPD(myRecord, 0, myKey) ; // Now read the record from the file and display the item description myFile.read(myRecord, KeyedFile.NO_LOCK) ; System.out.println("Item Description: " + new String(myRecord, 24, 15)) ; } // End of asking for keys loop } // End of Try // All possible catches for the try block that covers the whole class catch (FlexosException e) { String err = e.toString() ; System.out.println(err + " Return Code: " + e.getReturnCode()) ; } catch (InvalidParameterException e) { String err = e.toString() ; System.out.println(err) ; } catch (IOException e) { String err = e.toString() ; System.out.println(err) ; } } // End of Main /**

JAVA CODING EXAMPLES - 4690 Operating System Base


* Insert an unsigned packed decimal into a byte array. * * @param Array the byte array to which the <code>String</code> will be inserted. * @param Offset the position in the byte array from where insertion will begin. * @param Value the string to be inserted (this must be only digits). */ public static void packUPD( byte[] Array, int Offset, String Value ) { // If the string is an odd length then pack with an F if ( Value.length() % 2 == 1 ) Value = (char) 0xFF + Value ; // Now pack each pair of bytes into the array for ( int i = 0, j = Offset ; i < Value.length() ; i += 2, j++ ) Array[j] = (byte) ( ( ( Value.charAt(i) & 0x0F ) << 4 ) + ( Value.charAt(i+1) & 0x0F ) ) ; } } // End of Class _________________________________________________________________________________________ import com.ibm.OS4690.jiop.*; import com.ibm.OS4690.jiop.util.*; import com.ibm.OS4690.DeviceManager; /** * Quick demonstration of GSA signon with JIOP * * REQUIRED: GSA * JavaPOS installed configured * Redirection configured for JIOP * * Philip Robinson June 1999 */ public class Signon implements Runnable, IOPReadyListener, PromptChangeListener, QueueStatusChangeListener { /** * Main */ public static void main(String[] args) { System.out.println("JIOP demo starting"); // SystemMonitor will enable trace system for uncaught exceptions SystemMonitor sm = new SystemMonitor(); Thread mainThread = new Thread(sm, new Demo()); mainThread.start(); } public void run() { // Construct the JIOP and listen for Ready IOPReadyListener[] iopRListener = new IOPReadyListener[1]; iopRListener[0] = this; iop = new JIOProcessor(iopRListener);

JAVA CODING EXAMPLES - 4690 Operating System Base


// Inform DM we are ready DeviceManager.setDeviceRegistrationComplete(); } /** * Indicates the JIOP is ready * */ public void iopReady(IOPReadyEvent iopr) { System.out.println("JIOP ready"); iop.addPromptChangeListener(this); iop.getInputQueue().addQueueStatusListener(this); } /** * Provides information about the loading process * */ public void iopStatus(IOPInitStatus iops) { System.out.println(iops.getStatus()); } /** * Dump prompt changes to standard out * */ public void promptChanged(PromptChangeEvent pce) { System.out.println(pce.getPrompt()); } /** * Input queue status event indicates when Input Queue * will accept input */ public void queueStatusChange(QueueStatusChangeEvent q) { if( iop.getInputQueue().isLocked() ) { return; } try { // Post a 1 UserId and the Signon key when the JIOP // is in the signon state StateTableProcessor stp = iop.getTableProcessor(); if( stp.getCurrentStateId() == 1 ) { iop.getInputQueue().postString("1"); iop.getInputQueue().postCommand(76); System.out.println("Signon sent to JIOP"); } } catch (Exception e)

JAVA CODING EXAMPLES - 4690 Operating System Base


{ System.out.println(e); } } JIOProcessor iop; } _______________________________________________________________________________________ // Title: fnacGSA.java // Description: Read and Update of SQL tables using JDBC and Pipe's techniques // to communicate between GSA and JAVA. This program runs in a // terminal environnement // // Two parts: // // Customer loyalty processing: // Read calcultate points and update customer information of an // Oracle database. // // Picking order processing: // Many items of this transaction has to be prepared for // the customer. This part is used to warn the picking // manager that a batch has to be prepared for the customer. // import java.sql.*; import java.text.*; import oracle.jdbc.driver.*; import com.ibm.OS4690.*; public class fnacGSA { private static int linesize = 131; private static String ucode0 = "\u0000"; private static String ucode1 = "\u0001"; private static String ucode2 = "\u0002"; private static String ucode91 = "\u0041"; private static String ucode81 = "\u0031"; private static String ucode82 = "\u0032"; private static String ucode92 = "\u0042"; private static String ucode93 = "\u0043"; private static String ucode94 = "\u0044"; // Trim a character in a string private static String fnacTrim(String stringToTrim, char charTotrim) { String tempo = new String(); for (int i = 0 ; i < stringToTrim.length() ; ++i) { if (stringToTrim.charAt(i) != charTotrim) { tempo = tempo + stringToTrim.charAt(i); } } /* End for*/ return tempo; } // Main object public fnacGSA() { try { // Pipe creation streamInput = new POSPipeInputStream("PI:GSA2JAVA",6500);

JAVA CODING EXAMPLES - 4690 Operating System Base


System.out.println("waiting on PI:GSA2JAVA ..."); // Infinite loop on the reading pipe while (1 == 1) { // Waiting for the GSA message streamInput.read(streamIn); result = new String(streamIn); // Print the result System.out.println("message received ..."); System.out.println(result.toString()); // Process the GSA request traiter(); // Open the pipe from JAVA to GSA. this pipe has already been // created by the GSA program. streamOutput = new POSPipeOutputStream("PI:JAVA2GSA"); System.out.println("PI:JAVA2GSA opened"); // Send back the result to GSA streamOut = result.getBytes(); streamOutput.write(streamOut); } // end while } // end try OS4690 catch (InvalidParameterException e) { System.out.println("InvalidParameterException"); } catch (FlexosException e) { System.out.println(result.toString()); System.out.println("Length"+result.length()); System.out.println("FlexosException"); } // end try OS4690 System.out.println(result); } // Process the GSA's request void traiter() { // Customer information request if (result.startsWith(ucode91)) { // Read the customer information in the Oracle database Consultation(); } // Customer update request else if (result.startsWith(ucode93)) { // Update the customer information in the Oracle database majSolde(); } // Change the picking status in the Oracle database else if (result.startsWith(ucode81)) { // Update the picking information in the Oracle database picking(); } } // Read the customer information using JDBC void Consultation() { codeRetour = ucode0; SQLStatement = "select * from adherents where compte="+result.substring(2,22); try { DriverManager.registerDriver ( new oracle.jdbc.driver.OracleDriver ()); Connection conn = DriverManager.getConnection (

JAVA CODING EXAMPLES - 4690 Operating System Base


"jdbc:oracle:thin:@10.17.8.2:1526:STORE","SCOTT","TIGER"); Statement stmt = conn.createStatement (); ResultSet rset = stmt.executeQuery (SQLStatement); rset.next(); // Get the results // Date of the last customer buy dateDernierAchat = rset.getDate("DERNIER_ACHAT_CUMULE").toString(); // Time of the last customer buy timeDernierAchat = rset.getTime("DERNIER_ACHAT_CUMULE").toString(); // Date of the last customer buy dateDerniereAcquisitionPoints = rset.getDate("DERNIERE_ACQUISITION_POINTS").toString(); // Time of the last customer buy timeDerniereAcquisitionPoints = rset.getTime("DERNIERE_ACQUISITION_POINTS").toString(); // Calculate bonus points for this customer calculPoints(); // Format the results ancienCumulPointsString = "00000000"+rset.getString("CUMUL_POINTS"); //System.out.println("ancienCumulPoints: "+ancienCumulPointsString); ancienCumulAchatsString = "00000000"+rset.getString("CUMUL_ACHATS"); //System.out.println("ancienCumulAchats: "+ancienCumulAchatsString); nombrePointsAttribuerString = "000"+ nombrePointsAttribuer; //System.out.println("nombrePointsAttribuer: "+nombrePointsAttribuerString); nomAdherent = rset.getString("NOM") + new String(" "); padder(); dateDernierAchat = fnacTrim(dateDernierAchat,'-'); timeDernierAchat = fnacTrim(timeDernierAchat,':'); dateDerniereAcquisitionPoints= fnacTrim(dateDerniereAcquisitionPoints,'-'); timeDerniereAcquisitionPoints= fnacTrim(timeDerniereAcquisitionPoints,':'); // Format the result result = ucode92 +codeRetour +result.substring(2,30) +dateDernierAchat +timeDernierAchat +dateDerniereAcquisitionPoints +timeDerniereAcquisitionPoints +ancienCumulPointsString +ancienCumulAchatsString +nombrePointsAttribuerString +result.substring(78,94) +nomAdherent; } // end try Consultation catch (SQLException e) { if (e.getErrorCode() == 0 ) { codeRetour = ucode1; } else { codeRetour = ucode2; } // if any error occured, update the return code result = ucode92+codeRetour+result.substring(2,linesize); System.out.println("SQLException"); } // end try Consultation } // end consultation // Update the new customer information

JAVA CODING EXAMPLES - 4690 Operating System Base


void majSolde() { codeRetour = ucode0; // Set the transaction date dateDernierAchat = result.substring(30,34)+'-'+result.substring(34,36)+'-'+result.substring(36,38); dateDerniereAcquisitionPoints = dateDernierAchat; // Build the SQL statement SQLStatement = "update adherents set " +"DERNIER_ACHAT_CUMULE = to_date('" + dateDernierAchat + "','YYYY-MM-DD')" +", DERNIERE_ACQUISITION_POINTS = to_date('" + dateDerniereAcquisitionPoints + "','YYYY-MM-DD')" +", CUMUL_POINTS = "+result.substring(77,85) +", CUMUL_ACHATS = "+result.substring(85,93) +" where compte="+result.substring(2,22); try { DriverManager.registerDriver ( new oracle.jdbc.driver.OracleDriver ()); Connection conn = DriverManager.getConnection ( "jdbc:oracle:thin:@10.17.8.2:1526:STORE","SCOTT","TIGER"); Statement stmt = conn.createStatement (); // Update customer information stmt.executeUpdate(SQLStatement); // Format the result result = ucode94+codeRetour+result.substring(2,linesize); } // end try SQL catch (SQLException e) { if (e.getErrorCode() == 0 ) { codeRetour = ucode1; } else { codeRetour = ucode2; } // An error occured, advise GSA result = ucode94+codeRetour+result.substring(2,linesize); System.out.println("SQLException"); } // end try SQL } // end majSolde // Update the picking status in the Oracle database void picking() { codeRetour = ucode0; dateFinReservation = new Date(System.currentTimeMillis()); SQLStatement = "update WIN_STORE_TRC set " +"STATUS = 'P'" +", DATE_TIME = SYSDATE" +" where STORE ="+result.substring(2,6) +" and INVOICE_NUMBER ="+result.substring(6,18); System.out.println("update WIN_STORE_TRC set " System.out.println("STATUS = 'P'" ); System.out.println(", DATE_TIME = SYSDATE"); System.out.println(" where STORE ="+result.substring(2,6) ); System.out.println(" and INVOICE_NUMBER ="+result.substring(6,18)); try {

);

JAVA CODING EXAMPLES - 4690 Operating System Base


DriverManager.registerDriver ( new oracle.jdbc.driver.OracleDriver ()); Connection conn = DriverManager.getConnection ( "jdbc:oracle:thin:@10.17.8.2:1526:FIBPROTO","RMSDEV80","RMSDEV80"); Statement stmt = conn.createStatement (); //Update table WIN_STORE_TRC stmt.executeUpdate(SQLStatement); // Format he result message that will be senbd to GSA result = ucode82 + codeRetour + result.substring(2,19); stmt.close(); conn.close(); } // end try picking SQL catch (SQLException e) { codeRetour = ucode1; // an eror occured result = ucode82 + codeRetour+result.substring(2,19); System.out.println("SQLException"+e.getMessage()); } // end try picking SQL with catches } // Formatting routine void padder() { ancienCumulPointsString = ancienCumulPointsString.substring(ancienCumulPointsString.length()-8); System.out.println("ancienCumulPoints: "+ancienCumulPointsString); ancienCumulAchatsString = ancienCumulAchatsString.substring(ancienCumulAchatsString.length()-8); System.out.println("ancienCumulAchatsString: "+ ancienCumulAchatsString); nombrePointsAttribuerString = nombrePointsAttribuerString.substring(nombrePointsAttribuerString.length()-3); System.out.println("nombrePointsAttribuerString: "+ nombrePointsAttribuerString); nomAdherent = nomAdherent.substring(0,38); System.out.println("nomAdherent: "+ nomAdherent ); System.out.println("nomAdherent length: " + nomAdherent.length() ); } // Calculate bonus point. First transaction of the day, give 3 points 0 if // not. void calculPoints() { dateDernierAchatDate = dateDernierAchatDate.valueOf(dateDernierAchat); dateDerniereAcquisitionPointsDate = dateDerniereAcquisitionPointsDate.valueOf(dateDerniereAcquisitionPoints); if (dateDernierAchatDate.after(dateDerniereAcquisitionPointsDate)) { nombrePointsAttribuer = 3; } else { nombrePointsAttribuer = 0; } } // Init variables POSPipeOutputStream streamOutput; byte[] streamOut = new byte[linesize]; byte[] streamIn = new byte[linesize]; POSPipeInputStream streamInput; String result = "0123456789"; String codeRetour = ucode0; SimpleDateFormat formatDate = new SimpleDateFormat ("yyyyMMdd"); SimpleDateFormat formatHeure = new SimpleDateFormat ("HHmmss");

JAVA CODING EXAMPLES - 4690 Operating System Base


int nombrePointsAttribuer = 0; String ancienCumulPointsString = "00000000"; String ancienCumulAchatsString = "00000000"; String nomAdherent = "012345678901234567890123456789012345678"; String nombrePointsAttribuerString = "000"; String dateDernierAchat = "19990101"; String timeDernierAchat = "123456"; String dateDerniereAcquisitionPoints = "19990101"; String timeDerniereAcquisitionPoints = "123456"; Date dateDerniereAcquisitionPointsDate = new Date(System.currentTimeMillis()); Date dateDernierAchatDate = new Date(System.currentTimeMillis()); String SQLStatement = "select * from dual"; Date dateFinReservation; // Mainline public static void main(String[] args) { new fnacGSA(); } } // end fnacGSA()

You might also like