From dc428758c377f76bf62886cb8551b1c2fc58c11c Mon Sep 17 00:00:00 2001 From: iracross1 Date: Thu, 10 Oct 2013 14:58:43 -0500 Subject: [PATCH] Added a new Method and it Queries the database for AttackTypes By George I think Im getting it fellows!!!! I have updated the FeedService.java, Project.java, and the ProjectManager.java This update will not break David's code and if you choose to replace the files in your eclipse you will still be able to use terrorParam1 and terrorParam2 my new method takes in a terrorParam3 and queries the database for attacktype such as Assassination, Unknown, Armed Assault and Bombing/Explosion ....... Enough of me talking download the new files and try this!!!! http://localhost:8080/TerrorismData/TerrorData/WebService/GetFeedWithAttacktype?terrorParam3=assassination --- Ira's branch .1/FeedService.java | 116 ++++++++++++++++++++++++++++ Ira's branch .1/Project.java | 87 +++++++++++++++++++++ Ira's branch .1/ProjectManager.java | 56 ++++++++++++++ 3 files changed, 259 insertions(+) create mode 100644 Ira's branch .1/FeedService.java create mode 100644 Ira's branch .1/Project.java create mode 100644 Ira's branch .1/ProjectManager.java diff --git a/Ira's branch .1/FeedService.java b/Ira's branch .1/FeedService.java new file mode 100644 index 0000000..4e6d83e --- /dev/null +++ b/Ira's branch .1/FeedService.java @@ -0,0 +1,116 @@ +package webService; + +import java.util.ArrayList; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; + +import model.ProjectManager; + +import com.google.gson.Gson; + +import dto.FeedObjects; + +@Path("/WebService") +public class FeedService { + + @GET + @Path("/GetFeeds") + @Produces("application/json") + public String feed() + { + String feeds = null; + try + { + ArrayList feedData = null; + ProjectManager projectManager= new ProjectManager(); + feedData = projectManager.GetFeeds(); + Gson gson = new Gson(); + System.out.println(gson.toJson(feedData)); + feeds = gson.toJson(feedData); + //System.out.println("Are we doing anything?"); + + } + + catch (Exception e) + { + System.out.println("Exception Error:" + e.getMessage()); //Console + } + return feeds; + } + + @GET + @Path("/GetFeedWithDates") + @Produces("application/json") + public String feed2( + @QueryParam("terrorParam1") String terrorParam1, + @QueryParam("terrorParam2") String terrorParam2) + { + String feeds = null; + try + { + ArrayList feedData = null; + ProjectManager projectManager= new ProjectManager(); + feedData = projectManager.GetFeeds( terrorParam1, terrorParam2 ); + Gson gson = new Gson(); + System.out.println(gson.toJson(feedData)); + feeds = gson.toJson(feedData); + //System.out.println("Are we doing anything?"); + + } + + catch (Exception e) + { + System.out.println("Exception Error:" + e.getMessage()); //Console + } + return feeds; + } + + @GET + @Path("/GetFeedWithAttacktype") + @Produces("application/json") + public String feed3( + @QueryParam("terrorParam3") String terrorParam3) + + { + String feeds = null; + try + { + ArrayList feedData = null; + ProjectManager projectManager= new ProjectManager(); + feedData = projectManager.GetFeeds( terrorParam3 ); + Gson gson = new Gson(); + System.out.println(gson.toJson(feedData)); + feeds = gson.toJson(feedData); + //System.out.println("Are we doing anything?"); + + } + + catch (Exception e) + { + System.out.println("Exception Error:" + e.getMessage()); //Console + } + return feeds; + } + + + + + + + @GET + @Path("/GetMethod") + @Produces(MediaType.TEXT_PLAIN) + public String newMethod( + @QueryParam("terrorParam1") String terrorParam1, + @QueryParam("terrorParam2") String terrorParam2) + { + return "These are your two params---->" + + terrorParam1 + " and " + terrorParam2; + } + + +} diff --git a/Ira's branch .1/Project.java b/Ira's branch .1/Project.java new file mode 100644 index 0000000..ecd172f --- /dev/null +++ b/Ira's branch .1/Project.java @@ -0,0 +1,87 @@ +package dao; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.ArrayList; + +import dto.FeedObjects; + +public class Project +{ + public ArrayList GetFeeds(Connection connection) throws Exception + { + ArrayList feedData = new ArrayList(); + try + { + PreparedStatement ps = connection.prepareStatement("SELECT gname, count(gname) AS number FROM `terrorism_data` GROUP BY gname ORDER BY number DESC"); + ResultSet rs = ps.executeQuery(); + while(rs.next()) + { + FeedObjects feedObject = new FeedObjects(); + feedObject.setGroup(rs.getString("gname")); + feedObject.setNumberOfAttacks(rs.getString("number")); + feedData.add(feedObject); + } + return feedData; + } + catch(Exception e) + { + System.out.println("Error Here"); + throw e; + } + } + + public ArrayList GetFeeds(Connection connection, String terrorParam1, String terrorParam2) throws Exception + { + ArrayList feedData = new ArrayList(); + try + { + PreparedStatement ps = connection.prepareStatement("SELECT gname, count(gname) AS number FROM `terrorism_data`" + + "WHERE iyear >= '" + terrorParam1 + "' AND iyear<='" + terrorParam2 + "' GROUP BY gname ORDER BY number DESC"); + ResultSet rs = ps.executeQuery(); + while(rs.next()) + { + FeedObjects feedObject = new FeedObjects(); + feedObject.setGroup(rs.getString("gname")); + feedObject.setNumberOfAttacks(rs.getString("number")); + feedData.add(feedObject); + } + return feedData; + } + catch(Exception e) + { + System.out.println("Error Here"); + throw e; + } + } + + public ArrayList GetFeeds1(Connection connection, String terrorParam3) throws Exception + { + ArrayList feedData = new ArrayList(); + try + { + PreparedStatement ps = connection.prepareStatement("SELECT gname, count(gname) AS number FROM `terrorism_data`" + + "WHERE attacktype1_txt = '" + terrorParam3 + "' GROUP BY gname ORDER BY number DESC"); + ResultSet rs = ps.executeQuery(); + while(rs.next()) + { + FeedObjects feedObject = new FeedObjects(); + feedObject.setGroup(rs.getString("gname")); + feedObject.setNumberOfAttacks(rs.getString("number")); + feedData.add(feedObject); + } + return feedData; + } + catch(Exception e) + { + System.out.println("Error Here"); + throw e; + } + } + + + + + +} \ No newline at end of file diff --git a/Ira's branch .1/ProjectManager.java b/Ira's branch .1/ProjectManager.java new file mode 100644 index 0000000..262c3b7 --- /dev/null +++ b/Ira's branch .1/ProjectManager.java @@ -0,0 +1,56 @@ +package model; + +import java.sql.Connection; +import java.util.ArrayList; + +import dao.Database; +import dao.Project; +import dto.FeedObjects; + +public class ProjectManager { + + public ArrayList GetFeeds()throws Exception { + ArrayList feeds = null; + try { + Database database= new Database(); + Connection connection = database.Get_Connection(); + Project project= new Project(); + feeds=project.GetFeeds(connection); + } + catch (Exception e) { + throw e; + } + return feeds; + } + + public ArrayList GetFeeds(String terrorParam1, String terrorParam2)throws Exception { + ArrayList feeds = null; + try { + Database database= new Database(); + Connection connection = database.Get_Connection(); + Project project= new Project(); + feeds=project.GetFeeds(connection, terrorParam1, terrorParam2); + } + catch (Exception e) { + throw e; + } + return feeds; + } + + public ArrayList GetFeeds(String terrorParam3)throws Exception { + ArrayList feeds = null; + try { + Database database= new Database(); + Connection connection = database.Get_Connection(); + Project project= new Project(); + feeds=project.GetFeeds1(connection, terrorParam3); + } + catch (Exception e) { + throw e; + } + return feeds; + } + + + +}