Tuesday 3 December 2013

Java Program that provides optimized solution for a builder


Write a java program to provide the optimised solution for the below requirements.
  1. Program should have three input parameters:
    1. Total number of rooms
    2. Minimum rooms per building
    3. Maximum room per building.
  2. Should calculate the number of rooms per building between min-max number provided
  3. Choose a factor that gives minimum reminder
  4. If there are more than one factor giving minimum reminder, choose the highest factor.
Program:
package com.builder;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BuildingOptimizer {

 /**
  * @param args
  */
 public static void main(String[] args) {
  printOptimizedSolution(16, 4, 8);
 }
 
 public static void printOptimizedSolution(int totalNum, int minNum, int maxNum) {
  
  Map<Integer, Integer> reminderMap = new HashMap<Integer, Integer>();
  StringBuffer output = new StringBuffer("Optimised Solution is : " );
  
  for(int statNum = maxNum; statNum <= minNum; statNum--) {
   if(totalNum % statNum == 0) {
    reminderMap.put(0, statNum);
    break;
   }
  }
  
  if(reminderMap.size() == 0) {
   
   for(int statNum = minNum; statNum <= maxNum; statNum++) {
    reminderMap.put((totalNum % statNum), statNum);
   }
  }
  
  List<Integer> remindersList = new ArrayList<Integer>(reminderMap.keySet());
  Collections.sort(remindersList);
  
  Integer devider = reminderMap.get(remindersList.get(0));
  Integer divisions = totalNum / devider;
  
  for(int i = 1; i <= divisions; i++ ) {
   output.append(devider);
   if(i != divisions) {
    output.append(", "); // comma should not append at the end
   }
  }
  
  if(remindersList.get(0) != 0) {
   output.append(", ").append(remindersList.get(0));
  }
  
  System.out.println(output);
 }

}

Wednesday 20 November 2013

Why wait() ,notify() and notifyAll() methods are in Object class instead of Thread class?


These methods works on the locks and locks are associated with Object and not Threads. Hence, it is in Object class.

The methods wait(), notify() and notifyAll() are not only just methods, these are synchronization utility and used in communication mechanism among threads in Java.

Synchronization: The concept of synchronization will make sure that only one Thread is accessing an object/resource at the same time.
Java concurrency model uses locks to implement mutually exclusive access to objects in a multi-threaded environment. Locks are associated with objects not with the Threads. Hence, Locks are made available on per Object basis.
 In Java, to achieve mutually exclusive access on objects, a threads needs to acquire lock and other threads needs to wait to acquire lock. And they don’t know which threads holds lock instead they just know the lock is hold by some other thread and they should wait for lock instead of knowing which thread is inside the synchronized block and asking them to release lock. This analogy fits with wait and notify being on object class rather than thread in Java.

Example:


                                               
I would like to discuss with a well known example to get a clear understanding of this topic. Let’s take a real time banking scenario. Suppose two or more customers having a joint account, which can be permitted to perform the transactions through multiple channels like Teller, ATM, Mobile Banking, and Internet Banking. Currently the account having the balance 2000/- and the first user trying to purchase worth of 1500/- through Internet Banking other user trying to withdraw 1500/- through ATM. Here whichever the channel first access the account to perform the transaction will acquires a lock and other channel will wait for the lock. The channel which is waiting for lock status is not aware of which channel acquires the lock and at the same time the channel which is already acquired the lock is not aware of that who are waiting to acquire lock on the particular account. Here lock is applied on the account and not on channel.

Friday 15 November 2013

Prototype Design Pattern in Java

In general, a prototype is a template of any object before the actual object is constructed. This design pattern is useful when your application needs to create a number of similar instances of a class with small variations.
Also creation of a new object is a time taking process and an expensive too. So this pattern works by cloning of an object rather than creation.

When to use this pattern?
  • If the cost of creating the object is expensive or complicated.
  • When trying to keep the number of classes in an application to a minimum.
  • When adding or removing objects at runtime
  • When the client application needs to be unaware of the object creation, composition and representation.
  • Objects are required which are similar to the existing objects.
What does Prototype pattern do?
It allows making new instances by copying the existing instances. The copied cloned object is different from the original object. The state of the original is the same as the cloned one, at the time of cloning. Thereafter each object may undergo state change based on the operations performed on it. We can modify the objects to perform different things as well.
Please ensure that you want to deep clone or shallow clone your prototype because both will have different behaviour on runtime. If deep copy is needed, you can use a good technique given here using in memory serialization.
Shallow Clone:
When copied object contains references of other objects, the contained objects will not be cloned. Shallow clone copies only top level structure of an object. It can be done by using clone() method.
Deep Clone:
The object is copied along with the objects it refers to. Deep clone copies all the levels of the object from top to the bottom recursively. It can be done by using serialization or by overriding writing clone method in such way that to take all copied of contained objects.
Contained Object: Suppose Subject is a class which used by other classes.
package com.edu;

