Example usage for com.google.gson JsonObject add

List of usage examples for com.google.gson JsonObject add

Introduction

In this page you can find the example usage for com.google.gson JsonObject add.

Prototype

public void add(String property, JsonElement value) 

Source Link

Document

Adds a member, which is a name-value pair, to self.

Usage

From source file:account.TypeWiseSales.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from   ww w . jav  a2  s.c o m
 *
 * @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 {

    final String from_date = request.getParameter("from_date");
    final String to_date = request.getParameter("to_date");
    final String ac_cd = request.getParameter("ac_cd");
    final DBHelper helper = DBHelper.GetDBHelper();
    final Connection dataConnection = helper.getConnMpAdmin();
    final JsonObject jResultObj = new JsonObject();
    final String sr_cd = request.getParameter("sr_cd");
    final String type_cd = request.getParameter("type_cd");
    final String brand_cd = request.getParameter("brnad_cd");
    final String model_cd = request.getParameter("model_cd");
    final String branch_cd = request.getParameter("branch_cd");
    if (dataConnection != null) {
        try {

            PreparedStatement pstLocal = null;
            String sql = "select v.branch_cd,b.brand_name,m.model_name,t.TYPE_NAME,sum(v1.qty) as pcs,sum(v1.RATE) as tot_sales from VILSHD v left join VILSDT v1 on v.REF_NO=v1.REF_NO "
                    + " left join SERIESMST s on v1.SR_CD=s.SR_CD \n"
                    + " left join MODELMST m on s.MODEL_CD=m.MODEL_CD left join TYPEMST t on m.TYPE_CD=t.TYPE_CD"
                    + " left join brandmst b on b.brand_cd=m.brand_cd where v.IS_DEL=0 " + " and v.v_date>='"
                    + from_date + "' " + " and v.v_date<='" + to_date + "'";
            if (!type_cd.equalsIgnoreCase("")) {
                sql += " and m.type_cd='" + type_cd + "' ";
            }
            if (!sr_cd.equalsIgnoreCase("")) {
                sql += " and s.SR_CD='" + sr_cd + "'";
            } else if (!brand_cd.equalsIgnoreCase("")) {
                sql += " and m.brand_cd='" + brand_cd + "'";
            } else if (!model_cd.equalsIgnoreCase("")) {
                sql += " and s.model_cd='" + model_cd + "'";
            }
            if (!ac_cd.equalsIgnoreCase("")) {
                sql += " and v.ac_cd='" + ac_cd + "'";
            }
            if (!branch_cd.equalsIgnoreCase("0")) {
                sql += " and v.branch_cd=" + branch_cd;
            }

            sql += " group by v.branch_cd,t.TYPE_NAME,m.model_name,brand_name order by branch_cd,model_name";
            pstLocal = dataConnection.prepareStatement(sql);
            ResultSet viewDataRs = pstLocal.executeQuery();

            JsonArray array = new JsonArray();
            while (viewDataRs.next()) {
                JsonObject object = new JsonObject();
                object.addProperty("brand_name", viewDataRs.getString("brand_name"));
                object.addProperty("model_name", viewDataRs.getString("model_name"));
                object.addProperty("type_name", viewDataRs.getString("type_name"));
                object.addProperty("pcs", viewDataRs.getInt("pcs"));
                object.addProperty("tot_sales", viewDataRs.getDouble("tot_sales"));
                object.addProperty("branch_cd", viewDataRs.getString("branch_cd"));
                array.add(object);
            }

            sql = "select v.branch_cd,b.brand_name,m.model_name,t.TYPE_NAME,sum(v1.qty) as pcs,sum(v1.RATE) as tot_sales from srhd v left join srdt v1 on v.REF_NO=v1.REF_NO "
                    + " left join SERIESMST s on v1.SR_CD=s.SR_CD \n"
                    + " left join MODELMST m on s.MODEL_CD=m.MODEL_CD left join TYPEMST t on m.TYPE_CD=t.TYPE_CD "
                    + " left join brandmst b on b.brand_cd=m.brand_cd where v.IS_DEL=0 " + " and v.v_date>='"
                    + from_date + "' " + " and v.v_date<='" + to_date + "'";
            if (!type_cd.equalsIgnoreCase("")) {
                sql += " and m.type_cd='" + type_cd + "' ";
            }
            if (!sr_cd.equalsIgnoreCase("")) {
                sql += " and s.SR_CD='" + sr_cd + "'";
            } else if (!brand_cd.equalsIgnoreCase("")) {
                sql += " and m.brand_cd='" + brand_cd + "'";
            } else if (!model_cd.equalsIgnoreCase("")) {
                sql += " and s.model_cd='" + model_cd + "'";
            }
            if (!ac_cd.equalsIgnoreCase("")) {
                sql += " and v.ac_cd='" + ac_cd + "'";
            }
            if (!branch_cd.equalsIgnoreCase("0")) {
                sql += " and v.branch_cd=" + branch_cd + "";
            }

            sql += " group by t.TYPE_NAME,m.model_name order by m.model_name";
            pstLocal = dataConnection.prepareStatement(sql);
            viewDataRs = pstLocal.executeQuery();

            while (viewDataRs.next()) {
                JsonObject object = new JsonObject();
                object.addProperty("brand_name", viewDataRs.getString("brand_name"));
                object.addProperty("model_name", viewDataRs.getString("model_name"));
                object.addProperty("type_name", viewDataRs.getString("type_name"));
                object.addProperty("pcs", viewDataRs.getInt("pcs") * -1);
                object.addProperty("tot_sales", viewDataRs.getDouble("tot_sales") * -1);
                object.addProperty("branch_cd", viewDataRs.getString("branch_cd"));
                array.add(object);
            }
            jResultObj.addProperty("result", 1);
            jResultObj.addProperty("Cause", "success");
            jResultObj.add("data", array);
        } catch (SQLNonTransientConnectionException ex1) {
            jResultObj.addProperty("result", -1);
            jResultObj.addProperty("Cause", "Server is down");
        } catch (SQLException ex) {
            jResultObj.addProperty("result", -1);
            jResultObj.addProperty("Cause", ex.getMessage());
        }
    }
    response.getWriter().print(jResultObj);

}

