partial void DeleteDocumentationReq(DocumentationReq instance);
 partial void UpdateDocumentationReq(DocumentationReq instance);
 partial void InsertDocumentationReq(DocumentationReq instance);
		private void detach_DocumentationReqs(DocumentationReq entity)
		{
			this.SendPropertyChanging();
			entity.ProjectInformation = null;
		}
        public override PagedData ProcessRequest(HttpContext context, CookDBDataContext db)
        {
            IQueryable<Application> q = db.Applications;

            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":
                    {
                        string filter = context.Request.Params.Get("name");
                        if (!isNull(filter))
                        {
                            q = q.Where(a => a.name.IndexOf(filter) != -1);
                        }

                        return new PagedData(q.Select(a => new { a.applications_id, a.name, a.base_name, a.Product, a.Division, a.Platform, a.ServiceID }));
                    }
                case "POST":
                    {
                        if (blob["rows"].GetType() == typeof(JObject))
                        {
                            JObject app = (JObject)blob["rows"];

                            Application newApp = new Application();
                            newApp.name = (string)app["name"];
                            newApp.base_name = (string)app["base_name"];
                            newApp.Product = app["Product"] == null ? "" : (string)app["Product"];
                            newApp.Division = app["Division"] == null ? "" : (string)app["Division"];
                            newApp.Platform = app["Platform"] == null ? "" : (string)app["Platform"];
                            newApp.ServiceID = app["ServiceID"] == null ? "" : (string)app["ServiceID"];

                            db.Applications.InsertOnSubmit(newApp);
                            db.SubmitChanges();

                            return new PagedData(newApp);  //JsonConvert.SerializeObject(newApp));
                        }

                        JArray apps = (JArray)blob["rows"];
                        List<Application> list = new List<Application>();
                        for (int j = 0; j < apps.Count; j++)
                        {        
                            Application newApp = new Application();
                            newApp.name = (string)apps[j]["name"];
                            newApp.base_name = (string)apps[j]["base_name"];
                            newApp.Product = apps[j]["Product"] == null ? "" : (string)apps[j]["Product"];
                            newApp.Division = apps[j]["Division"] == null ? "" : (string)apps[j]["Division"];
                            newApp.Platform = apps[j]["Platform"] == null ? "" : (string)apps[j]["Platform"];
                            newApp.ServiceID = apps[j]["ServiceID"] == null ? "" : (string)apps[j]["ServiceID"];

                            db.Applications.InsertOnSubmit(newApp);
                            list.Add(newApp);
                        }

                        db.SubmitChanges();

                        DocumentationReq doc = new DocumentationReq();


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

                            Application app2 = db.Applications.Single(a => a.applications_id.Equals((int)app["applications_id"]));
                            app2.base_name = (string)app["base_name"];
                            app2.name = (string)app["name"];
                            app2.Product = app["Product"] == null ? "" : (string)app["Product"];
                            app2.Division = app["Division"] == null ? "" : (string)app["Division"];
                            app2.Platform = app["Platform"] == null ? "" : (string)app["Platform"];
                            app2.ServiceID = app["ServiceID"] == null ? "" : (string)app["ServiceID"];

                            db.SubmitChanges();

                            return new PagedData(app2);
                        }

                        JArray apps = (JArray)blob["rows"];
                        List<Application> list = new List<Application>();
                        for (int j = 0; j < apps.Count; j++)
                        {
                            Application app2 = db.Applications.Single(a => a.applications_id.Equals((int)apps[j]["applications_id"]));
                            app2.base_name = (string)apps[j]["base_name"];
                            app2.name = (string)apps[j]["name"];
                            app2.Product = apps[j]["Product"] == null ? "" : (string)apps[j]["Product"];
                            app2.Division = apps[j]["Division"] == null ? "" : (string)apps[j]["Division"];
                            app2.Platform = apps[j]["Platform"] == null ? "" : (string)apps[j]["Platform"];
                            app2.ServiceID = apps[j]["ServiceID"] == null ? "" : (string)apps[j]["ServiceID"];


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

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

                            Application app2 = db.Applications.Single(a => a.applications_id.Equals((int)app["applications_id"]));
                            db.Applications.DeleteOnSubmit(app2);

                            db.SubmitChanges();

                            return new PagedData("good");
                        }

                        JArray apps = (JArray)blob["rows"];
                        for (int j = 0; j < apps.Count; j++)
                        {
                            Application app2 = db.Applications.Single(a => a.applications_id.Equals((int)apps[j]["applications_id"]));
                            db.Applications.DeleteOnSubmit(app2);
                        }

                        db.SubmitChanges();

                        return new PagedData("deleted");
                    }
                default:
                    return new PagedData("Unsupported Http Request:  " + context.Request.RequestType + " not recognized");
            }
        }
        public override PagedData ProcessRequest(HttpContext context, CookDBDataContext db)
        {
            IQueryable<DocumentationReq> q = db.DocumentationReqs;
            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("GetDocumentationRequirements 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");
            }

            string username = context.Request.Params.Get("user_name");

            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.documentation_req_id, a.project_id, a.filename, a.latest_version, a.uat_version, a.prod_version, a.notes }));
                    }
                case "POST":
                    {
                        if (blob["rows"].GetType() == typeof(JObject))
                        {
                            JObject obj = (JObject)blob["rows"];
                            DocumentationReq record = new DocumentationReq();
                          
                            record.project_id = int.Parse(filter);
                            record.filename = (string)obj["filename"];
                            record.latest_version = (string)obj["latest_version"];
                            record.uat_version = (string)obj["uat_version"];
                            record.prod_version = (string)obj["prod_version"];
                            record.notes = (string)obj["notes"];
                           
                            db.DocumentationReqs.InsertOnSubmit(record);
                            db.SubmitChanges();

                            ChangeLog newLog = new ChangeLog();
                            newLog.project_id = Convert.ToInt32(int.Parse(filter));
                            newLog.time = DateTime.Now.ToShortTimeString();
                            newLog.date = DateTime.Now.ToShortDateString();
                            newLog.tab = "Requirements";
                            newLog.user_name = username;
                            newLog.description = "New Documentation requirement added";
                            if (!db.ChangeLogs.Contains(newLog))
                            {
                                db.ChangeLogs.InsertOnSubmit(newLog);
                                db.SubmitChanges();
                            }

                            return new PagedData(record);
                        }

                        JArray objs = (JArray)blob["rows"];
                        List<DocumentationReq> list = new List<DocumentationReq>();
                        for (int j = 0; j < objs.Count; j++)
                        {
                            DocumentationReq record = new DocumentationReq();
                            record.project_id = int.Parse(filter);
                            record.filename = (string)objs[j]["filename"];
                            record.latest_version = (string)objs[j]["latest_version"];
                            record.uat_version = (string)objs[j]["uat_version"];
                            record.prod_version = (string)objs[j]["prod_version"];
                            record.notes = (string)objs[j]["notes"];

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

                        
                        return new PagedData(list);

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

                            string logBuilder = "";
                            string intro = "Existing Documentation record modified: ";

                            DocumentationReq record = db.DocumentationReqs.Single(a => a.documentation_req_id.Equals((int)obj["documentation_req_id"]));
                            if (record.filename != (string)obj["filename"])
                            {
                                logBuilder += "Filename changed from \"" + record.filename + "\" to \"" + (string)obj["filename"] + "\".";
                            }
                            record.filename = (string)obj["filename"];
                            //record.project_id = int.Parse(filter);
                            if (record.latest_version != (string)obj["latest_version"])
                            {
                                logBuilder += "Latest Version changed from \"" + record.latest_version + "\" to \"" + (string)obj["latest_version"] + "\".";
                            }
                            record.latest_version = (string)obj["latest_version"];
                            if (record.uat_version != (string)obj["uat_version"])
                            {
                                logBuilder += "UAT Version changed from \"" + record.uat_version + "\" to \"" + (string)obj["uat_version"] + "\".";
                            }
                            record.uat_version = (string)obj["uat_version"];
                            if (record.prod_version != (string)obj["prod_version"])
                            {
                                logBuilder += "Prod Version changed from \"" + record.prod_version + "\" to \"" + (string)obj["prod_version"] + "\".";
                            }
                            record.prod_version = (string)obj["prod_version"];
                            if (record.notes != (string)obj["notes"])
                            {
                                logBuilder += "Notes changed from \"" + record.notes + "\" to \"" + (string)obj["notes"] + "\".";
                            }
                            record.notes = (string)obj["notes"];
                            
                            db.SubmitChanges();

                            if (logBuilder != "")
                            {
                                ChangeLog newLog = new ChangeLog();
                                newLog.project_id = Convert.ToInt32(int.Parse(filter));
                                newLog.time = DateTime.Now.ToShortTimeString();
                                newLog.date = DateTime.Now.ToShortDateString();
                                newLog.tab = "Requirements";
                                newLog.user_name = username;
                                newLog.description = intro + logBuilder;
                                if (!db.ChangeLogs.Contains(newLog))
                                {
                                    db.ChangeLogs.InsertOnSubmit(newLog);
                                    db.SubmitChanges();
                                }
                            }

                            return new PagedData(record);
                        }


                        JArray objs = (JArray)blob["rows"];
                        List<DocumentationReq> list = new List<DocumentationReq>();
                        for (int j = 0; j < objs.Count; j++)
                        {
                            DocumentationReq record = db.DocumentationReqs.Single(a => a.documentation_req_id.Equals((int)objs[j]["documentation_req_id"]));
                            record.filename = (string)objs[j]["filename"];
                            //record.project_id = int.Parse(filter);
                            record.latest_version = (string)objs[j]["latest_version"];
                            record.uat_version = (string)objs[j]["uat_version"];
                            record.prod_version = (string)objs[j]["prod_version"];
                            record.notes = (string)objs[j]["notes"];

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

                        return new PagedData(list);
                    }

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

                            JObject obj = (JObject)blob["rows"];

                            DocumentationReq record = db.DocumentationReqs.Single(a => a.documentation_req_id.Equals((int)obj["documentation_req_id"]));
                            logbuilder += "Filename: \"" + record.filename + "\"; Latest Version: \"" + record.latest_version + "\"; UAT Version: \"" + record.uat_version + "\"; Prod Version: \"" +
                                record.prod_version + "\"; Notes: \"" + record.notes + "\".";
                                
                            db.DocumentationReqs.DeleteOnSubmit(record);

                            db.SubmitChanges();

                            ChangeLog newLog = new ChangeLog();
                            newLog.project_id = Convert.ToInt32(int.Parse(filter));
                            newLog.time = DateTime.Now.ToShortTimeString();
                            newLog.date = DateTime.Now.ToShortDateString();
                            newLog.tab = "Requirements";
                            newLog.user_name = username;
                            newLog.description = "Existing Documentation Requirement deleted: " + logbuilder;
                            if (!db.ChangeLogs.Contains(newLog))
                            {
                                db.ChangeLogs.InsertOnSubmit(newLog);
                                db.SubmitChanges();
                            }

                            return new PagedData("good");
                        }


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

                        db.SubmitChanges();
                        return new PagedData("DocumentationReq deleted");
                    }


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