Esempio n. 1
0
        public GetListResponse<Configuration> GetChildren(ConfigRequest req)
        {
            try
            {
                string name = Util.GetConnName<Configuration>();
                var db = ImardaDatabase.CreateDatabase(name);

                object[] args = new object[5];
                args[0] = req.ID;
                Guid[] levels = req.GetLevels();
                int n = Math.Min(levels.Length, 4);
                Array.Copy(levels, 0, args, 1, n);

                using (IDataReader dr = db.ExecuteDataReader("GetChildren", args))
                {
                    var list = new List<Configuration>();
                    while (dr.Read())
                    {
                        var c = GetFromData<Configuration>(dr);
                        list.Add(c);
                    }

                    return new GetListResponse<Configuration>(list);
                }

            }
            catch (Exception ex)
            {
                return ErrorHandler.Handle<GetListResponse<Configuration>>(ex);
            }
        }
Esempio n. 2
0
        public SimpleResponse<int> GetChildrenCount(ConfigRequest req)
        {
            try
            {
                string name = Util.GetConnName<Configuration>();
                var db = ImardaDatabase.CreateDatabase(name);

                object[] args = new object[5];
                args[0] = req.ID;
                Guid[] levels = req.GetLevels();
                int n = Math.Min(levels.Length, 4);
                Array.Copy(levels, 0, args, 1, n);

                int count = (int)db.ExecuteScalar("GetChildrenCount", args);
                return new SimpleResponse<int>(count);
            }
            catch (Exception ex)
            {
                return ErrorHandler.Handle<SimpleResponse<int>>(ex);
            }
        }
Esempio n. 3
0
        public GetListResponse<Configuration> GetHierarchy(ConfigRequest req)
        {
            try
            {
                string name = Util.GetConnName<Configuration>();
                var db = ImardaDatabase.CreateDatabase(name);

                object[] args = new object[7];
                args[0] = req.ID;
                args[1] = 0; // hierarchy
                Guid[] levels = req.GetLevels();
                int n = levels.Length;
                for (int i = 0; i < n; i++) args[i + 2] = levels[i];

                using (IDataReader dr = db.ExecuteDataReader("GetConfiguration", args))
                {
                    var list = new List<Configuration>();
                    while (dr.Read())
                    {
                        var c = GetFromData<Configuration>(dr);
                        list.Add(c);
                    }
                    list.Reverse(); // -> [0] becomes root
                    return new GetListResponse<Configuration>(list);
                }
            }
            catch (Exception ex)
            {
                return ErrorHandler.Handle<GetListResponse<Configuration>>(ex);
            }
        }
