Esempio n. 1
0
    public static string SaveOrganization(int OrganizationId   = 0, string Organization = ""
                                          , string description = "", bool archive       = false
                                          , string roles       = "")
    {
        string errorMsg = string.Empty;
        bool   saved    = false;
        Dictionary <string, string> result = new Dictionary <string, string>()
        {
            { "saved", "" }, { "id", OrganizationId.ToString() }, { "error", "" }, { "Organization", "" }
        };

        try
        {
            #region Assign Organization object properties
            Organization o = new Organization();
            if (OrganizationId > 0)
            {
                o.Load(OrganizationId);
            }
            o.OrganizationNm = Organization.Trim();
            o.Description    = description.Trim();
            o.Archive        = archive;

            o.DefaultRoles.Clear();
            foreach (string r in roles.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                o.AddDefaultRole(r);
            }

            #endregion

            string validationMsg = string.Empty;
            if (!ValidateFields(o, out validationMsg))
            {
                saved    = false;
                errorMsg = validationMsg;
            }
            else
            {
                if (OrganizationId == 0)
                {
                    OrganizationId = o.Add(out saved, out errorMsg);
                }
                else
                {
                    saved = o.Update(out errorMsg);
                }
            }
        }
        catch (Exception ex)
        {
            LogUtility.LogException(ex);
            saved     = false;
            errorMsg += Environment.NewLine + ex.ToString();
        }

        result["saved"]        = saved.ToString();
        result["id"]           = OrganizationId.ToString();
        result["error"]        = errorMsg;
        result["Organization"] = Organization;

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