Friday 18 November 2011

ADF - Programmatically creating Master-Detail VO rows using a View Link

Lot of things in ADF can be done declaratively and in some cases to minimize the coding we can do half declaratively and then use that for our coding. In this case we will see how to use the view link to create rows in detail VO programmatically. When it comes to UI we do not need to do anything extra apart from creating a View Link. If you are not familiar with View Links then please go through this link for knowing more about View Links


Now if these VOs are used in the UI with Create and Delete buttons dragged from there operations then you would not need any additional coding. But if you want to do a setup job by creating master and detail VO rows when the user comes in intially. Then you can follow the steps below to create them programmatically.

You need to have a Master and Detail VO create based on a EO which has association between them. Then you create the View Link based on the association of those EOs. 

Please check the below screen shots for an example
EOs and Association (CommnPreferenceEO - Master VO and CommnPrefDetailEO - Detail VO)



Association's Composition behavior settings


If you want it to be delete cascaded then please select Implement Cascade Delete also.

Next we will see the how VOs and View Link looks.




Apart from the EO, Association, VO and View Links you need to create a View Criteria for the master vo to query based on a entity. In our case we will have a findByPatientId view criteria. So for the logged in Patient we will execute the Master VO using this View Criteria and then ADF automatically gets the Detail VO records for that master.

If you see the View Link is based on the association. 

Now we need to add these VO's to the AM through the View Link

Once you do that it will look like the following..


Now we are ready to code for creating the master and detail rows programmatically.

ViewObjectImpl commnPrefVO = getComunicationPreferenceVO();
commnPrefVO.setNamedWhereClauseParam("pPatientId", patientId);
commnPrefVO.executeQuery();

RowSetIterator commnPrefItr = commnPrefVO.createRowSetIterator(null);

// If the Master record already exist
if(commnPrefItr.hasNext()){
    Row row = commnPrefItr.next();
    // Access the detail VO using the a Attribute that was created in the Master VO through the View      Link
    // Remeber, it would return a RowIterator for the Detail VO
    RowIterator commnPrefDetailItr = (RowIterator) row.getAttribute("CommunicationPrefDetailVO");
    
    // We can direcly create Detail VO rows using the RowIterator itself.
    Row comnPrefDetailRow = commnPrefDetailItr.createRow();
    commnPrefDetailItr.insertRow(comnPrefDetailRow);
    
    // The foreign key attribute for detail vo will be automatically set.
    // If you want to set any other attributes please do it here.
    comnPrefDetailRow.setAttribute("<AnyAttribute>", <AttributeValue>);
    }
// If the Master record itself doesn't exist
else {
    Row row = commnPrefVO.createRow();
    commnPrefVO.insertRow(row);
    row.setAttribute("PatientId", patientId);

    // Following piece of code is same as above if condition.
    RowIterator commnPrefDetailItr = (RowIterator) row.getAttribute("CommunicationPrefDetailVO");
    
    Row comnPrefDetailRow = commnPrefDetailItr.createRow();
    commnPrefDetailItr.insertRow(comnPrefDetailRow);
        
    comnPrefDetailRow.setAttribute("<AnyAttribute>", <AttributeValue>);


We are done with the setup. Now when this method is called before the page is loaded the VOs will be setup with data for it to be displayed in the page.

No comments:

Post a Comment