//                    .///.
//                   (0 o)
//-------------0000--(_)--0000---------------
//
//  (C) 2012-2014 Frédérik BENOIST / Garsys
//  Tout droits réservés
//
//             oooO      Oooo
//------------(   )-----(   )---------------
//             \ (       ) /
//              \_)     (_/
package RestWebServices;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import my.lib.db.CallDB;
import my.lib.rest.RestUtils;
import org.json.JSONObject;

@Path("Concurrence")
public class Concurrence {

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of Concurrence
     */
    public Concurrence() {
    }

    /**
     * Récupération de la liste des enseignes concurrentes
     */
    @GET
    @Path("/liste")
    @Produces("application/json")
    public Response getListe(@QueryParam("guid") String pguid) {
        JSONObject oResult = new JSONObject();
        JSONObject oParam = new JSONObject();
        CallDB oDb = new CallDB();

        // json des paramètres
        oParam.put("guid", pguid);

        oResult = oDb.jsCallPROCReturnCLOB(
                "p_mportal_concurrence.ws_concurrence_ens_liste",
                oParam);

        if (oDb.CallSuccess()) {
            return RestUtils.MakeResponse(oResult.toString());
        } else {
            return RestUtils.MakeResponse("{}");
        }
    }

    /**
     * retourne les informations d'un concurrent
     */
    @GET
    @Path("/info/{pid_concurrence_enseigne}")
    @Produces("application/json")
    public Response getInfo(
            @PathParam("pid_concurrence_enseigne") int pid_concurrence_enseigne,
            @QueryParam("guid") String pguid) {
        JSONObject oResult = new JSONObject();
        JSONObject oParam = new JSONObject();
        CallDB oDb = new CallDB();

        // json des paramètres
        oParam.put("guid", pguid);
        oParam.put("id_concurrence_enseigne", pid_concurrence_enseigne);

        oResult = oDb.jsCallPROCReturnVARCHAR2(
                "p_mportal_concurrence.ws_concurrence_ens_get",
                oParam);

        if (oDb.CallSuccess()) {
            return RestUtils.MakeResponse(oResult.toString());
        } else {
            return RestUtils.MakeResponse("{}");
        }
    }

    /**
     * enregistre les informations d'un nouveau concurrent
     */
    @GET
    @Path("/nouveau")
    @Produces("application/json")
    public Response getNouveau(
            @QueryParam("guid") String pguid,
            @QueryParam("libelle") String plibelle,
            @QueryParam("site_web") String psite_web) {
        JSONObject oResult = new JSONObject();
        JSONObject oParam = new JSONObject();
        CallDB oDb = new CallDB();

        // json des paramètres
        oParam.put("guid", pguid);
        oParam.put("libelle", plibelle);
        oParam.put("site_web", psite_web);

        oResult = oDb.jsCallPROCReturnVARCHAR2(
                "p_mportal_concurrence.ws_concurrence_ens_new",
                oParam);

        if (oDb.CallSuccess()) {
            return RestUtils.MakeResponse(oResult.toString());
        } else {
            return RestUtils.MakeResponse("{}");
        }
    }

    /**
     * modifie les informations d'un concurrent
     */
    @PUT
    @Path("/modifie/{pid_concurrence_enseigne}")
    @Consumes("application/x-www-form-urlencoded")
    @Produces("application/json")
    public Response putModifie(
            @PathParam("pid_concurrence_enseigne") int pid_concurrence_enseigne,
            @FormParam("guid") String pguid,
            @FormParam("libelle") String plibelle,
            @FormParam("site_web") String psite_web) {
        JSONObject oResult = new JSONObject();
        JSONObject oParam = new JSONObject();
        CallDB oDb = new CallDB();

        // json des paramètres
        oParam.put("guid", pguid);
        oParam.put("id_concurrence_enseigne", pid_concurrence_enseigne);
        oParam.put("libelle", plibelle);
        oParam.put("site_web", psite_web);

        oResult = oDb.jsCallPROCReturnVARCHAR2(
                "p_mportal_concurrence.ws_concurrence_ens_mod",
                oParam);

        if (oDb.CallSuccess()) {
            return RestUtils.MakeResponse(oResult.toString());
        } else {
            return RestUtils.MakeResponse("{}");
        }
    }

    /**
     * supprime un concurrent
     */
    @DELETE
    @Path("/supprime/{pid_concurrence_enseigne}")
    @Consumes("application/x-www-form-urlencoded")
    @Produces("application/json")
    public Response delSupprime(
            @PathParam("pid_concurrence_enseigne") int pid_concurrence_enseigne,
            @FormParam("guid") String pguid) {
        JSONObject oResult = new JSONObject();
        JSONObject oParam = new JSONObject();
        CallDB oDb = new CallDB();

        // json des paramètres
        oParam.put("guid", pguid);
        oParam.put("id_concurrence_enseigne", pid_concurrence_enseigne);

        oResult = oDb.jsCallPROCReturnVARCHAR2(
                "p_mportal_concurrence.ws_concurrence_ens_del",
                oParam);

        if (oDb.CallSuccess()) {
            return RestUtils.MakeResponse(oResult.toString());
        } else {
            return RestUtils.MakeResponse("{}");
        }
    }
}