From source file:account.TypeWiseSalesDetail.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.// w  w w  .j av  a  2s  . co m
 *
 * @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 {

    final String from_date = request.getParameter("from_date");
    final String to_date = request.getParameter("to_date");
    final String ac_cd = request.getParameter("ac_cd");
    final DBHelper helper = DBHelper.GetDBHelper();
    final Connection dataConnection = helper.getConnMpAdmin();
    final JsonObject jResultObj = new JsonObject();
    final String sr_cd = request.getParameter("sr_cd");
    final String type_cd = request.getParameter("type_cd");
    final String brand_cd = request.getParameter("brnad_cd");
    final String model_cd = request.getParameter("model_cd");
    final String branch_cd = request.getParameter("branch_cd");
    if (dataConnection != null) {
        try {

            PreparedStatement pstLocal = null;
            String sql = "SELECT m.model_name,v.branch_cd,v.ref_no,a.fname,s.sr_name,v.v_date,t.TYPE_NAME,b.brand_name,"
                    + " CASE WHEN v1.IMEI_NO ='' THEN v1.SERAIL_NO ELSE v1.IMEI_NO END AS IMEI_NO,(v1.qty) AS pcs,(v1.RATE) AS tot_sales,"
                    + " t1.ac_name AS pur_party,MOBILE1,email  FROM VILSHD v LEFT JOIN vilsdt v1 ON v.REF_NO=v1.REF_NO "
                    + " LEFT JOIN SERIESMST s ON v1.SR_CD=s.SR_CD LEFT JOIN acntmst a ON v.ac_cd=a.ac_cd "
                    + " LEFT JOIN MODELMST m ON s.MODEL_CD=m.MODEL_CD LEFT JOIN TYPEMST t ON m.TYPE_CD=t.TYPE_CD"
                    + " left join  brandmst b on b.brand_cd=m.brand_cd left join phbkmst p1 on p1.ac_cd=a.ac_cd"
                    + " LEFT JOIN tag t1 ON t1.ref_no=v1.pur_tag_no  WHERE v.IS_DEL=0 " + " and v.v_date>='"
                    + from_date + "' " + " and v.v_date<='" + to_date + "'";
            if (!type_cd.equalsIgnoreCase("")) {
                sql += " and m.type_cd='" + type_cd + "' ";
            }
            if (!sr_cd.equalsIgnoreCase("")) {
                sql += " and s.SR_CD='" + sr_cd + "'";
            } else if (!brand_cd.equalsIgnoreCase("")) {
                sql += " and m.brand_cd='" + brand_cd + "'";
            } else if (!model_cd.equalsIgnoreCase("")) {
                sql += " and s.model_cd='" + model_cd + "'";
            }
            if (!ac_cd.equalsIgnoreCase("")) {
                sql += " and v.ac_cd='" + ac_cd + "'";
            }
            if (!branch_cd.equalsIgnoreCase("0")) {
                sql += " and v.branch_cd=" + branch_cd;
            }
            sql += " order by v.branch_cd";
            pstLocal = dataConnection.prepareStatement(sql);
            ResultSet viewDataRs = pstLocal.executeQuery();

            JsonArray array = new JsonArray();
            while (viewDataRs.next()) {
                JsonObject object = new JsonObject();
                object.addProperty("fname", viewDataRs.getString("fname"));
                object.addProperty("model_name", viewDataRs.getString("model_name"));
                object.addProperty("sr_name", viewDataRs.getString("sr_name"));
                object.addProperty("v_date", viewDataRs.getString("v_date"));
                object.addProperty("type_name", viewDataRs.getString("type_name"));
                object.addProperty("pcs", viewDataRs.getInt("pcs"));
                object.addProperty("tot_sales", viewDataRs.getDouble("tot_sales"));
                object.addProperty("IMEI_NO", viewDataRs.getString("IMEI_NO"));
                object.addProperty("brand_name", viewDataRs.getString("brand_name"));
                object.addProperty("PUR_PARTY", viewDataRs.getString("PUR_PARTY"));
                object.addProperty("REF_NO", viewDataRs.getString("REF_NO"));
                object.addProperty("MOBILE1",
                        (viewDataRs.getString("MOBILE1") == null) ? "" : viewDataRs.getString("MOBILE1"));
                object.addProperty("EMAIL",
                        (viewDataRs.getString("EMAIL") == null) ? "" : viewDataRs.getString("EMAIL"));
                object.addProperty("branch_cd", viewDataRs.getString("branch_cd"));
                array.add(object);
            }

            sql = "select   m.model_name,v.branch_cd,v.ref_no,a.fname,s.sr_name,v.v_date,t.TYPE_NAME,b.brand_name"
                    + " ,case when IMEI_NO ='' then SERAIL_NO else IMEI_NO end as IMEI_NO,(v1.qty) as pcs,(v1.RATE) as tot_sales"
                    + " ,MOBILE1,email  from srhd v left join srdt v1 on" + " v.REF_NO=v1.REF_NO "
                    + " left join SERIESMST s on v1.SR_CD=s.SR_CD left join acntmst a on v.ac_cd=a.ac_cd "
                    + " left join MODELMST m on s.MODEL_CD=m.MODEL_CD left join brandmst b on b.brand_cd=m.brand_cd"
                    + " left join TYPEMST t on m.TYPE_CD=t.TYPE_CD left join phbkmst p1 on p1.ac_cd=a.ac_cd where v.IS_DEL=0 "
                    + " and v.v_date>='" + from_date + "' " + " and v.v_date<='" + to_date + "'";
            if (!type_cd.equalsIgnoreCase("")) {
                sql += " and m.type_cd='" + type_cd + "' ";
            }
            if (!sr_cd.equalsIgnoreCase("")) {
                sql += " and s.SR_CD='" + sr_cd + "'";
            } else if (!brand_cd.equalsIgnoreCase("")) {
                sql += " and m.brand_cd='" + brand_cd + "'";
            } else if (!model_cd.equalsIgnoreCase("")) {
                sql += " and s.model_cd='" + model_cd + "'";
            }
            if (!ac_cd.equalsIgnoreCase("")) {
                sql += " and v.ac_cd='" + ac_cd + "'";
            }
            if (!branch_cd.equalsIgnoreCase("0")) {
                sql += " and v.branch_cd=" + branch_cd;
            }

            pstLocal = dataConnection.prepareStatement(sql);
            viewDataRs = pstLocal.executeQuery();
            while (viewDataRs.next()) {
                JsonObject object = new JsonObject();
                object.addProperty("fname", viewDataRs.getString("fname"));
                object.addProperty("sr_name", viewDataRs.getString("sr_name"));
                object.addProperty("model_name", viewDataRs.getString("model_name"));
                object.addProperty("brand_name", viewDataRs.getString("brand_name"));
                object.addProperty("v_date", viewDataRs.getString("v_date"));
                object.addProperty("type_name", viewDataRs.getString("type_name"));
                object.addProperty("pcs", viewDataRs.getInt("pcs") * -1);
                object.addProperty("tot_sales", viewDataRs.getDouble("tot_sales") * -1);
                object.addProperty("IMEI_NO", viewDataRs.getString("IMEI_NO"));
                object.addProperty("REF_NO", viewDataRs.getString("REF_NO"));
                object.addProperty("MOBILE1",
                        (viewDataRs.getString("MOBILE1") == null) ? "" : viewDataRs.getString("MOBILE1"));
                object.addProperty("EMAIL",
                        (viewDataRs.getString("EMAIL") == null) ? "" : viewDataRs.getString("EMAIL"));
                object.addProperty("PUR_PARTY", "");
                object.addProperty("branch_cd", viewDataRs.getString("branch_cd"));
                array.add(object);
            }

            jResultObj.addProperty("result", 1);
            jResultObj.addProperty("Cause", "success");
            jResultObj.add("data", array);
        } catch (SQLNonTransientConnectionException ex1) {
            jResultObj.addProperty("result", -1);
            jResultObj.addProperty("Cause", "Server is down");
        } catch (SQLException ex) {
            jResultObj.addProperty("result", -1);
            jResultObj.addProperty("Cause", ex.getMessage());
        }
    }
    response.getWriter().print(jResultObj);

}

