Friday, July 15, 2011

Rendering Current Date in inputDate component.

Currently I am working on a selection criteria screen where the values selected from the choice lists are stored in a session bean which is later on used to filter the queries of other screens. On of those component is inputDate which we have used to enable the user to enter transaction (business unit) creation dates. We are using 2 inputDate component, 1 for Creation Date (From) and the other for Creation Date (To) to see the transaction between from and to dates. The Creation Date (From) is mandatory on selection screen, whereas Creation Date (To) is not. The requirement is that if Creation Date (To) is not given, it should default to current date. As inputDate component does not provide any such property to show current date, we need to do it ourselves. So here is the piece of code to do that:

    public String getMsgCreationDateTo() {
        if(msgCreationDateTo!= null && (!msgCreationDateTo.equals("")))
          return msgCreationDateTo;
        else{
          Date convertedDate = new Date(Date.getCurrentDate());
          String convertedDateString;
          java.text.SimpleDateFormat displayDateFormat = new java.text.SimpleDateFormat ("MM/dd/yyyy");
          convertedDateString = displayDateFormat.format(convertedDate.dateValue());
          return convertedDateString;
        }
    }

Here the default Date class is from oracle.jbo.domain.Date. So if user enters any Creation Date (To) than that value is used, else current date is defaulted in that component, and user does not need to enter that if he wants to see records from 'some day' till today. Here is how it looks like when the page is rendered:


And here is the piece of code to show yesterday's date as From Creation Date ( Current Date - 1 ) :

public String getMsgCreationDateFrm() {
        if (msgCreationDateFrm != null && (!msgCreationDateFrm.equals("")))
            return msgCreationDateFrm;
        else {
            Calendar now = Calendar.getInstance();
            now.add(Calendar.DATE, -1);
            java.text.SimpleDateFormat displayDateFormat = new java.text.SimpleDateFormat("M/dd/yyyy");
            msgCreationDateFrm = displayDateFormat.format(now.getTime() );
            return msgCreationDateFrm;
        }
}


Here is how the output looks like:


In addition, incase you need to convert from yyyy/mm/dd to dd/mm/yyyy format and you are using oracle.jbo.domain.Date data type, then you can use the following piece of code to do that:

        Calendar calendar = Calendar.getInstance();
        oracle.jbo.domain.Date date = new oracle.jbo.domain.Date( new Timestamp( calendar.getTime().getTime() ) );
        System.out.println("Date = " + date);
        DefaultDateFormatter ddf = new DefaultDateFormatter();
        try{
           
            String dateVal = ddf.format("MM/dd/yyyy", date);
            System.out.println("New Date Val = " + dateVal);
        }catch(Exception e){
            e.printStackTrace();
        }

Here is the output:





JDev Release 11.1.1.4 & 11.1.2.

Wednesday, July 6, 2011

Regarding Session Invalidation.

A friend of mine asked me that while logout how are you taking care of session invalidation in ADFwhile using form based authentication. I thought that I need to write a method to do that, but ADF does it automatically for us if you have configure ADF security on your project, as suggested in the following forum post as well:

http://forums.oracle.com/forums/thread.jspa?messageID=9709240#9709240

However, with Basic authentication the browser caches authentication credentials and its a known issue.

JDev Release 11.1.1.4

Saturday, July 2, 2011

Issues Related to the creation of custom login page.

I was just following the steps given in FMW Dev Guide to create a custom login page and figured out that the DEV guide is not up-to-date.
The login method that is given in the guide is as follows:

public String doLogin() {
    String un = _username;
    byte[] pw = _password.getBytes();
    FacesContext ctx = FacesContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest)ctx.getExternalContext().getRequest();
    CallbackHandler handler = new SimpleCallbackHandler(un, pw);
    try {
         Subject mySubject = Authentication.login(handler);
         ServletAuthentication.runAs(mySubject, request);
         ServletAuthentication.generateNewSessionID(request);
         String loginUrl = "/adfAuthentication?success_url=/faces" + ctx.getViewRoot().getViewId();
         HttpServletResponse response = (HttpServletResponse)ctx.getExternalContext().getResponse();
         sendForward(request, response, loginUrl);
    } catch (FailedLoginException fle) {
         FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Incorrect Username or Password", "An incorrect Username or Password" + " was specified");
         ctx.addMessage(null, msg);
    } catch (LoginException le) {
        reportUnexpectedLoginError("LoginException", le);
    }
    return null;
 }

However, I am using JDev 11.1.1.4, and now seems like SimpleCallbackHandler is deprecated.
I tried to import the libraries required to import this class but I was getting deployment errors after making those changes.

Then, I came around this blog by Andrejus:
http://andrejusb.blogspot.com/2010/11/things-you-must-know-about-adf-faces.html

After going through this post, figured out that now we need to use URLCallbackHandler instead of SimpleCallbackHandler, and the above method should look like :

  public String doLogin() {
      String un = _username;
      byte[] pw = _password.getBytes();
      FacesContext ctx = FacesContext.getCurrentInstance();
      HttpServletRequest request = (HttpServletRequest)ctx.getExternalContext().getRequest();
      try {
          Subject subject = Authentication.login(new URLCallbackHandler(un, pw));
          weblogic.servlet.security.ServletAuthentication.runAs(subject, request);
         
          String loginUrl = "/adfAuthentication?success_url=/faces" + ctx.getViewRoot().getViewId();
          HttpServletResponse response = (HttpServletResponse)ctx.getExternalContext().getResponse();
          sendForward(request, response, loginUrl);
      } catch (FailedLoginException fle) {
          FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Incorrect Username or Password", "An incorrect Username or Password was specified");
          ctx.addMessage(null, msg);
      } catch (LoginException le) {
          reportUnexpectedLoginError("LoginException", le);
      }
      return null;
  }

However, to make this work, we need to include 'WebLogic 10.3 Remote-Client' library to our project. This library points to weblogic.jar and is required for the above method to run successfully.

JDev Release 11.1.1.4