public class Subject {
 
 private String subjectName;
 
 public Subject(String subj) { //Constructor
  this.subjectName = subj;
 }

 public String getSubjectName() {
  return subjectName;
 }

 public void setSubjectName(String subjectName) {
  this.subjectName = subjectName;
 }

}
Implementing Shallow Clonning:
package com.edu.shallow;

import com.edu.Subject;

public class Student implements Cloneable {

 private String studentName;
 private Subject subject; //Contained Object
 
 public Student(String studName, String subjName) {
  this.studentName = studName;
  this.subject = new Subject(subjName);
   }

 
 @Override
 public Object clone() {
  //Shallow Copy
  try {
   return super.clone();
  } catch(CloneNotSupportedException cnsExp) {
   return null;
  }
 }
 
 public String getStudentName() {
  return studentName;
 }
 public void setStudentName(String studentName) {
  this.studentName = studentName;
 }
 public Subject getSubject() {
  return subject;
 }
 public void setSubject(Subject subjectName) {
  this.subject = subjectName;
 }


 @Override
 public String toString() {
  return "studentName = " + studentName + ", subject = " + subject.getSubjectName();
 }
 

}
Test Shallow Clonning:
package com.edu.shallow;

public class ShallowCloneTest {

 
 public static void main(String[] args) {
  
  Student stud = new Student("Mohan", "Discrete Structures");

  System.out.println("Original Object: " + stud.toString());

  //Clone Object
  Student clonedStud = (Student) stud.clone();

  System.out.println("Cloned Object: " + clonedStud.toString());

  //Modify the Original Object 
  stud.setStudentName("Rajesh");
  stud.getSubject().setSubjectName("Computer Organisation");

  System.out.println("Original Object after it is updated: " + stud.toString());

  System.out.println("Cloned Object after updating original object: " + clonedStud.toString());
 }

}
Shallow Clonning Testing Output:
Original Object: studentName = Mohan, subject = Discrete Structures
Cloned Object: studentName = Mohan, subject = Discrete Structures
Original Object after it is updated: studentName = Rajesh, subject = Computer Organisation
Cloned Object after updating original object: studentName = Mohan, subject = Computer Organisation
Implementing Deep Clonning by overriding clone() method:
package com.edu.deep;

import com.edu.Subject;

public class Student implements Cloneable {

 private String studentName;
 private Subject subject; //Contained Object
 
 public Student(String studName, String subjName) {
  this.studentName = studName;
  this.subject = new Subject(subjName);
   }

 @Override
 public Object clone() {
  
  Student student = new Student(studentName, subject.getSubjectName());
  return student;
 }
 
 public String getStudentName() {
  return studentName;
 }
 public void setStudentName(String studentName) {
  this.studentName = studentName;
 }
 public Subject getSubject() {
  return subject;
 }
 public void setSubject(Subject subjectName) {
  this.subject = subjectName;
 }


 @Override
 public String toString() {
  return "studentName = " + studentName + ", subject = " + subject.getSubjectName();
 }
 

}
Test Deep Clonning:
package com.edu.deep;

public class DeepCloneTest {

 
 public static void main(String[] args) {
  
  Student stud = new Student("Mohan", "Discrete Structures");

  System.out.println("Original Object: " + stud.toString());

  //Clone Object
  Student clonedStud = (Student) stud.clone();

  System.out.println("Cloned Object: " + clonedStud.toString());

  //Modify the Original Object 
  stud.setStudentName("Rajesh");
  stud.getSubject().setSubjectName("Computer Organisation");

  System.out.println("Original Object after it is updated: " + stud.toString());

  System.out.println("Cloned Object after updating original object: " + clonedStud.toString());
 }

}
Deep Clonning Test Output:
Original Object: studentName = Mohan, subject = Discrete Structures
Cloned Object: studentName = Mohan, subject = Discrete Structures
Original Object after it is updated: studentName = Rajesh, subject = Computer Organisation
Cloned Object after updating original object: studentName = Mohan, subject = Discrete Structures

Thursday 14 November 2013

How to call Stored Procedures in Hibernate?


DB Objects:
      1.     Tables
      2.     Views
      3.     Sequences
      4.     Stored Procedures, etc.

All Object/Relational mapping frameworks will have entity relationship between DB Tables/Views with Java POJO Data Transfer Objects. To communicate with these DB objects is very easy and can be managed with the method given by framework like save(), update(), delete(), saveOrUpadate(), etc.
In Hibernate, if user wants to communicate with Stored Procedures from underlying DB, needs to follow the below approach.

Procedures have two kinds of parameters in any Database.
      1.     IN Parameters
      2.     OUT Parameters

