Wednesday, 26 December 2012

Why need to use ‘serialVersionUID’ in Serializable class



Class SerializeTest , Here we serialize - deserialized the class.


Why need to use ‘serialVersionUID’.


If you doesn’t write serialVersionUID in your class if interface Serializable is implemented than Eclipse tool gives warning

The serializable class SerializeTest does not declare a static final  serialVersionUID field of type long

If you not add SerialVersionUID for you class serialization runtime add Id with associate class.

By java doc

 ‘’ Note - It is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected serialVersionUID conflicts during deserialization, causing deserialization to fail. ‘’



Now you understand why to add serialVersionUID while creating Serializable class for persist.


For Test Create a class to Serialize and then read the same class.



public class SerializeTest implements Serializable{

      private static final long serialVersionUID = 1L;
     
      private int test;
     
      public int getTest() {
            return test;
      }
     
      public void setTest(int test) {
            this.test = test;
      }
}



For Writing a Serializable class

SerializeTest t = new SerializeTest();
t.setTest(10);

try {
      OutputStream file = new FileOutputStream("serializeTest.ser");
      OutputStream buffer = new BufferedOutputStream(file);
      ObjectOutput output = new ObjectOutputStream(buffer);
      try {
            output.writeObject(t);
      } finally {
            output.close();
      }
} catch (IOException ex) {
      ex.printStackTrace();
}



For Reading a Serializable class


try {
InputStream file = new FileInputStream("serializeTest.ser");
      InputStream buffer = new BufferedInputStream(file);
      ObjectInput input = new ObjectInputStream(buffer);
      try {
SerializeTest t = (SerializeTest) input.readObject();
            System.out.println("SerializeTest.test : "+ t.getTest());        

      } finally {
            input.close();
      }
} catch (ClassNotFoundException ex) {
      ex.printStackTrace();
} catch (IOException ex) {
      ex.printStackTrace();
}


Expected Result is
SerializeTest.test : 10

Now, If you change the ‘serialVersionUID’ of class SerializeTest another value like
private static final long serialVersionUID = 2L;


and now try to read the same object using above read code.

You get the below result.

java.io.InvalidClassException: SerializeTest; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 2
     




Wednesday, 19 December 2012

Resolve jQuery Conflicts for diff jQuery Plugins

 
 
jQuery Provide " noConflict " to resolve conflict for different version of jQuery
 
For Example if  you app use ,
 
a plugins that have jquery Version 1.5
 
and another plugins that have jquery Version 1.8
 
Now both you use in same page ,it's create conflicts.
 
you can resolve it by  jQuery Method "noConflict"
 
 
Reference : 
 
1). http://api.jquery.com/jQuery.noConflict/ 

2). http://stackoverflow.com/questions/10978770/how-to-resolve-two-jquery-conflict
 

Thursday, 19 July 2012

html form validation using jquery


Dynamically the whole form can validate by jquery,
Just only add some custom attribute in html tag and call the javascript function,the function validate whole page in one call,you doesn't need to write extra script.
Also the script run for multi level form in html.



* Phone Number  
* Address  
* Password  
* Date  
* Birth Date  
 
* Employee   
* Resume  
* Password  
* Email  
* Sex
Male
Female
  
* Job Type  
* Sepcialization
Java
Php
  

Saturday, 26 May 2012

Index Sorting

Sorting Mechanism for Index

like "1" ,"1.1" , "1.2.1" and more


Sorting logic using Comparator


/**
     * this is only for integer type index
     * {"1", "1.1", "1.1.1", "1.10", "1.10.1", "1.10.2", "1.11", "1.2"}
     * kind of sorting
     */



private static Comparator<String> comparatorIndices = new Comparator<String>() {

            
        public int compare(String o1, String o2) {
            String[] c1 = o1.contains(".")?o1.split("\\."):new String[]{o1};
            String[] c2 = o2.contains(".")?o2.split("\\."):new String[]{o2};
          
            int c1l = c1.length;
            int c2l = c2.length;
          
            int length;
          
            if(c1l == c2l){
                length = c1l;
                for(int i=0;i<length;i++){
                    if(!c1[i].equalsIgnoreCase(c2[i])){
                        return Integer.valueOf(c1[i]).compareTo(Integer.valueOf(c2[i]));
                    }
                }
                return 0;
            }
          
            if(c1l < c2l){
                length = c1l;
                for(int i=0;i<length;i++){
                    if(!c1[i].equalsIgnoreCase(c2[i])){
                        return Integer.valueOf(c1[i]).compareTo(Integer.valueOf(c2[i]));
                    }
                }
                return -1;
            }
          
            if(c1l > c2l){
                length = c2l;
                for(int i=0;i<length;i++){
                    if(!c1[i].equalsIgnoreCase(c2[i])){
                        return Integer.valueOf(c1[i]).compareTo(Integer.valueOf(c2[i]));
                    }
                }
                return -1;
              
            }
            return 0;
        }
    };
   

You can also generate Tree using DefaultMutableTreeNod



