Create Excel File in Java using Apache POI Library

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags , , , with 5 Comments on Create Excel File in Java using Apache POI Library

Recently, I came across requirement to create ExcelSheet from thin Java Client used by Salesforce. So, I though to share my experience on Creating Excel Sheet in Java. As we know that Java is product of Oracle and Excel is product of Microsoft. Off-course, There will be no standard functionality available in Java to achieve our requirement of creating Excel Sheet. However thanks to Apache for their POI Library. POI Stands for “Poor Obfuscation Implementation” as the file formats created by this library is obfuscated poorly with help of reverse Engineering. Anyways, we don’t have to bother about it and thankful to them for providing such wonderful library.

Apache POI library can be used to create Excel Sheet, Word Document and PowerPoint. In this post, we will be totally focusing on Excel Sheet using “XSSF (XML Spreadsheet Format)” component.

Prerequisite :
Add all jar files downloaded from Apache POI download site in Java Program’s build path. Continue reading “Create Excel File in Java using Apache POI Library”

Creating First Application in Heroku using Eclipse

Hello Readers, in this article we will see step by step procedure to deploy your first (Hello World) program in Heroku. There are two ways, first going through series of Commands of “Heroku” and “GIT” and other simple and smart way is to use Eclipse Capability with Heroku. To make things easier we will go by second method.

!Deprecated!

This tool / blog post is deprecated. Please follow this guide to deploying applications to Heroku from Eclipse. If you still want to follow this post, then download Eclipse plugin for Heroku from here.

Prerequisites:

Installation and Set up:
First we will need to Install Heroku Plugin in Eclipse. To install it, Navigate to “Help | Install New Software” and click on “Add” button.

Give any name in website like “Heroku Plugin” and enter this URL in Location https://eclipse-plugin.herokuapp.com/install and click on “Ok” and Finish Installation. It will install the Heroku Plugin required to start your first Plugin.

Next step is setting up your Credentials for Heroku :

Navigate to “Window | Preferences | Heroku”, Here either enter your UserName and Password of Heroku Account and click on “Login” or enter you API key, which you can find by logging into your Heroku Account.

Setting up Credentials for Heroku in Eclipse
Setting up Credentials for Heroku in Eclipse

Continue reading “Creating First Application in Heroku using Eclipse”

Merge PDF in Salesforce Using Java, ITextPDF and OAuth 2

Its long time, since i wrote any article because of my busy schedule However this time i came with advance one. In this article we are going to use the J2EE (Servlet) to Merge PDF attachment inside salesforce with the help of OAuth and ITextPDF jar file. The reason of writing this article is that there is no native support by Apex to merge two attachments in Salesforce. Either we have to go for AppExchange product like CongaMerg or Drawloop or we can write our own code in any other language like Java and C# and with the help of REST API we can save get and save attachment in Salesforce.

First we will need to setup the OAuth permission so that our local J2EE application can interact with Salesforce. For this login to Salesforce account and Navigate to “Set up | App Set up | Develop | Remote Access” and enter the information. Be careful about the “Callback URL”. It must match in your code. After creating the “Remote Access”, note “Consumer Key” and “Consumer Secret” which will be needed in your code.

Update : “Remote Access” is renamed to Connected App. So throughout this article, if you see image of “Remote Access” then please consider it as Connected App.

Create Remote Access in Salesforce.com for OAuth 2
Create Remote Access in Salesforce.com for OAuth 2

Continue reading “Merge PDF in Salesforce Using Java, ITextPDF and OAuth 2”

Simple guide to setup SSL in Tomcat

Author posted by Jitendra on Posted on under category Categories JAVA, JSP, Servlet and tagged as Tags , , with 1 Comment on Simple guide to setup SSL in Tomcat

I have enabled SSL in tomcat many times however initially I struggled to get it in running condition. So I thought to share a simple approach I am following now days.

Step 1:

Run tool “Keytool“ provided by the JRE to create a “keystore file”.
The command to run tool is:

keytool -genkey -alias tomcat -keyalg RSA -keystore D:/.keyStore

Where “D:/.keystore” is the path where file should be created.
Instead of alias “tomcat” any other name can be used.
After running above command, you will be asked many questions, so answer them correctly as shown in below image:

Tomcat SSL keytool to create keystore file
Tomcat SSL keytool to create keystore file

Remember the password provided, as it will be needed in next step. Continue reading “Simple guide to setup SSL in Tomcat”

Complete Salesforce Deployment Guide using Ant Migration Tool

Step by Step tutorial of Salesforce Migration using ANT tool with Proxy settings and retrieving content from Salesforce Organization. Also fix some common errors like java.lang.OutOfMemoryError or unable to find tools.jar

Following are the many tools available for Salesforce deployment like

  1. Change sets (From Salesforce site)
  2. Eclipse (Using “Deploy to force.com server” option in Eclipse)
  3. ANT (Java based tool)

