Esempio n. 1
0
        public static SettingCollection GetCalculatedPaidSettings(Guid OrgId, Guid InstId)
        {
            string cnnString = GetSettingsValuesHistoryDbConnectionString();

            if (string.IsNullOrEmpty(cnnString))
            {
                return(SettingProvider.GetAllPricedSettings(OrgId, InstId));
            }

            DataTable dtLastValues = SelectSettingsLastValues(OrgId, InstId);

            if (dtLastValues.Rows.Count == 0)
            {
                return(SettingProvider.GetAllPricedSettings(OrgId, InstId));
            }

            dtLastValues.PrimaryKey = new DataColumn[] { dtLastValues.Columns["SettingId"] };

            SettingCollection resCol = new SettingCollection();

            ConfigurationDataSet.SettingDataTable table = ConfigurationDataSet.Current.Setting;
            string filter = string.Format(CultureInfo.InvariantCulture, "{0} > 0", table.PriceColumn.ColumnName);

            Micajah.Common.Bll.Handlers.SettingHandler handler = Micajah.Common.Bll.Handlers.SettingHandler.Current;

            foreach (ConfigurationDataSet.SettingRow _srow in table.Select(filter))
            {
                Setting s    = SettingProvider.CreateSetting(_srow);
                DataRow rowS = dtLastValues.Rows.Find(_srow[table.SettingIdColumn.ColumnName]);
                if (rowS != null)
                {
                    s.Value = rowS["SettingValue"].ToString();
                }
                else
                {
                    int settingVal = handler.GetUsedItemsCount(s, OrgId, InstId);
                    s.Value = settingVal.ToString(CultureInfo.InvariantCulture);
                }
                resCol.Add(s);
            }

            return(resCol);
        }
Esempio n. 2
0
        public static int GetCalculatedCounterSettingValue(Guid OrgId, Guid InstId, Guid SettingId)
        {
            int?counterVal = null;

            counterVal = (int?)Micajah.Common.Application.CacheManager.Current.Get(string.Format("CounterSettingValue{0}{1}{2}", OrgId, InstId, SettingId));

            if (counterVal.HasValue)
            {
                return(counterVal.Value);
            }

            string cnnString = GetSettingsValuesHistoryDbConnectionString();

            if (!string.IsNullOrEmpty(cnnString))
            {
                DataTable dt = SelectByQuery(string.Format("SELECT TOP 1 SettingValue FROM SettingsValuesHistory WHERE OrganizationId='{0}' AND InstanceId='{1}' AND SettingId='{2}' ORDER BY Id DESC", OrgId, InstId, SettingId));
                if (dt.Rows.Count > 0 && !dt.Rows[0].IsNull(0))
                {
                    int cVal = 0;
                    if (int.TryParse(dt.Rows[0][0].ToString(), out cVal))
                    {
                        Micajah.Common.Application.CacheManager.Current.Put(string.Format("CounterSettingValue{0}{1}{2}", OrgId, InstId, SettingId), cVal, TimeSpan.FromMinutes(1440));
                        return(cVal);
                    }
                }
            }

            Micajah.Common.Bll.Handlers.SettingHandler handler = Micajah.Common.Bll.Handlers.SettingHandler.Current;

            Setting s = SettingProvider.GetSetting(SettingId);

            counterVal = handler.GetUsedItemsCount(s, OrgId, InstId);

            Micajah.Common.Application.CacheManager.Current.Put(string.Format("CounterSettingValue{0}{1}{2}", OrgId, InstId, SettingId), counterVal.Value, TimeSpan.FromMinutes(1440));

            return(counterVal.Value);
        }