Step #1: Open a connection from StatelessSession.
Step #2: Call stored procedure by using prepareCall() method of Statement object.
Step #3: Pass/Provide all IN parameters of stored procedures by using setXXX() of Statement object.
Step #4: Register the type of OUT parameters by using registerOutParameter() of Statement object.
Step #5: Call executeUpdate() method on Statement Object.
Step #6: Get the OUT parameters provided by stored procedures by using getXXX() method of Statement object.

Code Snippet:

String queryString = "{call schema.storedProcExample(?,?,?,?)}";
int sequenceNo = 0;

Connection con = getSessionFactory().openStatelessSession().connection();
CallableStatement stmt = null;
try {
            stmt = con.prepareCall(queryString);
            stmt.registerOutParameter(1, java.sql.Types.INTEGER);
            stmt.setInt(2, 1);
            stmt.setString(3, “2”);
            stmt.setDate(4, new java.sql.Date(System.currentTimeMillis()));
            stmt.executeUpdate();
            sequenceNo = stmt.getInt(1);
} catch (SQLException e) {
            LOGGER.info("getTransactionSequenceNo : {}", "Exception");
            LOGGER.info("getTransactionSequenceNo Exception: {}", e);
} finally {
            try {
                        if (stmt != null)
                                    stmt.close();
            } catch (Exception e) {
                        LOGGER.error("exception while closing statement", e);
            }
            try {
                        if (con != null)
                                    con.close();
            } catch (Exception e) {
                        LOGGER.error("exception while closing connection", e);
            }
}

Note: The types of OUT parameters can be available from java.sql.Types class file.

Characteristics of StatelessSession in Hibernate:
  Ø  Neither does not implement a first-level cache nor interact with any second-level cache.
  Ø  Collections are ignored.
  Ø  Operations performed via a stateless session bypass Hibernate's event model and interceptors.



Tuesday 12 November 2013

Singleton Design Pattern in Java

Singleton Class:
The implementation of a Singleton class enforces, there must be a maximum of only one object of its type at any given time. Also Singleton class will make sure that its object won’t be garbage collected during the application running.

First of all to construct a Singleton object, programmer should aware of in how many ways an object can be created. So that can take precautions to make sure that the class following Singleton Design Pattern.

1. Possible ways to create an Object:
1.1. By using new keyword. This is the most commonly used approach in java.
SingletonObject object = new SingletonObject();

1.2. By using Class.forName(). It can be possible if the class has public default constructor.
SingletonObject object = (SingletonObject) Class.forName("com.edu.SingletonObject").newInstance();

1.3. By using the clone() method we can create a copy of an existing object.
SingletonObject object = new SingletonObject();
SingletonObject anotherObj = object.clone();

1.4. By using the Object deserialization, can able to create an object from its serialized form.
ObjectInputStream inStream = new ObjectInputStream(inputStream );
SingletonObject object = (SingletonObject) inStream.readObject();

2. Construction of Singleton Object by preventing above possibilities:
2.1. Use a private constructor, hence we cannot create an object of a class outside of it’s by using new keyword. This also prevents creation of an object from Class.forName(). Will get the flowing exception at run time.
private SingletonObject() {}

2.2. To prevents from cloning, simple write a clone() method in Singleton java file and throw an exception saying that cloning is not supported.
public Object clone() throws CloneNotSupportedException {
 throw new CloneNotSupportedException();
} 

2.3. To prevent from de-serialization process, implement Serialization interface and provide the body for both methods readObject() and readResolve() in such a way that it should provide the same current object. The total implementation of a Singleton Java class as follows:
package com.edu;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;
import java.lang.reflect.Constructor;
import java.io.Serializable;

public class SingletonObject implements Serializable {
 
 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 
 private static SingletonObject singletonObject;
 public static int x;
 
 private SingletonObject() { }
 
 public static synchronized SingletonObject getInstance() {
  if (singletonObject == null) {
   singletonObject = new SingletonObject();
   singletonObject.x = x + 10;;
  }
  return singletonObject;
 }
 
 private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
  ois.defaultReadObject();
  synchronized (SingletonObject.class) {
   if (singletonObject == null) {
    singletonObject = this; // only if everything succeeds
   }
  }
 }
 
 private Object readResolve() throws ObjectStreamException {
        if(singletonObject != null);
        return singletonObject;
    }
 
 
  
 public Object clone() throws CloneNotSupportedException {
  throw new CloneNotSupportedException();
 }
}

3. Why synchronized keyword used for getInstance() method:
Since the singleton instance is a static class variable in the stored in the PermGen space of the heap. This applies getInstance() instance method as well since it is static too. In the multithreading environment to prevent each thread to create another instance of singleton object and thus creating concurrency issue we will need to use locking mechanism. This can be achieved by synchronized keyword. By using this synchronized keyword we prevent Thread2 or Thread3 to access the singleton instance while Thread1 inside the method getInstance().

