Monday, July 2, 2012

Printing a commandLink 'text' present in ADF Table

Hi Friends , sometimes we have a use case where we need a command link in the ADF table column. This scenario is required in cases where the column containing the command link represents the identifying key for the table and we want the user to click on that link and perform some action like going to details page for that record or opening the details in a pop up. This is a simple use case and easy to implement.  So our jsff will look something like this.











Problem:  A problem arises when we want to use print functionality on this table.
Here if we implement default print functionality provided by ADF i.e adding af:showPrintablePageBehavior element inside a command button, it omits the the command link and we get a blank field on the print page when user tries to print the contents of this page. But we wanted to display the 'text' value of commandLink.  This happens because by default ADF print functionality has been designed to omit the commandLinks and buttons present on an ADF page and provide the user with a better printable format of the page in consideration.        
   

Solution: Now to resolve this problem user can add an output text inside the same column of the ADF table(which contains a commandLink) and to the rendered property of the output text add the following expression.
 rendered="#{adfFacesContext.outputMode eq 'printable'}";

Your table jsff code should look like the following.






  


Ta-Da...so here it is , simple and easy solution. Now when user clicks the print button, he gets this output text which is rendered only if outputMode of the current adfFacesContext is 'printable'.
Thanks. Hope it works for you . Cheers.

Serialization (Persistence) in Java

           Hi Friends, everyone who is acquainted with JAVA even distantly knows that Java platform allows us to create objects which can be reused in memory, but these objects can be reused as long as your java application or JVM is running i.e. alive. Once the execution of the program finishes or JVM ends its execution the objects also perish to exist. Think of a scenario where you need to save(persist) the current state of the instantiated object(with all its variables and attributes assigned some value) and reuse it at a later stage even beyond the lifetime of JVM. This is what all this article talks about. This can be done with the powerful  Serialization API provided by Java itself. We can flatten the objects using serialization and then re-expand them at a later stage.

           Using object serialization an object's state is saved into form of bytes on the physical memory like hard disk and it can be inflated into a live object  at a later stage from those bytes. The emphasis in this article will be on understanding the classes and methods provided by the Java serialization API. Persisting objects is one of the tougher challenges which can easily be met by making use of serialization . Major problem is solved by understanding the Serialization API clearly which is rather misunderstood and misinterpreted while using in different scenarios.

Lets start with the most basic and simple case.


Persisting An Object
               
            To persist or serialize a java object, the class of that object must implement the marker interface java.io.Serializable , which indicates the underlying API that this object can be serialized to the disk in form of bytes and reused at a later point of time.




SerializableClass
               As you have seen above , making a class as serializable is pretty simple. You just need to extend the Serializable interface. Doing this allows the JVM to identify this class as serializable.
All subtypes of a serializable class are themselves serializable.

                   Now we have made a class serializable but it needs to be persisted. It can be done by using      java.io.ObjectOuputStream class. 

ObjectOutputStream 
                    An ObjectOutputStream class writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. If the stream is a network socket stream, we could easily transfer a flattened object across a network wire and have it be rebuilt on the other side. The method writeObject() is used to write an object to the stream.

FlattenedObject



    


                                                      
            The real work of serializing or persisting the object is done in the highlighted line. The serialized file i.e. "serialize.object" will be saved in the project folder of your java app if the path is not mentioned for the file.




Inflating the serialized Object


InflatedObject


                 The real work of reconstructing the serialized object into live instance is done by the highlighted line. The serialized object i.e. "serialize.object" is taken up and type casted to the original object type. Method readObject() can read the raw bytes which were previously saved during the serialization of the object. The class file for the raw object being deserialized must be available to the JVM for the restoration of the object , because during serialization only object's state is saved  to the file and not the class file and its methods.