Example usage for com.google.gson JsonObject JsonObject

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

Introduction

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

Prototype

JsonObject

Source Link

Usage

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.AddUpdateAccountMaster.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//  www  .j  av a  2 s.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 DBHelper helper = DBHelper.GetDBHelper();
    final Connection dataConnection = helper.getConnMpAdmin();

    final String acString = request.getParameter("ac_model");
    final JsonObject jResultObj = new JsonObject();
    AccountMasterModel acc = new Gson().fromJson(acString, AccountMasterModel.class);
    try {
        dataConnection.setAutoCommit(false);
        acc = saveVoucher(dataConnection, acc);
        dataConnection.commit();
        dataConnection.setAutoCommit(true);
        jResultObj.addProperty("result", 1);
        jResultObj.addProperty("Cause", "success");
        jResultObj.addProperty("ac_cd", acc.getAC_CD());
    } catch (SQLNonTransientConnectionException ex1) {
        jResultObj.addProperty("result", -1);
        jResultObj.addProperty("Cause", "Server is down");
        try {
            dataConnection.rollback();
            dataConnection.setAutoCommit(true);
        } catch (SQLException e) {

        }
    } catch (SQLException ex) {
        jResultObj.addProperty("result", -1);
        jResultObj.addProperty("Cause", ex.getMessage());
        try {
            dataConnection.rollback();
            dataConnection.setAutoCommit(true);
        } catch (SQLException e) {

        }
    } catch (Exception ex) {
        jResultObj.addProperty("result", -1);
        jResultObj.addProperty("Cause", ex.getMessage());
        try {
            dataConnection.rollback();
            dataConnection.setAutoCommit(true);
        } catch (SQLException e) {

        }
    }
    response.getWriter().print(jResultObj);
}

From source file:accountMaster.GetAccountMaster.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from w  w w. jav a  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   w  ww  . ja v a2  s  .com*/
    }
    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.ConnectionNonClientAction.java

@Override
public void execute(HttpServletRequest request, HttpServletResponse reponse) {

    // Session !!!
    HttpSession session = request.getSession(true);
    try {//from ww w  .  j  av a 2 s. c  o  m
        request.setCharacterEncoding("UTF-8");
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(ConnectionNonClientAction.class.getName()).log(Level.SEVERE, null, ex);
    }

    System.out.println("Je suisConnectionNonClient");

    String mdp = request.getParameter("mdp");
    System.out.println("Mor de passe :" + mdp);
    // attention lors de la conversion du mdp
    long mdpl = -123; // cette ID n'existe pas
    try {
        mdpl = Long.parseLong(mdp);
    } catch (NumberFormatException e) {
        System.out.println("Le mdp entr n'est pas un nb");
    }

    JsonObject jsonVelo = new JsonObject();
    Livreur l = null;
    try {
        l = ServiceMetier.connexionLivreur(mdpl);
        if (l.getTypeId().equals("drone")) {
            l = null;
        }
    } catch (Exception ex) {
        Logger.getLogger(ConnectionAction.class.getName()).log(Level.SEVERE, null, ex);
    }
    // si livreur trouv, enregistrement dans la session et l'objet Json est dit plein
    if (l != null) {
        // enregistrer le livreur pour la session
        session.setAttribute("livreur", l);
        System.out.println("Livreur trouv");
        jsonVelo.addProperty("plein", 1);
        int indexTiret = l.toString().indexOf(" -");
        int apresVeloIndex = l.toString().indexOf("Vlo : ") + 7;
        String nomComplet = l.toString().substring(apresVeloIndex, indexTiret);
        jsonVelo.addProperty("nomC", nomComplet);
        jsonVelo.addProperty("plein", 1);
    } else {
        System.out.println("Livreur pas trouv");
        jsonVelo.addProperty("plein", 0);
    }

    // envoi de l'objet 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(jsonVelo);
    out.close();

}

From source file:actions.ConnexionAction.java

