ordo.servlet.api.APILieuxController.java Source code

Java tutorial

Introduction

Here is the source code for ordo.servlet.api.APILieuxController.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 ordo.servlet.api;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ordo.data.dao.jpa.JpaCommandeClientDao;
import ordo.data.dao.jpa.JpaDepotDao;
import ordo.data.dao.jpa.JpaLieuDao;
import ordo.data.dao.jpa.JpaSwapLocationDao;
import ordo.data.entities.CommandeClient;
import ordo.data.entities.Depot;
import ordo.data.entities.Lieu;

/**
 *
 * @author Nicolas
 */
public class APILieuxController 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 {

        response.setContentType("application/json");

        JpaDepotDao daoDepot = JpaDepotDao.getInstance();
        JpaSwapLocationDao daoSwapLocation = JpaSwapLocationDao.getInstance();
        JpaCommandeClientDao daoCommandeClient = JpaCommandeClientDao.getInstance();

        Collection<Lieu> lieux = new ArrayList<>();
        lieux.addAll(daoDepot.findAll());
        lieux.addAll(daoSwapLocation.findAll());
        lieux.addAll(daoCommandeClient.findAll(false));

        if (lieux.isEmpty()) {
            CommandeClient c1 = new CommandeClient();
            Depot c2 = new Depot();
            c1.setId(1);
            c1.setQuantiteVoulue(26);
            c1.setCoordY(50.4291629f);
            c1.setCoordX(2.8278697f);
            c1.setLibelle("Sample Lieu");

            c2.setId(2);
            c2.setCoordY(50.5f);
            c2.setCoordX(2.8278697f);

            lieux.add(c1);
            lieux.add(c2);
        }

        String json = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
            @Override
            public boolean shouldSkipField(FieldAttributes f) {
                switch (f.getName()) {
                case "vehicule":
                case "colis":
                    return true;
                }

                return false;
            }

            @Override
            public boolean shouldSkipClass(Class<?> clazz) {
                return false;
            }
        }).create().toJson(lieux);
        json = "{\"lieux\": " + json + "}";
        PrintWriter out = response.getWriter();
        out.print(json);
    }

    // <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 {
        processRequest(request, response);
    }

    /**
     * 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 {
        processRequest(request, response);
    }

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

}