Client Code:
package com.edu;

import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;

public class SingletonTest {
 
 public static void main(String[] args) throws SecurityException, NoSuchMethodException, 
  IllegalArgumentException, InvocationTargetException, CloneNotSupportedException {
  try {
   
   //1. TO test for new keyword
   /*
   SingletonObject obj1 = new SingletonObject();
   */
   
   //2. To test Class.forName()
   /*
   SingletonObject obj = (SingletonObject)Class.forName("com.edu.SingletonObject").newInstance();
   System.out.println(obj.getInstance().x);
   */
   
   //3. To test Reflection
   /*
   Class<SingletonObject> obj = (Class<SingletonObject>) Class.forName("com.edu.SingletonObject");
   Constructor<SingletonObject> constructor = obj.getDeclaredConstructor();
   SingletonObject obj1 = constructor.newInstance("SingletonObject");
   SingletonObject object = obj1.getInstance();
   */
   
   
   //4. To test cloning
   /*
   SingletonObject obj = SingletonObject.getInstance();
   obj.clone();
   System.out.println(obj.x);
   */
   
   
   //5. To test object de-serialization
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(SingletonObject.getInstance());
            oos.close();

            java.io.InputStream is = new java.io.ByteArrayInputStream(baos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(is);
            SingletonObject singletonObject = (SingletonObject)ois.readObject();
            
            System.out.println(singletonObject.x);
   
  } catch(Exception e) {
   e.printStackTrace();
  }
 }
}

Test Results:
Enable either one option in the test program and run one by one.
1. Will get compilation error.
Error: The constructor SingletonObject() is not visible.
2. Will get Runtime error.
Error:
java.lang.IllegalAccessException: Class com.edu.SingletonTest can not access a member of class com.edu.SingletonObject with modifiers "private"
 at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
 at java.lang.Class.newInstance0(Class.java:351)
 at java.lang.Class.newInstance(Class.java:310)
 at com.edu.SingletonTest.main(SingletonTest.java:21)

3. Will get runtime error:
Error:
java.lang.IllegalAccessException: Class com.edu.SingletonTest can not access a member of class com.edu.SingletonObject with modifiers "private"
 at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:505)
 at com.edu.SingletonTest.main(SingletonTest.java:30)

4. Since we have provided the method implementation for this scenario in such a way that to provide the same copy of an object. It will provide the same.

When to use Singleton Design Pattern:
Any class which you want to be available to whole application and whole only one instance is viable is candidate of becoming Singleton. One example of this is Runtime class , since on whole java application only one runtime environment can be possible making Runtime Singleton is right decision. Another example is a utility classes like Popup in GUI application, if you want to show popup with message you can have one PopUp class on whole GUI application and anytime just get its instance, and call show() with message.

Uses of Singleton Design Pattern:
DB Connection: We can create a Database connection once and can provide a global point of reference.
Configuration File: It creates a single instance of the configuration file which can be accessed by multiple calls concurrently as it will provide static config data loaded into in-memory objects. The application only reads from the configuration file at the first time and thereafter from second call onwards the client applications read the data from in-memory objects.
Cache: We can use the cache as a singleton object as it can have a global point of reference and for all future calls to the cache object the client application will use the in-memory object.

Saturday 9 November 2013

Spring Batch Flat File Reader reads multiple files


Problem :
Implement a standalone flight search engine that lists matching flights for a traveller who is looking to fly between 2 locations on a given date. The input locations should be in 3-letter location code format as present in the airlines flight data.
Specifications: You are given 3 CSV files, each containing the available flights data for British Airways, Lufthansa Airlines and Air France respectively.
Each file contains the following fields:
  1. FLIGHT_NUM - Unique flight number, starting with 2-letter airline code.
  2. DEP_LOC - Departure location code of the flight.
  3. ARR_LOC - Arrival location code of the flight.
  4. VALID_TILL - Date (DD-MM-YYYY) till which each flight is available. It means that this flight would fly once every day till this date.
  5. FLIGHT_TIME - Local time (HHmm) at which the flight departs from the departure location.
  6. FLIGHT_DURN - Flight duration (HH.mm) from departure location to the arrival location.
  7. FARE - This is the fare of the flight per person in $ (US Dollars).
Following are the constraints should apply:
  • The engine should accept 4 input parameters: Departure Location, Arrival Location, Flight Date, Output Preference. “Output Preference” is a String suggesting whether the flight results should be sorted only by fare, or by both fare and flight duration.
  • The engine should then search for the flights in all 3 CSV files, and list the aggregate results on standard output, sorted by Fare and Flight Duration, depending on “Output Preference”.
  • The engine should be written considering that there could be more CSV files in future, and each CSV file might contain more data than present.
  • If the Departure location or/and Arrival location is not present in any of the CSV files, then engine should return a user friendly error.
  • If there are no flights available for the user entered input parameters, then engine should return a different user friendly error.