From source file:account.WithoutTagSalesReport.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from   w  w w .j a v a2 s .  c  o  m*/
 *
 * @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 {

    final String from_date = request.getParameter("from_date");
    final String to_date = request.getParameter("to_date");
    final String ac_cd = request.getParameter("ac_cd");
    final DBHelper helper = DBHelper.GetDBHelper();
    final Connection dataConnection = helper.getConnMpAdmin();
    final JsonObject jResultObj = new JsonObject();
    final String sr_cd = request.getParameter("sr_cd");
    final String type_cd = request.getParameter("type_cd");
    final String brand_cd = request.getParameter("brnad_cd");
    final String model_cd = request.getParameter("model_cd");
    if (dataConnection != null) {
        try {

            PreparedStatement pstLocal = null;
            String sql = "select v.ref_no,a.fname,s.sr_name,v.v_date,t.TYPE_NAME,case when IMEI_NO ='' then SERAIL_NO else IMEI_NO end as IMEI_NO,(v1.qty) as pcs,(v1.RATE) as tot_sales from VILSHD v left join vilsdt v1 on v.REF_NO=v1.REF_NO "
                    + " left join SERIESMST s on v1.SR_CD=s.SR_CD left join acntmst a on v.ac_cd=a.ac_cd "
                    + " left join MODELMST m on s.MODEL_CD=m.MODEL_CD left join TYPEMST t on m.TYPE_CD=t.TYPE_CD where v.IS_DEL=0 "
                    + " and v.v_date>='" + from_date + "' " + " and v.v_date<='" + to_date
                    + "' and v1.PUR_TAG_NO=''";
            if (!type_cd.equalsIgnoreCase("")) {
                sql += " and m.type_cd='" + type_cd + "' ";
            }
            if (!sr_cd.equalsIgnoreCase("")) {
                sql += " and s.SR_CD='" + sr_cd + "'";
            } else if (!brand_cd.equalsIgnoreCase("")) {
                sql += " and s.brand_cd='" + brand_cd + "'";
            } else if (!model_cd.equalsIgnoreCase("")) {
                sql += " and s.model_cd='" + model_cd + "'";
            }
            if (!ac_cd.equalsIgnoreCase("")) {
                sql += " and v.ac_cd='" + ac_cd + "'";
            }

            pstLocal = dataConnection.prepareStatement(sql);
            ResultSet viewDataRs = pstLocal.executeQuery();

            JsonArray array = new JsonArray();
            while (viewDataRs.next()) {
                JsonObject object = new JsonObject();
                object.addProperty("fname", viewDataRs.getString("fname"));
                object.addProperty("sr_name", viewDataRs.getString("sr_name"));
                object.addProperty("v_date", viewDataRs.getString("v_date"));
                object.addProperty("type_name", viewDataRs.getString("type_name"));
                object.addProperty("pcs", viewDataRs.getInt("pcs"));
                object.addProperty("tot_sales", viewDataRs.getDouble("tot_sales"));
                object.addProperty("IMEI_NO", viewDataRs.getString("IMEI_NO"));
                object.addProperty("REF_NO", viewDataRs.getString("REF_NO"));
                array.add(object);
            }

            jResultObj.addProperty("result", 1);
            jResultObj.addProperty("Cause", "success");
            jResultObj.add("data", array);
        } catch (SQLNonTransientConnectionException ex1) {
            jResultObj.addProperty("result", -1);
            jResultObj.addProperty("Cause", "Server is down");
        } catch (SQLException ex) {
            jResultObj.addProperty("result", -1);
            jResultObj.addProperty("Cause", ex.getMessage());
        }
    }
    response.getWriter().print(jResultObj);

}

