FileDocCategorySizeDatePackage
CldcTransactionRecordImpl.javaAPI DocphoneME MR2 API (J2ME)15327Wed May 02 18:00:44 BST 2007com.sun.j2me.payment

CldcTransactionRecordImpl

public final class CldcTransactionRecordImpl extends Object implements javax.microedition.payment.TransactionRecord
This class implements TransactionRecord.
version
1.4
see
javax.microedition.payment.TransactionRecord

Fields Summary
static final int
RESERVED
Status of Transaction - Reserved
static final int
MISSED
Status of Transaction - Missed
static final int
PASSED
Status of Transaction - Passed
private int
transactionID
Transaction ID
private int
applicationID
Application ID
private String
applicationName
Application Name
private int
featureID
Feature ID
private String
featureTitle
Feature Title
private double
price
price
private String
currency
currency
private int
state
state of Transaction
private long
timestamp
Timestamp of Transaction
private int
status
Transaction status - Reserved/Missed/Passed
private boolean
fake
Fake Transaction
private int
recordID
record ID
private static final String
EMPTY_CURRENCY
empty currency
private static final String
TEMPLATE_CURRENCY
USD currency
Constructors Summary
CldcTransactionRecordImpl(int applicationID, String applicationName, com.sun.j2me.payment.Transaction transaction, long timestamp, boolean fake)
Creates an instance of the CldcTransactionRecordImpl class from the given application ID, name of the MIDlet suite, transaction, the timestamp and the fake flag. The new transaction record has its wasMissed flag set.

param
applicationID the application ID
param
applicationName the application name
param
transaction the transaction
param
timestamp the timestamp
param
fake the fake flag

    
                                                                 
        
                  
        this.applicationID = applicationID;
        this.applicationName = applicationName;
        
        transactionID = transaction.getTransactionID();
        featureID = transaction.getFeatureID();
        featureTitle = transaction.getFeatureTitle();
        price = transaction.getPrice();
        currency = transaction.getCurrency();
        
        if (currency == null) {
            currency = EMPTY_CURRENCY;
        }

        switch (transaction.getState()) {
            case Transaction.SUCCESSFUL:
                state = TransactionRecord.TRANSACTION_SUCCESSFUL;
                break;
            case Transaction.REJECTED:
                state = TransactionRecord.TRANSACTION_REJECTED;
                break;
            case Transaction.FAILED:
                state = TransactionRecord.TRANSACTION_FAILED;
                break;
        }
        
        this.timestamp = timestamp;
        this.fake = fake;
        this.status = RESERVED;
        
        this.recordID = 0;
    
private CldcTransactionRecordImpl()
Constructs empty instance

    
Methods Summary
intadd2list(java.util.Vector v)
Adds Transaction Record into the list

param
v the list of sorted transactions
return
index in the element

        int count = v.size();
        if (count == 0) {
            v.addElement(this);
        } else {
            do {
                if (((CldcTransactionRecordImpl)v.elementAt(count-1)).
                        getFinishedTimestamp() <= this.timestamp) {
                    v.insertElementAt(this,  count);
                    break;
                }
                count--;
            } while (count > 0);
            if (count == 0) {
                v.insertElementAt(this,  count);
            }
        }
        return count;
    
static intcalculateSize(java.lang.String applicationName, java.lang.String featureTitle)
Calculates the size in bytes which will be needed to store a transaction record with the specified application name and feature title.

param
applicationName the application name
param
featureTitle the feature title
return
the size needed to store such record

        // calculate the required space
        int size;
        
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream os = new DataOutputStream(bos);
        
        try {
            os.writeUTF(applicationName);
            os.writeUTF(featureTitle);
            os.writeUTF(TEMPLATE_CURRENCY);
            size = bos.size();
            
        } finally {
            os.close();
        }
        
        size += 4 + // transactionID
                4 + // applicationID
                4 + // featureID
                8 + // price
                4 + // state
                8 + // timestamp
                4;  // status
        
        return size;
    
com.sun.j2me.payment.CldcTransactionRecordImplcreateDuplicateRecord()
Returns the exact duplicate of this transaction record except of the wasMissed flag which is set to the value of the input parameter.

return
the new duplicate transaction record

        CldcTransactionRecordImpl newRecord = new CldcTransactionRecordImpl();
        
        newRecord.transactionID = transactionID;
        newRecord.applicationID = applicationID;
        newRecord.applicationName = applicationName;
        newRecord.featureID = featureID;
        newRecord.featureTitle = featureTitle;
        newRecord.price = price;
        newRecord.currency = currency;
        newRecord.state = state;
        newRecord.timestamp = timestamp;
        newRecord.fake = fake;
        newRecord.recordID = recordID;

        newRecord.status = PASSED;
        
        return newRecord;
    
static com.sun.j2me.payment.CldcTransactionRecordImplcreateFakeRecord(int transactionID, int applicationID, java.lang.String applicationName, int featureID, java.lang.String featureTitle, double price, java.lang.String currency, int state, long timestamp)
Creates a fake transaction record from the given information. It's used in the debug mode.

