Sunday, October 23, 2016

Customize Databound Fixed List of Values in Oracle ADF


Hi folks,

Oracle ADF is a one of the most advanced framework use by many of companies nowadays. It will allow developers to build an MVC architecture based applications, basically ADF make developer life essayer than any other framework existing in today.

This post for developers who has basic knowledge with ADF.

Creating a fixed list dropdown, usually used for some static values. Let’s assume that we have a requirement of the selecting two type of age ranges for some app. 45> allowed and 45< not allowed, in DB side these operation handle by the Boolean type variable. 45> true and 45< false.

When we are checking the documentations of Oracle ADF, the recommended way is using a databound fixedlist,


But it will be a mess, when we need to customize the display value and DB item value, because we need to mention the exact same list which include in DB side, If we consider the scenario like above mentioned, we can’t use different value and label like this,

1:  <af:selectOneChoice value="#{bindings.Userstatus.inputValue}"  
2:            label="#{bindings.Userstatus.label}: ">  
3:   <f:selectItems value="#{bindings.Userstatus.items}" itemLabel="#{bindings.Userstatus.items == true ? Above 45 : Bellow 45}"/>  
4:  </af:selectOneChoice>  

Because it doesn’t allow to customize with different itemLabel and value, this drop down always display true and false instead of “Above 45” and “Bellow 45”.

In order to solve that, we can create a programmatic fixed list, this approach allows you to customize value and label according to our requirement.

According to SelectItem documentation, six (6) override constructors allow us to define following parameters,


  •          java.lang.Object value
  •          java.lang.String label
  •          java.lang.String description
  •          boolean disabled
  •          boolean escape

We can define fixed list in bean class and, simply customize dropdown value list.

1:  List<SelectItem> userStatus;  
2:    public void setUserStatus(List<SelectItem> userStatus) {  
3:      this.userStatus = userStatus;  
4:    }  
5:    public List<SelectItem> getUserStatus() {  
6:            if (userStatus == null) {  
7:        userStatus = new ArrayList<SelectItem>();  
8:        userStatus.add(new SelectItem(true,"Above 45"));  
9:        userStatus.add(new SelectItem(false,"Bellow 45"));  
10:            }  
11:      return userStatus;  
12:    }  
MyBean.java

1:  <af:selectOneChoice value="#{bindings.Userstatus.inputValue}"  
2:            label="#{bindings.Userstatus.label}: ">  
3:   <f:selectItems value="#{MyBean.userStatus}" />  
4:  </af:selectOneChoice>  

Happy Coding,

Regards,
Denuwan Himanga


Monday, April 18, 2016

JAVA Interface Implementation

Hi folks,

In this post, I decide to cover little bit confused area in programming, most of the candidates are little bit straggle in when they faced a job interview. On OOP inheritance concept depend on these two methodologies. I explained OOP & Abstract concept, in the previous post. Today I’m figuring out major differentiations between Interfaces & Abstract concepts.


Basically Interfaces & Abstract class are looks like same, when most of the programmers deal with those both methodologies in very first time. But the reality is these two techniques are introduced for the two different things. As well as in a very complex scenario, we must identify which technique implements in to the system. Otherwise, most probably we face difficulties in developing/maintaining stages in SDLC.   


Use Case Diagram

Ok, let’s try to implement above scenario, for the implementation process I use to unique features of the interface. Following list listed down those features. 

1.                  Interface variables are by default Constant.
2.                  The interface allows only method signatures / doesn’t allow method body (Above JAVA 8)
3.                  The interface allows to implement multiple interfaces.
4.                  Interface, Interface implementation can use extends keyword
5.                  Class, Interface implementation can using implements keyword
6.                  An interface can extend multiple interfaces.

Following code segments depict how Mobile Phone class extending using interface wise implementation. For the demonstration I used highlighted segment in above UML diagram. Try to implement whole scenario by yourself.  

package com.interfaces;

public interface Secondary {
    
    public double MEGA_PIXELS = 13;
    public int Resolution = 1080;
   
}
Secondary.java
package com.interfaces;

public interface Primary {
    
    public double MEGA_PIXELS = 13;
    public double FLASH_RANGE = 4;
    public int [] SENSITIVITY = { 100, 200, 400, 800, 1600 };
    public double ZOOM = 3;
    
    public boolean zoomLence(int zoomRange);
    
    public void onFlash();
    
    public void offFlash();
    
    public void autoFocus();
    
    public int changeSensitivity(int Sensitivity);
}
Primary.java

