" *****************************************************************************************************
*
* rel2.txt
*
*   This examples shows how to define a #Object relationship in a model
*
*
******************************************************************************************************"

| aModel aTextDisplay sectionType courseType attributesDict sectionEmployee employeeType |
aTextDisplay := TextWindow  windowLabeled: 'LPC Brokers - creating a #Object relationship'
                           frame: (0 @ 0 corner: 500 @ 500).
aTextDisplay cr. 
 
" let's get our model"
aModel := LPCModel getName: #LpcSample.
 
"Define the Employee type"
employeeType := LPCObjectType 
    createNewFromDbName: #Employee
            table: (Array with:'Employee' with: nil with: nil)
            fields: #( (nil nil 'Employee' 'EMPNUM' 4) 
                           (nil nil 'Employee' 'EMPNAME' 1)
                           (nil nil 'Employee' 'INITIALS' 1) )
            keys: #( (nil nil nil 'Empnum') ).
 

" let's add this type to the model"
aModel addType: employeeType.

"let's retrieve our section type"
sectionType := aModel typeNamed: #Section.

"let's define the relationship"
sectionEmployee := LPCObjectTypeRelationship new: #Object
    objectType: sectionType 
    relatedWith: employeeType 
    matching: ((Dictionary new) at: 'empnum'  put: 'empnum'; yourself ).

"add the attribute for the relationship to the section type"
sectionType addAttribute: 'employee'.

"add the relationship now to the section type attribute Employee"
sectionType addRelationship: sectionEmployee  forAttribute: 'employee'.


"let's retrieve our Course type"
courseType := aModel typeNamed: #Course.

" create and connect an object broker"                                                                                    
anOrb := LPCObjectRequestBroker new.
anOrb driverConnectWindow: aTextDisplay.
 
"retrieve all the courses ..."
courses := anOrb lookUpAllWithType: courseType.

 
" now display the section and the employee name and initials for all the course"
courses do: [ :course |
    aTextDisplay nextPutAll: 'Course:  ', course course; cr ; 
        nextPutAll: 'Sections: ' ; cr.    
    course sections do: [ :section |
        aTextDisplay tab ; tab; nextPutAll: section section asString; tab ;
                nextPutAll: section employee empname , ' ' ,
                    section employee initials ; cr
        ].
    aTextDisplay cr
].

anOrb disconnect.
                
                
    

 







































































































































































































































































































































































 
