public override PagedData ProcessRequest(HttpContext context, CookDBDataContext db)
        {
            try
            {
                System.IO.StreamReader reader = new System.IO.StreamReader(context.Request.InputStream, context.Request.ContentEncoding);
                var jsonSerializer            = new JsonSerializer();



                string             blobParam             = context.Request.Params.Get("jsonBlob");
                JObject            blob                  = (JObject)jsonSerializer.Deserialize(new JsonTextReader(new StringReader(blobParam)));
                string             user_name             = context.Request.Params.Get("user_name");
                string             filter                = context.Request.Params.Get("project_id");
                string             deleteExistingRecords = context.Request.Params.Get("delete_existing");
                string             deleteAdd             = context.Request.Params.Get("delete_add");
                string             deleteChange          = context.Request.Params.Get("delete_change");
                string             deleteRemove          = context.Request.Params.Get("delete_remove");
                byte[]             fileContent           = default(byte[]);
                string             filename              = "";
                HttpRequest        request               = context.Request;
                HttpFileCollection myFileCollection      = request.Files;
                string             fileType              = "";



                if (myFileCollection.Keys[0].ToString().Equals("xls_upload"))
                {
                    int fileLength = myFileCollection[0].ContentLength;
                    fileType = myFileCollection[0].ContentType;
                    if (fileLength > 0)
                    {
                        fileContent = new byte[fileLength];
                        filename    = myFileCollection[0].FileName;

                        if (filename.Substring(filename.LastIndexOf('.')) != ".xls")
                        {
                            return(new PagedData("File not uploaded.  Please upload the file in a .xls format"));
                        }
                        else
                        {
                            //comments += "IS AN xls|" + filename.Substring(filename.LastIndexOf('.')) + "|";
                        }


                        char[] arr           = { '\\', '/' };
                        int    filenameStart = filename.LastIndexOfAny(arr);
                        if (filenameStart > 0)
                        {
                            filename = filename.Substring(filenameStart + 1);
                        }
                        myFileCollection[0].InputStream.Read(fileContent, 0, fileLength);

                        if (filename != null && filename != "" && fileType != "" && filename != "")
                        {
                            try
                            {
                                if (deleteExistingRecords == "true")
                                {
                                    if (deleteAdd == "true")
                                    {
                                        if (db.RoutingRequirements.Count(a => a.project_id.Equals(int.Parse(filter)) && a.type == "add") > 0)
                                        {
                                            var p = db.RoutingRequirements.Where(a => a.project_id.Equals(int.Parse(filter)) && a.type == "add");
                                            db.RoutingRequirements.DeleteAllOnSubmit(p);
                                            db.SubmitChanges();
                                        }
                                    }
                                    if (deleteChange == "true")
                                    {
                                        if (db.RoutingRequirements.Count(a => a.project_id.Equals(int.Parse(filter)) && a.type == "change") > 0)
                                        {
                                            var p = db.RoutingRequirements.Where(a => a.project_id.Equals(int.Parse(filter)) && a.type == "change");
                                            db.RoutingRequirements.DeleteAllOnSubmit(p);
                                            db.SubmitChanges();
                                        }
                                    }
                                    if (deleteRemove == "true")
                                    {
                                        if (db.RoutingRequirements.Count(a => a.project_id.Equals(int.Parse(filter)) && a.type == "remove") > 0)
                                        {
                                            var p = db.RoutingRequirements.Where(a => a.project_id.Equals(int.Parse(filter)) && a.type == "remove");
                                            db.RoutingRequirements.DeleteAllOnSubmit(p);
                                            db.SubmitChanges();
                                        }
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                comments += "Error: Show this Screen to the Cookbook Admins!! Failed in delete routine: " + e.Message + "|" + e.StackTrace + "|" + e.InnerException;
                                throw new Exception(comments);
                            }

                            Workbook  book  = Workbook.Load(new MemoryStream(fileContent));
                            Worksheet sheet = book.Worksheets[0];

                            int  curRow  = 1;
                            bool isEmpty = false;
                            do
                            {
                                RoutingRequirement newReq = new RoutingRequirement();
                                try
                                {
                                    if (sheet.Cells[curRow, 0].IsEmpty)
                                    {
                                        isEmpty = true;
                                    }
                                    else
                                    {
                                        string firstCharInTypeColumn = sheet.Cells[curRow, 0].Value.ToString().Substring(0, 1).ToLower();
                                        if (firstCharInTypeColumn != "a" && firstCharInTypeColumn != "c" && firstCharInTypeColumn != "r")
                                        {
                                            isEmpty = true;
                                        }
                                        else
                                        {
                                            newReq.project_id = int.Parse(filter);

                                            //column 0
                                            if (firstCharInTypeColumn == "A" || firstCharInTypeColumn == "a")
                                            {
                                                newReq.type = "add";
                                            }
                                            else
                                            if (firstCharInTypeColumn == "C" || firstCharInTypeColumn == "c")
                                            {
                                                newReq.type = "change";
                                            }
                                            else
                                            if (firstCharInTypeColumn == "R" || firstCharInTypeColumn == "r")
                                            {
                                                newReq.type = "remove";
                                            }

                                            //column 1
                                            if (sheet.Cells[curRow, 1].Value != null)
                                            {
                                                newReq.dnis = sheet.Cells[curRow, 1].Value.ToString();
                                            }
                                            //column 2
                                            if (newReq.type == "add" || newReq.type == "change")
                                            {
                                                if (sheet.Cells[curRow, 2].Value != null)
                                                {
                                                    newReq.route_to = sheet.Cells[curRow, 2].Value.ToString();
                                                }
                                            }
                                            //column 3
                                            if (newReq.type == "add" || newReq.type == "change")
                                            {
                                                if (sheet.Cells[curRow, 3].Value != null)
                                                {
                                                    newReq.platform = sheet.Cells[curRow, 3].Value.ToString();
                                                }
                                            }
                                            //column 4
                                            if (sheet.Cells[curRow, 4].Value != null)
                                            {
                                                newReq.description = sheet.Cells[curRow, 4].Value.ToString();
                                            }
                                            //column 5
                                            if (newReq.type == "remove" || newReq.type == "change")
                                            {
                                                if (sheet.Cells[curRow, 5].Value != null)
                                                {
                                                    newReq.remove_from = sheet.Cells[curRow, 5].Value.ToString();
                                                }
                                            }
                                            //column 6
                                            if (newReq.type == "change")
                                            {
                                                if (sheet.Cells[curRow, 6].Value != null)
                                                {
                                                    newReq.platform_from = sheet.Cells[curRow, 6].Value.ToString();
                                                }
                                            }
                                            else if (newReq.type == "remove")  //ath design flaw since day 1 -- so just going with it..
                                            {
                                                if (sheet.Cells[curRow, 6].Value != null)
                                                {
                                                    newReq.platform = sheet.Cells[curRow, 6].Value.ToString();
                                                }
                                            }
                                            //column 7
                                            try
                                            {
                                                if (sheet.Cells[curRow, 7].Value != null)
                                                {
                                                    newReq.usan_date = DateTime.FromOADate(Convert.ToDouble(sheet.Cells[curRow, 7].Value.ToString())).ToShortDateString();
                                                }
                                            }
                                            catch (FormatException)
                                            {
                                                if (sheet.Cells[curRow, 7].Value != null)
                                                {
                                                    newReq.usan_date = sheet.Cells[curRow, 7].Value.ToString();
                                                }
                                            }
                                            //column 8
                                            try
                                            {
                                                if (sheet.Cells[curRow, 8].Value != null)
                                                {
                                                    newReq.usan_time = DateTime.FromOADate(Convert.ToDouble(sheet.Cells[curRow, 8].Value.ToString())).ToShortTimeString();
                                                }
                                            }
                                            catch (FormatException)
                                            {
                                                if (sheet.Cells[curRow, 8].Value != null)
                                                {
                                                    newReq.usan_time = sheet.Cells[curRow, 8].Value.ToString();
                                                }
                                            }
                                            //column 9
                                            if (newReq.type == "add" || newReq.type == "change")
                                            {
                                                try
                                                {
                                                    if (sheet.Cells[curRow, 9].Value != null)
                                                    {
                                                        newReq.dnis_date = DateTime.FromOADate(Convert.ToDouble(sheet.Cells[curRow, 9].Value.ToString())).ToShortDateString();
                                                    }
                                                }
                                                catch (FormatException)
                                                {
                                                    if (sheet.Cells[curRow, 9].Value != null)
                                                    {
                                                        newReq.dnis_date = sheet.Cells[curRow, 9].Value.ToString();
                                                    }
                                                }
                                            }
                                            //column 10
                                            if (newReq.type == "add" || newReq.type == "change")
                                            {
                                                try
                                                {
                                                    if (sheet.Cells[curRow, 10].Value != null)
                                                    {
                                                        newReq.dnis_time = DateTime.FromOADate(Convert.ToDouble(sheet.Cells[curRow, 10].Value.ToString())).ToShortTimeString();
                                                    }
                                                }
                                                catch (FormatException)
                                                {
                                                    if (sheet.Cells[curRow, 10].Value != null)
                                                    {
                                                        newReq.dnis_time = sheet.Cells[curRow, 10].Value.ToString();
                                                    }
                                                }
                                            }
                                            //column 11
                                            if (newReq.type == "add" || newReq.type == "change")
                                            {
                                                try
                                                {
                                                    if (sheet.Cells[curRow, 11].Value != null)
                                                    {
                                                        newReq.carrier_date = DateTime.FromOADate(Convert.ToDouble(sheet.Cells[curRow, 11].Value.ToString())).ToShortDateString();
                                                    }
                                                }
                                                catch (FormatException)
                                                {
                                                    if (sheet.Cells[curRow, 11].Value != null)
                                                    {
                                                        newReq.carrier_date = sheet.Cells[curRow, 11].Value.ToString();
                                                    }
                                                }
                                            }
                                            //column 12
                                            if (newReq.type == "add" || newReq.type == "change")
                                            {
                                                try
                                                {
                                                    if (sheet.Cells[curRow, 12].Value != null)
                                                    {
                                                        newReq.carrier_time = DateTime.FromOADate(Convert.ToDouble(sheet.Cells[curRow, 12].Value.ToString())).ToShortTimeString();
                                                    }
                                                }
                                                catch (FormatException)
                                                {
                                                    if (sheet.Cells[curRow, 12].Value != null)
                                                    {
                                                        newReq.carrier_time = sheet.Cells[curRow, 12].Value.ToString();
                                                    }
                                                }
                                            }
                                            //column 13
                                            if (newReq.type == "add")
                                            {
                                                if (sheet.Cells[curRow, 13].Value != null)
                                                {
                                                    newReq.alias = sheet.Cells[curRow, 13].Value.ToString();
                                                }
                                            }
                                            db.RoutingRequirements.InsertOnSubmit(newReq);
                                            db.SubmitChanges();
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    throw new Exception("Error! Show this screen to Cookbook Admins! Failed inside parsing routine: " + e.Message + "|" + e.StackTrace + "|" + e.TargetSite + "|" + e.HelpLink + "|" + e.Source + "|");
                                }
                                curRow++;
                            } while (isEmpty != true);
                            return(new PagedData("Success! Imported " + (curRow - 2) + " rows." + comments));
                        }
                    }
                    else
                    {
                        return(new PagedData("File not uploaded. Uploaded file is empty"));
                    }
                }

                if (fileContent == default(byte[]))
                {
                    return(new PagedData("File not uploaded. Uploaded file is missing"));
                }
            }
            catch (Exception e)
            {
                comments += "Error: Show this Screen to the Cookbook Admins!! " + e.Message + "|" + e.StackTrace + "|" + e.InnerException;
                throw new Exception(comments);
            }
            return(new PagedData(""));
        }
Esempio n. 2
0
        public override PagedData ProcessRequest(HttpContext context, CookDBDataContext db)
        {
            string readOnly = context.Request.Params.Get("read_only");

            if (isNull(readOnly))
            {
                return(new PagedData("read_only flag is expected"));
            }
            if (readOnly == "true" && context.Request.RequestType != "GET")
            {
                return(new PagedData("Read Only"));
            }

            IQueryable <RoutingRequirement> q = db.RoutingRequirements;

            System.IO.StreamReader reader = new System.IO.StreamReader(context.Request.InputStream, context.Request.ContentEncoding);

            var     jsonSerializer = new JsonSerializer();
            JObject blob           = (JObject)jsonSerializer.Deserialize(new JsonTextReader(new StringReader(reader.ReadToEnd())));
            string  user_name      = context.Request.Params.Get("user_name");
            string  permission     = context.Request.Params.Get("permission");
            string  filter         = context.Request.Params.Get("project_id");
            string  intro          = "The Traffic & Routing page's Remove Dnis grid had the following modifications: ";
            string  logBuilder     = "";

            switch (context.Request.RequestType)
            {
            case "GET":
            {
                filter = context.Request.Params.Get("project_id");
                if (!isNull(filter))
                {
                    int id = int.Parse(filter);
                    q = q.Where(a => a.project_id.Equals(id) && a.type.Equals("remove"));          //(smm - not needed) && a.remove_from.Length > 1 && a.route_to.Equals(null));
                }

                return(new PagedData(q.Select(a => new { a.routing_requirements_id, a.project_id, a.dnis, a.remove_from, a.platform, a.description, a.usan_date, a.usan_time, a.type })));
            }

            case "POST":
            {
                if (blob["rows"].GetType() == typeof(JObject))
                {
                    JObject obj = (JObject)blob["rows"];

                    RoutingRequirement record = new RoutingRequirement();
                    try
                    {
                        record.project_id = (int)obj["project_id"];
                    }
                    catch (ArgumentException)
                    {
                        record.project_id = int.Parse(filter);
                    }
                    record.dnis        = (String)obj["dnis"];
                    record.remove_from = (String)obj["remove_from"];
                    if (obj["platform"].GetType() == typeof(JValue))
                    {
                        record.platform = (String)obj["platform"];
                    }
                    else
                    {
                        string platforms = (String)((JArray)obj["platform"])[0];
                        for (int i = 1; i < ((JArray)obj["platform"]).Count; i++)
                        {
                            platforms = platforms + ", " + (String)((JArray)obj["platform"])[i];
                        }
                        record.platform = platforms;
                    }
                    record.description = (String)obj["description"];
                    record.usan_time   = (String)obj["usan_time"];
                    record.usan_date   = (String)obj["usan_date"];
                    record.type        = "remove";   //smm

                    db.RoutingRequirements.InsertOnSubmit(record);
                    db.SubmitChanges();
                    logBuilder = "New row added";
                    //if (permission != "PM")
                    //{
                    intro = (intro + logBuilder);
                    intro = intro.Trim();

                    if (intro.LastIndexOf(";") == intro.Length - 1)
                    {
                        intro = intro.Substring(0, intro.Length - 1);
                    }
                    ChangeLog newLog = new ChangeLog();
                    newLog.project_id  = Convert.ToInt32(filter);
                    newLog.time        = DateTime.Now.ToShortTimeString();
                    newLog.date        = DateTime.Now.ToShortDateString();
                    newLog.tab         = "Traffic & Routing";
                    newLog.user_name   = user_name;
                    newLog.description = intro;
                    if ((!db.ChangeLogs.Contains(newLog)) && (logBuilder.Length > 0))
                    {
                        db.ChangeLogs.InsertOnSubmit(newLog);
                        db.SubmitChanges();
                    }
                    //}
                    return(new PagedData(new { record.routing_requirements_id, record.project_id, record.dnis, record.remove_from, record.platform, record.description, record.usan_date, record.usan_time, record.type }));
                }

                JArray        objs = (JArray)blob["rows"];
                List <Object> list = new List <Object>();
                for (int j = 0; j < objs.Count; j++)
                {
                    RoutingRequirement record = new RoutingRequirement();
                    try
                    {
                        record.project_id = (int)objs[j]["project_id"];
                    }
                    catch (ArgumentException)
                    {
                        record.project_id = int.Parse(filter);
                    }
                    record.dnis        = (String)objs[j]["dnis"];
                    record.remove_from = (String)objs[j]["remove_from"];
                    if (objs[j]["platform"].GetType() == typeof(JValue))
                    {
                        record.platform = (String)objs[j]["platform"];
                    }
                    else
                    {
                        string platforms = (String)((JArray)objs[j]["platform"])[0];
                        for (int i = 1; i < ((JArray)objs[j]["platform"]).Count; i++)
                        {
                            platforms = platforms + ", " + (String)((JArray)objs[j]["platform"])[i];
                        }
                        record.platform = platforms;
                    }
                    record.description = (String)objs[j]["description"];
                    record.usan_date   = (String)objs[j]["usan_date"];
                    record.usan_time   = (String)objs[j]["usan_time"];
                    record.type        = "remove";   //smm

                    db.RoutingRequirements.InsertOnSubmit(record);
                    db.SubmitChanges();

                    list.Add(new { record.routing_requirements_id, record.project_id, record.dnis, record.remove_from, record.platform, record.description, record.usan_date, record.usan_time, record.type });
                }

                return(new PagedData(list));
            }

            case "PUT":
            {
                if (blob["rows"].GetType() == typeof(JObject))
                {
                    JObject obj = (JObject)blob["rows"];

                    RoutingRequirement record = db.RoutingRequirements.Single(a => a.routing_requirements_id.Equals((int)obj["routing_requirements_id"]));
                    if (record.dnis != null && record.dnis.Length > 0)
                    {
                        intro += "Row New Dnis: \"" + record.dnis + "\": ";
                    }
                    else
                    {
                        intro += "Row New Dnis: \"(empty DNIS)\": ";
                    }
                    if (record.dnis != (String)obj["dnis"])
                    {
                        logBuilder += "New Dnis changed from \"" + record.dnis + "\" -> \"" + (String)obj["dnis"] + "\"; ";
                    }
                    record.dnis = (String)obj["dnis"];
                    if (obj["remove_from"] != null)
                    {
                        if (record.remove_from != (String)obj["remove_from"])
                        {
                            logBuilder += "App To Be Removed From changed from \"" + record.remove_from + "\" -> \"" + (String)obj["remove_from"] + "\"; ";
                        }
                        record.remove_from = (String)obj["remove_from"];
                    }
                    //record.platform = (string)obj["platform"];

                    if (obj["platform"].GetType() == typeof(JValue))
                    {
                        if (record.platform != (String)obj["platform"])
                        {
                            logBuilder += "Platform changed from \"" + record.platform + "\" -> \"" + (String)obj["platform"] + "\"; ";
                        }
                        record.platform = (String)obj["platform"];
                    }
                    else
                    {
                        string platforms = (String)((JArray)obj["platform"])[0];
                        for (int i = 1; i < ((JArray)obj["platform"]).Count; i++)
                        {
                            platforms = platforms + ", " + (String)((JArray)obj["platform"])[i];
                        }
                        if (record.platform != platforms)
                        {
                            logBuilder += "Platform changed from \"" + record.platform + "\" -> \"" + platforms + "\"; ";
                        }
                        record.platform = platforms;
                    }
                    if (record.description != (String)obj["description"])
                    {
                        logBuilder += "Description changed from \"" + record.description + "\" -> \"" + (String)obj["description"] + "\"; ";
                    }
                    record.description = (String)obj["description"];
                    if (record.usan_time != (String)obj["usan_time"])
                    {
                        logBuilder += "USAN Prod Routing Time changed from \"" + record.usan_time + "\" -> \"" + (String)obj["usan_time"] + "\"; ";
                    }
                    record.usan_time = (String)obj["usan_time"];
                    if (record.usan_date != (String)obj["usan_date"])
                    {
                        logBuilder += "USAN Prod Routing Date changed from \"" + record.usan_date + "\" -> \"" + (String)obj["usan_date"] + "\"; ";
                    }
                    record.usan_date = (String)obj["usan_date"];
                    record.type      = "remove";     //smm

                    db.SubmitChanges();
                    //if (permission != "PM")
                    //{
                    intro = (intro + logBuilder);
                    intro = intro.Trim();

                    if (intro.LastIndexOf(";") == intro.Length - 1)
                    {
                        intro = intro.Substring(0, intro.Length - 1);
                    }
                    ChangeLog newLog = new ChangeLog();
                    newLog.project_id  = Convert.ToInt32(filter);
                    newLog.time        = DateTime.Now.ToShortTimeString();
                    newLog.date        = DateTime.Now.ToShortDateString();
                    newLog.tab         = "Traffic & Routing";
                    newLog.user_name   = user_name;
                    newLog.description = intro;
                    if ((!db.ChangeLogs.Contains(newLog)) && (logBuilder.Length > 0))
                    {
                        db.ChangeLogs.InsertOnSubmit(newLog);
                        db.SubmitChanges();
                    }
                    //}
                    return(new PagedData(new { record.routing_requirements_id, record.project_id, record.dnis, record.remove_from, record.platform, record.description, record.usan_date, record.usan_time, record.type }));
                }

                JArray        objs = (JArray)blob["rows"];
                List <Object> list = new List <Object>();
                for (int j = 0; j < objs.Count; j++)
                {
                    RoutingRequirement record = db.RoutingRequirements.Single(a => a.routing_requirements_id.Equals((int)objs[j]["routing_requirements_id"]));
                    record.dnis        = (String)objs[j]["dnis"];
                    record.remove_from = (String)objs[j]["remove_from"];
                    if (objs[j]["platform"].GetType() == typeof(JValue))
                    {
                        record.platform = (String)objs[j]["platform"];
                    }
                    else
                    {
                        string platforms = (String)((JArray)objs[j]["platform"])[0];
                        for (int i = 1; i < ((JArray)objs[j]["platform"]).Count; i++)
                        {
                            platforms = platforms + ", " + (String)((JArray)objs[j]["platform"])[i];
                        }
                        record.platform = platforms;
                    }
                    record.description = (String)objs[j]["description"];
                    record.usan_date   = (String)objs[j]["usan_date"];
                    record.usan_time   = (String)objs[j]["usan_time"];
                    record.type        = "remove";   //smm

                    list.Add(new { record.routing_requirements_id, record.project_id, record.dnis, record.remove_from, record.platform, record.description, record.usan_date, record.usan_time, record.type });
                }

                db.SubmitChanges();

                return(new PagedData(list));
            }

            case "DELETE":
            {
                if (blob["rows"].GetType() == typeof(JObject))
                {
                    JObject obj = (JObject)blob["rows"];

                    RoutingRequirement record = db.RoutingRequirements.Single(a => a.routing_requirements_id.Equals((int)obj["routing_requirements_id"]));
                    logBuilder += "Row deleted: New Dnis: \"" + record.dnis + "\"; APP To Be Removed To: \"" + record.route_to + "\"; Platform: \"" + record.platform + "\"; Description: \"" +
                                  record.description + "\"; USAN Prod Routing Date: \"" + record.usan_date + "\"; USAN Prod Routing Time: \"" + record.usan_time + "\"";
                    db.RoutingRequirements.DeleteOnSubmit(record);

                    db.SubmitChanges();
                    //if (permission != "PM")
                    //{
                    intro = (intro + logBuilder);
                    intro = intro.Trim();

                    if (intro.LastIndexOf(";") == intro.Length - 1)
                    {
                        intro = intro.Substring(0, intro.Length - 1);
                    }
                    ChangeLog newLog = new ChangeLog();
                    newLog.project_id  = Convert.ToInt32(filter);
                    newLog.time        = DateTime.Now.ToShortTimeString();
                    newLog.date        = DateTime.Now.ToShortDateString();
                    newLog.tab         = "Traffic & Routing";
                    newLog.user_name   = user_name;
                    newLog.description = intro;
                    if ((!db.ChangeLogs.Contains(newLog)) && (logBuilder.Length > 0))
                    {
                        db.ChangeLogs.InsertOnSubmit(newLog);
                        db.SubmitChanges();
                    }
                    //}
                    return(new PagedData("dnis deleted"));
                }

                JArray objs = (JArray)blob["rows"];
                for (int j = 0; j < objs.Count; j++)
                {
                    RoutingRequirement record = db.RoutingRequirements.Single(a => a.routing_requirements_id.Equals((int)objs[j]["routing_requirements_id"]));
                    db.RoutingRequirements.DeleteOnSubmit(record);
                }

                db.SubmitChanges();

                return(new PagedData("dnises deleted"));
            }

            default:
                return(new PagedData("Unsupported Http Request:  " + context.Request.RequestType + " not recognized"));
            }
        }