Tuesday, May 20, 2014

Export to excel in ADF

I believe most of you have come across the use-case to export data from a page as an excel. This is a pretty common use-case. I am going to describe a couple of ways this can be achieved in ADF Application.


Declarative Approach (using Export Collection Action Listener):
The first method is a declarative approach to achieving the export to excel functionality, however, as this is a declarative approach, this approach has its limitations, some of which I will be mentioning following the implementation. This is a pretty quick way of achieving the export functionality.
Below is the code I have for the export using the Export Collection Action Listener:



As you can see, it is pretty self straightforward to implement the export to excel functionality. We have a button which has the exportCollectionActionListener under it. Now, about the limitations for this approach that I briefly mentioned above, there are a few (may be more). Below are a couple that I can think of on the top of my head:
1. Data can be exported from a table, tree or tree table only. Well, what if you do not have any of these in the page? (for example, you could have an iterator, DVT component). I guess you are out of luck then (just kidding, go to approach #2)
2. What if you want to add styling to the exported excel?
There are a few other scenarios where you this approach may not be suitable for you (also depending on the version that you are on)

Programmatic Approach (using File Download Action Listener and Apache POI):
There may be other ways, other than Apache POI, but since Apache POI seem very popular and is open API for generating Microsoft documents (in this case Excel) in Java, I will be using this approach.

Below is a source snippet from the jspx page for this approach:


As you see in the properties of the fileDownloadActionListener, I have specified the file extension as .xlsx. This can be achieved using the XSSF implementation in Apache POI which is used in the EmployeeBean's exportToExcel method as below:


This method gives you a pretty good control if you want to tweak or skip some rows or if you want to style the cell, etc.

That is it for this post, if you have any comments or question please go ahead and comment on this post.

Sunday, May 18, 2014

ADF "Output Date" or "Button Date"

Well, you may ask, what a weird little title for the post. It was a little difficult to come up with a title.
The title will start making sense as you read through my post.

Below is how standard ADF Input Date component looks like:





Well, what if you wanted a component similar to Input Date. This component should not allow the user to type the date. It would only allow the user to pick the date using the date picker.
It is not possible to do this by setting properties like disabled or readonly on the input date component.

Disabled:
ReadOnly: 

As you see above setting the Disabled: true or ReadOnly: true on the input date component does not help in solving the problem.

There could be many ways to solve this problem. A few of them I could think of are described as below:
1. This could be achieved by javascript, css and/or both.
2. This could be achieved by JQuery (Note to self: another interesting POC for a separate post).
3. This could also be achieved completely in ADF without the use of any javascript (well technically we will be using javascript for closing the popup, but this solution relies heavily on ADF as you will see ahead).

I will be walking through approach #3 as I believe this is the easiest, quickest and does not rely too much on javascript.

Please note that I am using JDeveloper 11.1.1.7 version for this solution.
I am using the below components for the solution:
1. Output Text: with the Value set to a bean property.
2. Command button with showpopupbehaviour inside it opening the popup for selecting date
3. Popup with ChooseDate and InputDate components inside of the the popup.
       - The InputDate component has the chooseid as the id of the ChooseDate component and visible: false.
          It also has autosubmit: true and value change listener calling a bean method.

Most of the logic is written in the value change listener of the Input Date in the Popup.

Below is the behaviour:
1 ->

2 ->

3 ->


Below is my jspx code:


Below is my bean code:


You could have used any other component like button which will change the text to the selected date instead of the output text.

Wednesday, May 14, 2014

Disabling Input Date selection with keyboard

To disable input date selection with keyboard I followed the below blog written by Frank, it is really helpful!!!
disabling keyboard input on af:inputdate

All we are doing is to write a clientlistener inside the af:inputdate component:
<af:clientListener method="disableEntryOnDate" type="keyDown"/>

The above is calling the javascript which is cancelling the event, ultimately not allowing the user to type anything there:
function disableEntryOnDate(event){  
          event.cancel(); 


Monday, May 12, 2014

Lambda Expressions: Java 8

A couple of months back, I heard that Java 8 was out and while reading about it online, I kept coming across Lambda Expressions as a pretty important feature included. At that time, I did not have the time to look in detail of what it is and how it works. I was able to get the concept and idea of why Date & Time API was revamped completed. However, I was curious about what Lambda Expressions are and how and if I would ever use them in real life.
That was until I ran across the below article that paints a pretty neat picture of the use cases and syntax:
Lambda Expressions in Java 8

Please try it out...

Saturday, May 10, 2014

Cloud Computing - IAAS PAAS SAAS

I know what you are thinking, "one more person talking about already hyped Cloud Computing".
Wait, before you turn away from this post, I would like to say that I am not going to blabber much, except that I am going to point to a very helpful article that explains the cloud computing jargons (IAAS, PAAS, SAAS) in a very graphical way:
Cloud Explained

Wednesday, May 7, 2014

Using Calendar to find the # of days between 2 Date's

I am pretty sure most of you have done something like this before and it is a very simple solution. The below piece of code is to find the Period between two java.util.Date fields using the Calendar object:

import java.util.Date;
import java.util.Calendar;

public class CalendarTest{

     public static void main(String []args){
        Date fromDate = new Date();
        Date toDate = new Date();
       
        Calendar fromCal = Calendar.getInstance();
        Calendar toCal = Calendar.getInstance();
       
        fromCal.setTime(fromDate);
        toCal.setTime(toDate);
       
        int range = toCal.get(Calendar.DAY_OF_YEAR) - fromCal.get(Calendar.DAY_OF_YEAR);
       
        System.out.println("Range: " + range);
     }
}

The above is how you would have to do in and upto Java 7. However, Java 8 has a brand new Date and Time API and I am going to give it try and see how it looks.

public class Java8TimeTest
{
  public static void main(String[] args)
  {
    LocalDate today = LocalDate.now();
    LocalDate dayInFuture = LocalDate.of(2020, Month.JANUARY, 1);
   
    long dayCount = ChronoUnit.DAYS.between(today, dayInFuture);
    System.out.println("There are " + dayCount + " days left until 2020");
  }
}

Trying out for the first time, the API's are very intuitive and I am loving it. Let upgrade to Java 8 soon!!!

Tuesday, May 6, 2014

Custom Toolbar to ADF Calendar Object

ADF Calendar is a declarative component provide by Oracle to implement the Calendar functionality in ADF Faces. As it is declarative component, users may not like / need some of the out of the box functionality that comes built in with ADF Calendar object. However, Oracle provides lot of ways to customize the behavior to your requirement.

In this post, I will be talking about adding a custom toolbar to the ADF Calendar objects. It is pretty simple and straightforward, below are the steps:
1. Add a facet (f:facet) under af:calendar. Oracle recommends to use a name that starts with custom.... for the facet name (example "customToolbar").
2. Add toolbar under the facet (you can add button's text fields etc under the toolbar)
3. In the calendar's toolboxLayout property select the custom facet name that you added above (in point 1).

This is how we achieve using custom toolbar for the calendar object. I have included the link to the oracle documentation for this functionality below:

The above document explains pretty much everything with regards to customizing the toolbar in adf calendar.