From source file:accountMaster.GetAccountMaster.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//w ww. java 2  s. c  o m
 *
 * @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 {
    final DBHelper helper = DBHelper.GetDBHelper();
    final Connection dataConnection = helper.getConnMpAdmin();
    final JsonObject jResultObj = new JsonObject();
    final String ac_name = request.getParameter("AC_NAME");
    Library lb = Library.getInstance();
    if (dataConnection != null) {
        try {
            String sql = "select a.OPB_AMT,a.OPB_EFF,a.AC_CD,FNAME,g.GROUP_NAME,g.GRP_CD,CST,TIN,a1.ADD1,p.MOBILE1,a.card_no,p.EMAIL,gst_no from ACNTMST a left join GROUPMST g on a.GRP_CD=g.GRP_CD \n"
                    + "left join ADBKMST a1 on a.AC_CD=a1.AC_CD left join PHBKMST p on a.AC_CD=p.AC_CD ";
            if (ac_name != null) {
                sql += " where fname like '%" + ac_name + "%'";
            }
            sql += " order by fname";
            PreparedStatement pstLocal = dataConnection.prepareStatement(sql);
            ResultSet rsLocal = pstLocal.executeQuery();
            JsonArray array = new JsonArray();
            while (rsLocal.next()) {
                JsonObject object = new JsonObject();
                object.addProperty("AC_CD", rsLocal.getString("AC_CD"));
                object.addProperty("FNAME", rsLocal.getString("FNAME"));
                object.addProperty("GROUP_NAME", rsLocal.getString("GROUP_NAME"));
                object.addProperty("GRP_CD", rsLocal.getString("GRP_CD"));
                object.addProperty("CST", rsLocal.getString("CST"));
                object.addProperty("TIN", rsLocal.getString("TIN"));
                object.addProperty("ADD1", rsLocal.getString("ADD1"));
                object.addProperty("MOBILE1", rsLocal.getString("MOBILE1"));
                object.addProperty("EMAIL", rsLocal.getString("EMAIL"));
                object.addProperty("CARD_NO", rsLocal.getString("CARD_NO"));
                object.addProperty("OPB_AMT", rsLocal.getString("OPB_AMT"));
                object.addProperty("OPB_EFF", rsLocal.getString("OPB_EFF"));
                object.addProperty("GST_NO", rsLocal.getString("GST_NO"));
                array.add(object);
            }
            jResultObj.addProperty("result", 1);
            jResultObj.addProperty("Cause", "success");
            jResultObj.add("data", array);
        } catch (SQLNonTransientConnectionException ex1) {
            jResultObj.addProperty("result", -1);
            jResultObj.addProperty("Cause", "Server is down");
        } catch (SQLException ex) {
            jResultObj.addProperty("result", -1);
            jResultObj.addProperty("Cause", ex.getMessage());
        } finally {
            lb.closeConnection(dataConnection);
        }
    }
    response.getWriter().print(jResultObj);
}

