Java tutorial
package com.ajax.admin; import java.io.IOException; import java.sql.Connection; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONArray; import org.json.JSONObject; import com.dao.impl.admin.AdminServiceRequestDAOImpl; import com.dao.ManageServiceRequestDAO; import com.dao.admin.AdminBranchListDAO; import com.dao.admin.AdminFacilityListDAO; import com.dao.admin.AdminPartnerLicense; import com.dao.admin.AdminServiceRequestDAO; import com.dao.admin.EnquiryReportDAO; import com.dao.impl.ManageServiceRequestDAOImpl; import com.dao.impl.admin.AdminBranchListDAOImpl; import com.dao.impl.admin.AdminFacilityListDAOImpl; import com.dao.impl.admin.AdminManageMeterDAOImpl; import com.dao.impl.admin.AdminMemberListDAOImpl; import com.dao.impl.admin.AdminPartnerLicenseImpl; import com.dao.impl.admin.EnquiryReoprtDAOImpl; import com.dao.impl.admin.PartnerCityDaoImpl; import com.dao.impl.admin.PostReqListDAOImpl; import com.dao.impl.partner.PartnerHomeDAOImpl; import com.dao.partner.PartnerHomeDAO; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.reflect.TypeToken; import com.model.LicensePrice; import com.model.List_your_space; import com.model.LookupMaster; import com.model.MeterDetails; import com.model.NewMemberReg; import com.model.PartnerDetails; import com.model.Post_req_list; import com.util.DBUtil; @WebServlet("/AdminHomeAjax") public class AdminHomeAjax extends HttpServlet { private static final long serialVersionUID = 1L; private AdminManageMeterDAOImpl meterDetails; private PartnerCityDaoImpl partnerRep; private AdminMemberListDAOImpl newMemberReg; @SuppressWarnings("unused") private AdminServiceRequestDAO branchname; private String type, part_name, branch_owner, license_name, City, lead_status, jsonData; private List<MeterDetails> meterD = new ArrayList<MeterDetails>(); private List<LookupMaster> lookup = new ArrayList<LookupMaster>(); private List<List_your_space> list_Space = new ArrayList<List_your_space>(); private List<PartnerDetails> partD = new ArrayList<PartnerDetails>(); private List<NewMemberReg> newmember = new ArrayList<NewMemberReg>(); private List<NewMemberReg> tree = new ArrayList<NewMemberReg>(); private List<Post_req_list> reqlist = new ArrayList<Post_req_list>(); private List<LicensePrice> licenses, licenseType; private AdminPartnerLicense adminPartLicense; private PartnerHomeDAO partDao; private PostReqListDAOImpl postreq; private AdminBranchListDAO AdmBranchlist; private AdminFacilityListDAO AdminFacList; private ManageServiceRequestDAO serviceReqDao; private Gson gson; private JsonElement element; private JsonArray jsonArray; private JSONArray jsonArrayLicense; private List<JSONArray> licenseData; private AdminServiceRequestDAOImpl lookupD; private List<PartnerDetails> prtId, compname; private String branch_id; private Connection conn; private EnquiryReportDAO enqReport; public AdminHomeAjax() { try { meterDetails = new AdminManageMeterDAOImpl(); lookupD = new AdminServiceRequestDAOImpl(); enqReport = new EnquiryReoprtDAOImpl(); partnerRep = new PartnerCityDaoImpl(); newMemberReg = new AdminMemberListDAOImpl(); adminPartLicense = new AdminPartnerLicenseImpl(); postreq = new PostReqListDAOImpl(); partDao = new PartnerHomeDAOImpl(); AdmBranchlist = new AdminBranchListDAOImpl(); AdminFacList = new AdminFacilityListDAOImpl(); serviceReqDao = new ManageServiceRequestDAOImpl(); } catch (Exception e) { e.printStackTrace(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { if (conn == null || conn.isClosed()) { conn = (Connection) DBUtil.getConnection(); } type = request.getParameter("type"); System.out.println("Type : " + type); part_name = request.getParameter("part_name"); String venu_manager = request.getParameter("venu_manager"); String comp_name = request.getParameter("comp_name"); City = request.getParameter("City"); lead_status = request.getParameter("lead_status"); branch_id = request.getParameter("branch_id"); System.out.println("branch_id : " + branch_id); String mySiteId = request.getParameter("mySiteId"); String userMail = request.getParameter("myUserId"); if (type.equals("meter")) { meterD = meterDetails.getAllData(part_name, conn);// ajax gson = new Gson(); JsonElement element = gson.toJsonTree(meterD, new TypeToken<List<MeterDetails>>() { }.getType()); JsonArray jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if (type.equals("assignedTo")) { lookup = lookupD.getype(conn);// ajax gson = new Gson(); JsonElement element = gson.toJsonTree(lookup, new TypeToken<List<LookupMaster>>() { }.getType()); JsonArray jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if (type.equals("assignedTo1")) { List<String> lookupAssigned = serviceReqDao.getLookUpAssigned(branch_id, mySiteId, userMail, conn); gson = new Gson(); JsonElement element = gson.toJsonTree(lookupAssigned, new TypeToken<List<String>>() { }.getType()); JsonArray jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if (type.equals("raisedBy")) { String first_name = serviceReqDao.getRaisedBy(userMail, conn); List<String> First_name = new ArrayList<String>(); First_name.add(first_name); gson = new Gson(); JsonElement element = gson.toJsonTree(First_name, new TypeToken<List<String>>() { }.getType()); JsonArray jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if (type.equals("property_report")) { System.out.println("Inside property report"); list_Space = enqReport.getAllEnquery(venu_manager, conn); gson = new Gson(); JsonElement element = gson.toJsonTree(list_Space, new TypeToken<List<List_your_space>>() { }.getType()); JsonArray jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if (type.equals("business_enquiry_details")) { System.out.println("Inside business enquiry "); list_Space = enqReport.getAllEnqReport(venu_manager, comp_name, conn); System.out.println(list_Space); for (List_your_space list_your_space : list_Space) { System.out.println(list_your_space.getProperty_Name()); System.out.println(list_your_space.getStatus()); } gson = new Gson(); JsonElement element = gson.toJsonTree(list_Space, new TypeToken<List<List_your_space>>() { }.getType()); JsonArray jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if (type.equals("admin_partner_list")) { String cities = request.getParameter("city"); partD = partnerRep.getAllPartners(cities, conn); gson = new Gson(); JsonElement element = gson.toJsonTree(partD, new TypeToken<List<PartnerDetails>>() { }.getType()); JsonArray jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if (type.equals("admin_member_list")) { newmember = newMemberReg.getAllEnqReport(venu_manager, conn);// ajax gson = new Gson(); JsonElement element = gson.toJsonTree(newmember, new TypeToken<List<NewMemberReg>>() { }.getType()); JsonArray jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if (type.equals("post_requirement")) { reqlist = postreq.getAllRequirement(City, lead_status, conn);// ajax Gson gson = new Gson(); JsonElement element = gson.toJsonTree(reqlist, new TypeToken<List<Post_req_list>>() { }.getType()); JsonArray jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if (type.equals("business_enquiry_report")) { String enquiry_id = request.getParameter("enquiry_id"); list_Space = enqReport.getAllUpdate(enquiry_id, conn);// ajax gson = new Gson(); JsonElement element = gson.toJsonTree(list_Space, new TypeToken<List<List_your_space>>() { }.getType()); jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if (type.equals("admin_member_update")) { String profile_number = request.getParameter("profile_number"); tree = newMemberReg.getAllUpdates(profile_number, conn);// ajax System.out.println("my name: " + newmember); gson = new Gson(); JsonElement element = gson.toJsonTree(tree, new TypeToken<List<NewMemberReg>>() { }.getType()); jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } // Close connection if (conn != null || !conn.isClosed()) { DBUtil.closeConnection(conn); System.out.println(conn + " Connection closed"); } } catch (Exception e) { e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { if (conn == null || conn.isClosed()) { conn = (Connection) DBUtil.getConnection(); } type = request.getParameter("type"); branch_owner = request.getParameter("branch_owner"); license_name = request.getParameter("license_name"); type = request.getParameter("type"); String cities = request.getParameter("city"); String part_name = request.getParameter("part_name"); String partner_id = request.getParameter("partner_id"); String location = request.getParameter("location"); String cityId = request.getParameter("city"); String branch_id = request.getParameter("branch_id"); String layout_id = request.getParameter("layout_id"); String floor = request.getParameter("floor"); String fac_type = request.getParameter("fac_type"); if (type.equals("comp_name")) { partD = partnerRep.getAllPartners(cities, conn); Gson gson = new Gson(); JsonElement element = gson.toJsonTree(partD, new TypeToken<List<PartnerDetails>>() { }.getType()); JsonArray jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if ("getLicense".equals(type)) { licenses = adminPartLicense.getLicenseNames(conn); gson = new Gson(); element = gson.toJsonTree(licenses, new TypeToken<List<LicensePrice>>() { }.getType()); jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if ("manage_license".equals(type)) { licenseData = adminPartLicense.getLicenseData(branch_owner, conn); String jsonData = null; jsonArrayLicense = licenseData.get(0); jsonData = jsonArrayLicense.toString(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(jsonData); } else if ("License_Type".equals(type)) { licenseType = adminPartLicense.getLicenseType(license_name, conn); gson = new Gson(); element = gson.toJsonTree(licenseType, new TypeToken<List<LicensePrice>>() { }.getType()); jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if (type.equals("Partner_Update_Details")) { String part_id = request.getParameter("part_id"); prtId = partnerRep.getAllPartnerDetails(part_id, conn); Gson gson = new Gson(); JsonElement element = gson.toJsonTree(prtId, new TypeToken<List<PartnerDetails>>() { }.getType()); JsonArray jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } if (type.equals("Partner_Report")) { compname = partnerRep.getAllData(cities, part_name, conn); Gson gson = new Gson(); JsonElement element = gson.toJsonTree(compname, new TypeToken<List<PartnerDetails>>() { }.getType()); JsonArray jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if (type.equals("Vendor_City")) { JSONObject AdmCityListbj = null; try { AdmCityListbj = partDao.getAdmCityList(partner_id, conn); } catch (Exception e) { e.printStackTrace(); } jsonData = AdmCityListbj.toString(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(jsonData); } else if (type.equals("Location")) { JSONObject AdmlocationList = null; try { AdmlocationList = AdmBranchlist.getAdmBranchLocation(cityId, partner_id, conn); } catch (Exception e) { e.printStackTrace(); } jsonData = AdmlocationList.toString(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(jsonData); } else if (type.equals("Branch")) { JSONObject AdmBranchList = null; try { AdmBranchList = AdmBranchlist.getAdmBranchList(cityId, partner_id, location, conn); } catch (Exception e) { e.printStackTrace(); } jsonData = AdmBranchList.toString(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(jsonData); } else if (type.equals("Branch1")) { JSONObject partBranchList = null; try { partBranchList = AdmBranchlist.getpartBranchList(cityId, partner_id, conn); } catch (Exception e) { e.printStackTrace(); } jsonData = partBranchList.toString(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(jsonData); } else if (type.equals("Branch_Reports")) { JSONObject AdmBranchDatabj = null; try { AdmBranchDatabj = AdmBranchlist.getAdmBranchData(cityId, partner_id, location, branch_id, conn); } catch (Exception e) { e.printStackTrace(); } jsonData = AdmBranchDatabj.toString(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(jsonData); } else if (type.equals("OnLoadlicense_name")) { JSONObject licen_name = null; try { licen_name = AdmBranchlist.getOnLoadLicenseName(branch_owner, conn); } catch (Exception e) { e.printStackTrace(); } jsonData = licen_name.toString(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(jsonData); } else if (type.equals("Layout_Report")) { JSONObject Layout_Report = null; try { Layout_Report = AdmBranchlist.getAdmLayoutReport(cityId, location, branch_id, partner_id, conn); } catch (Exception e) { e.printStackTrace(); } jsonData = Layout_Report.toString(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(jsonData); } else if (type.equals("floor_update")) { JSONObject floor_update = null; try { floor_update = AdmBranchlist.getAdmFloorDetails(layout_id, conn); } catch (Exception e) { e.printStackTrace(); } jsonData = floor_update.toString(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(jsonData); } else if (type.equals("Floor")) { JSONObject addFloor = null; try { addFloor = AdminFacList.getAdmFloor(branch_id, conn); } catch (Exception e) { e.printStackTrace(); } jsonData = addFloor.toString(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(jsonData); } else if (type.equals("fac_type")) { JSONObject factype = null; try { factype = AdminFacList.getfac_type(branch_id, floor, conn); } catch (Exception e) { e.printStackTrace(); } jsonData = factype.toString(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(jsonData); } else if (type.equals("Facility_Reports")) { JSONObject Facility_Reports = null; try { Facility_Reports = AdminFacList.getFacilityReports(partner_id, cityId, location, branch_id, floor, fac_type, conn); } catch (Exception e) { e.printStackTrace(); } jsonData = Facility_Reports.toString(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(jsonData); } else if (type.equals("admin_license_name")) { JSONObject adm_license_name = null; try { adm_license_name = AdminFacList.onLoadLicenseName(partner_id, conn); } catch (Exception e) { e.printStackTrace(); } jsonData = adm_license_name.toString(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(jsonData); } else if (type.equals("currency")) { JSONObject currency = null; try { currency = AdminFacList.getPartcurrency(partner_id, conn); } catch (Exception e) { e.printStackTrace(); } jsonData = currency.toString(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(jsonData); } else if (type.equals("part_address")) { JSONObject part_address = null; try { part_address = AdminFacList.getPartcurrency(partner_id, conn); } catch (Exception e) { e.printStackTrace(); } jsonData = part_address.toString(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(jsonData); } // Close connection if (conn != null || !conn.isClosed()) { DBUtil.closeConnection(conn); System.out.println(conn + " Connection closed"); } } catch (Exception e) { e.printStackTrace(); } } }