ADF BC: Create a UI table binded to Business Components

In this example we are going to show how to create a simple UI table (af:table) which is binded to Business Components.

Once again, I tried to search on the net with the simple criteria:

‘How to create an af:table binded to business components ADF 11g’

And I must admit thatI did not get the answer I was looking for. The information was there, but hidden among other solutions and examples. This can be confusing some times.. So this is a small example of how to this. I hope it will clarify you some things.

Here we go.

Do not forget to always consult the official documentation:

http://docs.oracle.com/cd/E28389_01/web.1111/b31974/web_tables_forms.htm

First, I suppose you already have an 11g version installed on your machine along with some database connection already imported to your JDeveloper. In this example we are using JDeveloper 11.1.1.6.0

Start your Engines! JDeveloper, ok..

After creating the Business Components. You should have something like this on your JDeveloper:

In this example we are using the Countries table from the HR schema.

Next we are going to create a simple page.

1) Go to View Controller

2) Double Click the adfc-config.xml file to open it in the editor. We choose adfc-config in this example simply because we want to focus on the af:table. We could use Bounded Task Flows but it is no mandatory for this example.

3) We open the components section from the Components palette.

4) We select the View component and we drag and drop it to the adfc-config Diagram.

We havent created our jspx just yet. As you will see in the above screen, the name of the view component is changed to index. The default name was view1. I changed it just forconvenience. You can rename it or leave it as is.

5) We double click the the index View component in order to create the page:

In this example we are going to accept the default values and we will just press ok:

After pressing ok:

6) Now it is time to create our af:table which is binded to BC.

But lets just wait a second.

What is an af:table?

According to the Documentation ( click toEnlarge):

Well, this means that we have to provide a Data Collection to an af:table.

AData Collection can be a tree binding in the pageDefinition of our jspx!

But wait.. how are we going to create this… what you say… tree binding.. and how are we going to map it to our table?? It is getting a bit complicated.. is not it?

Well, of course not! We will do it with one drag and drop!! Yes thats right! In ADF and JDeveloper, With one drag and drop we can do everything! We can create the table, and map it to our Business Components!

All we have to do is to locate the Data Controls section in our application navigator.

After locating this section, we are gong to find the HRModuleDataControl and then we will find the
CountriesView1.

Long Story Short, this Data Control, one might say, that is the Data Control produced by the HRModule Application Module (Data Model).

So, every application module we create, we have it as a Data Control in the Data Controls section of the Application Navigator.

All we have to do is to select the Iterator (View object in the Application Module) and drag-and-drop it onto the jspx as a table.

7) select the CoutnriesView1 iterator. Have the jspx open in the editor. Drag and drop it as a table:

After selecting the ADF table option, another window will appear:

8) set the table options:

The above options, let you decide basic functionalities of the table with simple checkBoxes. Furthermore, they let you control which component you want to use for every attribute of the table. You can set the value of the Display label of every attribute. Furthermore you can add or remove attribute as you want. the most common option is the Row Selection, where you can decide wether you want to select none, one or many rows in your table.

All those options, and more can be configured after you actually create the table.

9) press ok in order to create the table AND the pageDefintion file and the DataBindings file (if the pageDef already exists for this page it will be created again. It will simply be updated. The same goes for the DataBindings as well.)

the pageDef. As you can see, a tree iterBindings was created by the wizard that points to the CoutnriesViews1 iterator which leads to HRModuleDataControl:

DataBindings.cpx file. This files holds the mappings between pageDefs and their pages or method calls and generally, whatever accepts pageDefs. It is the file where you can see those mappings.

And that is it! As you can see, it is pretty easy and rather fun! a simple drag and drop, some options and here we are! we have a live and kicking af:table ready !

Of course, you can always change the properties and experiment further with this component.


Reference:
Getting Started. How to create a UI table (af:table) binded to Business Components. ADF BC from our
JCG partner Dimitrios Stassinopoulos at the
Born To DeBug blog.

Source : http://www.javacodegeeks.com/2012/05/adf-bc-create-ui-table-binded-to.html

Advertisements

Maven Integration Testing And Spring Restful Services

Introduction

