package oracle.fodemo.storefront.store.queries;

import java.sql.ResultSet;

import oracle.jbo.Row;
import oracle.jbo.RowSetIterator;
import oracle.jbo.server.ViewObjectImpl;
import oracle.jbo.server.ViewRowImpl;
import oracle.jbo.domain.Number;
import oracle.jbo.server.ViewRowSetImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;
import oracle.jbo.AttributeDef;
import oracle.jbo.server.ViewObjectImpl;
import oracle.jbo.server.ViewRowSetImpl;
import oracle.jbo.Row;
import oracle.jbo.server.TransactionEvent;
// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class ShoppingCartTransientVOImpl extends ViewObjectImpl {
     private Number lineItemsSubtotal; 
    
    /**This is the default constructor (do not remove).
     */
    public ShoppingCartTransientVOImpl() {
    }

    protected void create() {
        super.create();
        // Enable the VO to manage rows by view row key
        setManageRowsByKey(true);
    }

    public void recalculateLineItemsSubtotal() {
      lineItemsSubtotal = null;
    }
    
    public Number getLineItemsSubtotal() {
      if (lineItemsSubtotal == null) {
        lineItemsSubtotal = new Number(0);
        RowSetIterator rsi = createRowSetIterator(null);
        while (rsi.hasNext()) {
          Row r = rsi.next();
          Number lineItemTotal = (Number)r.getAttribute("LineItemTotal");
          if (lineItemTotal != null) {
            lineItemsSubtotal = lineItemsSubtotal.add(lineItemTotal);
          }
        }
        rsi.closeRowSetIterator();
      }
      return lineItemsSubtotal;
    }

    /**executeQueryForCollection - overridden for custom java data source support.
     */
    protected void executeQueryForCollection(Object qc, Object[] params, 
                                             int noUserParams) {
        recalculateLineItemsSubtotal();
        super.executeQueryForCollection(qc, params, noUserParams);
    }

    protected void notifyRowDeleted(ViewRowSetImpl vrs, Row viewRow, int rowIndex){
      recalculateLineItemsSubtotal();
      super.notifyRowDeleted(vrs, viewRow, rowIndex);
    }

    /**hasNextForCollection - overridden for custom java data source support.
     */
    protected boolean hasNextForCollection(Object qc) {
        recalculateLineItemsSubtotal();
        boolean bRet = super.hasNextForCollection(qc);
        return bRet;
    }

    /**createRowFromResultSet - overridden for custom java data source support.
     */
    protected ViewRowImpl createRowFromResultSet(Object qc, 
                                                 ResultSet resultSet) {
        
        ViewRowImpl value = super.createRowFromResultSet(qc, resultSet);
        recalculateLineItemsSubtotal();
        return value;
    }

    /**getEstimatedRowCount - overridden for custom java data source support.
     */
    public long getEstimatedRowCount() {
        long value = super.getEstimatedRowCount();
        return value;
    }

    protected void notifyRowUpdated(ViewRowSetImpl vrs, Row[] viewRows, int[] attrIndices){
        super.notifyRowUpdated(vrs, viewRows, attrIndices);
        recalculateLineItemsSubtotal();
    }
    
    protected void notifyRowUpdated(ViewRowSetImpl vrs, Row viewRow, int attr){
        super.notifyRowInserted(vrs, viewRow, attr);
        recalculateLineItemsSubtotal();
    }
}