We are going to discuss the ANT based migration, step by step:

Prerequisite:
JDK 1.5 or above

Step 1:
Download ANT distribution from – “http://ant.apache.org/bindownload.cgi

Step 2:
Set Environment variable “ANT_HOME“. The path should be of parent folder of “bin”. Also add the “bin” folder to your path.

Step 3:
Check whether ANT is installed or not properly by running command “ant -version“. It might be possible that you receive message something like unable to find tools.jar. You can copy this jar from “JDK_HOME/lib/tools.jar” to “JRE/lib” folder.

Continue reading “Complete Salesforce Deployment Guide using Ant Migration Tool”

Method Parameters – Pass by Value or Pass by Reference

Author posted by Jitendra on Posted on under category Categories Apex, JAVA and tagged as Tags , with 5 Comments on Method Parameters – Pass by Value or Pass by Reference

Difference between pass by Value and pass by Reference in Salesforce and other Object Oriented Programming language

This topic has always been part of discussion where the answer is not satisfactory and convinced to others. Using this article, I will try to explain the concept of parameter passing in Apex Method. This Complete topic is applicable for Java or other programming languages as well.

Before moving ahead let’s have a summary on what is pass by value and pass by reference.

Pass by value means when a method is called, a second copy of the parameter variable is made in memory, and this copy is passed to the method as a parameter. This copy can be modified in the method, but the original variable in the caller is unchanged, and the changes to the copy are lost upon return.

Pass by reference means when a method is called, that actual variable is passed to the method.

Million dollar question: parameters in method are passed by value or pass by reference?
Most of answers: primitive data types like Integer, Double are pass by value and Objects are pass by reference.


However, the above statement is not 100% true. I know I just invited controversy on this post. Be Patient with me and I would prove with actual example, feel free to run these examples in your Developer Console.

Correct Answer: parameters are passed by value in Apex as well as in Java. Where, in case of object the copy of reference is first made and then passed, therefore we can change the fields of object inside method however we cannot change the Object itself.

Take a pause here and think what I just said. I’m saying, new variable copy created which points to same Object. So, it seems reference but variable technically itself is storing value which is address or reference of object.


Let’s discuss this in more detail. I am going to explain two scenarios in which I will try to change the field value and in second example I will try to change Object itself.

Example 1

Account a = new Account(); 
a.Name = 'Shivasoft'; 
a.Website = 'www.JitendraZaa.com'; 
changeWebsite(a); 
System.debug('Website - '+a.Website); 

public void changeWebsite(Account b) { 
    b.Website = 'www.salesforce.com'; 
}  

Output of above code is

Website - www.salesforce.com

What just happened in Example 1

Salesforce Pass by Value or Pass by Reference - Scenario 1
Salesforce Pass by Value or Pass by Reference – Scenario 1

Explanation:
As per code snippet, the object a is supplied in method changeWebsite(). The parameter is actually passed by value, where the copy of value a is passed to object b which is memory address (aka reference). As we can see that both variable have reference of same object and hence whatever changes done by variable b on that object it will be applicable for variable a also.

If you are still not convinced and think I’m wrong, wait for example 2.

Example 2

Account a = new Account(); 
a.Name = 'Shivasoft'; 
a.Website = 'www.JitendraZaa.com'; 
changeWebsite(a); 
System.debug('Website - '+a.Website); 

public void changeWebsite(Account b) { 
    //Create New Object and try to replace old 
    b = new Account();
    b.Website = 'www.salesforce.com'; 
}  

Before I go further and reveal answer, let’s think what would be output ?

If you assumed method parameters are passed by reference then output should be

Website - www.salesforce.com

However actual output is

Website - www.JitendraZaa.com

What just happened in example 2 ?

Salesforce Pass by Value or Pass by Reference - Scenario 2
Salesforce Pass by Value or Pass by Reference – Scenario 2

Explanation:
As per code snippet, the object a is supplied in method changeWebsite(). Like I said, the parameter itself is actually passed by value, where the copy of value a is passed to object b which was reference. As we can see that both variable had reference of same object initially. However, inside the method new object is created and reference assigned to b and just now important event occurred. Refer above image for clear explanation

Previously b was pointing to old object, if it was really reference then it should impact object a. However now a and b variables point to different object. Whatever the changes done inside method, it will not be reflected outside as object a still have old reference. And hence after method execution when we check the value of object a, it is not affected because of method execution.

Salesforce documentation says :

“In Apex, all primitive data type arguments, such as Integer or String, are passed into methods by value. This means that any changes to the arguments exist only within the scope of the method. When the method returns, the changes to the arguments are lost.
Non-primitive data type arguments, such as sObjects, are also passed into methods by value. This means that when the method returns, the passed-in argument still references the same object as before the method call, and can’t be changed to point to another object. However, the values of the object’s fields can be changed in the method.”