From source file:actions.ActivitesAction.java

@Override
public String execute(HttpServletRequest request) {

    ServiceMetier sm = new ServiceMetier();

    List<Activite> la = null;
    if (sm != null) {
        la = sm.obtenirActivites();//from   www  . j ava  2  s .c o  m
    }
    if (la != null) {
        request.setAttribute("ListActivite", la);
    } else {
        request.setAttribute("ListActivite", "NULL");
    }

    JsonArray jsonListe = new JsonArray();

    for (Activite act : la) {
        JsonObject jsonActivite = new JsonObject();

        jsonActivite.addProperty("id", act.getId());
        jsonActivite.addProperty("denomination", act.getDenomination());

        jsonListe.add(jsonActivite);
    }

    //Objet JSON "conteneur"
    JsonObject container = new JsonObject();
    container.add("activites", jsonListe);

    //Serialisation & Ecriture sur le flux de sortie
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json = gson.toJson(container);

    return json;

}

From source file:actions.DetailsAction.java

@Override
public String execute(HttpServletRequest request) {
    ServiceMetier sm = new ServiceMetier();
    HttpSession session = request.getSession();
    Adherent adherent = sm.connexion((String) session.getAttribute("Email"));
    List<DemandeEvenement> lde = sm.obtenirDemandesPerso(adherent, true);
    DemandeEvenement demande = null;//from   w  ww .  jav a2  s .  c o  m

    JsonObject jsonResponse = new JsonObject();

    for (DemandeEvenement de : lde) {
        long id = Long.parseLong(request.getParameter("id"));
        if (de.getId() == id) {
            demande = de;
            break;
        }
    }

    if (demande == null) {
        return "";
    }

    Evenement event = sm.obtenirEvenement(demande);
    if (event == null) {
        return "";
    }

    if (event.getLieu() != null) {
        jsonResponse.addProperty("lieu", event.getLieu().getDenomination());
    } else {
        jsonResponse.addProperty("lieu", "-");
    }

    if (event instanceof EvenementPayant) {
        EvenementPayant ep = (EvenementPayant) event;
        jsonResponse.addProperty("prix", ep.getPAF());
    } else {
        jsonResponse.addProperty("prix", "-");
    }

    jsonResponse.addProperty("activite", demande.getActivity().getDenomination());
    jsonResponse.addProperty("date", demande.getDate().toString());
    jsonResponse.addProperty("moment", demande.getDay_moment().toString());

    //demande.getList_adher();
    JsonArray jsonListe = new JsonArray();

    for (Adherent ad : demande.getList_adher()) {
        JsonObject jsonPersonne = new JsonObject();
        jsonPersonne.addProperty("id", ad.getId());
        jsonPersonne.addProperty("nom", ad.getNom());
        jsonPersonne.addProperty("prenom", ad.getPrenom());
        jsonListe.add(jsonPersonne);
    }

    //Objet JSON "conteneur"
    JsonObject container = new JsonObject();
    container.add("personnes", jsonListe);
    container.add("details", jsonResponse);

    //Serialisation & Ecriture sur le flux de sortie
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json = gson.toJson(container);

    return json;
}

