Java tutorial
package com.controller.partner; import java.io.IOException; import java.sql.Connection; 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 javax.servlet.http.HttpSession; import com.dao.admin.PromotionDAO; import com.dao.impl.partner.PartnerHomeDAOImpl; import com.dao.impl.partner.PartnerManagePromotionDAOImpl; import com.dao.impl.partner.PartnerRegisProfileDAOImpl; import com.dao.impl.partner.PromotionDAOImpl; import com.dao.partner.PartnerHomeDAO; import com.dao.partner.PartnerManagePromotionDAO; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.reflect.TypeToken; import com.model.BranchDetails; import com.model.FacilityMaster; import com.model.LookupMaster; import com.model.PartnerDetails; import com.model.PromotionMaster; import com.util.DBUtil; import com.util.GetDate; @WebServlet("/ManagePromotion") public class PartnerManagePromotion extends HttpServlet { private static final long serialVersionUID = 1L; private HttpSession session; private PartnerRegisProfileDAOImpl partRegisDao; private String enableRule, userMail, partner_id, type, branch_id, applicableToAll, promo_code, reference_id, reference_name, site_id; private PartnerHomeDAO partDao; private int promo_id, count; private PartnerManagePromotionDAO partnerManagePromotionDAO; private List<BranchDetails> branchList; private PromotionDAO promoDao; private List<PromotionMaster> promotion; private List<PartnerDetails> partnerList; private List<FacilityMaster> promotionData; private List<LookupMaster> profileCategory, genderData; private String appToStatus[]; private String enableRuleArr[] = new String[10]; private String applicableType[] = new String[10]; private String applicableTo[] = new String[10]; private Connection conn; public PartnerManagePromotion() { try { partRegisDao = new PartnerRegisProfileDAOImpl(); partDao = new PartnerHomeDAOImpl(); promoDao = new PromotionDAOImpl(); partnerManagePromotionDAO = new PartnerManagePromotionDAOImpl(); } catch (Exception e) { e.printStackTrace(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { session = request.getSession(false);// don't create if it doesn't exist if (session != null && !session.isNew()) { try { if (conn == null || conn.isClosed()) { conn = (Connection) DBUtil.getConnection(); } } catch (Exception e) { e.printStackTrace(); } userMail = (String) session.getAttribute("userId"); partnerList = partDao.getAllPartner(userMail, conn); request.setAttribute("partners", partnerList); for (PartnerDetails string : partnerList) { partner_id = string.getPartner_id(); } request.setAttribute("CurrentDate", GetDate.currentDate()); request.setAttribute("partners", partDao.getAllPartner(userMail, conn)); branchList = partRegisDao.getLicensedBranches(partner_id, userMail, "partner_manage_promotion.jsp", conn); request.setAttribute("branchlist", branchList); promotion = promoDao.getPromotion(partner_id, userMail, "partner_manage_promotion.jsp", conn); request.setAttribute("promotion", promotion); request.getRequestDispatcher("/WEB-INF/partner/partner_manage_promotion.jsp").include(request, response); // Close connection try { if (conn != null || !conn.isClosed()) { DBUtil.closeConnection(conn); System.out.println(conn + " Connection closed"); } } catch (Exception e) { e.printStackTrace(); } } else { request.setAttribute("errorDisplay", ""); request.setAttribute("error", "Session timed out,Please login again !!"); request.getRequestDispatcher("/login.jsp").include(request, response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { session = request.getSession(false);// don't create if it doesn't exist if (session != null && !session.isNew()) { try { if (conn == null || conn.isClosed()) { conn = (Connection) DBUtil.getConnection(); } } catch (Exception e) { e.printStackTrace(); } type = request.getParameter("type"); branch_id = request.getParameter("branch_id"); applicableToAll = request.getParameter("applicable_to"); promo_code = request.getParameter("promo_code"); appToStatus = request.getParameterValues("appToHidden"); partner_id = request.getParameter("branch_owner"); site_id = (String) session.getAttribute("siteId"); String[] include1 = request.getParameterValues("includeSelect1"); String[] include2 = request.getParameterValues("includeSelect2"); String[] exclude1 = request.getParameterValues("excludeSelect1"); String[] exclude2 = request.getParameterValues("excludeSelect2"); if ("facility_types_include".equals(type)) { promotionData = partnerManagePromotionDAO.getFacilitiesInclude(branch_id, conn); Gson gson = new Gson(); JsonElement element = gson.toJsonTree(promotionData, new TypeToken<List<FacilityMaster>>() { }.getType()); JsonArray jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if ("facility_types_exclude".equals(type)) { promotionData = partnerManagePromotionDAO.getFacilitiesExclude(branch_id, conn); Gson gson = new Gson(); JsonElement element = gson.toJsonTree(promotionData, new TypeToken<List<FacilityMaster>>() { }.getType()); JsonArray jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if ("profile_category_include".equals(type)) { profileCategory = partnerManagePromotionDAO.getProfileCategoryFacilitiesInclude(conn); Gson gson = new Gson(); JsonElement element = gson.toJsonTree(profileCategory, new TypeToken<List<LookupMaster>>() { }.getType()); JsonArray jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if ("profile_category_exclude".equals(type)) { profileCategory = partnerManagePromotionDAO.getProfileCategoryFacilitiesExclude(conn); Gson gson = new Gson(); JsonElement element = gson.toJsonTree(profileCategory, new TypeToken<List<LookupMaster>>() { }.getType()); JsonArray jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if ("gender_include".equals(type)) { genderData = partnerManagePromotionDAO.getGenderFacilitiesInclude(conn); Gson gson = new Gson(); JsonElement element = gson.toJsonTree(genderData, new TypeToken<List<LookupMaster>>() { }.getType()); JsonArray jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if ("gender_exclude".equals(type)) { genderData = partnerManagePromotionDAO.getGenderFacilitiesExclude(conn); Gson gson = new Gson(); JsonElement element = gson.toJsonTree(genderData, new TypeToken<List<LookupMaster>>() { }.getType()); JsonArray jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if ("check_promo_code_available".equals(type)) { promotion = partnerManagePromotionDAO.getPromoCodeDetails(promo_code, branch_id, conn); Gson gson = new Gson(); JsonElement element = gson.toJsonTree(promotion, new TypeToken<List<PromotionMaster>>() { }.getType()); JsonArray jsonArray = element.getAsJsonArray(); response.setContentType("application/json"); response.getWriter().print(jsonArray); } else if ("submit_promotion".equals(type)) { System.out.println("Submit promotion"); PromotionMaster pm = new PromotionMaster(); pm.setPromo_code(request.getParameter("promo_code")); pm.setPromo_name(request.getParameter("promo_name")); pm.setBranch_id(branch_id); pm.setPromo_type(request.getParameter("promo_type")); pm.setCreated_date(request.getParameter("fdate")); pm.setTransaction_limit_type(request.getParameter("transaction_limit_type")); pm.setStatus(request.getParameter("status")); pm.setLimit_value(Integer.parseInt(request.getParameter("limit_value"))); pm.setApproval_status(request.getParameter("approval_status")); pm.setEffective_start_date(request.getParameter("effective_from")); pm.setEffective_end_date(request.getParameter("effective_to")); pm.setApplicable_to_all(request.getParameter("applicable_to")); pm.setUsed_value(request.getParameter("accrued_value")); pm.setDiscount_type(request.getParameter("discount_type")); pm.setDiscount_value(Integer.parseInt(request.getParameter("discount_value"))); pm.setConditions(request.getParameter("condition")); pm.setPartner_id(partner_id); pm.setSite_id(site_id); promo_id = promoDao.addPromotionData(pm, conn); if ("No".equals(applicableToAll)) { for (int i = 0; i <= 1; i++) { enableRule = request.getParameter("enableRule" + i); if (enableRule == null) { enableRuleArr[i] = "Off"; } else { enableRuleArr[i] = "On"; } applicableType[i] = request.getParameter("applicable_type" + i); applicableTo[i] = request.getParameter("applicable_to" + i); } count = promoDao.addPromoDetailsRulesData(promo_id, branch_id, enableRuleArr, applicableType, applicableTo, appToStatus, include1, include2, exclude1, exclude2, reference_id, reference_name, conn); if (count > 0) { request.setAttribute("displayVal", "Promotion Registered !!"); request.setAttribute("condition", "Success"); request.getRequestDispatcher("/WEB-INF/common-jsp/successErrorLanding.jsp").forward(request, response); } else { request.setAttribute("displayVal", "Something wrong.Please try again !!"); request.setAttribute("condition", "Failure"); request.getRequestDispatcher("/WEB-INF/common-jsp/successErrorLanding.jsp").forward(request, response); } } else { if (count > 0) { request.setAttribute("displayVal", "Promotion Registered !!"); request.setAttribute("condition", "Success"); request.getRequestDispatcher("/WEB-INF/common-jsp/successErrorLanding.jsp").forward(request, response); } else { request.setAttribute("displayVal", "Something wrong.Please try again !!"); request.setAttribute("condition", "Failure"); request.getRequestDispatcher("/WEB-INF/common-jsp/successErrorLanding.jsp").forward(request, response); } } } // Close connection try { if (conn != null || !conn.isClosed()) { DBUtil.closeConnection(conn); System.out.println(conn + " Connection closed"); } } catch (Exception e) { e.printStackTrace(); } } else { request.setAttribute("errorDisplay", ""); request.setAttribute("error", "Session timed out,Please login again !!"); request.getRequestDispatcher("/login.jsp").include(request, response); } } }