24 Mart 2011 Perşembe

How to display a progressbar during gwt rpc?..


Well, it is a generic non-functional requirement, I have done some research on this item, I have implemented a solution that Thomas Broyer has suggested on gwt group.. This solution has distinct advantage over other solutions, You dont have to change your callback classes, what you have to do is just add a line of code after creation of async gwt-rpc service...

  

    IGwtPersistenceEngineRPCAsync persistenceEngine = GWT.create(IGwtPersistenceEngineRPC.class);
         ((ServiceDefTarget) persistenceEngine).setRpcRequestBuilder(
                          new ProgressRequestBuilder());


    package com.gurselkoca.gwt.client.service;
    import com.allen_sauer.gwt.log.client.Log;
    import com.google.gwt.http.client.Request;
    import com.google.gwt.http.client.RequestBuilder;
    import com.google.gwt.http.client.RequestCallback;
    import com.google.gwt.http.client.Response;
    import com.google.gwt.user.client.rpc.RpcRequestBuilder;
    
    public class ProgressRequestBuilder extends RpcRequestBuilder {
    
     private class RequestCallbackWrapper implements RequestCallback {
    
     private RequestCallback callback;
    
     RequestCallbackWrapper(RequestCallback aCallback) {
     this.callback = aCallback;
     }
    
     @Override
     public void onResponseReceived(Request request, Response response) {
     Log.debug("onResposenReceived is called");
     // put the code to hide your progress bar
     callback.onResponseReceived(request, response);
    
     }
    
     @Override
     public void onError(Request request, Throwable exception) {
     Log.error("onError is called",new Exception(exception));
     // put the code to hide your progress bar
     callback.onError(request, exception);
     }
     }
    
     @Override  
     protected RequestBuilder doCreate(String serviceEntryPoint) {
    
     RequestBuilder rb = super.doCreate(serviceEntryPoint);
     // put the code to show your progress bar
     return rb;  
     }
    
     @Override
     protected void doFinish(RequestBuilder rb) {
     super.doFinish(rb);
     rb.setCallback(new RequestCallbackWrapper(rb.getCallback()));
      
     }
    
    }

11 Mart 2011 Cuma

How to stop compilation of your gwt project every time you run a GwtTestCase in production (web) mode

When you run a GwtTestCase in production(web) mode , java to javascript compilation is done, whether source files are modified or not. And as already known, java to jaavascript is compilation is very slow. GwtTestCase does not have any option to prevent compilation. I have modified JUnitShell.maybeCompileWebMode method to make compilation depend on VM argument compile. You can download modified JUnitShell from here. You should add -Dcompile=false  to the script responsible running for your GwtTestCase.

3 Mart 2011 Perşembe

How to enable gzip compression for Tomcat?.

You should edit server.xml which is located under conf directory of tomcat installation.
You should add these attributes to connector element.


compression="on" compressableMimeTypes="text/html,text/xml,text/javascript,text/css"



After the modification connector element would be like this ;

<8080" protocol="HTTP/1.1" 
connectionTimeout="20000" redirectPort="8443"
compression="on" compressableMimeTypes="text/html,text/xml,
text/javascript,text/css"/>

2 Mart 2011 Çarşamba

SQL Server Datetime and Oracle Timestamp

Recently, I have migrated an oracle database to SQL Server, and I have used SQL Server Datetime for Oracle Timestamp without investigating subtle details of these two types..
Unfortunately, it seems that SQL Server datetime is not suitable for Oracle timestamp, because its precision is not enough to store miliseconds.. Its precision is up to 3.33 miliseconds. SQL Server datetime2 is better choice , its precision is up to 100 nanoseconds.. But you should know that datetime2 is available for SQL Server 2008 and later versions.



29 Aralık 2010 Çarşamba

How to construct image from byte array for gwt applications

Here is the scenario. You have images stored in database, and you want to use these images in your gwt application. As already known, the gwt image object constructor takes only String , this string should represent the url of the image.. You can  construct the image in the server, and you can save this image to your server's disk, and you can use the url of this image . But , this solution is not elegant, you have to save image to the server's disk..

As a second solution, you can write a servlet whose purpose is to fetch images from database and send them to the clients..  You can map this servlet ImageViewer , with  a  parameter. The request looks like this ;
   /imageViewer?imageId=1234 . You should create your gwt image widget like below;


  Image image = new Image( "/imageViewer?imageId=1234");



 ImageViewer servlet doGet method should be like that ;


public void doGet(HttpServletRequest request, HttpServletResponse response) {
        String imageId =  request.getParameter("imageId");
         byte[] imageData = readImageDataFromDB(imageId);
         response.setContentType("image/png");
         response.getOutputStream().write(imageData,0,imageData.length);
}




As a third solution , you can use the below code to convert image byte array data, to Base64 encoded string.. But you should know that this method does not work for IE7 and the previous versions. IE8 has 32 kb limit for encoded data.


public String getImageData(){

   byte[] imageByteArray = readImageDataFromDB(); //
   String base64 = com.google.gwt.user.server.Base64Utils.toBase64(imageByteArray);
    base64
= "data:image/png;base64,"+base64; 
   return base64;
}

And at gwt layer, you should use the below code ;


 
imageService.getImageData(new AsyncCallback() {
    @Override  
     public void onSuccess(String imageData) {    
       
Image image = new Image(imageData);    
        
RootPanel.get("image").add(image);  

      }
     @Override 
     public void onFailure(Throwable caught) {
      
Window.alert(caught.getMessage()); 

     }
}

17 Aralık 2010 Cuma

JPA 2.0 with GWT

Well, if you are using JPA 2.0 with GWT, and you are using your entity objects as DTO between GWT layer and web layer and you are using annotations for object mapping, you should do the following items ;
  • Download jpa2.0 sources . This file does not contain all source files of JPA 2.0 classes, only annotations and some interfaces are included. 
  • Put this jar file to your classpath
  • Add   <inherits name="javax.Persistence"/>  to  your [module name].gwt.xml file 
  • If your gwt version is below 2.1 , then you should add gwt-java-mathh to your project

9 Aralık 2010 Perşembe

How to write spring tests without spring transactional management facility

As you know, Spring 3.0 test framework does not allow transaction demercation . You should use beforeTransaction or afterTransaction method annotations to implement test logic after or before the transaction . But it means that you should write one class for every test method. As a result, the number of your test classes increase dramatically. Well, here is the solution , I have used AbstractJUnit4SpringContextTests instead of AbstractTransactionalJUnit4SpringContextTests.

@ContextConfiguration(value = "/applicationContext.xml") 
public class JPASpringTest extends AbstractJUnit4SpringContextTests { 
 @PersistenceContext(unitName="jpadenemelocal") 
 EntityManager entityManager; 
 
 @Autowired 
 protected PlatformTransactionManager transactionManager; 
 
 @Test 
 public void testInsertRolManyToMany() { 
 TransactionStatus status =  transactionManager.getTransaction(null); 
 // your code    
 
 transactionManager.commit(status); 
 } 
 
}