package ifss.controller; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.google.gson.Gson; import ifss.logindb.DbHandler; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpSession; /** * * @author 1121947 */ @WebServlet(name = "TicketServlet", urlPatterns = "/TicketServlet") public class TicketServlet extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println(""); out.println(""); out.println(""); out.println("Servlet TicketServlet"); out.println(""); out.println(""); out.println("

Servlet TicketServlet at " + request.getContextPath() + "

"); out.println(""); out.println(""); } } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int userId = Integer.parseInt(request.getParameter("userId")); HttpSession session = request.getSession(false); // String user = request.getParameter("UID"); String user = (String) session.getAttribute("user"); String role = (String) session.getAttribute("userRole"); String pacsId = (String)session.getAttribute("pacsId"); System.out.println("user Role Id: " + role); ResultSet rs = null; PreparedStatement ps = null; Connection conn = null; response.setContentType("application/json"); // Map chartData = new HashMap<>(); Map chartData = new HashMap<>(); int processed = 0; int pending = 0; if(role.equalsIgnoreCase("201603000004023")) { try { conn = DbHandler.getDBConnection(); String tapalChart = "select TAPAL_UPLOAD_FLAG,count(ACKNOWLEDGEMENT_NO) as flag_count from ACKNOWLEDGEMENT_DETAILS where TAPAL_USER_ID = ? group by TAPAL_UPLOAD_FLAG"; ps = conn.prepareStatement(tapalChart); ps.setInt(1, userId); rs = ps.executeQuery(); while (rs.next()) { // chartData.put(rs.getString("TAPAL_UPLOAD_FLAG"), rs.getInt("flag_count")); String flag = rs.getString("TAPAL_UPLOAD_FLAG"); int count = rs.getInt("flag_count"); if ("Y".equalsIgnoreCase(flag)) { processed = count; } else { pending = count; } } chartData.put("processed", processed); chartData.put("pending", pending); } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if (ps != null) { ps.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if (conn != null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } String json = new Gson().toJson(chartData); PrintWriter out = response.getWriter(); out.print(json); System.out.println("Controller.TicketServlet.do()" + json); }else if (role.equalsIgnoreCase("201603000004022")) { conn = DbHandler.getDBConnection(); try { String counterChart ="select count(ACKNOWLEDGEMENT_NO) AS GENERATED_ACK_NO from ACKNOWLEDGEMENT_DETAILS AD where AD.CREATED_BY_USER_ID = ("+ userId + ") and AD.INSTITUTION_ID = ("+ pacsId + ") "; ps=conn.prepareStatement(counterChart); rs= ps.executeQuery(); while(rs.next()) { processed = rs.getInt("GENERATED_ACK_NO"); } chartData.put("GENERATED_ACK_NO", processed); }catch(Exception ex) { ex.printStackTrace(); }finally { try { if (rs != null) { rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if (ps != null) { ps.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if (conn != null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } String json = new Gson().toJson(chartData); PrintWriter out = response.getWriter(); out.print(json); System.out.println("Controller.TicketServlet.do()" + json); } else { ResultSet rs1 = null; try { String getUserRole ="Select user_role_id from login_details ld where ld.login_id = ? order by ld.login_id desc"; conn = DbHandler.getDBConnection(); PreparedStatement ps1 = conn.prepareStatement(getUserRole); ps1.setInt(1, userId); rs1 = ps1.executeQuery(); while (rs1.next()) { System.out.println("fhjm,."); String userRoleId = rs1.getString("user_role_id"); System.out.println("This user role is :" +userRoleId); if(userRoleId == "201603000004023"){ System.out.println("rtyhujki"); try { // conn = DbHandler.getDBConnection(); System.out.println("dcfvgbhnjmk,cvbnm,"); String tapalChart = "select TAPAL_UPLOAD_FLAG,count(ACKNOWLEDGEMENT_NO) as flag_count from ACKNOWLEDGEMENT_DETAILS where TAPAL_USER_ID = ? group by TAPAL_UPLOAD_FLAG"; PreparedStatement ps2 = conn.prepareStatement(tapalChart); ps2.setInt(1, userId); ResultSet rs2 = ps2.executeQuery(); while (rs2.next()) { // chartData.put(rs.getString("TAPAL_UPLOAD_FLAG"), rs.getInt("flag_count")); String flag = rs2.getString("TAPAL_UPLOAD_FLAG"); int count = rs2.getInt("flag_count"); if ("Y".equalsIgnoreCase(flag)) { processed = count; } else { pending = count; } } chartData.put("processed", processed); chartData.put("pending", pending); } catch (Exception e) { e.printStackTrace(); } // } finally { // try { // if (rs2 != null) { // rs2.close(); // } // } catch (SQLException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // try { // if (ps2 != null) { // ps2.close(); // } // } catch (SQLException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // try { // if (conn != null) { // conn.close(); // } // } catch (SQLException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } }else { try { String counterChart ="select count(ACKNOWLEDGEMENT_NO) AS GENERATED_ACK_NO from ACKNOWLEDGEMENT_DETAILS AD where AD.CREATED_BY_USER_ID = ("+ userId + ") and AD.INSTITUTION_ID = ("+ pacsId + ") "; ps=conn.prepareStatement(counterChart); rs= ps.executeQuery(); while(rs.next()) { processed = rs.getInt("GENERATED_ACK_NO"); } chartData.put("GENERATED_ACK_NO", processed); }catch(Exception ex) { ex.printStackTrace(); }finally { try { if (rs != null) { rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if (ps != null) { ps.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if (conn != null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { System.out.println("Chart done display done"); } String json = new Gson().toJson(chartData); PrintWriter out = response.getWriter(); out.print(json); System.out.println("Controller.TicketServlet.do()" + json); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { HttpSession session = request.getSession(false); // String user = request.getParameter("UID"); String user = (String) session.getAttribute("user"); String role = (String) session.getAttribute("userRole"); String pacsId = (String)session.getAttribute("pacsId"); System.out.println("user Role Id: " + role); System.out.println("Ticket Servlet"); PrintWriter out = response.getWriter(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); List> ticketData = new ArrayList<>(); // List users = new ArrayList<>(); // try { try { // Database Connection Connection conn = DbHandler.getDBConnection(); if (role.equalsIgnoreCase("201603000004023")) { String tapalUser = "select distinct(LOGIN_ID) as USER_ID,LOGIN_NAME from LOGIN_DETAILS where USER_ROLE_ID =(" + role + ") and login_id =(" + user + ")"; try { PreparedStatement ps = conn.prepareStatement(tapalUser); ResultSet rs = ps.executeQuery(); while (rs.next()) { Map detail = new HashMap<>(); detail.put("USER_ID", rs.getString("USER_ID")); detail.put("LOGIN_NAME", rs.getString("LOGIN_NAME")); ticketData.add(detail); } String json = new Gson().toJson(ticketData); out.print(json); System.out.println("Controller.TicketServlet.doGet()" + json); out.flush(); rs.close(); ps.close(); } catch (SQLException e) { e.printStackTrace(); } finally { conn.close(); // rs.close(); } } else if (role.equalsIgnoreCase("201603000004022")) { String counterUser = "select distinct(LOGIN_ID) as USER_ID,LOGIN_NAME from LOGIN_DETAILS where USER_ROLE_ID =(" + role + ") and login_id =(" + user + ")"; try { PreparedStatement ps = conn.prepareStatement(counterUser); ResultSet rs = ps.executeQuery(); while (rs.next()) { Map detail = new HashMap<>(); detail.put("USER_ID", rs.getString("USER_ID")); detail.put("LOGIN_NAME", rs.getString("LOGIN_NAME")); ticketData.add(detail); } String json = new Gson().toJson(ticketData); out.print(json); System.out.println("Controller.TicketServlet.doGet()" + json); out.flush(); rs.close(); ps.close(); } catch (SQLException e) { e.printStackTrace(); } finally { conn.close(); // rs.close(); } }else { System.out.println("Inside else block"); String allUser = "select distinct(LOGIN_ID) as USER_ID,LOGIN_NAME from LOGIN_DETAILS where pacs_id = (" + pacsId + ")"; try { PreparedStatement ps = conn.prepareStatement(allUser); ResultSet rs = ps.executeQuery(); while (rs.next()) { Map detail = new HashMap<>(); detail.put("USER_ID", rs.getString("USER_ID")); detail.put("LOGIN_NAME", rs.getString("LOGIN_NAME")); ticketData.add(detail); } String json = new Gson().toJson(ticketData); out.print(json); System.out.println("Controller.TicketServlet.doGet()" + json); out.flush(); rs.close(); ps.close(); } catch (SQLException e) { e.printStackTrace(); } finally { conn.close(); // rs.close(); } } } catch (Exception e) { // TODO: handle exception } } }