My original blog showed how to seperate maven unit and integration tests using a very simple example.
http://johndobie.blogspot.com/2011/06/seperating-maven-unit-integration-tests.html Since then a lot of people asked me for a more realistic example than the one used originally. This post shows how you split your unit and integration tests using the original method in a realistic environment where the application is actually deployed to a server.

  • We use Maven to build and unit test some Spring based restful webservices.
  • We then use the Maven Jetty plugin to start a Web server and deploy them to it.
  • We create an In-memory database and create the schema
  • Finally we run all of the integration tests in the seperate

    \src\integrationtest\java
    directory

This article is aimed squarely at showing how to use Maven in a realistic way to start and deploy a set of services to a running server, before running your integration tests. It is not about the subtle details of REST or Spring MVC. I’ll cover this lightly enough to build a working application whilst providing references to more in depth articles for those that want more details.


Code Structure


Running the Example

The full code is hosted at google code. Use the following commands to check it out and run it. Make sure you have nothing running on port 8080 before running the tests.

svn co https://designbycontract.googlecode.com/svn/trunk/examples/maven/spring-rest-example
cd spring-rest-example
mvn clean install -Pit,jetty

You can see the full build on the following Cloudbees hosted Jenkins instance.
https://designbycontract.ci.cloudbees.com/job/spring-rest-example/

Results of running the example

  • The tests in the standard maven test structure are run during the unit test phase as usual.
  • A Jetty Webserver is started
  • The war containing the web server is deployed to the server
  • The hsqldb in-memory database is started and the schema created.
  • The tests in the

    \src\integrationtest\java
    directory are run during the integration test phase.

  • The server is shutdown.


How to create the Spring Service class

The trade service is very simple. It uses a repository to create and find trades. I haven’t included exceptions to keep the whole thing as simple as possible. The only trick here is to add the @Service annotation, otherwise it is straight Java.

@Service
public class SimpleTradeService implements TradeService {
  @Autowired
  TradeRepository tradeRepository; 
 
  public SimpleTradeService(TradeRepository tradeRepository)  {
    this.tradeRepository = tradeRepository;
  }
 
  @Override
  public Long createTrade(Trade t) {
    Long id = tradeRepository.createTrade(t);
    return id;
  }

  @Override
  public Trade getTradeById(Long id) {
    return tradeRepository.getTradeById(id);
  }


How to create the Database repository class

The above service uses a trade repository to create and find trades. We use the Spring class HibernateDaoSupoort to create this class and keep things simple. By extending this class we simply need to create our trade object class, and define our database details in the spring config. All of the other details are taken care of by the framework.

public class HibernateTradeRepository  extends HibernateDaoSupport implements TradeRepository{
  @Override
  public Trade getTradeByReference(String reference) {
       throw new RuntimeException();
  }

  @Override
  public Long createTrade(Trade trade) {
      return (Long) getHibernateTemplate().save(trade);
  }