Input data: The following PIPE delimited sample data is given to you. You are allowed to extend on this data and create heavier input files. Flight_Details_1.csv
FLIGHT_NUM|DEP_LOC|ARR_LOC|VALID_TILL|FLIGHT_TIME|FLIGHT_DURN|FARE
AF299|FRA|LHR|20-11-2010|0600|4.10|480
AF118|DUB|MUC|21-12-2010|1410|5.40|580
AF371|AMS|MAD|30-11-2010|1210|3.45|320
AF453|BOS|CDG|20-11-2010|1350|7.30|1000
AF544|BOM|LHR|10-12-2010|1150|8.10|950
AF271|AMS|MAD|27-10-2010|1100|3.30|500
AF249|JFK|LHR|01-12-2010|1550|8.40|1030
Flight_Details_2.csv
FLIGHT_NUM|DEP_LOC|ARR_LOC|VALID_TILL|FLIGHT_TIME|FLIGHT_DURN|FARE
AF299|FRA|LHR|20-11-2010|0600|4.10|480
AF118|DUB|MUC|21-12-2010|1410|5.40|580
AF371|AMS|MAD|30-11-2010|1210|3.45|320
AF453|BOS|CDG|20-11-2010|1350|7.30|1000
AF544|BOM|LHR|10-12-2010|1150|8.10|950
AF271|AMS|MAD|27-10-2010|1100|3.30|500
AF249|JFK|LHR|01-12-2010|1550|8.40|1030
Flight_Details_3.csv
FLIGHT_NUM|DEP_LOC|ARR_LOC|VALID_TILL|FLIGHT_TIME|FLIGHT_DURN|FARE
BA123|DEL|AMS|12-10-2010|0050|8.00|950
BA412|BOS|CDG|31-12-2010|0210|7.50|800
BA413|BOS|AMS|30-11-2010|1530|7.00|750
BA111|LHR|PEK|30-10-2010|2340|12.50|1200
BA765|LHR|BOM|31-12-2010|1420|8.50|825
BA322|CDG|NRT|15-11-2010|0010|13.00|1150
BA438|DEL|AMS|30-11-2010|1325|10.50|920
Problem Solution:
  1. Create a Data TransferObject/ValueObject which can hold the properties of the flight.
  2. Create another Data Transfer Objects which suits for search criteria.
  3. Create a mapper class which splits the each line at specified delimiter and map the tokens to ValueObject.
  4. Create a Writer class and implement the business logic.
  5. Make necessary configurations to run the program.
  6. Create a client program and to provide search criteria.
FlightDetailsVO.java: A Data Transfer/Value Object which has the flight properties.
package com.flight.dto;

import java.io.Serializable;
import java.util.Date;

public class FlightDetailsVO implements Serializable {

 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 
 private String flightNum;
 private String depLoc;
 private String arrLoc;
 private Date validTill;
 private String flightTime;
 private double flightDuration;
 private double fare;
 
 public String getFlightNum() {
  return flightNum;
 }
 public void setFlightNum(String flightNum) {
  this.flightNum = flightNum;
 }
 public String getDepLoc() {
  return depLoc;
 }
 public void setDepLoc(String depLoc) {
  this.depLoc = depLoc;
 }
 public String getArrLoc() {
  return arrLoc;
 }
 public void setArrLoc(String arrLoc) {
  this.arrLoc = arrLoc;
 }
 public Date getValidTill() {
  return validTill;
 }
 public void setValidTill(Date validTill) {
  this.validTill = validTill;
 }
 public String getFlightTime() {
  return flightTime;
 }
 public void setFlightTime(String flightTime) {
  this.flightTime = flightTime;
 }
 public double getFlightDuration() {
  return flightDuration;
 }
 public void setFlightDuration(double flightDuration) {
  this.flightDuration = flightDuration;
 }
 public double getFare() {
  return fare;
 }
 public void setFare(double fare) {
  this.fare = fare;
 }
 
 @Override
 public String toString() {
  return flightNum + "|" 
    + depLoc + "|" 
    + arrLoc + "|" 
    + validTill + "|" 
    + flightTime + "|" 
    + flightDuration + "|" 
    + fare;
 }
 
}

SearchVO.java: It is used to pass the user given search criteria to search engine.
package com.flight.dto;

import java.io.Serializable;
import java.util.Date;

public class SearchVO implements Serializable {
 
 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 
 private String depLoc;
 private String arrLoc;
 private Date flightDate;
 private String sortedBy;
 