public static void CreaateTree{

String[] str = new String[]{"1", "1.1", "1.1.1", "1.10", "1.10.1", "1.10.2", "1.11", "1.2", "1.2.1", "1.3", "1.3.1", "1.4", "1.4.1", "1.5", "1.5.1", "1.6", "1.6.1", "1.7", "1.7.1", "1.8", "1.8.1", "1.9", "1.9.1", "1.9.2", "2", "2.1", "2.1.1", "2.10", "2.10.1", "2.10.2", "2.11", "2.12", "2.13", "2.2", "2.2.1", "2.2.1.1", "2.2.1.1.1", "2.2.1.1.1.1", "2.2.1.2", "2.3", "2.3.1", "2.4", "2.4.1", "2.5", "2.5.1", "2.6", "2.6.1", "2.7", "2.7.1", "2.8", "2.8.1", "2.9", "2.9.1", "2.9.2", "3", "3.1", "3.1.1"};
        

Collections.sort(Arrays.asList(str), comparatorIndices);
       
        DefaultTreeModel model;
        DefaultMutableTreeNode top = new DefaultMutableTreeNode("0",true);
        model = new DefaultTreeModel(top);
       
        for(String string:str){
            DefaultMutableTreeNode findTreeNode = findTreeNode(getParentId(string));
            if(null != findTreeNode){
                top = findTreeNode;
                top.add(new DefaultMutableTreeNode(string,true));
            }else{
                top.add(new DefaultMutableTreeNode(string,true));
            }
        }
}



public static String getParentId(String child){
        if(null != child && !"".equalsIgnoreCase(child)){
            if(-1 != child.lastIndexOf(".")){
                return child.substring(0, child.lastIndexOf("."));
            }else{
                return "0";
            }
        }
        return "";
    }
   
    public static DefaultMutableTreeNode findTreeNode(String nodeId) {
        DefaultMutableTreeNode rootNode =
                (DefaultMutableTreeNode) model.getRoot();
        DefaultMutableTreeNode node;
        String tmp;
        Enumeration nodes = rootNode.depthFirstEnumeration();
        while (nodes.hasMoreElements()) {
            node = (DefaultMutableTreeNode) nodes.nextElement();
            tmp = (String) node.getUserObject();
            if (nodeId.equals(String.valueOf(tmp))) {
                return node;
            }
        }
        return null;
    }





Sunday, 22 January 2012

Liferay Client Side IPC using javaScript and call Icefaces Backing bean

To Communicate portlets by clientSide using javaScript,
Liferay Provide javascript function trigger and bind.
you can trigger a event from one portlet and send data to another 
portlet where you bind event using liferay javaScript function.


  • Trigger a event from sender Portlet.
 

   Liferay.trigger('event-name', {parameter: 'parameter'});



  • Now in another receiver portlet you have to bind the same event-name which one you have trigger from sender portlet.

      Liferay.bind('event-name',
             function(event, data){
                
                   var message = data.
parameter;
        }); 



  •   In Icefaces portlet if you want to communicate with you backing bean.Icefaes support iceSubmit(form,element,event).So you can trigger iceface commandButton or CommandLink using javaScript.Icefaces commandButton/Link have actionListener/Action fired on iceSubmit javaScript Call.

             Liferay.bind('event-name',
                  function(event, data){
                    var message = data.
parameter;
                    var form = document.getElementById('formName');
                    var element = document.getElementById('element');//button/Link
                    var evObj=document.createEvent("MouseEvents");
evObj.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);

                    iceSubmit(form,element,evObj);
                  // or you can trigger element click 
                 // element.click();
             });




............................................................................ 

Monday, 2 January 2012

Icefaces Facelet Custom Component

How to create Icefaces Facelet Custom Component.

Do the following configuration
Add following Jar files
    el-ri.jar
    icefaces-facelets.jar
    servlet-api.jar
   
in web.xml

<context-param>
    <param-name>facelets.DEVELOPMENT</param-name>
    <param-value>true</param-value>
</context-param>

<context-param>
    <param-name>facelets.LIBRARIES</param-name>
    <param-value>/WEB-INF/facelets/tags/externalcomp.taglib.xml</param-value>
</context-param>


in faces-config.xml

<application>
    <view-handler>com.icesoft.faces.facelets.D2DFaceletViewHandler</view-handler>
</application>


now create "externalcomp.taglib.xml" in /WEB-INF/facelets/tags/ and add following

<facelet-taglib>
    <namespace>http://www.abctaglib.com/jsf</namespace>
    <tag>
        <tag-name>MyComponent</tag-name>
        <source>mycomponent.jspx</source>
    </tag>
</facelet-taglib>

   
Now you can use your custom tag in your jspx file including folloeing content..


<ui:composition xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:ice="http://www.icesoft.com/icefaces/component"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:mycomp="http://www.abctaglib.com/jsf">
               
        <mycomp:MyComponent .... here you can pass your parameter ....  ></mycomp:MyComponent>
        /// like
        <mycomp:MyComponent myvalue="10"></mycomp:MyComponent>
        //.....
        //.....
       
</ui:composition>



<mycomp:MyComponent myvalue="10"></mycomp:MyComponent>

You can use myvalue parameter in "mycomponent.jspx".
for example in "mycomponent.jspx" you want to print "myvalue",

<ice:outputText id="fromMyValue" value="#{myvalue}" />




...................................................................................................................

Infinidb _CpNoTf_ problem

infinidb table with a varchar column insert string as a '_CpNoTf_' while using Cpimport. The Problem is occured if inserted string ...