Sunday, April 29, 2007

Invoking Create method programmatically

Wizards are cool, but I'm sure that one day you will need to remember the roots and add some code by hand. Just don't afraid this, it could be even simpler than using wizards. In this sample I will explain how you could develop Create form without (or may be almost without ;) using JDeveloper wizards. Described principles can be applied in the same way for other CRUD type operations.

Provided sample application - CreateMethod.zip is based on standard HR schema, two tables are used - COUNTRIES and REGIONS. First table is used for Create operation, second is a source for a LOV component. So, developed application can be used to insert countries data into a table from HR schema.

While developing Create form, at first I have used simple drag and drop method to create two af:inputText and one af:selectOneChoice components. Those components were created without binding to Data Control. After that I have declared in page definition file three variables and generated attributeValue elements for them. When described steps were done, values of components available in JSPX page were binded to elements defined in page definition file. For example, for Country ID value binding is equal to: #{bindings.countryIdAttr.inputValue}, where countryIdAttr is generated attributeValue element. Page definition file structure:


When components and page definition were created, I have developed create method code in Application Module class. Method code is rather simple, it acquires definition for Countries entity and opens transaction. After that, new instance of Countries entity is initialized by values submited from ADF Faces components available in JSPX page. If there is no exceptions, data for new row is commited to the database. Create method code:


When method code is created, don't forget to add method signature into Application Module client interface. This will allow to call created method from JSPX page.

And, my last step was to drag and drop createCountry(String, String, Number) from Data Controls palette to af:panelForm footer facet. Method parameters should be binded to variables defined in page definition file. Data Controls palette for this sample:


af:panelForm component contains two af:inputText, one af:selectOneChoice and one af:commandButton:


Finally, Create form in action:


When running sample application, don't forget to add adf-faces-impl.jar and jsf-impl.jar to application's WEB-INF\lib directory.