@Override
public String execute(HttpServletRequest request) {

    ServiceMetier sm = new ServiceMetier();
    Adherent adherent = sm.connexion(request.getParameter("email"));

    JsonObject jsonResponse = new JsonObject();
    if (adherent == null) { // Connexion chou
        jsonResponse.addProperty("Connexion", "KO");
    } else {//  w  ww  . j ava  2 s  .c o m
        jsonResponse.addProperty("Connexion", "OK");
        HttpSession session = request.getSession();
        session.setAttribute("Id", adherent.getId());
        session.setAttribute("Email", adherent.getMail());
    }

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

From source file:Actions.DetailDroneAction.java

@Override
public void execute(HttpServletRequest request, HttpServletResponse reponse) {
    // rcuprer l'instance concern 
    System.out.println("Je suis dans action DetailDrone");
    HttpSession session = request.getSession(true);
    String idStr = request.getParameter("idDrone");
    System.out.println(idStr);/* w w  w.j  av  a 2 s .  com*/
    long id = parseLong(idStr.substring(1, idStr.length()));
    System.out.println(id);
    List<Livreur> listDrone = ServiceMetier.findAllDrones();
    LivreurDrone drone = null;
    for (Livreur l : listDrone) {
        if (l.getIdLivreur() == id) {
            drone = (LivreurDrone) l;
            break;
        }
    }
    session.setAttribute("livreur", drone);
    String str = "";
    if (drone.getCmdeEnCours() == null) {
        str = "OUI";
    } else {
        str = "NON";
    }
    JsonObject jsonDrone = new JsonObject();
    jsonDrone.addProperty("id", id);
    jsonDrone.addProperty("disponibilite", str);
    jsonDrone.addProperty("adresse", drone.getAdresse());
    jsonDrone.addProperty("vitesse", drone.getVitesseMoyenne());
    jsonDrone.addProperty("poidsMax", drone.getChargeMaxi());

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

From source file:Actions.DetailProduitAction.java

@Override
public void execute(HttpServletRequest request, HttpServletResponse reponse) {
    // rcuprer l'instance concern 
    System.out.println("Je suis dans action DetailProduit");
    long idRestaurant = parseLong((request.getParameter("idRestaurant")));
    Restaurant restaurant = null;//from  w  w w .  j a va2  s .  c  o  m
    try {
        restaurant = ServiceMetier.findAllRestaurants().get((int) idRestaurant - 1);
    } catch (Exception ex) {
        Logger.getLogger(GetProduitsAction.class.getName()).log(Level.SEVERE, null, ex);
    }
    long idProduit = parseLong((request.getParameter("idProduit")));
    System.out.println("idProduit: " + idProduit);
    List<Produit> produits = ServiceMetier.searchProduits(" ", restaurant);
    Produit produitFind = null;
    for (Produit produit : produits) {
        if (produit.getId() == idProduit) {
            produitFind = produit;
            break;
        }
    }
    //System.out.println(restaurant);
    // convertir en JSON
    JsonObject jsonProduit = new JsonObject();
    jsonProduit.addProperty("id", idProduit);
    jsonProduit.addProperty("denomination", produitFind.getDenomination());
    jsonProduit.addProperty("description", produitFind.getDescription());
    jsonProduit.addProperty("prix", produitFind.getPrix());

    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(jsonProduit);
    out.close();
}

From source file:Actions.DetailRestaurantsAction.java

@Override
public void execute(HttpServletRequest request, HttpServletResponse reponse) {
    // rcuprer l'instance concern 
    System.out.println("Je suis dans action DetailRestaurantsAction");
    long id = parseLong((request.getParameter("idRestaurant")));
    //System.out.println(id);
    Restaurant restaurant = ServiceMetier.findAllRestaurants().get((int) id - 1);
    //System.out.println(restaurant);
    // convertir en JSON
    JsonObject jsonRestaurant = new JsonObject();
    jsonRestaurant.addProperty("id", id);
    jsonRestaurant.addProperty("denomination", restaurant.getDenomination());
    jsonRestaurant.addProperty("adresse", restaurant.getAdresse());
    jsonRestaurant.addProperty("description", restaurant.getDescription());

    reponse.setContentType("text/html;charset=UTF-8");
    PrintWriter out = null;/*from  w ww  .j  a  va  2s. c o  m*/
    try {
        out = reponse.getWriter();
    } catch (IOException ex) {
        Logger.getLogger(GetRestaurantsAction.class.getName()).log(Level.SEVERE, null, ex);
    }
    out.println(jsonRestaurant);
    out.close();
}

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. j a va 2 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;
}