Esempio n. 1
0
 public static string add_RolXPerfil(RolXPerfil d)
 {
     return "{" +
         '"' + "IdRolXPerfil" + '"' + ": " + d.IdRolXPerfil.ToString() + ',' +
         '"' + "IdPerfil" + '"' + ": " + d.IdPerfil.ToString() + ',' +
         '"' + "IdRol" + '"' + ": " + d.IdRol.ToString() + ',' +
         '"' + "FechaAsignacion" + '"' + ": " + '"' + Utils.dateToJson(d.FechaAsignacion) + '"' +
         "}";
 }
Esempio n. 2
0
        public ActionResult Create(string nombre, int[] selectedRoles, bool estado= false)
        {
            //Declaraciones Generales para los request
            ASCIIEncoding encoding = new ASCIIEncoding();
            HttpWebRequest webrequest;
            HttpWebResponse webresponse;
            byte[] data;
            Stream newStream;
            ResponseBD u = new ResponseBD();

            //Seleccciono Roles
            List<Rol> p = new List<Rol>();
            data = encoding.GetBytes("");

            webrequest = (HttpWebRequest)WebRequest.Create(Constantes.SelectAll_Rol);
            webrequest.Method = Constantes.PostMethod;
            webrequest.ContentType = Constantes.ContentType;
            webrequest.ContentLength = data.Length;

            newStream = webrequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            webresponse = (HttpWebResponse)webrequest.GetResponse();

            using (var reader = new StreamReader(webresponse.GetResponseStream()))
            {
                JavaScriptSerializer js = new JavaScriptSerializer();
                var objText = reader.ReadToEnd();
                p = (List<Rol>)js.Deserialize(objText, typeof(List<Rol>));
            }

            //Obtener Roles seleccionados
            List<Rol> sp = (List<Rol>)Session["selectedRoles"];

            if (selectedRoles == null)
            {
                //Guardar Perfil
                Perfil per = new Perfil()
                {
                    IdPerfil = 0,
                    Nombre = nombre,
                    Activo = estado,
                    FechaCreacion = DateTime.ParseExact(DateTime.Now.ToString("dd/MM/yyyy"), "dd/MM/yyyy", CultureInfo.InvariantCulture),
                    UltimaModificacion = DateTime.ParseExact(DateTime.Now.ToString("dd/MM/yyyy"), "dd/MM/yyyy", CultureInfo.InvariantCulture),
                };

                data = encoding.GetBytes(JsonSerializer.add_Perfil(per));

                webrequest = (HttpWebRequest)WebRequest.Create(Constantes.Add_Perfil);
                webrequest.Method = Constantes.PostMethod;
                webrequest.ContentType = Constantes.ContentType;
                webrequest.ContentLength = data.Length;

                newStream = webrequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();

                webresponse = (HttpWebResponse)webrequest.GetResponse();

                using (var reader = new StreamReader(webresponse.GetResponseStream()))
                {
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    var objText = reader.ReadToEnd();
                    u = (ResponseBD)js.Deserialize(objText, typeof(ResponseBD));
                }

                if (u.Flujo.Equals(Constantes.OK))
                {
                    int idPerfil = Int32.Parse(u.Mensaje);

                    //Agregar Productos seleccionados

                    int i = 0;

                    foreach (var rol in sp)
                    {
                        RolXPerfil rxp = new RolXPerfil()
                        {
                            IdRol = rol.IdRol,
                            IdPerfil = idPerfil,
                            IdRolXPerfil = 0,
                            FechaAsignacion = DateTime.ParseExact(DateTime.Now.ToString("dd/MM/yyyy"), "dd/MM/yyyy", CultureInfo.InvariantCulture)
                        };

                        data = encoding.GetBytes(JsonSerializer.add_RolXPerfil(rxp));

                        webrequest = (HttpWebRequest)WebRequest.Create(Constantes.Add_RolXPerfil);
                        webrequest.Method = Constantes.PostMethod;
                        webrequest.ContentType = Constantes.ContentType;
                        webrequest.ContentLength = data.Length;

                        newStream = webrequest.GetRequestStream();
                        newStream.Write(data, 0, data.Length);
                        newStream.Close();

                        webresponse = (HttpWebResponse)webrequest.GetResponse();

                        using (var reader = new StreamReader(webresponse.GetResponseStream()))
                        {
                            JavaScriptSerializer js = new JavaScriptSerializer();
                            var objText = reader.ReadToEnd();
                            u = (ResponseBD)js.Deserialize(objText, typeof(ResponseBD));
                        }

                        if (u.Flujo.Equals("ERROR"))
                        {
                            ViewBag.Mensaje = u.Mensaje;
                            ViewBag.Productos = sp;
                            return View(p);
                        }

                        i++;
                    }

                    return RedirectToAction("Index", "Usuario");
                }
                else
                {
                    ViewBag.Mensaje = u.Mensaje;
                    ViewBag.Perfiles = p;
                    return View(sp);
                }
            }

            foreach (var item in selectedRoles)
            {
                Rol rol = new Rol();

                data = encoding.GetBytes(JsonSerializer.selectById_Rol(item));

                webrequest = (HttpWebRequest)WebRequest.Create(Constantes.SelectById_Rol);
                webrequest.Method = Constantes.PostMethod;
                webrequest.ContentType = Constantes.ContentType;
                webrequest.ContentLength = data.Length;

                newStream = webrequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();

                webresponse = (HttpWebResponse)webrequest.GetResponse();

                using (var reader = new StreamReader(webresponse.GetResponseStream()))
                {
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    var objText = reader.ReadToEnd();
                    rol = (Rol)js.Deserialize(objText, typeof(Rol));
                }

                sp.Add(rol);

            }

            foreach (var i in sp)
            {
                p.RemoveAll(s => s.IdRol == i.IdRol);
            }

            ViewBag.Roles = p;

            Session["selectedProducts"] = sp;

            return View(sp);
        }