/// <summary> /// Get bulletin details for specified ID. /// </summary> /// <param name="bulletinID">The bulletin ID.</param> /// <returns>The bulletin details.</returns> public BulletinModel Get(int bulletinID) { var key = new KeyModel(CacheType.Global, "BulletinItem").Add(bulletinID); BulletinModel data = null; if (!CacheService.TryGet(key, out data)) { var sqlParameters = new List <SqlParameter>(); sqlParameters.Add(new SqlParameter { ParameterName = "@SiteId", SqlDbType = SqlDbType.VarChar, Value = SiteID }); sqlParameters.Add(new SqlParameter { ParameterName = "@PageId", SqlDbType = SqlDbType.Int, Value = bulletinID }); data = SqlService.Execute <BulletinModel>(connectionName, "AjsCmsPageGet", sqlParameters, reader => { var contentPage = new BulletinModel(); contentPage.Url = reader.GetString(0).ToLower(); contentPage.Title = reader.GetString(1); contentPage.PageId = reader.GetInt32(2); contentPage.Html = reader[10] as string; contentPage.HtmlExtended = reader[11] as string; contentPage.ExtensionData = reader[12] as string; contentPage.ExtensionType = reader[13] as string; contentPage.LiveDate = reader.GetDateTime(15); if (!string.IsNullOrEmpty(contentPage.ExtensionData) && contentPage.ExtensionType == "BULLETIN") { contentPage.BulletinContracts = Deserialize <BulletinExtensionData>(contentPage.ExtensionData).Contracts; } else { return(null); } return(contentPage); } ).FirstOrDefault(); if (data == null) { throw new DataException(String.Format("Bulletin data is not available for the bulletin code - {0}", bulletinID)); } data.Html = ReplaceContent(data.Html); data.HtmlExtended = ReplaceContent(data.HtmlExtended); CacheService.Set(key, data, defaultTimeSpan); } if (data == null) { throw new DataException(String.Format("Bulletin data is not available for the bulletin code - {0}", bulletinID)); } return(data); }
/// <summary> /// Gets the recent history item for a specified history type and object ID /// </summary> /// <param name="historyType">The history type.</param> /// <param name="values">The values necessary for loading the object as a dictionary of key value pairs.</param> /// <returns>Returns <see cref="IEnumerable{HistoryModel}"/> for a single matching recent history record. If no matching record found returns null.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="values"/> is <c>null</c> or empty.</exception> public HistoryModel Get(HistoryType historyType, IDictionary <string, object> values) { if (values == null || !values.Any()) { throw new ArgumentNullException("values"); } var queryStringValues = values.ToQueryString(true); var key = new KeyModel(CacheType.User, "History").Add("Single").Add(historyType).Add(queryStringValues); HistoryModel data; if (!CacheService.TryGet(key, out data)) { var sqlParameters = new List <SqlParameter>(); sqlParameters.Add(new SqlParameter { ParameterName = "@return_value", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.ReturnValue }); sqlParameters.Add(new SqlParameter { ParameterName = "@HistoryType_RHC", SqlDbType = SqlDbType.VarChar, Value = historyType.ToString() }); sqlParameters.Add(new SqlParameter { ParameterName = "@ObjectValues", SqlDbType = SqlDbType.VarChar, Value = queryStringValues }); sqlParameters.Add(new SqlParameter { ParameterName = "@UserID", SqlDbType = SqlDbType.VarChar, Value = UserService.Username }); data = SqlService.Execute <HistoryModel>(connectionName, "RecentHistoryGetSingle", sqlParameters, reader => { var model = new HistoryModel(); model.HistoryType = historyType; string vals = (reader[2] as string); if (!string.IsNullOrEmpty(vals) && vals.IndexOf('=') >= 0) { model.Values = new Dictionary <string, object>(); var split = vals.Split(new char[] { '=' }, StringSplitOptions.None); model.Values.Add(split[0], split[1]); } model.DisplayName = reader[3] as string; model.IsPinned = reader.GetBoolean(4); model.DateAccessed = reader.GetDateTime(5); model.Username = reader[6] as string; return(model); } ).FirstOrDefault(); if (data != null) { // Successful so store in cache CacheService.Set(key, data); } } return(data); }
public void LoadFromContext(string widgetContext) { string context; var key = new KeyModel(CacheType.User, widgetContext); if (!CacheService.TryGet <string>(key, out context)) { // load from db var sqlParameters = new List <SqlParameter>(); sqlParameters.Add(new SqlParameter { ParameterName = "@return_value", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.ReturnValue }); sqlParameters.Add(new SqlParameter { ParameterName = "@UserID", SqlDbType = SqlDbType.VarChar, Value = UserService.Username }); sqlParameters.Add(new SqlParameter { ParameterName = "@WidgetContext", SqlDbType = SqlDbType.VarChar, Value = widgetContext }); IEnumerable <string> data = SqlService.Execute <string>(connectionName, "DashboardGet", sqlParameters, reader => { return(reader[2] as string); } ); context = data.Any() ? data.First() : ","; // Set cache CacheService.Set <string>(key, context); } OpenWidgetNames = context.Split(',').ToList(); DataContext = OpenWidgetNames[0]; OpenWidgetNames.RemoveAt(0); }
/// <summary> /// Gets data from SQL server. /// </summary> /// <returns></returns> public DataAccessSqlServerModel GetSomeSqlServerData() { // Some SQL calls will benefit from using the ICacheService - you need to evaluate this for each call. // The name of the connection string to your database in the web.config string connectionName = "DbConn_AjsCms"; // --------------------------------------------------------------------------------------------------------- // Example of executing a stored procedure - Retrieves the value of the Site ID for use in the next example. // --------------------------------------------------------------------------------------------------------- string SiteCode = "BULLETINS"; short siteId = 0; SqlParameter siteCodeParameter = new SqlParameter() { ParameterName = "@SiteCode", SqlDbType = SqlDbType.VarChar, Value = SiteCode }; SqlParameter siteIdParameter = new SqlParameter() { ParameterName = "@SiteId", Direction = ParameterDirection.Output, Value = 0 }; var sqlParameters = new List <SqlParameter>(); sqlParameters.Add(siteCodeParameter); // Input parameter sqlParameters.Add(siteIdParameter); // Output parameter SqlService.ExecuteNonQuery(connectionName, "AjsCmsSiteGet", sqlParameters); if (siteIdParameter.Value == null) { throw new Exception(string.Format("Could not find Site ID for {0}", SiteCode)); } short.TryParse(siteIdParameter.Value.ToString(), out siteId); // --------------------------------------------------------------------------------------------------------- // Example of executing a query - Returns a simple set of data // --------------------------------------------------------------------------------------------------------- var sqlParameters2 = new List <SqlParameter> { new SqlParameter { ParameterName = "@SiteId", SqlDbType = SqlDbType.VarChar, Value = siteId } }; var bulletinList = SqlService.Execute <DataAccessSqlServerModel>(connectionName, "AjsCmsPageGetAll", sqlParameters2, reader => { var contentPage = new DataAccessSqlServerModel(); contentPage.BulletinTitle = reader.GetString(1); contentPage.BulletinId = reader.GetInt32(2); contentPage.BulletinLiveDate = reader.GetDateTime(12); return(contentPage); } ).ToList(); // Just return the first bulletin we find for a demo. return(bulletinList.FirstOrDefault()); }