public override PagedData ProcessRequest(HttpContext context, CookDBDataContext db)
        {
            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())));

            if (!(db.ProjectInformations.Count(c=>c.project_id == (int)blob["project_id"]) > 0))
            {
                return new PagedData("UpdateProjectStatus.ashx requires a valid project ID");
            }

            if (db.StatusTypes.Count(a => a.type.Equals((string)blob["type"])) > 0)
            {
                ProjectStatus status = new ProjectStatus();
                status.project_id = (int)blob["project_id"];
                status.date = (string)blob["date"];
                status.status_type_id = db.StatusTypes.Single(a => a.type.Equals((string)blob["type"])).status_type_id;
                db.ProjectStatus.InsertOnSubmit(status);
                db.SubmitChanges();

                //update project histories
                ProjectHistory history = new ProjectHistory();
                history.project_id = (int)blob["project_id"];
                history.user_name = (string)blob["name"];
                history.date = (string)blob["date"];
                history.description = "Project status changed to \"" + db.StatusTypes.Single(a => a.type.Equals((string)blob["type"])).type + "\"";
                history.status_id = status.project_status_id;
                db.ProjectHistories.InsertOnSubmit(history);
                db.SubmitChanges();

                //update current_project_status
                var currProject = db.ProjectInformations.Single(a => a.project_id.Equals((int)blob["project_id"]));
                currProject.current_project_status = (string)blob["type"];
                db.SubmitChanges();

                return new PagedData("ProjectStatus and ProjectHistory updated", true);
            }
            else
            {
                return new PagedData("Error: Status not found. Please enter status via Status Editor or select an available status.", false);
            }
        }
        public override PagedData ProcessRequest(HttpContext context, CookDBDataContext db)
        {
            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())));

            if (!(db.ProjectInformations.Count(c => c.project_id == (int)blob["project_id"]) > 0))
            {
                return(new PagedData("UpdateProjectStatus.ashx requires a valid project ID"));
            }

            if (db.StatusTypes.Count(a => a.type.Equals((string)blob["type"])) > 0)
            {
                ProjectStatus status = new ProjectStatus();
                status.project_id     = (int)blob["project_id"];
                status.date           = (string)blob["date"];
                status.status_type_id = db.StatusTypes.Single(a => a.type.Equals((string)blob["type"])).status_type_id;
                db.ProjectStatus.InsertOnSubmit(status);
                db.SubmitChanges();

                //update project histories
                ProjectHistory history = new ProjectHistory();
                history.project_id  = (int)blob["project_id"];
                history.user_name   = (string)blob["name"];
                history.date        = (string)blob["date"];
                history.description = "Project status changed to \"" + db.StatusTypes.Single(a => a.type.Equals((string)blob["type"])).type + "\"";
                history.status_id   = status.project_status_id;
                db.ProjectHistories.InsertOnSubmit(history);
                db.SubmitChanges();

                //update current_project_status
                var currProject = db.ProjectInformations.Single(a => a.project_id.Equals((int)blob["project_id"]));
                currProject.current_project_status = (string)blob["type"];
                db.SubmitChanges();

                return(new PagedData("ProjectStatus and ProjectHistory updated", true));
            }
            else
            {
                return(new PagedData("Error: Status not found. Please enter status via Status Editor or select an available status.", false));
            }
        }
		private void detach_ProjectStatus(ProjectStatus entity)
		{
			this.SendPropertyChanging();
			entity.ProjectInformation = null;
		}
 partial void DeleteProjectStatus(ProjectStatus instance);
 partial void UpdateProjectStatus(ProjectStatus instance);
 partial void InsertProjectStatus(ProjectStatus instance);
		private void detach_ProjectStatus(ProjectStatus entity)
		{
			this.SendPropertyChanging();
			entity.StatusType = null;
		}
        public override PagedData ProcessRequest(HttpContext context, CookDBDataContext db)
        {
            System.IO.StreamReader reader   = new System.IO.StreamReader(context.Request.InputStream, context.Request.ContentEncoding);
            var           jsonSerializer    = new JsonSerializer();
            List <String> failedProjects    = new List <string>();
            List <String> succeededProjects = new List <string>();
            List <String> lockedProjects    = new List <string>();
            string        username          = context.Request.Params.Get("user_name");

            switch (context.Request.RequestType)
            {
            case "PUT":
            {
                return(new PagedData("fake sync complete! records should no longer be dirty you cheating bastard", true));
            }

            case "POST":
            {
                String type = "";
                JArray blob = (JArray)jsonSerializer.Deserialize(new JsonTextReader(new StringReader(reader.ReadToEnd())));
                foreach (JObject currentRecord in blob)
                {
                    var currentProject = db.ProjectInformations.Single(a => a.project_id.Equals((Int32)currentRecord["project_id"]));
                    if ((currentProject.locked != true) || (currentProject.locked == true && (DateTime.Now - DateTime.Parse(currentProject.last_time)).TotalSeconds > 75))
                    {
                        //expedite
                        type = "Expedite";
                        if (currentRecord["exp"] != null)
                        {
                            try
                            {
                                if ((String)currentRecord["exp"] == "True")
                                {
                                    if (!(currentProject.expedite == true))
                                    {
                                        currentProject.expedite = true;
                                    }
                                }
                                else if ((String)currentRecord["exp"] == "False")
                                {
                                    if (!(currentProject.expedite == false))
                                    {
                                        currentProject.expedite = false;
                                    }
                                }
                                succeededProjects.Add(currentProject.project_number + "'s " + type);
                            }
                            catch (Exception)
                            {
                                failedProjects.Add(currentProject.project_number + "'s " + type);
                                continue;
                            }
                        }
                        //project name
                        type = "Project Name";
                        if (currentRecord["project_name"] != null)
                        {
                            try
                            {
                                string temp = (String)currentRecord["project_name"];
                                if (temp.ToLower().Trim() != currentProject.project_name.ToLower().Trim())
                                {
                                    currentProject.project_name = (String)currentRecord["project_name"];
                                }
                                succeededProjects.Add(currentProject.project_number + "'s " + type);
                            }
                            catch (Exception)
                            {
                                failedProjects.Add(currentProject.project_number + "'s " + type);
                                continue;
                            }
                        }
                        //status
                        type = "Project Status";
                        if (currentRecord["project_status"] != null)
                        {
                            try
                            {
                                string temp = (String)currentRecord["project_status"];

                                if (db.StatusTypes.Count(a => a.type.Equals((String)currentRecord["project_status"])) > 0)
                                {
                                    ProjectStatus status = new ProjectStatus();
                                    status.project_id     = currentProject.project_id;
                                    status.date           = DateTime.Now.ToString("s");
                                    status.status_type_id = db.StatusTypes.Single(a => a.type.Equals((String)currentRecord["project_status"])).status_type_id;
                                    db.ProjectStatus.InsertOnSubmit(status);
                                    db.SubmitChanges();

                                    //update project histories
                                    ProjectHistory history = new ProjectHistory();
                                    history.project_id  = currentProject.project_id;
                                    history.user_name   = username;
                                    history.date        = DateTime.Now.ToString("s");
                                    history.description = "Project status changed to \"" + db.StatusTypes.Single(a => a.type.Equals((String)currentRecord["project_status"])).type + "\"";
                                    history.status_id   = status.project_status_id;
                                    db.ProjectHistories.InsertOnSubmit(history);
                                    db.SubmitChanges();

                                    //update new current_project_status on pi table
                                    db.ProjectInformations.Single(a => a.project_id.Equals(currentProject.project_id)).current_project_status = (String)currentRecord["project_status"];
                                    db.SubmitChanges();

                                    succeededProjects.Add(currentProject.project_number + "'s " + type);
                                }
                                else
                                {
                                    failedProjects.Add(currentProject.project_number + "'s " + type);
                                    continue;
                                }
                            }
                            catch (Exception)
                            {
                                failedProjects.Add(currentProject.project_number + "'s " + type);
                                continue;
                            }
                        }
                        //business unit
                        type = "Business Unit";
                        if (currentRecord["business_unit"] != null)
                        {
                            try
                            {
                                string temp = (String)currentRecord["business_unit"];
                                if (db.BusinessUnits.Count(a => a.name.Equals(temp)) > 0)
                                {
                                    currentProject.primary_business_unit = temp;
                                }
                                else
                                {
                                    failedProjects.Add(currentProject.project_number + "'s " + type);
                                    continue;
                                }
                                succeededProjects.Add(currentProject.project_number + "'s " + type);
                            }
                            catch (Exception)
                            {
                                failedProjects.Add(currentProject.project_number + "'s " + type);
                                continue;
                            }
                        }
                        //TC
                        type = "TC";
                        if (currentRecord["tc"] != null)
                        {
                            try
                            {
                                string[] names = ((String)currentRecord["tc"]).Split(',');
                                IQueryable <ProjectContact> q = db.ProjectContacts.Where(a => a.project_id.Equals(currentProject.project_id) && a.type.Equals("USAN TC"));
                                foreach (String currName in names)
                                {
                                    if (db.Contacts.Count(a => a.name.ToLower().Trim().Equals(currName.ToLower().Trim())) > 0)
                                    {
                                        int            contact_id = db.Contacts.First(a => a.name.ToLower().Trim().Equals(currName.ToLower().Trim())).contact_id;
                                        ProjectContact contact    = new ProjectContact();
                                        contact.Contact    = db.Contacts.Single(a => a.contact_id.Equals(contact_id));
                                        contact.project_id = currentProject.project_id;
                                        contact.type       = "USAN TC";
                                        db.ProjectContacts.InsertOnSubmit(contact);
                                    }
                                }
                                if (db.ProjectContacts.Count(a => a.project_id.Equals(currentProject.project_id) && a.type.Equals("USAN TC")) > 0)
                                {
                                    var oldRecsToDelete = db.ProjectContacts.Where(a => a.project_id.Equals(currentProject.project_id) && a.type.Equals("USAN TC"));
                                    db.ProjectContacts.DeleteAllOnSubmit(oldRecsToDelete);
                                }
                                db.SubmitChanges();
                                succeededProjects.Add(currentProject.project_number + "'s " + type);
                            }
                            catch (Exception e)
                            {
                                failedProjects.Add(currentProject.project_number + "'s " + type + e.ToString());
                                continue;
                            }
                        }
                        //PM
                        type = "PM";
                        if (currentRecord["pm"] != null)
                        {
                            try
                            {
                                string[] names = ((String)currentRecord["pm"]).Split(',');
                                IQueryable <ProjectContact> q = db.ProjectContacts.Where(a => a.project_id.Equals(currentProject.project_id) && a.type.Equals("USAN Dev PM"));
                                foreach (String currName in names)
                                {
                                    if (db.Contacts.Count(a => a.name.ToLower().Trim().Equals(currName.ToLower().Trim())) > 0)
                                    {
                                        int            contact_id = db.Contacts.First(a => a.name.ToLower().Trim().Equals(currName.ToLower().Trim())).contact_id;
                                        ProjectContact contact    = new ProjectContact();
                                        contact.Contact    = db.Contacts.Single(a => a.contact_id.Equals(contact_id));
                                        contact.project_id = currentProject.project_id;
                                        contact.type       = "USAN Dev PM";
                                        db.ProjectContacts.InsertOnSubmit(contact);
                                    }
                                }
                                if (db.ProjectContacts.Count(a => a.project_id.Equals(currentProject.project_id) && a.type.Equals("USAN Dev PM")) > 0)
                                {
                                    var oldRecsToDelete = db.ProjectContacts.Where(a => a.project_id.Equals(currentProject.project_id) && a.type.Equals("USAN Dev PM"));
                                    db.ProjectContacts.DeleteAllOnSubmit(oldRecsToDelete);
                                }
                                db.SubmitChanges();
                                succeededProjects.Add(currentProject.project_number + "'s " + type);
                            }
                            catch (Exception e)
                            {
                                failedProjects.Add(currentProject.project_number + "'s " + type + e.ToString());
                                continue;
                            }
                        }
                        //flow
                        type = "Flow";
                        if (currentRecord["flow"] != null)
                        {
                            try
                            {
                                if ((String)currentRecord["flow"] == "N/A")
                                {
                                    currentProject.doc_visio = "0";
                                    currentProject.doc_vui   = "0";
                                }
                                else if ((String)currentRecord["flow"] == "Visio")
                                {
                                    if (currentProject.doc_visio != "1" && currentProject.doc_visio != "2" && currentProject.doc_visio != "3")
                                    {
                                        currentProject.doc_visio = "1";
                                    }
                                    if (!(currentProject.doc_vui == "0"))
                                    {
                                        currentProject.doc_vui = "0";
                                    }
                                }
                                else if ((String)currentRecord["flow"] == "VUI")
                                {
                                    if (!(currentProject.doc_visio == "0"))
                                    {
                                        currentProject.doc_visio = "0";
                                    }
                                    if (currentProject.doc_vui != "1" && currentProject.doc_vui != "2" && currentProject.doc_vui != "3")
                                    {
                                        currentProject.doc_vui = "1";
                                    }
                                }
                                else if ((String)currentRecord["flow"] == "VUI/Visio")
                                {
                                    if (currentProject.doc_visio == "0" || currentProject.doc_visio == null)
                                    {
                                        currentProject.doc_visio = "1";
                                    }
                                    if (currentProject.doc_vui == "0" || currentProject.doc_vui == null)
                                    {
                                        currentProject.doc_vui = "1";
                                    }
                                }
                                succeededProjects.Add(currentProject.project_number + "'s " + type);
                            }
                            catch (Exception)
                            {
                                failedProjects.Add(currentProject.project_number + "'s " + type);
                                continue;
                            }
                        }
                        //rfq recd
                        type = "RFQ Rec\'d";
                        if (currentRecord["rfq_recd"] != null)
                        {
                            try
                            {
                                string temp = (String)currentRecord["rfq_recd"];
                                if (temp.ToLower().Trim() != currentProject.rfq_loe_recv_date.ToLower().Trim())
                                {
                                    currentProject.rfq_loe_recv_date = temp;
                                }
                                succeededProjects.Add(currentProject.project_number + "'s " + type);
                            }
                            catch (Exception)
                            {
                                failedProjects.Add(currentProject.project_number + "'s " + type);
                                continue;
                            }
                        }
                        //quote due
                        type = "Quote Due";
                        if (currentRecord["quote_due"] != null)
                        {
                            try
                            {
                                string temp = (String)currentRecord["quote_due"];
                                if (temp.ToLower().Trim() != currentProject.quote_loe_due_date.ToLower().Trim())
                                {
                                    currentProject.quote_loe_due_date = temp;
                                }
                                succeededProjects.Add(currentProject.project_number + "'s " + type);
                            }
                            catch (Exception)
                            {
                                failedProjects.Add(currentProject.project_number + "'s " + type);
                                continue;
                            }
                        }
                        //req uat
                        type = "Req UAT";
                        if (currentRecord["req_uat"] != null)
                        {
                            try
                            {
                                string temp = (String)currentRecord["req_uat"];
                                if (temp.ToLower().Trim() != currentProject.requested_uat_date.ToLower().Trim())
                                {
                                    currentProject.requested_uat_date = temp;
                                }
                                succeededProjects.Add(currentProject.project_number + "'s " + type);
                            }
                            catch (Exception)
                            {
                                failedProjects.Add(currentProject.project_number + "'s " + type);
                                continue;
                            }
                        }
                        //req uat
                        type = "Req PROD";
                        if (currentRecord["req_prod"] != null)
                        {
                            try
                            {
                                string temp = (String)currentRecord["req_prod"];
                                if (temp.ToLower().Trim() != currentProject.requested_prod_date.ToLower().Trim())
                                {
                                    currentProject.requested_prod_date = temp;
                                }
                                succeededProjects.Add(currentProject.project_number + "'s " + type);
                            }
                            catch (Exception)
                            {
                                failedProjects.Add(currentProject.project_number + "'s " + type);
                                continue;
                            }
                        }
                    }
                    else
                    {
                        if (!lockedProjects.Contains(currentProject.project_number))
                        {
                            lockedProjects.Add(currentProject.project_number);
                        }
                        continue;
                    }
                    db.SubmitChanges();
                }
                break;
            }

            default:
                return(new PagedData("Error: Unsupported Http Request:  " + context.Request.RequestType + " not recognized", false));
            }//endswitch


            ProjectReportViewReturn returnString = new ProjectReportViewReturn();

            for (int i = 0; i < succeededProjects.Count; i++)
            {
                if (i + 1 < succeededProjects.Count)
                {
                    returnString.succeededProjects += succeededProjects[i] + "; ";
                }
                else
                {
                    returnString.succeededProjects += succeededProjects[i];
                }
            }
            for (int i = 0; i < failedProjects.Count; i++)
            {
                if (i + 1 < failedProjects.Count)
                {
                    returnString.failedProjects += failedProjects[i] + "; ";
                }
                else
                {
                    returnString.failedProjects += failedProjects[i];
                }
            }
            for (int i = 0; i < lockedProjects.Count; i++)
            {
                if (i + 1 < lockedProjects.Count)
                {
                    returnString.lockedProjects += lockedProjects[i] + "; ";
                }
                else
                {
                    returnString.lockedProjects += lockedProjects[i];
                }
            }
            return(new PagedData(returnString, true));
        }
        public override PagedData ProcessRequest(HttpContext context, CookDBDataContext db)
        {
            System.IO.StreamReader reader = new System.IO.StreamReader(context.Request.InputStream, context.Request.ContentEncoding);
            var jsonSerializer = new JsonSerializer();
            List<String> failedProjects = new List<string>();
            List<String> succeededProjects = new List<string>();
            List<String> lockedProjects = new List<string>();
            string username = context.Request.Params.Get("user_name");
            switch (context.Request.RequestType)
            {
                case "PUT":
                    {
                        return new PagedData("fake sync complete! records should no longer be dirty you cheating bastard", true);
                    }
                case "POST":
                    {
                        String type = "";
                        JArray blob = (JArray)jsonSerializer.Deserialize(new JsonTextReader(new StringReader(reader.ReadToEnd())));
                        foreach (JObject currentRecord in blob)
                        {
                            var currentProject = db.ProjectInformations.Single(a => a.project_id.Equals((Int32)currentRecord["project_id"]));
                            if ((currentProject.locked != true) || (currentProject.locked == true && (DateTime.Now - DateTime.Parse(currentProject.last_time)).TotalSeconds > 75))
                            {
                                //expedite
                                type = "Expedite";
                                if (currentRecord["exp"] != null)
                                {
                                    try
                                    {
                                        if ((String)currentRecord["exp"] == "True")
                                        {
                                            if (!(currentProject.expedite == true))
                                            {
                                                currentProject.expedite = true;
                                            }
                                        }
                                        else if ((String)currentRecord["exp"] == "False")
                                        {
                                            if (!(currentProject.expedite == false))
                                            {
                                                currentProject.expedite = false;
                                            }
                                        }
                                        succeededProjects.Add(currentProject.project_number + "'s " + type);
                                    }
                                    catch (Exception)
                                    {
                                        failedProjects.Add(currentProject.project_number + "'s " + type);
                                        continue;
                                    }
                                }
                                //project name
                                type = "Project Name";
                                if (currentRecord["project_name"] != null)
                                {
                                    try
                                    {
                                        string temp = (String)currentRecord["project_name"];
                                        if (temp.ToLower().Trim() != currentProject.project_name.ToLower().Trim())
                                        {
                                            currentProject.project_name = (String)currentRecord["project_name"];
                                        }
                                        succeededProjects.Add(currentProject.project_number + "'s " + type);
                                    }
                                    catch (Exception)
                                    {
                                        failedProjects.Add(currentProject.project_number + "'s " + type);
                                        continue;
                                    }
                                }
                                //status
                                type = "Project Status";
                                if (currentRecord["project_status"] != null)
                                {
                                    try
                                    {
                                        string temp = (String)currentRecord["project_status"];

                                        if (db.StatusTypes.Count(a => a.type.Equals((String)currentRecord["project_status"])) > 0)
                                        {
                                            ProjectStatus status = new ProjectStatus();
                                            status.project_id = currentProject.project_id;
                                            status.date = DateTime.Now.ToString("s");
                                            status.status_type_id = db.StatusTypes.Single(a => a.type.Equals((String)currentRecord["project_status"])).status_type_id;
                                            db.ProjectStatus.InsertOnSubmit(status);
                                            db.SubmitChanges();

                                            //update project histories
                                            ProjectHistory history = new ProjectHistory();
                                            history.project_id = currentProject.project_id;
                                            history.user_name = username;
                                            history.date = DateTime.Now.ToString("s");
                                            history.description = "Project status changed to \"" + db.StatusTypes.Single(a => a.type.Equals((String)currentRecord["project_status"])).type + "\"";
                                            history.status_id = status.project_status_id;
                                            db.ProjectHistories.InsertOnSubmit(history);
                                            db.SubmitChanges();

                                            //update new current_project_status on pi table
                                            db.ProjectInformations.Single(a => a.project_id.Equals(currentProject.project_id)).current_project_status = (String)currentRecord["project_status"];
                                            db.SubmitChanges();

                                            succeededProjects.Add(currentProject.project_number + "'s " + type);
                                        }
                                        else
                                        {
                                            failedProjects.Add(currentProject.project_number + "'s " + type);
                                            continue;
                                        }
                                    }
                                    catch (Exception)
                                    {
                                        failedProjects.Add(currentProject.project_number + "'s " + type);
                                        continue;
                                    }
                                }
                                //business unit
                                type = "Business Unit";
                                if (currentRecord["business_unit"] != null)
                                {
                                    try
                                    {
                                        string temp = (String)currentRecord["business_unit"];
                                        if (db.BusinessUnits.Count(a => a.name.Equals(temp)) > 0)
                                        {
                                            currentProject.primary_business_unit = temp;
                                        }
                                        else
                                        {
                                            failedProjects.Add(currentProject.project_number + "'s " + type);
                                            continue;
                                        }
                                        succeededProjects.Add(currentProject.project_number + "'s " + type);
                                    }
                                    catch (Exception)
                                    {
                                        failedProjects.Add(currentProject.project_number + "'s " + type);
                                        continue;
                                    }
                                }
                                //TC
                                type = "TC";
                                if (currentRecord["tc"] != null)
                                {
                                    try
                                    {
                                        string[] names = ((String)currentRecord["tc"]).Split(',');
                                        IQueryable<ProjectContact> q = db.ProjectContacts.Where(a => a.project_id.Equals(currentProject.project_id) && a.type.Equals("USAN TC"));
                                        foreach (String currName in names)
                                        {
                                            if (db.Contacts.Count(a => a.name.ToLower().Trim().Equals(currName.ToLower().Trim())) > 0)
                                            {
                                                int contact_id = db.Contacts.First(a => a.name.ToLower().Trim().Equals(currName.ToLower().Trim())).contact_id;
                                                ProjectContact contact = new ProjectContact();
                                                contact.Contact = db.Contacts.Single(a => a.contact_id.Equals(contact_id));
                                                contact.project_id = currentProject.project_id;
                                                contact.type = "USAN TC";
                                                db.ProjectContacts.InsertOnSubmit(contact);
                                            }
                                        }
                                        if (db.ProjectContacts.Count(a => a.project_id.Equals(currentProject.project_id) && a.type.Equals("USAN TC")) > 0)
                                        {
                                            var oldRecsToDelete = db.ProjectContacts.Where(a => a.project_id.Equals(currentProject.project_id) && a.type.Equals("USAN TC"));
                                            db.ProjectContacts.DeleteAllOnSubmit(oldRecsToDelete);
                                        }
                                        db.SubmitChanges();
                                        succeededProjects.Add(currentProject.project_number + "'s " + type);
                                    }
                                    catch (Exception e)
                                    {
                                        failedProjects.Add(currentProject.project_number + "'s " + type + e.ToString());
                                        continue;
                                    }
                                }
                                //PM
                                type = "PM";
                                if (currentRecord["pm"] != null)
                                {
                                    try
                                    {
                                        string[] names = ((String)currentRecord["pm"]).Split(',');
                                        IQueryable<ProjectContact> q = db.ProjectContacts.Where(a => a.project_id.Equals(currentProject.project_id) && a.type.Equals("USAN Dev PM"));
                                        foreach (String currName in names)
                                        {
                                            if (db.Contacts.Count(a => a.name.ToLower().Trim().Equals(currName.ToLower().Trim())) > 0)
                                            {
                                                int contact_id = db.Contacts.First(a => a.name.ToLower().Trim().Equals(currName.ToLower().Trim())).contact_id;
                                                ProjectContact contact = new ProjectContact();
                                                contact.Contact = db.Contacts.Single(a => a.contact_id.Equals(contact_id));
                                                contact.project_id = currentProject.project_id;
                                                contact.type = "USAN Dev PM";
                                                db.ProjectContacts.InsertOnSubmit(contact);
                                            }
                                        }
                                        if (db.ProjectContacts.Count(a => a.project_id.Equals(currentProject.project_id) && a.type.Equals("USAN Dev PM")) > 0)
                                        {
                                            var oldRecsToDelete = db.ProjectContacts.Where(a => a.project_id.Equals(currentProject.project_id) && a.type.Equals("USAN Dev PM"));
                                            db.ProjectContacts.DeleteAllOnSubmit(oldRecsToDelete);
                                        }
                                        db.SubmitChanges();
                                        succeededProjects.Add(currentProject.project_number + "'s " + type);
                                    }
                                    catch (Exception e)
                                    {
                                        failedProjects.Add(currentProject.project_number + "'s " + type + e.ToString());
                                        continue;
                                    }
                                }
                                //flow
                                type = "Flow";
                                if (currentRecord["flow"] != null)
                                {
                                    try
                                    {
                                        if ((String)currentRecord["flow"] == "N/A")
                                        {
                                            currentProject.doc_visio = "0";
                                            currentProject.doc_vui = "0";
                                        }
                                        else if ((String)currentRecord["flow"] == "Visio")
                                        {
                                            if (currentProject.doc_visio != "1" && currentProject.doc_visio != "2" && currentProject.doc_visio != "3")
                                            {
                                                currentProject.doc_visio = "1";
                                            }
                                            if (!(currentProject.doc_vui == "0"))
                                            {
                                                currentProject.doc_vui = "0";
                                            }
                                        }
                                        else if ((String)currentRecord["flow"] == "VUI")
                                        {
                                            if (!(currentProject.doc_visio == "0"))
                                            {
                                                currentProject.doc_visio = "0";
                                            }
                                            if (currentProject.doc_vui != "1" && currentProject.doc_vui != "2" && currentProject.doc_vui != "3")
                                            {
                                                currentProject.doc_vui = "1";
                                            }
                                        }
                                        else if ((String)currentRecord["flow"] == "VUI/Visio")
                                        {
                                            if (currentProject.doc_visio == "0" || currentProject.doc_visio == null)
                                            {
                                                currentProject.doc_visio = "1";
                                            }
                                            if (currentProject.doc_vui == "0" || currentProject.doc_vui == null)
                                            {
                                                currentProject.doc_vui = "1";
                                            }
                                        }
                                        succeededProjects.Add(currentProject.project_number + "'s " + type);
                                    }
                                    catch (Exception)
                                    {
                                        failedProjects.Add(currentProject.project_number + "'s " + type);
                                        continue;
                                    }
                                }
                                //rfq recd
                                type = "RFQ Rec\'d";
                                if (currentRecord["rfq_recd"] != null)
                                {
                                    try
                                    {
                                        string temp = (String)currentRecord["rfq_recd"];
                                        if (temp.ToLower().Trim() != currentProject.rfq_loe_recv_date.ToLower().Trim())
                                        {
                                            currentProject.rfq_loe_recv_date = temp;
                                        }
                                        succeededProjects.Add(currentProject.project_number + "'s " + type);
                                    }
                                    catch (Exception)
                                    {
                                        failedProjects.Add(currentProject.project_number + "'s " + type);
                                        continue;
                                    }
                                }
                                //quote due
                                type = "Quote Due";
                                if (currentRecord["quote_due"] != null)
                                {
                                    try
                                    {
                                        string temp = (String)currentRecord["quote_due"];
                                        if (temp.ToLower().Trim() != currentProject.quote_loe_due_date.ToLower().Trim())
                                        {
                                            currentProject.quote_loe_due_date = temp;
                                        }
                                        succeededProjects.Add(currentProject.project_number + "'s " + type);
                                    }
                                    catch (Exception)
                                    {
                                        failedProjects.Add(currentProject.project_number + "'s " + type);
                                        continue;
                                    }
                                }
                                //req uat
                                type = "Req UAT";
                                if (currentRecord["req_uat"] != null)
                                {
                                    try
                                    {
                                        string temp = (String)currentRecord["req_uat"];
                                        if (temp.ToLower().Trim() != currentProject.requested_uat_date.ToLower().Trim())
                                        {
                                            currentProject.requested_uat_date = temp;
                                        }
                                        succeededProjects.Add(currentProject.project_number + "'s " + type);
                                    }
                                    catch (Exception)
                                    {
                                        failedProjects.Add(currentProject.project_number + "'s " + type);
                                        continue;
                                    }
                                }
                                //req uat
                                type = "Req PROD";
                                if (currentRecord["req_prod"] != null)
                                {
                                    try
                                    {
                                        string temp = (String)currentRecord["req_prod"];
                                        if (temp.ToLower().Trim() != currentProject.requested_prod_date.ToLower().Trim())
                                        {
                                            currentProject.requested_prod_date = temp;
                                        }
                                        succeededProjects.Add(currentProject.project_number + "'s " + type);
                                    }
                                    catch (Exception)
                                    {
                                        failedProjects.Add(currentProject.project_number + "'s " + type);
                                        continue;
                                    }
                                }
                            }
                            else
                            {
                                if (!lockedProjects.Contains(currentProject.project_number))
                                {
                                    lockedProjects.Add(currentProject.project_number);
                                }
                                continue;
                            }
                            db.SubmitChanges();
                        }
                        break;
                    }
                default:
                    return new PagedData("Error: Unsupported Http Request:  " + context.Request.RequestType + " not recognized", false);
            }//endswitch

            
            ProjectReportViewReturn returnString = new ProjectReportViewReturn();

            for (int i = 0; i < succeededProjects.Count; i++)
            {
                if (i + 1 < succeededProjects.Count)
                {
                    returnString.succeededProjects += succeededProjects[i] + "; ";
                }
                else
                {
                    returnString.succeededProjects += succeededProjects[i];
                }
            }
            for (int i = 0; i < failedProjects.Count; i++)
            {
                if (i + 1 < failedProjects.Count)
                {
                    returnString.failedProjects += failedProjects[i] + "; ";
                }
                else
                {
                    returnString.failedProjects += failedProjects[i];
                }
            }
            for (int i = 0; i < lockedProjects.Count; i++)
            {
                if (i + 1 < lockedProjects.Count)
                {
                    returnString.lockedProjects += lockedProjects[i] + "; ";
                }
                else
                {
                    returnString.lockedProjects += lockedProjects[i];
                }
            }
            return new PagedData(returnString, true);
        }
        public override PagedData ProcessRequest(HttpContext context, CookDBDataContext db)
        {
            IQueryable<ProjectHistory> q = db.ProjectHistories;
            string filter = context.Request.Params.Get("project_id");
            if (!isNull(filter))
            {
                q = q.Where(a => a.project_id == int.Parse(filter));
            }
            else { return new PagedData("GetProjectHistory expects a project_id"); }

            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");
            }

            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())));


            switch (context.Request.RequestType)
            {
                case "GET":
                    {
                        return new PagedData(q.Select(a => new { a.project_history_id, a.project_id, a.description, a.date, a.user_name }));
                    }
                case "POST":
                    {
                        if (blob["rows"].GetType() == typeof(JObject))
                        {
                            JObject obj = (JObject)blob["rows"];

                            ProjectHistory record = new ProjectHistory();
                            record.project_id = int.Parse(filter);
                            record.description = (string)obj["description"];

                            if ((String)obj["date"] != null)
                            {
                                record.date = (string)obj["date"];
                            }
                            else
                            {
                                //default to today
                                record.date = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
                            }
                            record.user_name = (string)obj["user_name"];

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

                            return new PagedData(new { record.project_history_id, record.project_id, record.description, record.date, record.user_name });  //JsonConvert.SerializeObject(newApp));
                        }

                        JArray objs = (JArray)blob["rows"];
                        List<Object> list = new List<Object>();
                        for (int j = 0; j < objs.Count; j++)
                        {
                            ProjectHistory record = new ProjectHistory();
                            record.project_id = int.Parse(filter);
                            record.description = (string)objs[j]["description"];
                            if ((String)objs[j]["date"] != null)
                            {
                                record.date = (string)objs[j]["date"];
                            }
                            else
                            {
                                //default to today
                                record.date = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
                            }
                            record.user_name = (string)objs[j]["user_name"];

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

                            list.Add(new { record.project_history_id, record.project_id, record.description, record.date, record.user_name });
                        }

                        return new PagedData(list);
                    }
                case "PUT":
                    {
                        if (blob["rows"].GetType() == typeof(JObject))
                        {
                            JObject obj = (JObject)blob["rows"];

                            ProjectHistory record = db.ProjectHistories.Single(a => a.project_history_id.Equals((int)obj["project_history_id"]));
                            record.description = (string)obj["description"];
                            if ((String)obj["date"] != null)
                            {
                                record.date = (string)obj["date"];
                                //try to update corresponding status object's date
                                if (db.ProjectStatus.Count(k => k.project_status_id == record.status_id) > 0)
                                {
                                    try
                                    {
                                        ProjectStatus statusRecord = db.ProjectStatus.First(k => k.project_status_id == record.status_id);
                                        statusRecord.date = (string)obj["date"];
                                    }
                                    catch (Exception)
                                    {
                                        //do nothing if error, just don't want to choke the handler
                                    }
                                }
                            }
                            else
                            {
                                //default to today
                                record.date = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
                            }
                            if (obj["user_name"] != null) { record.user_name = (string)obj["user_name"]; }

                            db.SubmitChanges();

                            return new PagedData(new { record.project_history_id, record.project_id, record.description, record.date, record.user_name });
                        }

                        JArray objs = (JArray)blob["rows"];
                        List<Object> list = new List<Object>();
                        for (int j = 0; j < objs.Count; j++)
                        {
                            ProjectHistory record = db.ProjectHistories.Single(a => a.project_history_id.Equals((int)objs[j]["project_history_id"]));
                            record.description = (string)objs[j]["description"];
                            if ((String)objs[j]["date"] != null)
                            {
                                record.date = (string)objs[j]["date"];
                                //try to update corresponding status object's date
                                if (db.ProjectStatus.Count(k => k.project_status_id == record.status_id) > 0)
                                {
                                    try
                                    {
                                        ProjectStatus statusRecord = db.ProjectStatus.First(k => k.project_status_id == record.status_id);
                                        statusRecord.date = (string)objs[j]["date"];
                                    }
                                    catch (Exception)
                                    {
                                        //do nothing if error, just don't want to choke the handler
                                    }
                                }
                            }
                            else
                            {
                                //default to today
                                record.date = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
                            }
                            if (objs[j]["user_name"] != null) { record.user_name = (string)objs[j]["user_name"]; }

                            db.SubmitChanges();
                            list.Add(record);
                        }

                        return new PagedData(list);
                    }
                case "DELETE":
                    {
                        if (blob["rows"].GetType() == typeof(JObject))
                        {
                            JObject obj = (JObject)blob["rows"];

                            string status = "";
                            ProjectHistory record = db.ProjectHistories.Single(a => a.project_history_id.Equals((int)obj["project_history_id"]));
                            var deletedRecordStatusID = record.status_id;
                            db.ProjectHistories.DeleteOnSubmit(record);
                            db.SubmitChanges();

                            if (deletedRecordStatusID != null)
                            {
                                if (db.ProjectStatus.Count(k => k.project_status_id == deletedRecordStatusID) > 0)
                                {
                                    try
                                    {
                                        ProjectStatus statusRecord = db.ProjectStatus.First(k => k.project_status_id == record.status_id);
                                        db.ProjectStatus.DeleteOnSubmit(statusRecord);
                                        db.SubmitChanges();

                                        string newCurrentStatus = "";
                                        try
                                        {
                                            List<ProjectStatus> sortedStatuses = db.ProjectStatus.Where(x => x.project_id == int.Parse(filter)).OrderByDescending(o => o.date).Take(1).ToList();
                                            newCurrentStatus = "new status_id to use as current: [" + sortedStatuses[0].project_status_id + "]";
                                        }
                                        catch (Exception)
                                        {

                                        }
                                        
                                        
                                        

                                        status += " corresponding Status deleted| " + newCurrentStatus;
                                    }
                                    catch (Exception)
                                    {
                                        status += " corresponding Status deletion error";
                                        //do nothing if error, just don't want to choke the handler
                                    }
                                }
                                else
                                {
                                    status += " corresponding Status not found";
                                }
                            }
                            else
                            {
                                status += " corresponding Status is null";
                            }
                            return new PagedData("deleted single ProjectHistory entry succesfully."+status);
                        }

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

                        db.SubmitChanges();

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