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

        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;
                string name = string.Empty, description = string.Empty, notes = string.Empty;

                HttpServerUtility server = HttpContext.Current.Server;
                //save
                foreach (DataRow dr in dtjson.Rows)
                {
                    id          = 0;
                    name        = string.Empty;
                    description = string.Empty;
                    notes       = string.Empty;

                    tempMsg = string.Empty;
                    int.TryParse(dr["AOREstimationID"].ToString(), out id);
                    name        = server.UrlDecode(dr["AOREstimationName"].ToString());
                    description = server.UrlDecode(dr["Description"].ToString());
                    notes       = server.UrlDecode(dr["Notes"].ToString());

                    if (id == 0)
                    {
                        exists = false;
                        saved  = MasterData.AOREstimation_Add(
                            AorEstimationID: id, AOREstimationName: name, Descr: description, Notes: notes, exists: out exists, newID: out id, errorMsg: out tempMsg);
                        if (exists)
                        {
                            saved   = false;
                            tempMsg = string.Format("{0}{1}{2}", tempMsg, tempMsg.Length > 0 ? Environment.NewLine : "", "Cannot add duplicate AOR Estimation record.");
                        }
                    }
                    else
                    {
                        saved = MasterData.AOREstimation_Update(
                            AorEstimationID: id, AOREstimationName: name, Descr: description, Notes: notes, duplicate: out duplicate, errorMsg: out tempMsg);
                    }

                    if (saved)
                    {
                        savedQty += 1;
                        ids      += string.Format("{0}{1}", ids.Length > 0 ? "," : "", id.ToString());
                    }

                    else
                    {
                        failedQty += 1;
                        failedIds += string.Format("{0}{1}", failedIds.Length > 0 ? "," : "", failedIds.ToString());
                    }
                    if (tempMsg.Length > 0)
                    {
                        errorMsg = string.Format("{0}{1}{2}", errorMsg, errorMsg.Length > 0 ? Environment.NewLine : "", tempMsg);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            LogUtility.LogException(ex);
            saved    = false;
            errorMsg = 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));
    }