Skip to content
March 10, 2013 / Pedro Gabriel

Create a Dynamic View Object

Sometimes you may feel the need to display different data and different attributes depending on the query you are performing for the same View Object. If you are feeling this need you should consider creating a Dynamic View Object. For this purpose, today I am going to show how to create a Dynamic View Object.

There are already some examples of how to create dynamic view objects like Andrejus Baranovskis have done in his blog in ADF Generator for Dynamic ADF BC and ADF UI post. In his case he created a dynamic view object based on a SQL query to the database. In my case I will show how to create a dynamic view object based on some external data (a service, a library that you already developed, and so on).

You can download this example here: DynamicViewObject.

The first step you need to take is to create a View Object. In the View Object creation wizard select the option “Rows populated programmatically, not based on a query”, click “Next” followed by “Finish”. And then expose it at the application model.

For now you have created an empty view object with no data but whether you can access in the Data Controls tab.

Next generate the classes for the Application Module.

DynamicVOAppModel

Now it comes the dynamic VO population. In the “AppModuleImpl.java” class create the method that will trigger the VO creation at runtime. Afterwards set this method as a Client Interface in order to call it at the page that uses this VO.

private static final String DYNAMICVO_NAME = "DynamicVO1";
private static final String DYNAMICVO_PATH = "model.viewObjects.DynamicVO1";

public void createDynamicVO(String voType) {
    List dynamicVOAttributes = getDynamicVOAttributes(voType);

    //Get a new View Object Definition.
    ViewDefImpl dynamicVODef = new ViewDefImpl(DYNAMICVO_PATH);

    //Get the current View Object instance.
    ViewObjectImpl dynamicVO = (ViewObjectImpl)findViewObject(DYNAMICVO_NAME);

    //Remove the current View Object instance.
    dynamicVO.remove();

    //Create a new View Object based on the new ViewDefImpl previously created.
    dynamicVO = (ViewObjectImpl)this.createViewObject(DYNAMICVO_NAME, dynamicVODef);

    //Create the attributes for the new ViewDefImpl.
    //The attributes created over the the ViewDefImpl, at this point, get passed to the ViewObjectImpl.
    List readOnlyTaskPayloadItems = createDynamicVOAttributes(dynamicVODef, dynamicVOAttributes);

    //Populates the attributes' values.
    populateDynamicVOAttributes(dynamicVO, dynamicVOAttributes);

    //Set the read only attributes.
    setReadOnlyDynamicVOAttributes(dynamicVODef, readOnlyTaskPayloadItems);
}

The “CreateDynamicVOAttributes” method creates the attributes for the view Object. All attributes must be created as “Updatable” in order to set their values, otherwise you’ll get an error saying that you cannot update read only attributes.

private List createDynamicVOAttributes(ViewDefImpl dynamicVODef, List dynamicVOAttributes) {
    List list = new ArrayList();
    try
    {
        if(dynamicVOAttributes.size() > 0) {
            for(int i = 0; i < dynamicVOAttributes.size(); i++) {
                 DynamicVOAttribute dynamicVOAttribute = dynamicVOAttributes.get(i);                 
                 if (dynamicVOAttribute.isReadOnly())                     
                     list.add(dynamicVOAttribute);                 

                 Class itemClass = getTypeClass(dynamicVOAttribute.getType());                 
                 byte updateable = getByteUpdateable(true);                 
                 AttributeDefImpl attribute = dynamicVODef.addViewAttribute(dynamicVOAttribute.getName(), dynamicVOAttribute.getName(), itemClass);                 
                 attribute.setProperty(AttributeHints.ATTRIBUTE_LABEL, dynamicVOAttribute.getName());
                 attribute.setMandatory(dynamicVOAttribute.isRequired());                 
                 attribute.setUpdateableFlag(updateable);             
            }         
        }         
        dynamicVODef.resolveDefObject();         
        dynamicVODef.registerDefObject();     
    }     
    catch (Exception ex) {         
        ex.printStackTrace();     
    }     
    return list; 
} 

The “populateDynamicVOAttributes” method set the dynamic VO attributes values.

 
private void populateDynamicVOAttributes(ViewObjectImpl dynamicVO, List dynamicVOAttributes) {     
    try     
    {         
        if(dynamicVOAttributes.size() > 0 ) {
            Row notifDinamicaPayloadVORow = dynamicVO.createRow();

            for (int i = 0; i < dynamicVOAttributes.size(); i++)
            {
                DynamicVOAttribute payloadItem = dynamicVOAttributes.get(i);
                notifDinamicaPayloadVORow.setAttribute(payloadItem.getName(), payloadItem.getValue());
            }
            dynamicVO.insertRow(notifDinamicaPayloadVORow);
         }
    }
    catch (Exception ex) {
         ex.printStackTrace();
    }
}

The “setReadOnlyDynamicVOAttributes” set “UpdateableFlag” property to false for read only attributes.

private void setReadOnlyDynamicVOAttributes(ViewDefImpl dynamicVODef, List readOnlyAttributes) {
    try
    {
        for(int i = 0; i < readOnlyAttributes.size(); i++)
        {
            DynamicVOAttribute payloadItem = readOnlyAttributes.get(i);

            byte updateable = getByteUpdateable(false);

            int indexAttribute = dynamicVODef.getAttributeIndexOf(payloadItem.getName());
            AttributeDefImpl attribute = (AttributeDefImpl)dynamicVODef.getAttributeDef(indexAttribute);
            attribute.setUpdateableFlag(updateable);
        }
    }
    catch (Exception ex) {
        ex.printStackTrace();
    }
}

Auxiliar methods.

private Class getTypeClass(int type) {
    Class result;
    switch(type){
       case 0:
         result = Integer.class;
         break;
       case 1:
         result = String.class;
         break;
       case 2:
         result = Date.class;
         break;
       default:
         throw new IllegalArgumentException("Not Suported Type: " +  type);
    }
    return result;
}

private byte getByteUpdateable(boolean updateable) {
    byte result;

    if (updateable)
        result = AttributeDef.UPDATEABLE;
    else
        result = AttributeDef.READONLY;

    return result;
}

Then set the task flow with two pages, one that calls the “createDynamicVOAttributes” (Home page) and other that displays the data filtered (DynamicVO).

DynamicVOTaskFlow

Both Home and DynamicVo pages has the following content:

DynamicVOPages

After dragging and drop the dynamic VO as a Dynamic Form into the page you will get the next result.

DynamicVOAppFinalResult

4 Comments

Leave a Comment
  1. jagare / Apr 27 2013 8:37 am

    Hi there, This is a very interesting post. I would like to try this design. There is no code attached to this post. Can you please send this over to me or upload the code to this post? All, I’m missing is the DynamicVOAttribute.java. The rest of the code, I have salvaged from the post.

    Thank you

  2. jagare / Apr 28 2013 2:39 am

    I was able to redevelop the solution. Thanks.

  3. Alireza / May 18 2013 9:48 am

    hi,where is download link ??

  4. alireza / May 22 2013 10:33 am

    where is download link ???

Leave a reply to Alireza Cancel reply