Sunday, 29 September 2013

Event Driven GWT RPC using Hibernate Annotation and SQLServer 2008

            Event Driven GWT RPC using Hibernate Annotation



Hi Reader,
    
  This is my seventh post. I going to start on GWT RPC using Hibernate Annotation using SQLServer 2008.


The Google Web Toolkit (GWT) is a toolkit to develop Ajax web application with Java. The programmer writes Java code and this code is translated into HTML and Javascript via the GWT compiler. The compiler creates browser specific HTML and JavaScript to support all the major browsers correctly. GWT supports a standard set of UI widgets, has build in support for the browser back button and a JUnit based test framework.
GWT provides two modes
      Development Mode: 
      Allows to debug the Java code of your application directly via the standard Java debugger.
  Web mode: 
   
   The application is translated into HTML and Javascript code and can be deployed to a 
   webserver.

 Modules :

 A module defines a configuration file as "modulename.gwt.xml" . Each module can define one or more Entry Point classes.

Entry Points :

An  Entry Point is the starting point for the GWT Application similar to the main method in the java program. A java class which is an entry point must implement the interface "com.google.gwt.core.client.EntryPoint". Entry Point defined one method onModuleLoad() which is the starting point of the GWT application.

HTML Pages :

Module is connected to the HTML page or the host page. Code for the GWT web application executes within this HTML document.
HTML page defined the "div" containers to which GWT application can assign UI components.
GWT UI components are simply assigned to the body tag of the HTML page. GWT application can be customized by the CSS files.

GWT Remote Procedure Call :


GWT provides its own remote procedure calls  which allow the GWT client to call server-side methods. The implementation of GWT RPC is based on the Servlets technology. GWT allows Java objects to be sent directly between the client and the server; which are automatically serialized by the framework. With GWT RPC the communication is almost transparent for the GWT client and always asynchronous so that the client does not block during the communication.The server-side Servlets is usually referred to as a "service" and the remote procedure call is referred to as "invoking a service." These objects can then be used on the client (UI) side.



  Above we have mentioned the code flow for the Application.

Step 1:

 We have two types of controller one is ListController and  DetailController. We need to register the events in the controller.

IAEventManager :

public class IAEventManager{
                                                    /**
                                                            event type
                                                   */

                                                      public static final getRoles();

                                                }   

Step 2:

We are using event class IAEventManager to define all the event.  Depending  upon the event delegator will delegate the request to appropriate service call.  We need to register all the events in the constructor of the controller.


public class RoleListController extends PagingListController {

/**
* Constructor
*/
public RoleListController() {
// Register Events

registerEventTypes(IAEventManager.getRoles());


}

Step 3:

 We need to register the delegator in the initialize()  method of the controller.

 public class RoleListController extends PagingListController {

private RoleDelegator roleDelegator;

public void initialize() {
roleDelegator = new RoleDelegator();



}

Step 4:

 All the request is handled by handleEvent(AppEvent pevent) method of the Controller.  In handleEvent method we need to check the incoming event type (e.g. pevent) with the register event type in IAEventManager.

After matching the appropriate event type we will invoke the method from the server.


public void handleEvent(final AppEvent pEvent) {
if (pEvent.getType() == ApplicationEventTypes.getRoles()) {
private void getRoles(AppEvent pevent);


Step 5:


Async Call from Controller will forward to Role Delegator. Delegator is responsible for calling appropriate method in server side class RoleServiceImpl.

public class RoleDelegator {

/**
* Constructor creates RolePermissionAsync object instance
*/
public RoleDelegator() {
} // RoleDelegator

/**
* Delete of role
* @param pRoleListRequest contains request
* @param pCallback contains Asynchronous call 
*/
public void getRoles(final RoleListRequest pRoleListRequest, final AsyncCallback<RoleListResponse> pCallback) {
ServiceLocator.getRoleServiceAsync().deleteRole(pRoleListRequest, pCallback);

  }


Step 6:

  We need to define two methods using onSuccess() and onFailure() methods. We need to provide the request object (e.g. RoleDetailRequest) to the server. 


public void handleEvent(final AppEvent pEvent) {
if (pEvent.getType() == ApplicationEventTypes.DELETE) {
private void getRoles(AppEvent event){

final RoleDetailEventDataWrapper<RoleDetailDTO> roleDetailEventDataWrapper = pEvent.getData();
final RoleDetailDTO roleDetailDTO = roleDetailEventDataWrapper.getEventData();
DeleteRoleRequest deleteRoleRequest = new DeleteRoleRequest();
deleteRoleRequest .setRoleDetailDTO(roleDetailDTO);

roleDelegator.getRoles(deleteRoleRequest, new SecuredAsyncCallback<DeleteRoleResponse>() {

@Override
public void onFailureException(final Throwable pCaught) {

RequesterException error = (RequesterException) pCaught;
}

public void onSuccess(final DeleteRoleResponse pResult) {



                         }



Step 7:

After getting all the data we can use RoleDetailModel class that contains all the keys which will be used for displaying  data in the grid view.

Here all the data is set in the bean using key and same keyed value is used to retrieve data.


public class RoleDetailDTOModel extends BaseBeanModel {

    public static final String MEMBER_roleId = "roleId".toUpperCase();
    
    public RoleDetailDTOModel(final RoleDetailDTO pDto) {
        super();
        setBean(pDto);
        beanProperties.add(MEMBER_roleId);
      }

    public <X> X get(String pKey) {
        String key = pKey.toUpperCase();
        RoleDetailDTO bean = getBean();
        if (allowNestedValues && NestedModelUtil.isNestedProperty(key)) {
            return (X) NestedModelUtil.getNestedValue(this, key);
        }
        if (key.equals(MEMBER_roleId)) {
            Object value = bean.getRoleId();
            return (X) processValue(value);
        }
      }
    
    public <X> X set(String pKey, X val) {
        String key = pKey.toUpperCase();
        Object obj = val;
        obj = setNestedModel(key, val, obj);
        RoleDetailDTO bean = getBean();
        if (allowNestedValues && NestedModelUtil.isNestedProperty(key)) {
            X old = (X) NestedModelUtil.setNestedValue(this, key, val);
            notifyPropertyChanged(key, val, old);
            return old;
        }
        if (key.equals(MEMBER_roleId)) {
        Object old = get(key);
            bean.setRoleId((Integer) obj);
            notifyPropertyChanged(key, val, old);
            return (X) old;
        }
      }
   }

Step 8:

We need to create the Data Wrapper class which will return the DTO object. Finally DTO object will be saved in the request object.

public class RoleDetailEventDataWrapper<T> extends IAEventDataWrapper<RoleDetailEventDataWrapper<T>> {

/**

*/
private static final long serialVersionUID = -8878697788912917430L;

private T data = null;

//CHECKSTYLE:OFF
private static final String TOKEN_NAME_DATA = "data";

public RoleDetailEventDataWrapper(final T pEventData) {
set(TOKEN_NAME_DATA, pEventData);
data = pEventData;
}

/**
* @param eventData the eventData to set
*/
public void setEventData(final T pEventData) {
set(TOKEN_NAME_DATA, pEventData);
}

/**
* @return the eventData
*/
@SuppressWarnings("unchecked")
public T getEventData() {
return (T) get(TOKEN_NAME_DATA);
}

  }

Step 9:

  We need to create DeleteRoleRequest and RoleDetailDTO for creating request object for the user.

  public class DeleteRoleRequest implements serializable{

        private RoleDetailDTO roleDetailDTO;

        //   getter  and setter methods.
     }


public class DeleteRoleResponse implements serializable{
         
         private RoleDetailDTO roleDetailDTO;

          // getter and setter methods.

     }


Step 10 :

   We are using transfer object to transfer multiple data elements over a tier. A single method call will be used to send and retrieve the transfer object.
When the client requests the enterprise bean for the business data, the enterprise bean  construct the transfer object, populates it with it's attribute value,  and pass it by value to the client.

Advantages :

1. Simplifies the Entity Bean and Remote interfaces.

2. Transfer more data in fewer remote calls.

3. Reduces Network Traffic.

4. Reduces code duplication.

5. Concurrent access  and transactions.

6. Simplifies remote object and remote interface.

Data Transfer Object must be serialized to transfer data from application tier to web tier.

RoleDetailDTO : 
public class RoleDetailDTO implements serializable{

         public RoleDetailDTO(){
        
                                              }          private Integer roleId;

       // getter and setter methods.

   }






Saturday, 28 September 2013

Accessing Application outside Virtual Directory in Apache 2

                                            Virtual  Directory in Apache 


Hi  Reader,
This is my  sixth post. I am going to start  how to access application outside the virtual directory in Apache.

In my previous post I have described about Document Root, NameVirtualHost and VirtualHost.

We can access the Application outside the Document Root of the Apache using virtual directory.
Here we are not putting the application inside Apache's Document Root. We can access Application outside the Document Root using virtual directory.

Step 1:
   
  We need to load mod_alias Apache module in order to create the virtual directory. Open the httpd.conf and ensure below line is there.

LoadModule   alias_module   modules/mod_alias.so

If there is a # sign remove proceeding that line. Finally we need to restart the Apache.

Step 2:

 Once Apache mod_alias has been loaded, we will try to create the virtual directory.
Open httpd.conf file and search for the  <IfModule alias_module> tag and insert below lines. Suppose we storing our Application in /home/web/satya1. 
So, below we are creating alias of the virtual directory named as /satya1 and also specify it's physical location as /home/web/satya1.

<IfModule alias_module>
    # Add this line for your virtual directories
     Alias  /satya1  /home/web/satya1
     Alias  /satya2  /home/web/satya2

We can create more than one virtual directory in the same syntax.

Step 3:

We need to configure the virtual directory. We need to add below lines to configure virtual host.

<Directory "/home/web/satya1">
Options None
AllowOverride All
Order allow,deny
Allow from all
</Directory>

Finally we need to restart the Apache 2. Make the configuration change effect.


Creating and Accessing Application inside Virtual Directory in Apache 2

                                                    Virtual Directory in Apache 

Hi 
      This is my fifth post. I am going to start how creating Virtual Directory for Document Root using Apache. 

We have to put our application in the Document Root in order to make the application accessible from the Network.

Document Root 


The document root is a directory (a folder) that is stored on your host's servers and that is designated for holding web pages. When someone else looks at your web site, this is the location they will be accessing.
For a website to be accessible to visitors, it must be published to the correct directory, the "document root."

Document Root is the "www" directory, but of course your host may well use a different folder.
      1. htdocs 2. httpdocs  3.html  4. public_html  5. web  6. empty — no value.

Your host may even have several folders nested inside each other, one of which is the document root. In the example above the document root was:
/www/
but your host may use something like one of these:
/web/public/
/example.com/www/public/
/example.com/public_html/

Virtual  Host 
Virtual Host refers to the practice of running more than one web site on a single machine. 
Virtual Host are of two types 
1. IP Based - We have different IP Address for for every website.
2. Name Based - Multiple Names running on the each IP Address.
Named Virtual Hosts
NameVirtualHost directive you specify the IP address on which the server will receive requests for the name-based virtual hosts. This will usually be the address to which your name-based virtual host names resolve.

We need to configure two website www.sreemandira.com and www.sreemandira.org using Virtual Host.

Step 1
We need to configure the IP Address in httpd.conf file.

sudo nano /etc/httpd/httpd.conf

Step 2

We need to set the NameVirtualHost in httpd.conf file.

NameVirtualHost 10.0.90.06

Step 3

We need to setup the virtual host configuration for two nodes.

Code for sreemandira.com Domain :

<VirtualHost sreemandira.com/>
ServerAdmin webmaster@sreemandira.com
DocumentRoot /var/www/sreemandira.com
ServerName sreemandira.com
ServerAlias www.sreemandira.com
ErrorLog /var/logs/httpd/sreemandira.com/error_log
CustomLog /var/logs/httpd/sreemandira.com/access_log common
</VirtualHost>

Code for sreemandira.net Domain :


<VirtualHost sreemandira.net/>
ServerAdmin webmaster@sreemandira.net
DocumentRoot /var/www/sreemandira.net
ServerName sreemandira.net
ServerAlias www.sreemandira.net
ErrorLog /var/logs/httpd/sreemandira.net/error_log
CustomLog /var/logs/httpd/sreemandira.net/access_log common
</VirtualHost>


Save and close the file and restart the Apache 2

/etc/init.d/httpd restart

Make sure both the domains point to the same 10.0.90.06. Both the nodes sreemandira.com and sreemandira.net sending request to the same server.

Make sure var/log/httpd/domain name point to the appropriate  user directory.





Next blog I will explain about how we can access web application outside Document Root.













Saturday, 14 September 2013

Maven DEV ENVIRONMENT SETUP

                            Maven DEV ENVIRONMENT  SETUP Instructions

Hi  Reader,

       This is my fourth post. I going to start on how to setup maven DEV environment in Eclipse IDE. 
     Maven is a project management and comprehension tool. Maven provides developers a            complete build lifecycle framework. Development team can automate the project's build               infrastructure in almost no time as Maven uses a standard directory layout and a default               build lifecycle.

Objective

  1. Make the development process visible or transparent
  2. Provide an easy way to see the health and status of a project
  3. Decreasing training time for new developers
  4. Bringing together the tools required in a uniform way
  5. Preventing inconsistent setups
  6. Providing a standard development infrastructure across projects
  7. Focus energy on writing applications

Build Profile

Build profile is a set of configuration values which can be used to set or override default values of Maven build. Using a build profile, you can customize build for different environments such as Production v/s Development environments.
Different Types of Profile
  1. Per Project Defined in the POM itself (pom.xml).
  2. Per User Defined in the Maven-settings (%USER_HOME%/.m2/settings.xml).
  3. Global Defined in the global maven-settings (%M2_HOME%/conf/settings.xml).
  4. Profile descriptor Descriptor located in project basedir (profiles.xml)
MAVEN Repository

Repository is in maven is used to build artifacts and dependencies of varying types.
1. Local Repository
2. Remote Repository

Local Repository refers to a copy on your own installation that is a cache of remote downloads and contain temporary downloads.

We need to change the default local repository folder from default .m2({M2_HOME}\conf\settings.xml) to another meaningful name.

<settings>
<localRepository>D:/maven_repo</localRepository>

Local Repository changed to D:/maven_repo.

Remote Repository refers to any other type of repository access by variety of protocols as file and http.



<project ...>
<repositories>
    <repository>
      <id>java.net</id>
      <url>https://maven.java.net/content/repositories/public/</url>
    </repository>
 </repositories>
</project>

Install Apache Maven

We can download Maven from the below link.
http://maven.apache.org/download.cgi

Copy the Apache Maven into the folder D:/

Environmental Variable

1. Create JAVA_HOME System Variable.



2. Create MAVEN_HOME System Variable.
3. We need to add JAVA_HOME and MAVEN_HOME in System path variable.

   Add %JAVA_HOME%\bin;%M2_HOME%\bin; to the System path.
Install Tortoise Hg

We can install tortoise Hg from the below link.

http://tortoisehg.bitbucket.org/download/

Install tortoise Hg in the system.

Cloning the Code from Repository

Clone the latest codebase from the repository.

Steps

1. Right click on drive where you need to copy the codebase.



2. Click on the Tortoise Hg.
     Click on clone.
Click Cone and provide Username and Password.



Once the clone is completed then below screen will display

Once the Clone is successfull click cancel.

Cloning maven_repo for IA from Repository

Cloning process is same for codebase and maven_repo.

Source - URL path
Destination - D:/dev/codebase

Bulid Project

In order to build project we need to configure maven_repo

Go to D:/apache-maven-2.2.1/conf folder.


In  settings.xml file change the configuration.

<localRepository> D:\dev\Maven_repo</localRepository>

Open the command promot.

Use "mvn clean install" command at particular project which we need to build.




Build successful message will come in the command promot once the build is successful.

I will continue Eclipse Integration in my next blog




Spring 3 Rest Web Service Batch Update with JQuery & Ajax Integration

                      Spring 3  Rest Web Service Batch Update with JQuery & Ajax Integration Hi     In this blog I will describe the...