param
transactionID unique ID of transaction
param
applicationID the application ID
param
applicationName the application name
param
featureID the feature ID
param
featureTitle the title of the feature
param
price the price of the feature
param
currency the currency to pay
param
state the state of the transaction
param
timestamp the timestamp
return
new instance of CldcTransactionRecordImpl

        CldcTransactionRecordImpl record = new CldcTransactionRecordImpl();
                
        record.transactionID = transactionID;
        record.applicationID = applicationID;
        record.applicationName = applicationName;
        record.featureID = featureID;
        record.featureTitle = featureTitle;
        record.price = price;
        record.currency = currency;
        record.state = state;
        record.timestamp = timestamp;
        
        record.status = RESERVED;
        record.fake = true;
        
        record.recordID = 0;
        
        return record;
    
public intgetApplicationID()
Returns the appplication ID of the application (MIDlet) which initiated the transaction.

return
the application ID
see
com.sun.j2me.payment.TransactionStore#getNextApplicationID

        return applicationID;
    
public java.lang.StringgetApplicationName()
Returns the name of the application (MIDlet) which initiated the transaction.

return
the application name

        return applicationName;
    
public java.lang.StringgetCurrency()
Returns the currency of the price or an empty string if it hasn't been set when the transaction ended.

return
the currency code

        return currency;
    
public intgetFeatureID()
Returns the feature ID.

return
the feature ID

        return featureID;
    
public java.lang.StringgetFeatureTitle()
Returns the title of the feature.

return
the feature title

        return featureTitle;
    
public longgetFinishedTimestamp()
Returns the timestamp when the transaction was finished.

return
the timestamp

        return timestamp;
    
public doublegetPrice()
Returns the price of the feature or 0 if it hasn't been set when the transaction ended.

return
the price

        return price;
    
intgetRecordID()
Returns Record ID of the transaction

return
recordID of the Transaction

        return recordID;
    
intgetSerializedSize()
Returns the size (in bytes) which is needed to serialize the transaction record.

return
the size of the serialized transaction record

        if (fake) {
            return 0;
        }
        
        int size = 0;
        try {
            size = calculateSize(applicationName, featureTitle);
        } catch (IOException e) {
        }
        
        return size;
    
public intgetState()
Returns the final state of the transaction. It can be one of TRANSACTION_SUCCESSFUL, TRANSACTION_FAILED, TRANSACTION_REJECTED.

return
the final state
see
javax.microedition.payment.TransactionRecord#TRANSACTION_SUCCESSFUL
see
javax.microedition.payment.TransactionRecord#TRANSACTION_FAILED
see
javax.microedition.payment.TransactionRecord#TRANSACTION_REJECTED

        return state;
    
public intgetStatus()
Indicates status of Transaction: RESERVED/MISSED/PASSED

return
RESERVED if Transaction is started MISSED if confirmation is received PASSED if user was notifyed

        return status;
    
public intgetTransactionID()
Returns the transaction ID of the transaction.

return
the transaction ID

        return transactionID;
    
public booleanisFake()
Indicates if the current transaction record is fake. A fake record isn't permanently stored into the transaction store file. Fake records are produced in the debug mode and by MIDlet suites which are run without beeing installed.

return
true if the record is fake

        return fake;
    
static com.sun.j2me.payment.CldcTransactionRecordImplread(java.io.DataInputStream is)
Creates a new transaction record from the data read from the given input stream.

param
is the input stream
return
the new transaction record
throws
IOException indicates a reading failure

        CldcTransactionRecordImpl newRecord = new CldcTransactionRecordImpl();
        
        newRecord.transactionID = is.readInt();
        newRecord.applicationID = is.readInt();
        newRecord.applicationName = is.readUTF();
        newRecord.featureID = is.readInt();
        newRecord.featureTitle = is.readUTF();
        newRecord.price = is.readDouble();
        newRecord.currency = is.readUTF();
        newRecord.state = is.readInt();
        newRecord.timestamp = is.readLong();
        newRecord.status = is.readInt();

        return newRecord;
    
voidsetRecordID(int recordID)
Stores Record ID for internal usage

param
recordID unique ID of the Transaction Record

        this.recordID = recordID;
    
voidsetStatus(int status)
Change status of Transaction: RESERVED/MISSED/PASSED

param
status of Transaction

        if ((status == MISSED) || (status == PASSED))
        {
            this.status = status;
        }
    
voidupdate(com.sun.j2me.payment.Transaction transaction)
Sets the state of the transaction. It can be one of TRANSACTION_SUCCESSFUL, TRANSACTION_FAILED, TRANSACTION_REJECTED.

param
transaction the transaction

            timestamp = System.currentTimeMillis();
            switch (transaction.getState()) {
            case Transaction.SUCCESSFUL:
                state = TransactionRecord.TRANSACTION_SUCCESSFUL;
                break;
            case Transaction.REJECTED:
                state = TransactionRecord.TRANSACTION_REJECTED;
                break;
            case Transaction.FAILED:
                state = TransactionRecord.TRANSACTION_FAILED;
                break;
        }
    
public booleanwasMissed()
Indicates if the MIDlet have or haven't been notified about the final state of the transaction.

return
true if the MIDlet haven't been notified yet

        return (status != PASSED);
    
voidwrite(java.io.DataOutputStream os)
Stores the transaction record into the given output stream.

param
os the output stream
throws
IOException indicates a writing failure

        os.writeInt(transactionID);
        os.writeInt(applicationID);
        os.writeUTF(applicationName);
        os.writeInt(featureID);
        os.writeUTF(featureTitle);
        os.writeDouble(price);
        os.writeUTF(currency);
        os.writeInt(state);
        os.writeLong(timestamp);
        os.writeInt(status);