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.
}


No comments:
Post a Comment