Saturday, 19 September 2015

Call to Servlet with query parameters using groovy script.

Below is Java Servlet class . It has been given a functionality to make it dynamic on each request.
It contains some predefined employee  data which it sorts based on firstly on first  name, then by last name and lastly by salary. Also employee data can be send as query parameters . Server will take into account the new employee data and do sorting after considering it.


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.web.domain.Employee;
import com.web.misc.GetSortedEmp;

public class HelloServlet extends HttpServlet {

private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{
System.out.println("Servlet called");
PrintWriter out = response.getWriter();
//If request has emp info

String fname = request.getParameter("fname");
String lname = request.getParameter("lname");
int salary =0;
Employee web = null;
if(fname !=null){
salary = Integer.parseInt(request.getParameter("salary"));
}
if(salary !=0){
web = new Employee(fname,lname,salary);
}
List<Employee> list = GetSortedEmp.getSortedEmp();
if(web!=null){
list.add(web);
}
Collections.sort(list);
String s ="Employee in Sorted Order\n";
for(Employee e : list){
s += e+"\n";
}
out.println(s);
System.out.println("Servlet ends");
}

}

The other helper classes are as follows

package com.web.domain;
public class Employee implements Comparable<Employee> {

private String fname;
private String lname;
private int salary;
public Employee(String fname,String lname,int salary){
this.fname = fname;
this.lname = lname;
this.salary = salary;
}
public String toString(){
return this.fname+" "+this.lname+" "+this.salary;
}
@Override
public int compareTo(Employee o) {
int r = this.fname.compareTo(o.fname);
if(r!=0){
return r;
}
r = this.lname.compareTo(o.lname);
if(r!=0){
return r;
}else{
return Integer.compare(this.salary, o.salary);
}

}


}

package com.web.misc;

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

import com.web.domain.Employee;

public class GetSortedEmp {
public static List<Employee> getSortedEmp(){
List<Employee> list = new ArrayList<Employee>();
list.add(new Employee("Abhishek","Saxena" , 10000));
list.add(new Employee("Abhishek","Goyal" , 20000));
list.add(new Employee("Abhishek","Goyal" , 30000));
list.add(new Employee("Amar","Yadav" , 10000));
list.add(new Employee("Ankur","M" , 10000));
Collections.sort(list);
return list;
}
}

Now comes the part of interest . Groovy script to call this service. It can be seen how simple it is 

// client.groovy file

println 'Server call started'
def base ='http://localhost:8080/WebDeploy/hello?'    // base url
def params = [fname:'Abhishek',lname:'Saxena',salary:10909]   // map containing query params
url = base + params.collect {k,v->"$k=$v"}.join("&")  // modify url as required 
println url.toURL().getText();    // do server call and return result as String
println 'Server call ends'

Below is the output post run of above groovy script

Server call started

Employee in Sorted Order
Abhishek Goyal 20000
Abhishek Goyal 30000
Abhishek Saxena 10000
Abhishek Saxena 10909
Amar Yadav 10000
Ankur M 10000


Server call ends


Saturday, 28 March 2015

BFS implementation in java using Adjacency Matrix for Graph traversal

Following java program implements BFS algorithm . The Graph for input/output is below
In order to traverse graph we need following data structures.
  • Vertex object to store label and visited(true/false) .
  • Adjacency Matrix to hold links between given vertices.
  • Vertex list to keep hold given vertex.
  • Queue to make traversal info tracked.

package com.abhishek.graph;
import java.util.PriorityQueue;
import java.util.Queue;

public class BFS {

public static void main(String ... args){
GraphBFS g = new GraphBFS();
g.addVertex('A');
g.addVertex('B');
g.addVertex('C');
g.addVertex('D');
g.addVertex('E');
g.addVertex('F');
g.addVertex('G');
g.addVertex('H');
g.addEdge(0,1);g.addEdge(0,5);g.addEdge(0,6);g.addEdge(0,3);g.addEdge(1,4);g.addEdge(1,5);g.addEdge(2,7);g.addEdge(3,5);
g.addEdge(4, 6);g.addEdge(2, 5);
g.bfs();
}

}

class VertexBFS {
public char label;
public boolean visited;

public VertexBFS(char pLabel){
label = pLabel;
visited = false;

}

}

class GraphBFS{
public VertexBFS vertexList[];
public int maxVertices = 20;
public int vertexCount;
public Queue<Integer> theQueue;
public int adjM[][];
public GraphBFS(){
vertexList = new VertexBFS[maxVertices];
vertexCount =0;
theQueue = new PriorityQueue<Integer>();
adjM = new int[maxVertices][maxVertices];
for(int i=0;i<maxVertices;i++)
for(int j=0;j<maxVertices;j++)
adjM[i][j]=0;
}

public void addVertex(char pLabel){
vertexList[vertexCount++]= new VertexBFS(pLabel);
}
public void addEdge(int start,int end){
adjM[start][end]=1;
adjM[end][start]=1;
}
public void displayVertex(int v){
System.out.print(vertexList[v].label+" ");
}

public void bfs(){
vertexList[0].visited=true;
displayVertex(0);
theQueue.add(0);
int v2;
while(!theQueue.isEmpty()){
int v1 = theQueue.remove();
while((v2=getUnVisitedVertex(v1))!=-1){
vertexList[v2].visited=true;
displayVertex(v2);
theQueue.add(v2);
}

}

for(int i=0;i<vertexCount;i++){
vertexList[i].visited=false;
}
}
public int getUnVisitedVertex(int v){
for(int i=0;i<vertexCount;i++){
if(adjM[v][i]==1&&vertexList[i].visited==false)
return i;
}
return -1;
}
}

The output will be
A B D F G E C H 

To understand BFS/DFS better follow below video . A 10 minute video with very good explanation

Reference for code/theory used
Data Structures and Algorithms Made easy in Java by Narasimha Karumanchi