com.bios.controller.services.StopRoundService.java Source code

Java tutorial

Introduction

Here is the source code for com.bios.controller.services.StopRoundService.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bios.controller.services;

import com.bios.controller.BiddingRoundServlet;
import com.bios.model.Bid;
import com.bios.model.BidDAO;
import com.bios.model.RoundOneBidDAO;
import com.bios.model.RoundTwoBidDAO;
import com.bios.utilities.ConnectionManager;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import is203.JWTException;
import is203.JWTUtility;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** 
 * Represents a JSON Web Service for Stop Round
 * @author Teh Ming Yi
 */
public class StopRoundService extends HttpServlet {

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method which stops a bidding round. It checks that the user is a verified admin
     * and proceeds to stop the round if the round is started. Subsequently, it creates a json object with the status
     * "success". However, if a round is already stopped, it creates a json object with the status "error" and stores the error
     * message "round already ended" in a json array. Additionally, if the user is not a verified admin with a valid token, it
     * creates a json object with the status "error" and stores the error message in a json array "invalid username/token".
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        JsonObject object = new JsonObject();
        if (request.getParameter("token") == null) {
            JsonArray array = new JsonArray();
            object.addProperty("status", "error");
            array.add(new JsonPrimitive("missing token"));
            object.add("message", array);

        } else if (request.getParameter("token").length() == 0) {

            JsonArray array = new JsonArray();
            object.addProperty("status", "error");
            array.add(new JsonPrimitive("blank token"));
            object.add("message", array);

        } else {
            String token = request.getParameter("token");
            try {
                String result = JWTUtility.verify(token, "abcdefgh12345678");
                if (result.equals("admin")) {

                    JsonArray array = new JsonArray();
                    ConnectionManager manager = new ConnectionManager();
                    if (manager.getBiddingRoundStatus().equals("started") && manager.getBiddingRound() == 1) {
                        // Stop the round here
                        object.addProperty("status", "success");

                        //                        RoundOneBidDAO.getInstance().drop();
                        //                        // add all round 1 pending bids into roundonebiddao
                        //                        ArrayList<Bid> allBids = BidDAO.getInstance().getPendingBids();
                        //                        for (Bid bid : allBids){
                        //                            RoundOneBidDAO.getInstance().addBid(bid);
                        //                        }

                        BiddingRoundServlet.incrementBiddingRound("true");
                    } else if (manager.getBiddingRoundStatus().equals("started")
                            && manager.getBiddingRound() == 2) {
                        // Stop the round here
                        object.addProperty("status", "success");

                        //                        RoundTwoBidDAO.getInstance().drop();
                        //                        // add all round 1 pending bids into roundonebiddao
                        //                        ArrayList<Bid> allBids = BidDAO.getInstance().getPendingBids();
                        //                        for (Bid bid : allBids){
                        //                            RoundTwoBidDAO.getInstance().addBid(bid);
                        //                        }

                        BiddingRoundServlet.incrementBiddingRound("true");
                    } else {
                        object.addProperty("status", "error");
                        array.add(new JsonPrimitive("round already ended"));
                        object.add("message", array);
                    }
                }
            } catch (JWTException e) {
                // This means not an admin, or token expired
                JsonArray array = new JsonArray();
                JsonPrimitive value = new JsonPrimitive("invalid token");
                array.add(value);
                object.addProperty("status", "error");
                object.add("message", array);
            }
        }

        System.out.println(object);
        response.setContentType("application/json");
        response.getWriter().write(object.toString());
        return;
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}