예제 #1
0
    public static string SaveChanges(string rows)
    {
        Dictionary <string, string> result = new Dictionary <string, string>()
        {
            { "saved", "0" }
            , { "failed", "0" }
            , { "savedIds", "" }
            , { "failedIds", "" }
            , { "error", "" }
        };
        int    savedQty = 0, failedQty = 0;
        string ids = string.Empty, failedIds = string.Empty, errorMsg = string.Empty, tempMsg = string.Empty;
        int    savedInt = 0;

        try
        {
            DataTable dtjson = (DataTable)JsonConvert.DeserializeObject(rows, (typeof(DataTable)));
            if (dtjson == null || dtjson.Rows.Count == 0)
            {
                errorMsg = "Unable to save. No list of changes was provided.";
            }
            else
            {
                int  id = 0;
                int  workItemID = 0, workItemStatusID = 0, workItemProgress = 0, workItemAssignedID = 0, workItemPrimaryID = 0, workItemTesterID = 0;
                int  testItemID = 0, testItemStatusID = 0, testItemProgress = 0, testItemAssignedID = 0, testItemPrimaryID = 0, testItemTesterID = 0;
                bool saved = false, duplicate = false;

                WorkloadItem item = null, testItem = null;

                //save
                foreach (DataRow dr in dtjson.Rows)
                {
                    saved            = duplicate = false;
                    id               = workItemID = testItemID = 0;
                    workItemStatusID = workItemProgress = workItemAssignedID = workItemPrimaryID = workItemTesterID = 0;
                    testItemStatusID = testItemProgress = testItemAssignedID = testItemPrimaryID = testItemTesterID = 0;
                    tempMsg          = string.Empty;

                    int.TryParse(dr["WorkItem_TestItemID"].ToString(), out id);
                    int.TryParse(dr["WorkItem_Number"].ToString(), out workItemID);
                    int.TryParse(dr["WorkItem_STATUS"].ToString(), out workItemStatusID);
                    int.TryParse(dr["WorkItem_Progress"].ToString(), out workItemProgress);
                    // SCB - Added "ID" to the end of these two - didn't match up with the datatable.
                    int.TryParse(dr["WorkItem_AssignedToID"].ToString(), out workItemAssignedID);
                    int.TryParse(dr["WorkItem_Primary_ResourceID"].ToString(), out workItemPrimaryID);
                    int.TryParse(dr["WorkItem_TesterID"].ToString(), out workItemTesterID);

                    int.TryParse(dr["TestItem_Number"].ToString(), out testItemID);

                    int.TryParse(dr["TestItem_STATUS"].ToString(), out testItemStatusID);
                    int.TryParse(dr["TestItem_Progress"].ToString(), out testItemProgress);
                    int.TryParse(dr["TestItem_AssignedTo"].ToString(), out testItemAssignedID);
                    int.TryParse(dr["TestItem_Primary_Resource"].ToString(), out testItemPrimaryID);
                    int.TryParse(dr["TestItem_Tester"].ToString(), out testItemTesterID);

                    if (workItemID == 0 || testItemID == 0)
                    {
                        saved      = false;
                        failedQty += 1;
                        tempMsg    = "You must specify both a Work Item and a Test Item.";
                    }
                    else
                    {
                        if (id == 0)
                        {
                            if (!Workload.ItemExists(workItemID, -1, "Primary Task"))
                            {
                                saved   = false;
                                tempMsg = string.Format("Unable to find Primary Task with ID = [{0}]", workItemID.ToString());
                            }
                            else if (!Workload.ItemExists(testItemID, -1, "Primary Task"))
                            {
                                saved   = false;
                                tempMsg = string.Format("Unable to find Test Task with ID = [{0}]", testItemID.ToString());
                            }
                            else
                            {
                                saved = WorkloadItem.WorkItem_TestItem_Add(workItemID: workItemID, testItemID: testItemID, archive: false, duplicate: out duplicate, newID: out id, errorMsg: out tempMsg);
                            }
                        }
                        else
                        {
                            item     = WorkloadItem.WorkItem_GetObject(workItemID);
                            testItem = WorkloadItem.WorkItem_GetObject(testItemID);

                            if (itemChanged(ref item, workItemStatusID, workItemProgress, workItemAssignedID, workItemPrimaryID, workItemTesterID))
                            {
                                savedInt = WorkloadItem.WorkItem_Update(item, out tempMsg);
                            }
                            if (itemChanged(ref testItem, testItemStatusID, testItemProgress, testItemAssignedID, testItemPrimaryID, testItemTesterID))
                            {
                                savedInt = WorkloadItem.WorkItem_Update(testItem, out tempMsg);
                            }

                            if (savedInt == 0)
                            {
                                saved = false;
                            }
                            else
                            {
                                saved = true;
                            }
                        }

                        if (saved)
                        {
                            savedQty += 1;
                            ids      += string.Format("{0}{1}", ids.Length > 0 ? "," : "", id.ToString());
                        }
                        else
                        {
                            failedQty += 1;
                            errorMsg   = string.Format("{0}{1}{2}", errorMsg, errorMsg.Length > 0 ? Environment.NewLine : "", tempMsg);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            LogUtility.LogException(ex);
            errorMsg = errorMsg.Length > 0 ? ", " + Environment.NewLine + ex.Message : ex.Message;
        }

        result["savedIds"]  = ids;
        result["failedIds"] = failedIds;
        result["saved"]     = savedQty.ToString();
        result["failed"]    = failedQty.ToString();
        result["error"]     = errorMsg;

        return(JsonConvert.SerializeObject(result, Formatting.None));
    }