  @Override
  public Trade getTradeById(Long id) {
      return getHibernateTemplate().get(Trade.class, id);
  }
}


How to create the Database Trade Class

We use standard JPA annotations to define our database trade object

@Entity
public class Trade {
 @Id
 private long id;

The @Entity annotation marks the object as a database entity. The @Id annotation shows which field we want to be our table primary key. For the rest of the fields we use default behaviour so no other annotations are required.

How to Configure the Database

For this example we are going to use Hsqldb to create our database.
http://hsqldb.org/ A new instance of this will be created every time we start the server. To setup the database all we have to do is define it in the spring config trade-servlet.xml

<bean id="sessionFactory"   
<bean id="sessionFactory"  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="packagesToScan"
      value="com.dbc.model" />
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.format_sql">true</prop>
        <prop key="hibernate.transaction.factory_class">
          org.hibernate.transaction.JDBCTransactionFactory
        </prop>
        <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
        <prop key="hibernate.connection.pool_size">0</prop>
        <prop key="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</prop>
        <prop key="hibernate.connection.url">
          jdbc:hsqldb:target/data/tradedatabase;shutdown=true
        </prop>
        <prop key="hibernate.connection.username">sa</prop>
        <prop key="hibernate.connection.password"></prop>
        <prop key="hibernate.connection.autocommit">true</prop>
        <prop key="hibernate.jdbc.batch_size">0</prop>
        <prop key="hibernate.hbm2ddl.auto">update</prop>
      </props>
    </property>
  </bean>

The session factory defines our database connection details. The most important property is

<prop key="hibernate.hbm2ddl.auto">update</prop>

This property tells hibernate to update the database when the application starts. It will effectively create the table for the trade object from the annotations on our trade object. When you run the tests, you will see that the following SQL is executed on startup.

11:30:31,899 DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate SchemaUpdate:203 
- create table 
Trade (id bigint          not null, 
       description        varchar(255), 
       reference          varchar(255), 
       primary key (id))

Thats a new database setup and ready to go.

Creating The Restful Interface.

I’m just going to cover the basics here. For some great examples follow these links
http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/
http://www.stupidjavatricks.com/?p=54

How to Create the Spring Controller

The Spring controller is the key to this whole example. It is the controller that takes our requests and passes them to the trade Service for processing. It defines the restful interface. We use @PathVariable to make things simple.

@RequestMapping(value = "/create/trade/{id}")
public ModelAndView createTrade(@PathVariable Long id) {
  Trade trade = new Trade(id); 
  service.createTrade(trade);
  ModelAndView mav = new ModelAndView("tradeView", BindingResult.MODEL_KEY_PREFIX + "trade", trade);
  return mav;
}

@RequestMapping(value = "/find/trade/{id}")
public ModelAndView findTradeById(@PathVariable Long id) {
  Trade trade = service.getTradeById(id);
  ModelAndView mav = new ModelAndView("tradeView", BindingResult.MODEL_KEY_PREFIX + "trade", trade);
  return mav;
}

It works quite simply by populating the

@PathVariable id
with the value from

/find/trade/{id}
For example, requesting /find/trade/1 will populate reference with "1" requesting /find/trade/29 will populate reference with "29" More information can be found here:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates

How to configure the Web Application

The configuration of the web application in web.xml is very straightforward. First we register the Spring Servlet

                   
 
  
    
   
   
     
      
    trade
   
     
   
   
     
      
    org.springframework.web.servlet.DispatcherServlet
   
     
   
   
     
      
    1
   
     

  
    

Next we define a mapping to the servlet. This mapping will pass all requests to our servlet.

                    

  
    
  
   
     
      
    trade
   
     
  
   
     
      
    /*
   
     

  
    


How to Configure Spring

The Spring configuration consists of a number of distinct elements. The first line simply tells Spring where to look for annotations


  
    

The BeanNameViewResolver takes the name


  
    

This scary looking piece of XML does the job of making sure that the Trade object is returned as XML. XStream will take the object and automatically convert to an XML Format.


  
    
  
   
     
    
      
      
     
       
    
     
       
    
      
  
   
     
  
    

The Trade class defines the XStream annotation for this.

@XStreamAlias("trade")
public class Trade {

In our case you can see from the test that we get the following from /search/trade/1

                   

  
      
 
   
     
      
    
  1
 
   
     

  
    


How to start and stop the Jetty Server

I use the Jetty Plugin to start the server and deploy the war file contained the services.
http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin The server is started with the following snippet from pom.xml

<execution>
  <id>start-jetty</id>
  <phase>pre-integration-test</phase>
  <goals>
    <goal>run</goal>
  </goals>
</execution>

The server is stoped with the following snippet from pom.xml

<execution>
  <id>stop-jetty</id>
  <phase>post-integration-test</phase>
  <goals>
    <goal>stop</goal>
  </goals>
</execution>


How to run the Integration Tests

The integration tests are run using failsafe as described in the orgiinal article.
http://johndobie.blogspot.com/2011/06/seperating-maven-unit-integration-tests.html We use the new Spring RestTemplate to make the call to the service easy.

@Test
public void testGetTradeFromRestService() throws Exception {
  long id = 10L;
  createTrade(id);
  String tradeXml = new RestTemplate()
                          .getForObject(
                          "http://localhost:8080/find/trade/{id}",
                          String.class, id);
  
  System.out.println(tradeXml);
  Trade trade = getTradeFromXml(tradeXml);
  assertEquals(trade.getId(), id);
}


Reference:
Maven Integration Testing And Spring Restful Services from our
JCG partner John Dobie at the
Agile Engineering Techniques blog.

Source : http://www.javacodegeeks.com/2012/05/maven-integration-testing-and-spring.html