github.com/ismailbayram/bigpicture@v0.0.0-20231225173155-e4b21f5efcff/internal/browser/javaproject/src/main/com/shashi/servlets/AdminCancleTrain.java (about)

     1  package com.shashi.servlets;
     2  
     3  import java.io.IOException;
     4  import java.io.PrintWriter;
     5  
     6  import javax.servlet.RequestDispatcher;
     7  import javax.servlet.ServletException;
     8  import javax.servlet.annotation.WebServlet;
     9  import javax.servlet.http.HttpServlet;
    10  import javax.servlet.http.HttpServletRequest;
    11  import javax.servlet.http.HttpServletResponse;
    12  
    13  import com.shashi.beans.TrainException;
    14  import com.shashi.constant.ResponseCode;
    15  import com.shashi.constant.UserRole;
    16  import com.shashi.service.TrainService;
    17  import com.shashi.service.impl.TrainServiceImpl;
    18  import com.shashi.utility.TrainUtil;
    19  
    20  @WebServlet("/admincancletrain")
    21  public class AdminCancleTrain extends HttpServlet {
    22  
    23  	/**
    24  	 * 
    25  	 */
    26  	private static final long serialVersionUID = 1L;
    27  	private TrainService trainService = new TrainServiceImpl();
    28  
    29  	/**
    30  	 * 
    31  	 * @param req
    32  	 * @param res
    33  	 * @throws IOException
    34  	 * @throws ServletException
    35  	 */
    36  	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    37  		res.setContentType("text/html");
    38  		PrintWriter pw = res.getWriter();
    39  		TrainUtil.validateUserAuthorization(req, UserRole.ADMIN);
    40  		try {
    41  			String trainNo = req.getParameter("trainno");
    42  			String message = trainService.deleteTrainById(trainNo);
    43  			if (ResponseCode.SUCCESS.toString().equalsIgnoreCase(message)) {
    44  				RequestDispatcher rd = req.getRequestDispatcher("CancleTrain.html");
    45  				rd.include(req, res);
    46  				pw.println("<div class='main'><p1 class='menu'>Train number " + trainNo
    47  						+ " has been Deleted Successfully.</p1></div>");
    48  			} else {
    49  				RequestDispatcher rd = req.getRequestDispatcher("CancleTrain.html");
    50  				rd.include(req, res);
    51  				pw.println("<div class='tab'><p1 class='menu'>Train No." + trainNo + " is Not Available !</p1></div>");
    52  			}
    53  		} catch (Exception e) {
    54  			throw new TrainException(422, this.getClass().getName() + "_FAILED", e.getMessage());
    55  		}
    56  
    57  	}
    58  
    59  }