 public String getDepLoc() {
  return depLoc;
 }
 public void setDepLoc(String depLoc) {
  this.depLoc = depLoc;
 }
 public String getArrLoc() {
  return arrLoc;
 }
 public void setArrLoc(String arrLoc) {
  this.arrLoc = arrLoc;
 }
 public Date getFlightDate() {
  return flightDate;
 }
 public void setFlightDate(Date flightDate) {
  this.flightDate = flightDate;
 }
 public String getSortedBy() {
  return sortedBy;
 }
 public void setSortedBy(String sortedBy) {
  this.sortedBy = sortedBy;
 }

}
FlightSearchFieldSetMapper.java: It’s a field set mapper and it will fetch all the lines from multiple files and will set to a list and will provide the same to writer.
package com.flight.mapper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

import com.flight.dto.FlightDetailsVO;

public class FlightSearchFieldSetMapper implements FieldSetMapper<FlightDetailsVO> {

 private static final String PARSE_ERROR = "PARSE_ERROR";
 @Override
 public FlightDetailsVO mapFieldSet(FieldSet fieldSet) throws BindException {
  
  FlightDetailsVO flightDetailsVO = new FlightDetailsVO();
  
  flightDetailsVO.setFlightNum(fieldSet.readString(0));
  flightDetailsVO.setDepLoc(fieldSet.readRawString(1));
  flightDetailsVO.setArrLoc(fieldSet.readRawString(2));
  
  String validTill = fieldSet.readRawString(3);

  try {
   flightDetailsVO.setValidTill(convertStringToDate(validTill));
  } catch(ParseException pe) {
   flightDetailsVO.setFlightNum(PARSE_ERROR);
  }
  
  flightDetailsVO.setFlightTime(fieldSet.readRawString(4));
           flightDetailsVO.setFlightDuration(Double.parseDouble(fieldSet.readRawString(5)));
  flightDetailsVO.setFare(Double.parseDouble(fieldSet.readRawString(6)));
  
  return flightDetailsVO;
 }
 
 public Date convertStringToDate(String strDate) throws ParseException {
  
  SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
  return formatter.parse(strDate);
 }
 
 public Date converStringToTime(String StrTime) throws ParseException {
  SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
  return formatter.parse(StrTime);
 }
 
}
FlightSearchItemWriter.java: It’s Item Writer, in our program it will act as search engine.
package com.flight.writer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.ItemWriter;

import com.flight.dto.FlightDetailsVO;
import com.flight.dto.SearchVO;

public class FlightSearchItemWriter implements ItemWriter<FlightDetailsVO>, ItemStream {
 
 private SearchVO searchVO;

 @Override
 public void write(List<? extends FlightDetailsVO> flightDetailsVOList) throws Exception {
  ArrayList<FlightDetailsVO> flightDetailsList = new ArrayList>FlightDetailsVO<(flightDetailsVOList);
  
  ArrayList<FlightDetailsVO> availFlightsList = new ArrayList>FlightDetailsVO<();
  
  if(getSearchVO() != null) {
   for(FlightDetailsVO currVO : flightDetailsList) {
    if(currVO.getValidTill().compareTo(searchVO.getFlightDate()) >= 0) {
     availFlightsList.add(currVO);
    }
   }
   
   if(availFlightsList != null && availFlightsList.size() > 0) {
    System.out.println("FLIGHT_NUM|DEP_LOC|ARR_LOC|VALID_TILL|FLIGHT_TIME|FLIGHT_DURN|FARE");
    System.out.println("------------------------------------------------------------------");
    
    Collections.sort(availFlightsList, new Comparator<FlightDetailsVO>() {  
        @Override  
        public int compare(FlightDetailsVO o1, FlightDetailsVO o2) {  
            int fareComp = Double.compare(o1.getFare(), o2.getFare());  
            if(searchVO.getSortedBy().equalsIgnoreCase("both")) {
             if (fareComp != 0) {  
                 return fareComp;  
             }  
             int durComp = Double.compare(o1.getFlightDuration(), o2.getFlightDuration());
             return durComp;
            } 
            return fareComp; 
        }  
    });    
    for(FlightDetailsVO availFlightVO : availFlightsList) {
     System.out.println(availFlightVO.toString());
    }
   }
  } else {
   System.out.println("Search criteria is not provided.....");
  }
 }

 @Override
 public void close() throws ItemStreamException {
  
 }

 @Override
 public void open(ExecutionContext arg0) throws ItemStreamException {
  
 }

 @Override
 public void update(ExecutionContext arg0) throws ItemStreamException {
  
 }

 public SearchVO getSearchVO() {
  return searchVO;
 }

