package devguide.advanced.postingorder;

import oracle.jbo.AttributeList;
import oracle.jbo.Key;
import oracle.jbo.RowIterator;
import oracle.jbo.RowSet;
import oracle.jbo.domain.DBSequence;
import oracle.jbo.domain.Date;
import oracle.jbo.domain.Number;
import oracle.jbo.server.AttributeDefImpl;
import oracle.jbo.server.EntityDefImpl;
import oracle.jbo.server.EntityImpl;
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 SuppliersImpl extends EntityImpl {
    public static final int SUPPLIERID = 0;
    public static final int SUPPLIERNAME = 1;
    public static final int SUPPLIERSTATUS = 2;
    public static final int PHONENUMBER = 3;
    public static final int EMAIL = 4;
    public static final int UISKIN = 5;
    public static final int CREATEDBY = 6;
    public static final int CREATIONDATE = 7;
    public static final int LASTUPDATEDBY = 8;
    public static final int LASTUPDATEDATE = 9;
    public static final int OBJECTVERSIONID = 10;
    public static final int PRODUCTSBASE = 11;
    private static EntityDefImpl mDefinitionObject;

    /**This is the default constructor (do not remove).
     */
    public SuppliersImpl() {
    }

    /**@return the definition object for this instance class.
     */
    public static synchronized EntityDefImpl getDefinitionObject() {
        if (mDefinitionObject == null) {
            mDefinitionObject = EntityDefImpl.findDefObject("devguide.advanced.postingorder.Suppliers");
        }
        return mDefinitionObject;
    }

    /**Gets the attribute value for SupplierId, using the alias name SupplierId.
     */
    public DBSequence getSupplierId() {
        return (DBSequence)getAttributeInternal(SUPPLIERID);
    }

    /**Sets <code>value</code> as the attribute value for SupplierId.
     */
    public void setSupplierId(DBSequence value) {
        setAttributeInternal(SUPPLIERID, value);
    }

    /**Gets the attribute value for SupplierName, using the alias name SupplierName.
     */
    public String getSupplierName() {
        return (String)getAttributeInternal(SUPPLIERNAME);
    }

    /**Sets <code>value</code> as the attribute value for SupplierName.
     */
    public void setSupplierName(String value) {
        setAttributeInternal(SUPPLIERNAME, value);
    }

    /**Gets the attribute value for SupplierStatus, using the alias name SupplierStatus.
     */
    public String getSupplierStatus() {
        return (String)getAttributeInternal(SUPPLIERSTATUS);
    }

    /**Sets <code>value</code> as the attribute value for SupplierStatus.
     */
    public void setSupplierStatus(String value) {
        setAttributeInternal(SUPPLIERSTATUS, value);
    }

    /**Gets the attribute value for PhoneNumber, using the alias name PhoneNumber.
     */
    public String getPhoneNumber() {
        return (String)getAttributeInternal(PHONENUMBER);
    }

    /**Sets <code>value</code> as the attribute value for PhoneNumber.
     */
    public void setPhoneNumber(String value) {
        setAttributeInternal(PHONENUMBER, value);
    }

    /**Gets the attribute value for Email, using the alias name Email.
     */
    public String getEmail() {
        return (String)getAttributeInternal(EMAIL);
    }

    /**Sets <code>value</code> as the attribute value for Email.
     */
    public void setEmail(String value) {
        setAttributeInternal(EMAIL, value);
    }

    /**Gets the attribute value for UiSkin, using the alias name UiSkin.
     */
    public String getUiSkin() {
        return (String)getAttributeInternal(UISKIN);
    }

    /**Sets <code>value</code> as the attribute value for UiSkin.
     */
    public void setUiSkin(String value) {
        setAttributeInternal(UISKIN, value);
    }

    /**Gets the attribute value for CreatedBy, using the alias name CreatedBy.
     */
    public String getCreatedBy() {
        return (String)getAttributeInternal(CREATEDBY);
    }

    /**Sets <code>value</code> as the attribute value for CreatedBy.
     */
    public void setCreatedBy(String value) {
        setAttributeInternal(CREATEDBY, value);
    }

    /**Gets the attribute value for CreationDate, using the alias name CreationDate.
     */
    public Date getCreationDate() {
        return (Date)getAttributeInternal(CREATIONDATE);
    }

    /**Sets <code>value</code> as the attribute value for CreationDate.
     */
    public void setCreationDate(Date value) {
        setAttributeInternal(CREATIONDATE, value);
    }

    /**Gets the attribute value for LastUpdatedBy, using the alias name LastUpdatedBy.
     */
    public String getLastUpdatedBy() {
        return (String)getAttributeInternal(LASTUPDATEDBY);
    }

    /**Sets <code>value</code> as the attribute value for LastUpdatedBy.
     */
    public void setLastUpdatedBy(String value) {
        setAttributeInternal(LASTUPDATEDBY, value);
    }

    /**Gets the attribute value for LastUpdateDate, using the alias name LastUpdateDate.
     */
    public Date getLastUpdateDate() {
        return (Date)getAttributeInternal(LASTUPDATEDATE);
    }

    /**Sets <code>value</code> as the attribute value for LastUpdateDate.
     */
    public void setLastUpdateDate(Date value) {
        setAttributeInternal(LASTUPDATEDATE, value);
    }

    /**Gets the attribute value for ObjectVersionId, using the alias name ObjectVersionId.
     */
    public Number getObjectVersionId() {
        return (Number)getAttributeInternal(OBJECTVERSIONID);
    }

    /**Sets <code>value</code> as the attribute value for ObjectVersionId.
     */
    public void setObjectVersionId(Number value) {
        setAttributeInternal(OBJECTVERSIONID, value);
    }

    /**getAttrInvokeAccessor: generated method. Do not modify.
     */
    protected Object getAttrInvokeAccessor(int index, 
                                           AttributeDefImpl attrDef) throws Exception {
        switch (index) {
        case SUPPLIERID:
            return getSupplierId();
        case SUPPLIERNAME:
            return getSupplierName();
        case SUPPLIERSTATUS:
            return getSupplierStatus();
        case PHONENUMBER:
            return getPhoneNumber();
        case EMAIL:
            return getEmail();
        case UISKIN:
            return getUiSkin();
        case CREATEDBY:
            return getCreatedBy();
        case CREATIONDATE:
            return getCreationDate();
        case LASTUPDATEDBY:
            return getLastUpdatedBy();
        case LASTUPDATEDATE:
            return getLastUpdateDate();
        case OBJECTVERSIONID:
            return getObjectVersionId();
        case PRODUCTSBASE:
            return getProductsBase();
        default:
            return super.getAttrInvokeAccessor(index, attrDef);
        }
    }

    /**setAttrInvokeAccessor: generated method. Do not modify.
     */
    protected void setAttrInvokeAccessor(int index, Object value, 
                                         AttributeDefImpl attrDef) throws Exception {
        switch (index) {
        case SUPPLIERID:
            setSupplierId((DBSequence)value);
            return;
        case SUPPLIERNAME:
            setSupplierName((String)value);
            return;
        case SUPPLIERSTATUS:
            setSupplierStatus((String)value);
            return;
        case PHONENUMBER:
            setPhoneNumber((String)value);
            return;
        case EMAIL:
            setEmail((String)value);
            return;
        case UISKIN:
            setUiSkin((String)value);
            return;
        case CREATEDBY:
            setCreatedBy((String)value);
            return;
        case CREATIONDATE:
            setCreationDate((Date)value);
            return;
        case LASTUPDATEDBY:
            setLastUpdatedBy((String)value);
            return;
        case LASTUPDATEDATE:
            setLastUpdateDate((Date)value);
            return;
        case OBJECTVERSIONID:
            setObjectVersionId((Number)value);
            return;
        default:
            super.setAttrInvokeAccessor(index, value, attrDef);
            return;
        }
    }

    /**Gets the associated entity oracle.jbo.RowIterator.
     */
    public RowIterator getProductsBase() {
        return (RowIterator)getAttributeInternal(PRODUCTSBASE);
    }

    /**Creates a Key object based on given key constituents.
     */
    public static Key createPrimaryKey(DBSequence supplierId) {
        return new Key(new Object[]{supplierId});
    }

    /*
     * This member field is used to hang onto a reference to
     * the rowset of one or more new ServiceRequest that might
     * be references a new product.
     */
    
    RowSet newProductsBeforePost = null;
    @Override
    public void postChanges(TransactionEvent transactionEvent) {
    /* Only bother to update references if Supplier is a NEW one */
        if (getPostState() == STATUS_NEW) {
        /*
        * Get a rowset of products related
        * to this new supplier before calling super
        */
        newProductsBeforePost = (RowSet)getProductsBase();
        }
        super.postChanges(transactionEvent);
    }
    
    /**
     * Overridding this method in this class allows
     * friendly access to the create() method by other
     * classes in this same package, like the ServiceRequestsAndProduct
     * view object implementation class, whose overridden create()
     * method needs to call this.
     * @param nameValuePair
     */
    protected void create(AttributeList nameValuePair) {
      super.create(nameValuePair);
    }
    protected void refreshFKInNewContainees() {
      if (newProductsBeforePost != null) {
          Number newSupplierId = getSupplierId().getSequenceNumber();
        /* 
         * Process the rowset of service requests that referenced
         * the new product prior to posting, and update their
         * ProdId attribute to reflect the refreshed ProdId value
         * that was assigned by a database sequence during posting.
         */
          while (newProductsBeforePost.hasNext()){
              ProductsBaseImpl product =
                  (ProductsBaseImpl)newProductsBeforePost.next();
              product.setSupplierId(newSupplierId);
          }
          closeNewProductRowSet();

      }  
    }
    private void closeNewProductRowSet() {
      if (newProductsBeforePost != null) {
        newProductsBeforePost.closeRowSet();
        newProductsBeforePost = null;
      }
    }
    protected void handlePostChangesError() {
      super.handlePostChangesError();
      closeNewProductRowSet();
    }
}
