Thursday, November 17, 2011

Adding Ajax Behavior for Generate PDF in Apache wicket framework



Step 1: Override the AbstractAjaxBehavior class functionality as below.

public abstract class ClassName extends AbstractAjaxBehavior {

    private static final long serialVersionUID = 1L;
 
    /**
     * initiate to call the java script file open dialog
     * @param target
     */
    public void initiate(AjaxRequestTarget target) {
        target.appendJavascript("window.open('" + getCallbackUrl()
            + "', '', 'scrollbars=yes,location=no,menuBar=no,resizable=yes,status=no,toolbar=no')");
    }

    /**
     * Called when a request to a behavior is received.
     * @see org.apache.wicket.behavior.IBehaviorListener#onRequest()
     */
    @Override
    public void onRequest() {

        RequestCycle rc = getComponent().getRequestCycle();
        final WebResponse webResponse = (WebResponse) getComponent().getRequestCycle().getResponse();
        webResponse.setLastModifiedTime(Time.now());
        webResponse.setContentType("application/pdf");
        webResponse.setHeader("Content-Disposition", "inline; filename=\"" + getFileName() + "\"");

        rc.setRequestTarget(new IRequestTarget() {

            public void respond(RequestCycle requestCycle) {
                OutputStreamWriter writer = null;
                try {
                    OutputStream stream = webResponse.getOutputStream();
                    writer = new OutputStreamWriter(stream, "ISO-8859-1");
                    writer.write(getStringToWriteonPDF());
                    writer.flush();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                } finally {
                    try {
                        writer.close();
                    } catch (IOException ioException) {
                    }
                }
            }

            public void detach(RequestCycle requestCycle) {

            }

        });

    }

    /**
     * get the File Name
     * @see ResourceStreamRequestTarget#getFileName()
     */
    protected abstract String getFileName();

    protected abstract String getStringToWriteonPDF();

}

Step 2: Use the override behavior in Component class

public void onClick(AjaxRequestTarget target) {

  ClassName  behavior =  new ClassName () {
             private static final long serialVersionUID = 1L;

             @Override
             protected String getStringToWriteonPDF() {
                  return "out put of PDF-it may get from Source (Database/webservices)";
             }

             @Override
             protected String getFileName() {
                  return "File Name of PDF";
             }
          };
                  add(behavior);
             behavior.initiate(target);
}

Learn This: When to use an Abstract Class and an Interface

Learn This: When to use an Abstract Class and an Interface

Configure Java environment on Windows

Configure Java environment on Windows

Wednesday, November 16, 2011

Singleton


The Singleton is a useful Design Pattern for allowing only one instance of your class.

The Singleton's purpose is to control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields.

Below are the example for singleton..

Case 1:

public class Singleton {

private static Singleton singleton;

private Singleton(){
}

public static synchronized Singleton getInstance(){
if(singleton==null)
singleton = new Singleton();
return singleton;
}

}

Case 2:

public class Singleton {


private static Singleton singleton = new Singleton();

private Singleton(){
}

public static synchronized Singleton getInstance(){
return singleton;
}
}


The difference between Case 1 and Case 2 is that in Case 2 singleton is instantiated when the class is loaded, while in the Case 1, it is not instantiated until it is actually needed.

J2EE Video tutorial.

Java Brains: J2EE video tutorial :  JSP/Servlet, Maven, Hibernat and Spring framework video tutorial.