 public void setSearchVO(SearchVO searchVO) {
  this.searchVO = searchVO;
 }


}
flight-seaarch-context.xml: It’s a configuration file, which contain all spring beans definitions.

    
 
  
   
    
   
  
 

 
  
  
 
  
 
  
  
  
   
    
     
      
     
    
    
     
    
   
  
 
 
 
  
 
 
 

 
  
 
 
 
  
 



FlightSearchRun.java: Its client program for Flight Search engine.
package com.flight.main;

import java.text.SimpleDateFormat;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.flight.dto.SearchVO;
import com.flight.writer.FlightSearchItemWriter;

public class FlightSearchRun {
 
 public static void main(String[] args) {
  String[] springConfig  = {"resources/xml/flight-seaarch-context.xml"};
  
  ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);
  
  JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
  Job job = (Job) context.getBean("flightSearchJob");
  try {
   SearchVO searchVO = new SearchVO();
   searchVO.setDepLoc("DUB");
   searchVO.setArrLoc("MUC");
   searchVO.setSortedBy("both");
   
   SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
   searchVO.setFlightDate(formatter.parse("21-12-2013"));
   
   FlightSearchItemWriter writer = (FlightSearchItemWriter) context.getBean("flightSearchItemWriter");
   writer.setSearchVO(searchVO);
   
   JobExecution execution = jobLauncher.run(job, new JobParameters());
   System.out.println("Exit Status : " + execution.getStatus());

  } catch (Exception e) {
   e.printStackTrace();
  }
 }

}

Tuesday 29 October 2013

How to describe a table in DB2.


The below query will describe the table.


select distinct(name), coltype, length from sysibm.syscolumns where tbname = '<table_name>' for read only with ur

Spring method Replacement


Need of method replacement: Suppose some bug is identified during last phase of testing, analysed the same and found that the root cause is third party jar/API. To rectify the same we need to look for update on this API. Since our testing is in last phase and there is no time to analyse the impact of using updated API in our system. We can use this feature of spring.

Simple Requirement:
Now there is a requirement to wish a customer saying “Dear Customer, Good Morning/Good Afternoon/Good Evening….”, based on the customer login time.

package com.thirdpartyapi;

import java.util.Calendar;

public class WishCustomer {
   
    public void sayWishes() {
       Calendar cal = Calendar.getInstance();
       int currHour = cal.get(Calendar.HOUR_OF_DAY);
      
       if(currHour < 12) {
           System.out.println("Dear Valued Customer, Good Morning.....");
       } else if(currHour >= 12 && currHour < 18) {
           System.out.println("Dear Valued Customer, Good Afternoon.....");
       } else if(currHour >= 18 && currHour < 24) {
           System.out.println("Dear Valued Customer, Good Evening.....");
       }
    }

}

Spring configuration:
<bean id=”wishCustomer” class="com.thirdpartyapi.WishCustomer" />

The above code given by third party API has clearly satisfied the requirement. Now the business has enhanced and got the client over the world.

Problem: Irrespective of the user time zone, the above class wishes the customer based on the application server time zone.
Solution: Need to override sayWishes() method of WishCustomer class alone without impacting of other logic in this API.
package com.saywish;

import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.TimeZone;

import org.springframework.beans.factory.support.MethodReplacer;

public class WishCustomerReplacer implements MethodReplacer {

public Object reimplement(Object arg0, Method arg1, Object[] arg2) throws Throwable {
      
       Calendar cal = Calendar.getInstance();
       cal.setTimeZone(TimeZone.getTimeZone("IST"));
       //IST is hard coded, can be pass dynamically based on user location
       int currHour = cal.get(Calendar.HOUR_OF_DAY);
      
       if(currHour < 12) {
           System.out.println("Dear Valued Customer, Good Morning.....");
       } else if(currHour >= 12 && currHour < 18) {
           System.out.println("Dear Valued Customer, Good Afternoon.....");
       } else if(currHour >= 18 && currHour < 24) {
           System.out.println("Dear Valued Customer, Good Evening.....");
       }
      
       return null;
    }
}

The yellow highlighted is the customization, setting the time zone value to the Calendar instance based on the user country time zone.

Spring configuration for Method Replacer:
<bean id="wishCustomer" class="com.thirdpartyapi.WishCustomer" >
       <replaced-method name="sayWishes" replacer="wishCustomerReplacer" />
</bean>
      
<bean id="wishCustomerReplacer" calss="com.saywish.WishCustomerReplacer" />

Sunday 27 October 2013

Setter Injection Vs Constructor Injection


Setter-Injection
The basic-idea is that class have a no argument-constructor which creates the object with “reasonable-defaults”. The user of the object can then call setters on the object to override the collaborators of the object in order to wire the object graph together or to replace the key collaborators with test-doubles.

