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;

// ---------------------------------------------------------------------
// ---    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;
    }

    /**
     * @return
     */
    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.
     * @param qc
     * @param params
     * @param noUserParams
     */
    protected void executeQueryForCollection(Object qc, Object[] params, 
                                             int noUserParams) {
        recalculateLineItemsSubtotal();
        super.executeQueryForCollection(qc, params, noUserParams);
    }

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

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

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

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

    /**
     * @param vrs
     * @param viewRows
     * @param attrIndices
     */
    protected void notifyRowUpdated(ViewRowSetImpl vrs, Row[] viewRows, int[] attrIndices){
        super.notifyRowUpdated(vrs, viewRows, attrIndices);
        recalculateLineItemsSubtotal();
    }

    /**
     * @param vrs
     * @param viewRow
     * @param attr
     */
    protected void notifyRowUpdated(ViewRowSetImpl vrs, Row viewRow, int attr){
        super.notifyRowInserted(vrs, viewRow, attr);
        recalculateLineItemsSubtotal();
    }
}