From source file:Actions.GetActeursAction.java

@Override
public void execute(HttpServletRequest request, HttpServletResponse reponse) {
    System.out.println("Je suis dans action getActeurs");
    JsonObject jsonContainer = new JsonObject();

    // ---------------- rcupration des restos---------------------
    JsonArray jsonListe = new JsonArray();
    List<Restaurant> listeResto = ServiceMetier.findAllRestaurants();
    for (Restaurant restaurant : listeResto) {
        JsonObject jsonRestaurant = new JsonObject();
        jsonRestaurant.addProperty("adresse", restaurant.getAdresse());
        jsonRestaurant.addProperty("denomination", restaurant.getDenomination());
        jsonRestaurant.addProperty("latitude", restaurant.getLatitude());
        jsonRestaurant.addProperty("longitude", restaurant.getLongitude());
        jsonListe.add(jsonRestaurant);/*from   w ww  .  ja  v  a2s  .co  m*/
    }
    jsonContainer.add("restos", jsonListe);

    // ---------------- rcupration des clients---------------------
    jsonListe = new JsonArray();
    List<Client> listeClient = ServiceMetier.findAllClients();
    for (Client client : listeClient) {
        JsonObject jsonClient = new JsonObject();
        jsonClient.addProperty("adresse", client.getAdresse());
        jsonClient.addProperty("nomC", client.getNom() + " " + client.getPrenom());
        jsonClient.addProperty("latitude", client.getLatitude());
        jsonClient.addProperty("longitude", client.getLongitude());
        jsonListe.add(jsonClient);
    }
    jsonContainer.add("clients", jsonListe);

    // ---------------- rcupration des vlos---------------------
    jsonListe = new JsonArray();
    List<Livreur> listeVelos = ServiceMetier.findAllVelos();
    for (Livreur velo : listeVelos) {
        JsonObject jsonVelo = new JsonObject();
        jsonVelo.addProperty("adresse", velo.getAdresse());
        int indexTiret = velo.toString().indexOf(" -");
        int apresVeloIndex = velo.toString().indexOf("Vlo : ") + 7;
        String nomComplet = velo.toString().substring(apresVeloIndex, indexTiret);
        System.out.println("nom complet velo : " + nomComplet);
        jsonVelo.addProperty("nomC", nomComplet);
        String estDisp = "NON";
        if (velo.getCmdeEnCours() == null) {
            estDisp = "OUI";
        }
        jsonVelo.addProperty("dispo", estDisp);
        jsonVelo.addProperty("poidsMax", velo.getChargeMaxi());
        jsonVelo.addProperty("latitude", velo.getLatitude());
        jsonVelo.addProperty("longitude", velo.getLongitude());
        jsonVelo.addProperty("idLivreur", velo.getIdLivreur());
        jsonListe.add(jsonVelo);
    }
    jsonContainer.add("velos", jsonListe);

    // ---------------- rcupration des drones---------------------
    jsonListe = new JsonArray();
    List<Livreur> listeDrones = ServiceMetier.findAllDrones();
    for (Livreur drone : listeDrones) {
        JsonObject jsonDrone = new JsonObject();
        jsonDrone.addProperty("code", drone.getIdLivreur());
        String estDisp = "NON";
        if (drone.getCmdeEnCours() == null) {
            estDisp = "OUI";
        }
        jsonDrone.addProperty("dispo", estDisp);
        jsonDrone.addProperty("adresse", drone.getAdresse());
        jsonDrone.addProperty("latitude", drone.getLatitude());
        jsonDrone.addProperty("longitude", drone.getLongitude());
        int indexVit = drone.toString().indexOf("Vitesse moyenne : ");
        String vitMoyenne = drone.toString().substring(indexVit + 18);
        jsonDrone.addProperty("vit", vitMoyenne);
        jsonDrone.addProperty("poidsMax", drone.getChargeMaxi());

        jsonListe.add(jsonDrone);
    }
    jsonContainer.add("drones", jsonListe);

    // ---------------------envoi de la rponse Json------------------------
    reponse.setContentType("text/html;charset=UTF-8");
    PrintWriter out = null;
    try {
        out = reponse.getWriter();
    } catch (IOException ex) {
        Logger.getLogger(GetRestaurantsAction.class.getName()).log(Level.SEVERE, null, ex);
    }
    out.println(jsonContainer);
    out.close();
}