Constructor-Injection
The basic idea with constructor-injection is that the object has no defaults and instead you have a single constructor where all of the collaborators and values need to be supplied before you can instantiate the object.
Fundamental difference between setter and constructor injection, as their name implies is how dependency is injected.  Setter injection in Spring uses setter methods like setDependency() to inject dependency on any bean managed by Spring's IOC container. On the other hand constructor injection uses constructor to inject dependency on any Spring managed bean.
Constructor based dependency injection forces to set the property. However the property cannot be changed later on. Setter based dependency injection allows reconfiguration later on. We can have both Constructor based dependency injection and Setter based dependency injection also together.

  •   Setter Injection is more readable than constructor injection in Spring configuration file. Since setter method has name e.x. setReporotService() by reading Spring XML configuration file you know which dependency you are setting. While in constructor injection, since it uses index to inject dependency, it’s not as readable as setter injection and you need to refer either Java documentation or code to find which index corresponds to which property.
  •     Another difference between setter vs constructor injection in Spring and one of the drawback of setter injection is that it does not ensures dependency Injection. You cannot guarantee that certain dependency is injected or not, which means you may have an object with incomplete dependency. On other hand constructor Injection does not allow you to construct object, until your dependencies are ready.
  •   One more drawback of setter Injection is Security. By using setter injection, you can override certain dependency which is not possible which is not possible with constructor injection because every time you call constructor, a new object is gets created.
In Summary both Setter Injection and Constructor Injection has their own advantage and disadvantage. Good thing about Spring is that it doesn't restrict to use either Setter Injection or Constructor Injection and free to use both of them in one Spring configuration file. Use Setter injection when number of dependency is more or if readability needed. Use Constructor Injection when Object must be created with all of its dependencies.

Constructor Injection:

ConstructorInjection.java:
package com.scb.spring;

public class ConstructorInjection {
  private int bankid;
  private String name;
  private double salary;
  private SupportingInfo supportingInfo;

  public ConstructorInjection(int bankid, String name, double salary,                                                    SupportingInfo supportingInfo) {
    this.bankid = bankid;
    this.name = name;
    this.salary = salary;
    this.supportingInfo = supportingInfo;
  }

  public String getEmpInfo() {
    return "Bank ID :"+ getBankid() +", Name :"+ name +", Salary :"+salary ;
  }

  public int getBankid() {
    return bankid;
  }
  public String getName() {
      return name;
  }

  public double getSalary() {
      return salary;
  }
   public SupportingInfo getSupportingInfo() {
      return supportingInfo;
  }
}

 

context.xml
<beans>
 <bean id="constructorInjection" class="com.scb.spring.ConstructorInjection">
   <constructor-arg index="0"type=”int” value="1361606" />
   <constructor-arg index="1" type=”java.lang.String” value="Paramesh" />
   <constructor-arg index="2" type=”double” value="1234" />
   <constructor-arg index="3" ref=”supportingInfo” />
  </bean>
  <bean id=”supportingInfo” class=”com.scb.spring.SupportingInfo” />
</beans>

 

Setter Injection:

ConstructorInjection.java:
package com.scb.spring;

public class ConstructorInjection {
 
  private int bankid;
  private String name;
  private double salary;
  private SupportingInfo supportingInfo;

  public String getEmpInfo() {
    return "Bank ID :"+ getBankid() +", Name :"+ name +", Salary :"+salary ;
  }
  public void setBankid(int bankid) {
      this.bankid = bankid;
  }

  public int getBankid() {
    return bankid;
  }
  public void setName(String name) {
        this.name = name;
  }
  public String getName() {
      return name;
  }
  public void setSalary(double salary) {
       this.salary = salary;
  }
  public double getSalary() {
      return salary;
  }
  public SupportingInfo getSupportingInfo() {
      return supportingInfo;
  }
  public void setSupportingInfo(SupportingInfo supportingInfo) {
      this.supportingInfo = supportingInfo;
  }
}

context.xml
<bean id=" constructorInjection" class="com.scb.spring.ConstructorInjection">
   <property name="
bankid" value="1361606" />
   <property name="
name" value="Paramesh" />
   <property name="salary" value="1234" />
   <property name="supportingInfo" ref="supportingInfo" />
  </bean>
  <bean id=”supportingInfo” class=”com.scb.spring.SupportingInfo” />
</beans>

Injecting Collection Objects

TO inject Java Collection types List, Set, Map, and Properties etc, Spring offers four types of collection configuration elements which are as follows:
Data Type
Element
Description
java.util.List/
array
<list>
Helps to inject a list of values, allowing duplicates.
java.util.Set
<set>
Helps to inject a set of values, without duplicates.
java.util.Map
<map>
Helps to inject a collection of name-value pairs where name and value can be of any type.
java.util.Properties
<props>
Helps to inject a collection of name-value pairs where the name and value are both Strings.
Another Bean /
Any java class
<ref>
Helps to inject another bean.