public HttpResponseMessage PutChecklistActionOnItem(int id, checklist_action_xref data)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

            if (id != data.checklist_action_xref_id)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            db.Entry(data).State = System.Data.Entity.EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }

            return(Request.CreateResponse(HttpStatusCode.OK));
        }
        public HttpResponseMessage PostChecklistAction(checklist_action_ref data)
        {
            if (ModelState.IsValid)
            {
                // Get sub type ID from template
                checklist_template_ref tmprefdata = db.CheckListTemplates.Find(data.checklist_template_id);

                if (tmprefdata != null)
                {
                    data.sub_type_id = tmprefdata.sub_type_id;
                }

                db.CheckListActions.Add(data);
                db.SaveChanges();

                //For start new checklist : Add all checklist items from template : This will happen only first time when new checklist-action is added

                var itemsToBeAddedfromTemplate = (from chktempxref in db.CheckListTemplatesXref
                                                  where chktempxref.checklist_template_id == data.checklist_template_id
                                                  select chktempxref).ToList();

                List <checklist_action_xref> addchecklistActionItems = new List <checklist_action_xref>();
                foreach (var item in itemsToBeAddedfromTemplate)
                {
                    checklist_action_xref row = new checklist_action_xref();
                    row.checklist_action_id        = data.checklist_action_id;
                    row.checklist_id               = item.checklist_id;
                    row.priority_order             = item.priority_order;
                    row.status_id                  = (short)CheckListStatus.NOTSTARTED;
                    row.powershell_script          = item.powershell_script;
                    row.powershell_script_timedout = item.powershell_script_timedout;
                    addchecklistActionItems.Add(row);
                }
                db.CheckListActionsXref.AddRange(addchecklistActionItems);
                db.SaveChanges();
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, data);
                //response.Headers.Location = new Uri(Url.Link("ApiByName", new { id = configuration.config_id }));
                return(response);
            }
            else
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
        public HttpResponseMessage GetExecutePSCheckListActionOnItem(int checklist_action_xref_id)
        {
            checklist_action_xref data = db.CheckListActionsXref.Find(checklist_action_xref_id);

            if (data == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }


            try
            {
                CommonPowerShell cpshell                 = new CommonPowerShell();
                string           powershell_script       = data.powershell_script;
                StringBuilder    powershell_returnoutput = new StringBuilder();

                int powershell_script_timedout = Convert.ToInt32(data.powershell_script_timedout);

                if (powershell_script_timedout <= 0)
                {
                    powershell_script_timedout = Constants.DEFAULT_PS_TIMEDOUT;
                    powershell_returnoutput.Append("There was no/0 timed out set. New default timed out is set : " + Constants.DEFAULT_PS_TIMEDOUT + " ms.");
                }
                var task = Task.Run(() => cpshell.ExecutePowerShellSynechronously(powershell_script, powershell_script_timedout));
                if (task.Wait(TimeSpan.FromMilliseconds(powershell_script_timedout)))
                {
                    powershell_returnoutput.AppendLine(task.Result);
                }
                else
                {
                    powershell_returnoutput.AppendLine("Timed out error. Current timeout setting for this PS is : " + powershell_script_timedout.ToString() + " ms.");
                }

                // powershell_returnoutput = cpshell.ExecutePowerShellSynechronously(powershell_script, powershell_script_timedout);

                if (powershell_returnoutput.ToString().Contains("[STATUS:"))
                {
                    int    firstposition = powershell_returnoutput.ToString().IndexOf("[STATUS:");
                    int    lastposition  = powershell_returnoutput.ToString().IndexOf("]", firstposition);
                    string getstatus     = powershell_returnoutput.ToString().Substring(firstposition + "[STATUS:".Length, lastposition - (firstposition + "[STATUS:".Length));

                    try
                    {
                        CheckListStatus checkListStatus = (CheckListStatus)Enum.Parse(typeof(CheckListStatus), getstatus);
                        data.status_id = Convert.ToInt16(checkListStatus);
                    }
                    catch {
                        powershell_returnoutput.AppendLine("ERROR : " + getstatus + " is not valid status. Please use valid status to be returned from Powershell script output. Eg. [STATUS:COMPLETED]");
                        data.status_id = Convert.ToInt16(CheckListStatus.NOTSTARTED);
                    }
                }

                data.powershell_script_execution_comments = powershell_returnoutput.ToString();
                data.powershell_script_timedout           = powershell_script_timedout;
                data.comments += "PS executed on : " + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + "\n";

                db.Entry(data).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }

            return(Request.CreateResponse(HttpStatusCode.OK, data));
        }