Time for Best Practice

Because of behavior of parameters are passed as a value, it is always best practice to return object back to calling method. JUST DON’T ASSUME that object is passed by reference and whatever happened inside method will reflect calling method.

So code would become

Account a = new Account(); 
a.Name = 'Shivasoft'; 
a.Website = 'www.JitendraZaa.com'; 
a = changeWebsite(a); 
System.debug('Website - '+a.Website); 

public Account changeWebsite(Account b) { 
    //Create New Object and try to replace old 
    b = new Account();
    b.Website = 'www.salesforce.com'; 
    return b ;
}  

How to create Immutable Class in Java

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags , with Leave a comment on How to create Immutable Class in Java

creating the Immutable class in Java with simple source code example

We have heard the word “Immutable class” lots of time in Java. The best example is class “String“. Immutable class is the class whose value cannot be changed throughout the life cycle. We cannot change the content of String class, everytime new reference is created when we change the content, that is the basic difference between String and StringBuffer Class.
In this article, i will explain the step by step process to create the custom Immutable class in Java.
Our class should not able to derived and for that we will declare our class as final. The values cannot be change and thats why we will declare all the variables as final and we will provide only the getter methods as we cannot write setters because of final variables.
So to summarize, following steps needs to be taken: Continue reading “How to create Immutable Class in Java”

Create SOAP message using Java

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags , , with 1 Comment on Create SOAP message using Java

Step by Step Example of creating SOAP Message using core Java API

In this article, i am going to create the SOAP Message by using core Java Only. SOAP Stands for ” Simple Object Access Protocol”, which is used to exchange the structured information via Webservices.

SOAP Message consist of following three parts:

  1. SOAP-ENV:Envelope
  2. SOAP-ENV:Header
  3. SOAP-ENV:Body

SOAP Message Format for Web Services
SOAP Message Format for Web Services

Continue reading “Create SOAP message using Java”

Quartz framework tutorial with example – Schedule job in Java

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags , with 8 Comments on Quartz framework tutorial with example – Schedule job in Java

Tutorial of the Quartz framework for Java. Schedule job without loosing the performance of the application

What is Quartz?

Quartz is a job scheduling system that can be integrated with, or used along side virtually any other software system. The term “job scheduler” seems to conjure different ideas for different people. As you read this tutorial, you should be able to get a firm idea of what we mean when we use this term, but in short, a job scheduler is a system that is responsible for executing (or notifying) other software components when a pre-determined (scheduled) time arrives.
Quartz is quite flexible, and contains multiple usage paradigms that can be used separately or together, in order to achieve your desired behavior, and enable you to write your code in the manner that seems most ‘natural’ to your project.
Quartz is very light-weight, and requires very little setup/configuration – it can actually be used ‘out-of-the-box’ if your needs are relatively basic.
Quartz is fault-tolerant, and can persist (‘remember’) your scheduled jobs between system restarts.
Although Quartz is extremely useful for simply running certain system processes on given schedules, the full potential of Quartz can be realized when you learn how to use it to drive the flow of your application’s business processes.

Why not just use java.util.Timer?
Since JDK 1.3, Java has “built-in” timer capabilities, through the java.util.Timer and java.util.TimerTask classes – why would someone use Quartz rather than these standard features?
There are many reasons! Here are a few:

  1. Timers have no persistence mechanism.
  2. Timers have inflexible scheduling (only able to set start-time & repeat interval, nothing based on dates, time of day, etc.)
  3. Timers don’t utilize a thread-pool (one thread per timer)
  4. Timers have no real management schemes – you’d have to write your own mechanism for being able to remember, organize and retrieve your tasks by name, etc.

Continue reading “Quartz framework tutorial with example – Schedule job in Java”

Step By Step Hibernate Tutorial Using eclipse WTP

Author posted by Jitendra on Posted on under category Categories Hibernate, My SQL and tagged as Tags , with 31 Comments on Step By Step Hibernate Tutorial Using eclipse WTP

Step By Step Hibernate (ORM Tool) Tutorial Using eclipse WTP

Hibernate is the ORM tool widely used in java community to persist the java object using Object Relational Mapping (ORM) concept. ORM reduces number of lines to interact with database with optimized query language which is Hibernate Query language (HQL).

In this example, we will create a simple login application using hibernate tool of eclipse. We will use eclipse WTP (Web Tools Platform), to install “Hibernate Tools”. Follow below steps :

In Eclipse IDE, menu bar, select “Help” >> “Install New Software …” put the Eclipse update site URL “http://download.jboss.org/jbosstools/updates/stable/helios”

Eclipse Install New Software - Hibernate
Eclipse Install New Software - Hibernate

Continue reading “Step By Step Hibernate Tutorial Using eclipse WTP”