/**********************************************************************
 * Copyright (C) 2006-2013 by Consultingwerk Ltd. ("CW") -            *
 * www.consultingwerk.de and other contributors as listed             *
 * below.  All Rights Reserved.                                       *
 *                                                                    *
 *  Software is distributed on an "AS IS", WITHOUT WARRANTY OF ANY    *
 *   KIND, either express or implied.                                 *
 *                                                                    *
 *  Contributors:                                                     *
 *                                                                    *
 **********************************************************************/ 
/*------------------------------------------------------------------------
    File        : CustomerBusinessEntity
    Purpose     : Business Entity for Customer
    Syntax      : 
    Description : Example Business Entity for customer
    Author(s)   : Mike Fechner / Consultingwerk Ltd.
    Created     : 25.02.2011 20:53:22
    Notes       : 
  ----------------------------------------------------------------------*/

ROUTINE-LEVEL ON ERROR UNDO, THROW.

USING Consultingwerk.OERA.*           FROM PROPATH .
USING Consultingwerk.SmartFramework.* FROM PROPATH . 
USING Progress.Lang.*                 FROM PROPATH .

CLASS Consultingwerk.SmartComponentsDemo.OERA.Sports2000.CustomerBusinessEntity 
    INHERITS SmartBusinessEntity: 

    { Consultingwerk/SmartComponentsDemo/OERA/Sports2000/dsCustomer.i }

    /*------------------------------------------------------------------------------
        Purpose: Constructor of the CustomerBusinessEntity class                                                                     
        Notes:   Passes the handle of the dataset instance to the base class and 
                 sets the default DataAccessName                                                                        
    ------------------------------------------------------------------------------*/
    CONSTRUCTOR PUBLIC CustomerBusinessEntity ():
        SUPER (DATASET dsCustomer:HANDLE).
        
        THIS-OBJECT:DataAccessName = "Consultingwerk.SmartComponentsDemo.OERA.Sports2000.CustomerDataAccess":U .

    END CONSTRUCTOR.

    /*------------------------------------------------------------------------------
        Purpose: Provides a hook to modify data in the ProDataset after Read and 
                 Update operations (i.e. population of aggregated values)                                                                     
        Notes:   Invoked during FetchData () and SaveChanges ()                                                                     
    ------------------------------------------------------------------------------*/
    METHOD OVERRIDE PUBLIC VOID ReceiveData ():
        
        FOR EACH eCustomer:
    
            IF (eCustomer.Comments > "":U) THEN 
                ASSIGN eCustomer.Flags = "C":U .  
            ELSE IF (eCustomer.Balance > 1000) THEN 
                ASSIGN eCustomer.Flags = "B":U .                
            ELSE 
                ASSIGN eCustomer.Flags = "":U .
        END.         
        
    END METHOD.

    /*------------------------------------------------------------------------------
        Purpose: Provides a hook for high level data validation before Update 
                 operations                                                                     
        Notes:   Invoked during SaveChanges (). When the ERROR flag of the ProDataset
                 is set, the Update operation will be cancelled before writing back
                 the data to the database using the DataAccess object                                                                      
    ------------------------------------------------------------------------------*/
    METHOD OVERRIDE PUBLIC VOID ValidateData ():
        
        FOR EACH eCustomer ON ERROR UNDO, THROW:

            Validate:IsNotNullOrEmpty (BUFFER eCustomer:HANDLE, "Name":U, 
                                       MessageFormatter:GetMessage ("VALMSG":U, 1, "Customer Name")).
            
            IF 
            Validate:IsNotNullOrEmpty (BUFFER eCustomer:HANDLE, "SalesRep":U, 
                                       MessageFormatter:GetMessage ("VALMSG":U, 1, "Sales Rep")) 
            THEN 
            Validate:CanFind (BUFFER eCustomer:HANDLE, 
                              "SalesRep":U,
                              "Consultingwerk.SmartComponentsDemo.OERA.Sports2000.SalesRepBusinessEntity":U,
                              "eSalesRep":U,
                              SUBSTITUTE ("FOR EACH eSalesRep WHERE eSalesRep.SalesRep = &1":U, QUOTER (eCustomer.SalesRep)),
                              MessageFormatter:GetMessage ("VALMSG":U, 2, "Sales Rep", eCustomer.Salesrep)) .
        END.
        
    END METHOD.

END CLASS.
