Controllers.ProfileController.java Source code

Java tutorial

Introduction

Here is the source code for Controllers.ProfileController.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 Controllers;

import Models.User;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
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.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 *
 * @author Caine
 */
@WebServlet(name = "ProfileController", urlPatterns = { "/ProfileController" })
public class ProfileController extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        if ("GET_PROFILE".equals(request.getParameter("TASK"))) {
            //Returns a user as a JSON string from a user ID
            String userID = request.getParameter("user_id");
            User user = new User();
            user.setFacebookID(userID);
            user = user.getUser();

            //GET RIDES TAKEN AND GIVEN HERE
            //user.updateGivenAndTaken();

            JSONObject jsonUser = user.toJSON();
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            out.append(jsonUser.toString()); //Might be toString instead
            out.flush();
        } else if ("UPDATE_CAR".equals(request.getParameter("TASK"))) {
            //Returns a user as a JSON string from a user ID
            String userID = request.getParameter("user_id");
            User user = new User();
            user.setFacebookID(userID);
            user = user.getUser();

            String newBrand = request.getParameter("car_brand");
            String newModel = request.getParameter("car_model");
            String newColour = request.getParameter("car_colour");
            String newLicence = request.getParameter("licence_plate");

            user.setCarBrand(newBrand);
            user.setCarModel(newModel);
            user.setCarColour(newColour);
            user.setLicencePlate(newLicence);
            user.updateDB();

            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.append("Profile Updated"); //Might be toString instead
            out.flush();
        }
    }

    // <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.
     *
     * @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 {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(ProfileController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ProfileController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * 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 {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(ProfileController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ProfileController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

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

}