Esempio n. 3
0
        public static void CalculateCounterSettingsValues()
        {
            string cnnString = GetSettingsValuesHistoryDbConnectionString();

            if (string.IsNullOrEmpty(cnnString))
            {
                return;
            }

            ConfigurationDataSet.SettingDataTable table = ConfigurationDataSet.Current.Setting;
            string            filter          = string.Format(CultureInfo.InvariantCulture, "{0} = " + ((int)SettingType.Counter).ToString(), table.SettingTypeIdColumn.ColumnName);
            SettingCollection counterSettings = new SettingCollection();

            foreach (ConfigurationDataSet.SettingRow _srow in table.Select(filter))
            {
                counterSettings.Add(SettingProvider.CreateSetting(_srow));
            }

            filter = string.Format(CultureInfo.InvariantCulture, "{0} > 0", table.PriceColumn.ColumnName);
            SettingCollection paidSettings = new SettingCollection();

            foreach (ConfigurationDataSet.SettingRow _srow in table.Select(filter))
            {
                paidSettings.Add(SettingProvider.CreateSetting(_srow));
            }

            if (counterSettings.Count == 0 && paidSettings.Count == 0)
            {
                return;
            }

            OrganizationCollection orgs = OrganizationProvider.GetOrganizations(false, false);

            DataTable dtModifiedSettings = new DataTable();

            dtModifiedSettings.Columns.Add(new DataColumn("OrganizationId", typeof(Guid)));
            dtModifiedSettings.Columns.Add(new DataColumn("InstanceId", typeof(Guid)));
            dtModifiedSettings.Columns.Add(new DataColumn("SettingId", typeof(Guid)));
            dtModifiedSettings.Columns.Add(new DataColumn("SettingValue", typeof(string)));

            Micajah.Common.Bll.Handlers.SettingHandler handler = Micajah.Common.Bll.Handlers.SettingHandler.Current;

            foreach (Organization org in orgs)
            {
                InstanceCollection insts = InstanceProvider.GetInstances(org.OrganizationId, false);
                if (insts.Count == 0)
                {
                    continue;
                }

                foreach (Instance inst in insts)
                {
                    DataTable dtLastValues = SelectSettingsLastValues(org.OrganizationId, inst.InstanceId);
                    dtLastValues.PrimaryKey = new DataColumn[] { dtLastValues.Columns["SettingId"] };

                    foreach (Setting setting in counterSettings)
                    {
                        int     settingVal = handler.GetUsedItemsCount(setting, org.OrganizationId, inst.InstanceId);
                        DataRow rowLastVal = dtLastValues.Rows.Find(setting.SettingId);
                        if (rowLastVal == null || rowLastVal["SettingValue"].ToString() != settingVal.ToString())
                        {
                            DataRow rowNewVal = dtModifiedSettings.NewRow();
                            rowNewVal["OrganizationId"] = org.OrganizationId;
                            rowNewVal["InstanceId"]     = inst.InstanceId;
                            rowNewVal["SettingId"]      = setting.SettingId;
                            rowNewVal["SettingValue"]   = settingVal.ToString(CultureInfo.InvariantCulture);
                            dtModifiedSettings.Rows.Add(rowNewVal);
                        }
                    }

                    SettingCollection settings = SettingProvider.GetAllPricedSettings(org.OrganizationId, inst.InstanceId);
                    foreach (Setting setting in settings)
                    {
                        string settingVal = string.Empty;
                        if (setting.Paid)
                        {
                            if (string.IsNullOrEmpty(setting.Value))
                            {
                                settingVal = setting.DefaultValue;
                            }
                            else
                            {
                                settingVal = setting.Value;
                            }
                        }
                        else
                        {
                            settingVal = handler.GetUsedItemsCount(setting, org.OrganizationId, inst.InstanceId).ToString();
                        }

                        DataRow rowLastVal = dtLastValues.Rows.Find(setting.SettingId);

                        if (rowLastVal == null || rowLastVal["SettingValue"].ToString() != settingVal)
                        {
                            DataRow rowNewVal = dtModifiedSettings.NewRow();
                            rowNewVal["OrganizationId"] = org.OrganizationId;
                            rowNewVal["InstanceId"]     = inst.InstanceId;
                            rowNewVal["SettingId"]      = setting.SettingId;
                            rowNewVal["SettingValue"]   = settingVal.ToString(CultureInfo.InvariantCulture);
                            dtModifiedSettings.Rows.Add(rowNewVal);
                        }
                    }
                }
            }

            if (dtModifiedSettings.Rows.Count == 0)
            {
                return;
            }

            using (SqlConnection connection = new SqlConnection(cnnString))
            {
                using (var cmd = GetInsertHistoryRecordCommand(connection))
                {
                    SqlParameter pOrgId        = cmd.Parameters["@OrganizationId"];
                    SqlParameter pInstId       = cmd.Parameters["@InstanceId"];
                    SqlParameter pSettingId    = cmd.Parameters["@SettingId"];
                    SqlParameter pSettingValue = cmd.Parameters["@SettingValue"];
                    cmd.Connection.Open();
                    foreach (DataRow rowS in dtModifiedSettings.Rows)
                    {
                        pOrgId.Value        = rowS["OrganizationId"];
                        pInstId.Value       = rowS["InstanceId"];
                        pSettingId.Value    = rowS["SettingId"];
                        pSettingValue.Value = rowS["SettingValue"];
                        cmd.ExecuteNonQuery();
                    }
                    pOrgId.Value        = Guid.Empty;
                    pInstId.Value       = Guid.Empty;
                    pSettingId.Value    = Guid.Empty;
                    pSettingValue.Value = DBNull.Value;
                    cmd.ExecuteNonQuery();
                    cmd.Connection.Close();
                }
            }
        }