Friday 27 March 2015

Native Inner Query join in EJB

EJB stands for Enterprise JavaBeans. There are two types of query one is Native Query and other is Named Query.
Named query means Java Persistence query language(JPQA). In Native query SQL is used to express database queries. An SQL INNER JOIN return all rows from multiple tables based on a common field between them. As a IDE Jdeveloper has been used for creating this application.(Download Application)

Steps::
  1. Create a Custom Application and Generate Entities from tables for Person, Country, State and City tables (From HR schema)

  2. Generate a new Session Bean with default options.

  3. Create a java class which will be used for mapping native query result. Generate accessors for it (such as i have created PersonDetail class). This class should implements Serializable interface.

  4. Create a method inside session bean.

  5.    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)  
       public List<PersonDetail> getInnerJoin() {  
            Query query = em.createNativeQuery("SELECT Person.id,Person.name,Country.name,State.name,City.name from Person INNER JOIN Country ON Person.COUNTRY_ID = Country.ID INNER JOIN State ON Person.STATE_ID = State.ID INNER JOIN City ON Person.CITY_ID = City.ID order by Person.id");  
            List<Person> list = query.getResultList();  
            Iterator person = list.iterator();  
            List<PersonDetail> personIterator = new ArrayList();  
            while (person.hasNext()) {  
                  Object col[] = (Object[]) person.next();  
                  PersonDetail test1 = new PersonDetail();  
                   test1.setPersonId(new Integer(col[0].toString()));  
                   test1.setPersonName(col[1].toString());  
                   test1.setCountryName(col[2].toString());  
                   test1.setStateName(col[3].toString());  
                   test1.setCityName(col[4].toString());  
                personIterator.add(test1);  
              }  
         return (personIterator);  
       }  
    

  6. Add this method into Session EJB, Local and Remote java file.
    List<PersonDetail> getInnerJoin();

  7. Right click on SessionEJBBean, 

    •  Select "Session Bean Client.."
    •  Choose client type as "Java Client", finish it.

  8. Add this code inside Session EJB Client

  9.   for (PersonDetail person : (List<PersonDetail>) sessionEJB.getInnerJoin())  
                {  
                    System.out.println(".................................................");  
                    System.out.println("Person Id = "+person.getPersonId());  
                    System.out.println("Person Name = "+person.getPersonName());  
                    System.out.println("Country Name = "+person.getCountryName());  
                    System.out.println("State Name = "+person.getStateName());  
                    System.out.println("City Name = "+person.getCityName());  
                    System.out.println(".................................................");  
                }  
    

  10. First run session EJB bean and after that run Session EJB client.

  11. Screen shot of output console


Tuesday 3 March 2015

Automatic button click in ADF using Java Script

Some time we want to perform a click operation automatically on page load. Consider a scenario where we want to call managed bean on page load event.
To achieve this::


  • Create a jsp or jspx page.
  • Drag & Drop button component inside form.
 <af:button text="click" id="b3" binding="#{backingBeanScope.backing_Shelf.b3}"  
          visible="false"  
          actionListener="#{backingBeanScope.backing_Shelf.callRowFinder}"  
      clientComponent="true"/>   
  •  On page inside <document > tag you can search for <resource> tag. 
  •  First create resource tag, select type="javascript". ( Type defines which type of function you will be keeping inside resource tag, it can be either javascript or css)
  • Create a function such as "invokeOnLoad" inside resource tag. To locate button component onto page pass button id inside method (which button you want to click). In my case button id is "b3". 

Method


  <af:resource type="javascript">  
         function invokeOnLoad() {  
             var button = AdfPage.PAGE.findComponentByAbsoluteId('b3');  
             AdfActionEvent.queue(button, true);  
             event.cancel();  
         }  
     </af:resource>  
  • Now its time to call  method. To achieve this inside <Document> tag,  client listener tag is present, this tag has load property. It invokes method during document load.
                 <af:clientListener method="invokeOnLoad" type="load"/>


Note::
If you use "AdfActionEvent.queue(button)" instead of "AdfActionEvent.queue(button,true) then   button click will occur infinity times. 

Monday 2 March 2015

JAX RS to RAML Command Line Interface


The JAX-RS API uses Java programming language annotations to simplify the development of RESTful web services (refer). Java class files contain jax-rs annotation for actions and resources.
Jax-rs make developer to easily develop Rest Service. RAML stands for RESTful API Modeling Language, it encourages user for re-usability, pattern sharing and enable discovery. As you understood Jax-Rs and RAML technologies.  Now its time for conversion from Jax-Rs to RAML.

  1. Create  REST – JAX RS Service for Employee Download service
  2. Download  1.0 Final: jsr311-api-1.1.1.jar.
  3. Download Command line tool jax-rs-to-raml.jar.
  4. Create JAX RSToRAML/service folder, put java files inside this folder. Open command prompt from this folder.
  5.   In order to generate your RAML code, you must run it by invoking:: 

  6.         javac [classes]  
             -sourcepath [path-to-your-java-source-code]  
             -classpath [your-classpath]  
             -processorpath [path-to-your-jar]/jax-rs-to-raml.jar  
             -processor com.mulesoft.jaxrs.raml.annotation.model.  
              apt.RAMLAnnotationProcessor  
             -Aramlpath=[path]  
             -implicit:class  

    Options::
    ·       classes: The JAVA classes from which you want to generate a RAML representation.
    ·       -sourcepath: The folder containing the packages where the classes (and classes' dependencies) are located
    ·       -classpath: The classpath of the Java project, the classes belong to. 
    ·       -processorpath: The path to the jar you downloaded (or built).
    ·       -processor: Indicates the annotation processor. Use:com.mulesoft.jaxrs.raml.annotation.model.apt.RAMLAnnotationProcessor
    ·       -Aramlpath: The folder which the generated RAML definition will be placed.
    ·       -imlicit: class specifies that the annotations in the dependent files must also be resolved by the processor.

  7. Example::

  8.      javac src/project1/Employee.java src/project1/EmployeeList.java src/project1/EmployeeService.java src/project1/GenericApplication.java   
             -processorpath jax-rs-to-raml.jar   
             -classpath jsr311-api-1.1.1.jar   
             -processor com.mulesoft.jaxrs.raml.annotation.model.apt.RAMLAnnotationProcessor   
             -Aramlpath=RAML   
             -implicit:class  
    

  9. Generated RAML document doesn’t include schema. Steps to make valid RAML document::

    •           Convert JSON output into JSON schema (JsonToJsonSchema)               
     JSON Code

                           {  
                               "list" : [  
                                        {  
                                           "id" : 1,  
                                           "name" : "Arnold"  
                                         },  
                                         {  
                                            "id" : 2,  
                                             "name" : "Simon"  
                                           }  
                                      ]  
                              }  
    

    •             Add JSON schema into RAML file.
    •        EmployeeService.raml is valid raml document.(RAML Location)