Esempio n. 4
0
        public SimpleResponse<int> SaveHierarchy(ConfigRequest req)
        {
            try
            {
                int result;
                object value = req.AppParameter;
                req.AppParameter = null;

                var resp = GetHierarchy(req);
                ErrorHandler.Check(resp);

                // Example:
                // array of existing Configuration objects:
                //
                //	L1 L2 L3 L4 L5
                // 0  -  -  -  -  -
                // 1  x1 -  -  -  -
                // 2  x1 x2 -  -  -	 existingLevel=2
                //
                // request new
                //
                //	[0] [1] [2] [3]
                //	x1  x2  n3  n4	depth=4
                //
                // create:
                //
                //	 L1 L2 L3 L4 L5
                // i=3 x1 x2 n3 -  -
                // i=4 x1 x2 n3 n4 -
                //

                var list = resp.List;
                int existingLevel = list.Count - 1;
                int depth = req.Depth;

                if (existingLevel == depth)
                {
                    if (value == null)
                    {
                        var resp1 = GetChildrenCount(req);
                        ErrorHandler.Check(resp1);
                        if (resp1.Value == 0)
                        {
                            // no child nodes: can delete
                            var resp3 = DeleteConfigurationByUID(new IDRequest(list[existingLevel].UID));
                            ErrorHandler.Check(resp3);
                            result = -1;
                        }
                        else
                        {
                            // has child nodes, can not delete: do nothing
                            result = 0;
                        }
                    }
                    else
                    {
                        Configuration c = list[existingLevel];
                        c.VersionValue = ConfigItemVersion.Create(req.ValueType, value, false, Guid.Empty).ToString();
                        var resp1 = SaveConfigurationByUID(new SaveRequest<Configuration>(c));
                        ErrorHandler.Check(resp1);
                        result = 1;
                    }
                }
                else // exisitingLevel < depth
                {
                    if (value == null)
                    {
                        result = 0;
                    }
                    else
                    {
                        Guid[] levels = new Guid[5];
                        Array.Copy(req.GetLevels(), levels, depth);
                        string sValue = ConfigItemVersion.Create(req.ValueType, value, false, Guid.Empty).ToString();
                        result = 0;
                        for (int i = existingLevel + 1; i <= depth; i++)
                        {
                            bool combine = i != 0 && req.Combine;
                            var c = new Configuration
                            {
                                ID = req.ID,
                                Combine = combine,
                                ValueType = req.ValueType,
                                Notes = req.Notes,
                                VersionValue = combine ? string.Empty : sValue,
                                Level1 = i >= 1 ? levels[0] : Guid.Empty,
                                Level2 = i >= 2 ? levels[1] : Guid.Empty,
                                Level3 = i >= 3 ? levels[2] : Guid.Empty,
                                Level4 = i >= 4 ? levels[3] : Guid.Empty,
                                Level5 = i >= 5 ? levels[4] : Guid.Empty,
                                UID = SequentialGuid.NewDbGuid(),
                            };
                            var resp2 = SaveConfigurationByUID(new SaveRequest<Configuration>(c));
                            ErrorHandler.Check(resp2);
                            result++;
                        }
                    }
                }
                return new SimpleResponse<int>(result);
            }
            catch (Exception ex)
            {
                return ErrorHandler.Handle<SimpleResponse<int>>(ex);
            }
        }
        public BusinessMessageResponse SetUserLocale(ConfigRequest req)
        {
            //
            // L0 L1 L2 | null			| not null
            // ---------+-----------------+-----------------
            // 1  0  0  | do nothing [a]  | create L1,L2 [d]
            // 1  1  0  | do nothing [b]  | create L2	[e]
            // 1  1  1  | delete L2  [c]  | update L2	[f]
            //

            BusinessMessageResponse resp = null;
            try
            {
                Guid[] levels = req.GetLevels();
                Guid companyID = levels[0]; // Database column LEVEL1
                Guid personID = levels[1];  // Database column LEVEL2

                Guid localeItemID = new Guid(CultureConfigGroup.LocaleItemID);
                req.ID = localeItemID;
                string locale = (string)req.AppParameter ?? string.Empty;
                req.AppParameter = null;

                // root level
                var req0 = new ConfigRequest(localeItemID, null) { IgnoreCache = true };
                var resp0 = GetConfigValue(req0);
                ErrorHandler.CheckItem(resp0);
                Guid uid0 = resp0.Item.UID;

                // company level (L1)
                var req1 = new ConfigRequest(localeItemID, null, companyID) { IgnoreCache = true };
                var resp1 = GetConfigValue(req1);
                ErrorHandler.CheckItem(resp1);
                Guid uid1 = resp1.Item.UID;

                // user/person level (L2)
                var req2 = new ConfigRequest(localeItemID, null, companyID, personID) { IgnoreCache = true };
                var resp2 = GetConfigValue(req2);
                ErrorHandler.CheckItem(resp2);
                Guid uid2 = resp2.Item.UID;

                if (uid1.Equals(uid0))
                {
                    // [a] or [d]
                    if (locale == string.Empty)
                    {
                        // [a]
                        resp = new BusinessMessageResponse();
                    }
                    else
                    {
                        // [d]
                        string locale0 = (string)resp0.Item.Value;
                        resp = CreateLocaleConfig(locale0, companyID, Guid.Empty);
                        ErrorHandler.Check(resp);
                        resp = CreateLocaleConfig(locale, companyID, personID);
                    }
                }
                else if (uid2.Equals(uid1))
                {
                    // [b] or [e]
                    if (locale == string.Empty)
                    {
                        // [b]
                        resp = new BusinessMessageResponse();
                    }
                    else
                    {
                        // [e]
                        resp = CreateLocaleConfig(locale, companyID, personID);
                    }
                }
                else // level 2 record exists
                {
                    // [c] or [f]
                    if (locale == string.Empty)
                    {
                        // [c]
                        resp = DeleteConfigurationByUID(new IDRequest(uid2));
                    }
                    else
                    {
                        // [f]
                        resp2.Item.Value = locale;
                        resp = UpdateConfigValue(new SaveRequest<ConfigValue>(resp2.Item));
                    }
                }
                ErrorHandler.Check(resp);
                return resp;
            }
            catch (Exception ex)
            {
                return ErrorHandler.Handle(ex);
            }
        }
        public BusinessMessageResponse SetCompanyValue(ConfigRequest req)
        {
            //
            // L0 L1 | null			| not null
            // ------+-----------------+-----------------
            // 1  0  | do nothing  [a] | create L1 [c]
            // 1  1  | update L1   [b]*| update L1 [d]
            //
            // *assign L0 value to L1, coz combine=false

            BusinessMessageResponse resp = null;
            try
            {
                Guid id = req.ID;

                // first get root level (L0)
                var req0 = new ConfigRequest(id, null) { IgnoreCache = true };
                var resp0 = GetConfigValue(req0);
                Guid uid0 = resp0.Item.UID; // we need this to compare to uid1

                string newValue = (string)req.AppParameter;
                Guid companyID = req.GetLevels()[0]; // Database column LEVEL1

                // now get L1 (company level)
                var req1 = new ConfigRequest(id, null, companyID) { IgnoreCache = true };
                GetItemResponse<ConfigValue> resp1 = GetConfigValue(req1);
                ErrorHandler.CheckItem(resp1);
                if (resp1.Item.UID.Equals(uid0))
                {
                    // Csae [a] or [c]
                    if (newValue == null)
                    {
                        // Case [a]
                        resp = new BusinessMessageResponse();
                    }
                    else
                    {
                        // Case [c]
                        Configuration cfg = new Configuration
                        {
                            UID = SequentialGuid.NewDbGuid(),
                            ID = id,
                            Combine = false,
                            ValueType = 1,
                            VersionValue = newValue,
                            Notes = req.Notes,
                            Level1 = companyID
                        };
                        resp = SaveConfigurationByUID(new SaveRequest<Configuration>(cfg));
                    }
                }
                else
                {
                    // case [b] or [d]
                    if (newValue == null)
                    {
                        // case [b]
                        resp1.Item.Value = resp0.Item.Value;
                    }
                    else
                    {
                        // case [d]
                        resp1.Item.Value = newValue;
                    }
                    resp = UpdateConfigValue(new SaveRequest<ConfigValue>(resp1.Item));
                }
                ErrorHandler.Check(resp);
                return resp;
            }
            catch (Exception ex)
            {
                return ErrorHandler.Handle(ex);
            }
        }
        public BusinessMessageResponse SetCompanyCustomUnits(ConfigRequest req)
        {
            //
            // L0 L1 | null			| not null
            // ------+-----------------+-----------------
            // 1  0  | do nothing  [a] | create L1 [c]
            // 1  1  | update L1   [b]*| update L1 [d]
            //
            // *assign L0 value to L1, coz combine=false

            BusinessMessageResponse resp = null;
            try
            {
                // first get root level
                var req0 = new ConfigRequest(CultureHelper.PreferredMeasurementUnitsID, null) { IgnoreCache = true };
                var resp0 = GetConfigValue(req0);
                ErrorHandler.CheckItem(resp0);
                Guid uid0 = resp0.Item.UID; // we need this to compare to level 1

                string newValue = (string)req.AppParameter;
                Guid companyID = req.GetLevels()[0]; // Database column LEVEL1

                // now get level 1, may not exist
                var req1 = new ConfigRequest(CultureHelper.PreferredMeasurementUnitsID, null, companyID) { IgnoreCache = true };
                GetItemResponse<ConfigValue> resp1 = GetConfigValue(req1);
                ErrorHandler.CheckItem(resp1);
                if (resp1.Item.UID.Equals(uid0))
                {
                    // Case [a] or [c]
                    if (string.IsNullOrEmpty(newValue))
                    {
                        // Case [a]
                        resp = new BusinessMessageResponse();
                    }
                    else
                    {
                        // Case [c]
                        // L1 record not in database, create one
                        Configuration cfg = new Configuration
                        {
                            UID = SequentialGuid.NewDbGuid(),
                            ID = CultureHelper.PreferredMeasurementUnitsID,
                            Combine = true,
                            ValueType = 11,
                            VersionValue = newValue,
                            Notes = req.Notes,
                            Level1 = companyID,
                        };
                        resp = SaveConfigurationByUID(new SaveRequest<Configuration>(cfg));
                    }
                }
                else
                {
                    // Case [b] or [d]
                    string val = newValue ?? string.Empty;
                    resp = UpdateConfigValue(new SaveRequest<ConfigValue>(resp1.Item));
                    // case [b] or [d]
                    if (string.IsNullOrEmpty(newValue))
                    {
                        // case [b]
                        val = (string)resp0.Item.Value;
                    }
                    else
                    {
                        // case [d]
                        val = newValue;
                    }
                    resp1.Item.Value = val;
                    resp = UpdateConfigValue(new SaveRequest<ConfigValue>(resp1.Item));
                }
                ErrorHandler.Check(resp);
                return resp;
            }
            catch (Exception ex)
            {
                return ErrorHandler.Handle(ex);
            }
        }