From source file:Actions.GetCommandeAction.java

@Override
public void execute(HttpServletRequest request, HttpServletResponse reponse) {
    HttpSession session = request.getSession(true);
    Livreur livreur = (Livreur) session.getAttribute("livreur");

    Commande commande = livreur.getCmdeEnCours();
    System.out.println("dans action GetCommandeAction");
    System.out.println("livreur: " + livreur);
    JsonObject jsonContainer = new JsonObject();
    // contrairement  commande, qui est le contenu (les produtis commands), commandeEnPers est le contenant           
    JsonObject jsonCmdEnPers = new JsonObject();

    // Si aucune commande n'est associe
    if (commande == null) {
        jsonCmdEnPers.addProperty("plein", 0);
    } else {/* ww w .j  a va2  s  . co m*/
        jsonCmdEnPers.addProperty("plein", 1);
        List<ProduitCommande> produits = commande.getProduitCommande();
        JsonArray jsonListe = new JsonArray();
        for (ProduitCommande pc : produits) {
            //System.out.println(restaurant);
            JsonObject jsonProduitCommande = new JsonObject();
            jsonProduitCommande.addProperty("nom", pc.getProduit().getDenomination());
            jsonProduitCommande.addProperty("quantite", pc.getQte());
            jsonProduitCommande.addProperty("prix", pc.getProduit().getPrix() * pc.getQte());
            jsonListe.add(jsonProduitCommande);
        }

        JsonObject jsonRestaurant = new JsonObject();
        jsonRestaurant.addProperty("adresseRestaurant", commande.getRestaurant().getAdresse());

        JsonObject jsonClient = new JsonObject();
        jsonClient.addProperty("adresseClient", commande.getClient().getAdresse());

        JsonObject jsonLivreur = new JsonObject();
        int indexTiret = livreur.toString().indexOf(" -");
        int apresVeloIndex = livreur.toString().indexOf("Vlo : ") + 7;
        String nomComplet = livreur.toString().substring(apresVeloIndex, indexTiret);
        jsonLivreur.addProperty("nomC", nomComplet);

        jsonContainer.add("commande", jsonListe);
        jsonContainer.add("restaurant", jsonRestaurant);
        jsonContainer.add("client", jsonClient);
    }

    // Dans tous les cas, commande ou pas, on rcup le livreur
    JsonObject jsonLivreur = new JsonObject();
    int indexTiret = livreur.toString().indexOf(" -");
    int apresVeloIndex = livreur.toString().indexOf("Vlo : ") + 7;
    String nomComplet = livreur.toString().substring(apresVeloIndex, indexTiret);
    jsonLivreur.addProperty("nomC", nomComplet);

    jsonContainer.add("commandeEnPers", jsonCmdEnPers);
    jsonContainer.add("livreur", jsonLivreur);
    System.out.println("jsonCmdEnPers:" + jsonCmdEnPers);
    // Envoi de la rponse
    reponse.setContentType("text/html;charset=UTF-8");
    PrintWriter out = null;
    try {
        out = reponse.getWriter();
    } catch (IOException ex) {
        Logger.getLogger(GetRestaurantsAction.class.getName()).log(Level.SEVERE, null, ex);
    }
    out.println(jsonContainer);
    out.close();

}