22 comments:

  1. Hi Addrejus,

    How to replicate oracle forms toolbars in adf faces?

    I have a insert page with 1 master and 5 child tables where master table is in main form and all childs are in tabbed pane.I have to perform create operation in all 6 tables by clicking one single command button which is part of a toolbar.
    Oracle forms provides such facilities and what about adf faces?

    ReplyDelete
  2. Hi,

    Yes, it's possible in ADF, but I'm not sure it is easier or harder comparing to Oracle Forms.

    As a hint you can use my post - Create, Edit and Delete operations in Master-Detail af:table components. There are separate Create buttons, but you can use only one for all your tables.

    General idea how to do this, just double click on any Create button and implement Action Method Binding in Backing bean. Into generated action method, will be automatically included code, associated with Create button. You can add into this method, Create functionality code, related to other tables as well. So, you will have one button, which creates new rows for several tables.

    Regards,
    Andrejus

    ReplyDelete
  3. Hi Andrejus,

    Thanks for your response. To get the toolbar set I am using af:toolbox,af:toolbar and af:commandToolBarButton. Is this proper way?

    Regarding the functionality, I dragged and droped view obj as ADF creation form on the page and put one button for create at the top. In the backing bean method of the button I did like following
    ...
    ...
    ...
    //table1
    AddressViewImpl addressV = (AddressViewImpl)appM.findViewObject("AddressView1");
    AddressViewRowImpl addrow = (AddressViewRowImpl)addressV.getCurrentRow();
    addrow.setManId(manId);

    //table2
    ContactsViewImpl contV = (ContactsViewImpl)appM.findViewObject("ContactsView1");
    ContactsViewRowImpl contRow = (ContactsViewRowImpl)contV.getCurrentRow();
    contRow.setManId(manId);

    .....
    ....... appM.getDBTransaction().commit();


    Is this the feasible way of doing this?

    ReplyDelete
  4. Hi,

    No, method should look like:

    public String createButton_action() {
    BindingContainer bindings = getBindings();
    OperationBinding operationBinding =
    bindings.getOperationBinding("Create");
    Object result = operationBinding.execute();
    if (!operationBinding.getErrors().isEmpty()) {
    return null;
    }

    operationBinding = bindings.getOperationBinding("Create1");
    result = operationBinding.execute();
    if (!operationBinding.getErrors().isEmpty()) {
    return null;
    }

    return null;
    }

    Regards,
    Andrejus

    ReplyDelete
  5. Hi Andrejus

    The code snippet for creating a record was userful. Could you tell how to update a record in similar manner

    Regards
    Prasad Jayakumar

    ReplyDelete
  6. Hi Prasad,

    You can check this section from ADF documentation - 7.8.2 Example of Finding a Row and Updating a Foreign Key Value.

    I believe, it will be useful.

    Regards,
    Andrejus

    ReplyDelete
  7. Hi Andrejus,
    Is it possible Update 3 entities at once.
    I'm using jdev 10.1.3 and ADF BC.
    I have 3 entity object and i have created a ADF form using
    entity1's view object.
    When I submit the form using the commit button I have to Insert one
    new row in entity1 & entity2 and update some field in entity3
    using the form values.
    what should i do to achieve this.Please help me....

    ReplyDelete
  8. Hi Ans,

    It is possible.

    You can insert new row in Entity1 as it described in my post. When new row will be inserted in Entity1 you can access DBSequence assigned value for primary key (if you are using it) and use this value during insert in Entity2. Finally Entity3 - you can use primary key to find needed row and update it.

    Regards,
    Andrejus

    ReplyDelete
  9. Hi Addrejus,
    Thank u for ur replay.
    Will u please explain ho to do it.
    Im new to Jdev and ADF.
    I'm using a DBSequence to assign a primary key during insert into entity1.
    will u please tel me how to access this DBSequence assigned value and, How to use it to insert and update other entities
    thanks
    Ans

    ReplyDelete
  10. Hi,

    In Oracle documentation they explain it with examples - 7.8 Working Programmatically with Entity-Based View Objects. In section 7.8.3 DBSequence is used as well.

    Tell me if there will be problems.

    Regards,
    Andrejus

    ReplyDelete
  11. Hi,
    i'm new in ADF and i want to create 3 object_views in one page (page1) and in another page (page2)i want creation of list of values to filter or search data of object_views in page1
    what's the code of search button
    best regards,
    Abeer

    ReplyDelete
  12. Hi,

    I am working on the table where my requirement is to create row to add Item numbers for my order. I have used simple table bounded by VO on Popup and when i clicked "create insert" button it should add new empty row to the table. but since its on popup its closing down the popup. Any suggestion???

    Thanks & Regards,
    Viraj Save

    ReplyDelete
  13. Hi,

    You can check my post with Insert PopUp: http://andrejusb.blogspot.com/2009/11/crud-operations-in-oracle-adf-11g-table.html

    Regards,
    Andrejus

    ReplyDelete
  14. Hi,

    Thanks for your quick responce.

    Actually the thing is, on popup I want to add rows on the table so that I can enter multiple records.

    Also the method getBindings() giving error for me.


    Thanks & Regards,
    Viraj Save

    ReplyDelete
  15. Hi,

    How can I implement a delete function based on the example above?

    Thanks
    Regards Corneliu

    ReplyDelete
  16. What happend if i want create a method with one more table. I mean: Regions and Countries and Locations. On the form, if i want know this this Location from where Region( i mean: from Locations table i could know it's come from which Region? through CoutryID).
    Exactly form Locations like this i want:
    LocationID
    City
    State_Province
    RegionName (list of values from Regions table)
    CountryName (list of values from Countries table)

    When you selected RegionName --> CountryName will show but only CountryName in that Region show.
    It's like US only have Washington state, Texas state.....


    I'm trouble on this...If you can give me some solution..i'm realy thank you.





    ReplyDelete
  17. This is out of the box Master-Detail functionality in ADF.

    Andrejus

    ReplyDelete
  18. can you give a referent for this? maybe a link which one explan about this trouble. Thank you!

    ReplyDelete
  19. You should check adf developer guide...

    ReplyDelete
  20. Hi Andrejus, Thanks for your wonderful posts. I find them quite usefull.

    I'm trying to implement this example but soon after dropping input texts I cant find the page definition file where I can create the variables and generate bindings.

    Even on the page that I've created, when you click on the bindings all options are disabled.

    Is there anything that I've done wrong or could it be that Jdeveloper 11.1.2.4 doesn't support the feature?

    ReplyDelete
  21. Links to the sources are broken :-(

    ReplyDelete
  22. Great, many thanks for the good examples for learning!

    ReplyDelete