package com.interfaces;

public interface Display {
    
    public String TYPE = "CAPACITIVE TOUCHSCREEN, 16M COLORS";
    public double SIZE = 5.0;
    public int[] RESOLUTION = {720 , 1280};
    
    public boolean multiTouch();
    
    public boolean turnOnDisplay();
    
    public boolean turnOffDisplay();
    
}
Display.java
package com.interfaces;

public interface Camera extends Primary, Secondary{
    
    public String FILE_FORMAT = ".jpeg";
    
    public boolean saveFile(String fileName);
    
    public boolean capture();

}
Camera.java
package com.application;

import com.interfaces.Camera;
import com.interfaces.Display;

public class MobilePhone implements Camera, Display{
    
    private String brandName;
    private int year;
    private String price;
    
    @Override
    public boolean zoomLence(int zoomRange){
        return true;
    }
    
    @Override
    public boolean saveFile(String fileName){
        return true;
    }
    
    @Override
    public void onFlash(){
    }
    
    @Override
    public void offFlash(){
    }
    
    @Override
    public int changeSensitivity(int Sensitivity){
        return 0;
    }
    
    @Override
    public boolean turnOnDisplay(){
        return true;
    }
    
    @Override
    public boolean multiTouch(){
        return true;
    }
    
    @Override
    public boolean turnOffDisplay(){
        return true;
    }
    
    @Override
    public boolean capture(){
        return true;
    }
    
    @Override
    public void autoFocus(){
    }    
}
MobilePhone.java
Additionally, JAVA 8 allow to implement method body inside interface, it looks like the evolution of Interface concept. Following example, I explained how to implement a method body in the interface. Using default & static key word we can implement a method body inside an interface scoop. Also override a default method is not a mandatory, but two interfaces, content same name compiler doesn’t identify which method belongs to which interface. So mitigate demanding issues if there any method to contain the same method signature in two different interfaces, overriding is mandatory. But method which are implements using the static key word can’t override in any concern.
 interface Java8Interface {  
   static void playCaptureSound(){  
     System.out.println("Click !!!");  
   }  
   default void formatMemory(){  
     //Do stuff here;  
   }  
 }  


Happy coding,
Regards,
Denuwan Himanaga

Monday, March 21, 2016

String vs. StringBuffer/StringBulider

Hi all,

In this post, I’ll demonstrate how memory behave while string type data involving scenario in our JAVA program. Basically, when we execute JAVA program on our PC, Java mange PCs main memory in its own way. For string type operations are handled through following 3type of memory segments.
  • Stack
  • Heap
  • Pool






String data type is immutable, mean when changes happen to literals particular literals never change, instead of that reference variable pointed to new value. This behavior helps to manage literals which have more than one reference.

When we consider above scenario, step 1 to 4 nameList variable reference to new literals in each step of the loop, at the same time previous values of literals are reaming inside the pool without any references, instead of JAVA garbage collector involving to clear pool, String pool will be full of garbage value.

For mitigate these kind of scenario JAVA 5 introduces StringBuffer & StringBulider both of the classes are not immutable. Let’s see how to implement above code segment using StringBuffer class.


Happy Coding,
Regards,
Denuwan Himanga

Saturday, March 12, 2016

Solution for Apache POI Date format Issue

Hi folks,

Today I’m here with some tricky solution, if you are familiar with Apache POI you might be facing this problem before. I’ll explain in fundamental level.

What is Apache POI

POI is the one of the common open source libraries introduced by Apache for interact (Inserting/Deleting/Reading) with applications in MS office package. Using POI library, we can easily create an interconnection with MS office application and our JAVA app.  

But most of the time, Apache POI doesn’t support for large Excel files. Because of that, most of developers are useing another third partition library call Streaming Reader for filtering specified excel sheets from large excel file.  But when we use this library some time reader doesn’t identify the exact cell type in excel file. Most of the time the cells, which we change the cell type as Date, Streaming reader, read this cell without any exception, but then we can notice, it displays a totally different number when we compare with exact value in the Excel cell.

Actually the reality is Streaming reader little bit intelligent than our eye, usually excel assign some formulated value for each date formatted cell for their calculation purpose. So what happens is streaming reader read that value instead of actual displayed value in excel cell.  

The reading value is not a random value, 01/Jan/1900 excel numbering as 01. So using a small conversion method we can easily convert these numbers in to actual date.

Here's the code, or you can simply use Convert Serial To Date library for that conversion process,


Regards, 
Denuwan Himanga