From source file:Actions.GetCommandeLAction.java

@Override
public void execute(HttpServletRequest request, HttpServletResponse reponse) {
    System.out.println("dans action GetCommandeLAction");

    // rcupration des infos pour l'id du livreur donne 
    long idLivreur = Long.parseLong(request.getParameter("idLivreur"));
    System.out.println("id lireur " + idLivreur);
    Livreur livreur = ServiceMetier.connexionLivreur(idLivreur);

    Commande commande = livreur.getCmdeEnCours();

    // Les adresses stockes dans un tableau (je comprends pas pq on ne peut stocker direct des objet non
    // tableau dans un container (qui conteint au moin sun autre tableau ???)
    JsonArray adressesJson = new JsonArray();

    // L'adresse client 
    JsonObject adrC = new JsonObject();
    Client client = commande.getClient();
    System.out.println("adresse vlien : " + client.getAdresse());
    adrC.addProperty("a", client.getAdresse());
    adrC.addProperty("adresse2", client.getAdresse());
    adressesJson.add(adrC);/*w  w  w  . ja  va2 s  .  c om*/

    // L'adresse resto
    adrC = new JsonObject();
    Restaurant resto = commande.getRestaurant();
    adrC.addProperty("a", resto.getAdresse());
    adrC.addProperty("adresse2", client.getAdresse());
    adressesJson.add(adrC);

    // Les produits : chacun a un nom et une quantit
    JsonArray jsonListeP = new JsonArray();
    List<ProduitCommande> produits = commande.getProduitCommande();
    for (ProduitCommande pc : produits) {
        JsonObject jsonProduitCommande = new JsonObject();
        jsonProduitCommande.addProperty("nom", pc.getProduit().getDenomination());
        jsonProduitCommande.addProperty("quantite", pc.getQte());
        jsonListeP.add(jsonProduitCommande);
    }

    // Ajout des donnes dans un container
    JsonObject jsonContainer = new JsonObject();
    jsonContainer.add("produits", jsonListeP);
    jsonContainer.add("adr", adressesJson);

    // Envoi de la rponse
    reponse.setContentType("text/html;charset=UTF-8");
    PrintWriter out = null;
    try {
        out = reponse.getWriter();
    } catch (IOException ex) {
        Logger.getLogger(GetRestaurantsAction.class.getName()).log(Level.SEVERE, null, ex);
    }
    out.println(jsonContainer);
    out.close();

}

From source file:Actions.GetProduitsAction.java

@Override
public void execute(HttpServletRequest request, HttpServletResponse reponse) {
    System.out.println("Je suis dans action getProduits");
    long idRestaurant = parseLong((request.getParameter("idRestaurant")));
    Restaurant restaurant = null;//  w w w.  j  av a2s .c o  m
    try {
        restaurant = ServiceMetier.findAllRestaurants().get((int) idRestaurant - 1);
    } catch (Exception ex) {
        Logger.getLogger(GetProduitsAction.class.getName()).log(Level.SEVERE, null, ex);
    }
    JsonArray jsonListe = new JsonArray();
    String recherche = request.getParameter("nomProduit");
    System.out.println("recherche: " + recherche);
    List<Produit> listeProduit = null;
    if (recherche == null) {
        try {
            listeProduit = ServiceMetier.searchProduits(" ", restaurant);
        } catch (Exception ex) {
            Logger.getLogger(GetProduitsAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        listeProduit = ServiceMetier.searchProduits(recherche, restaurant);
    }
    for (Produit produit : listeProduit) {
        //System.out.println(restaurant);
        JsonObject jsonProduit = new JsonObject();
        jsonProduit.addProperty("id", produit.getId());
        jsonProduit.addProperty("prix", produit.getPrix());
        jsonProduit.addProperty("denomination", produit.getDenomination());
        jsonListe.add(jsonProduit);
    }
    JsonObject jsonContainer = new JsonObject();
    jsonContainer.add("produits", jsonListe);
    reponse.setContentType("text/html;charset=UTF-8");
    PrintWriter out = null;
    try {
        out = reponse.getWriter();
    } catch (IOException ex) {
        Logger.getLogger(GetRestaurantsAction.class.getName()).log(Level.SEVERE, null, ex);
    }
    out.println(jsonContainer);
    out.close();
}