/// <summary> /// Reads the specified label by id. /// </summary> /// <param name="id">The id.</param> /// <returns></returns> public static KioskLabel Read( int id ) { string cacheKey = KioskLabel.CacheKey( id ); ObjectCache cache = MemoryCache.Default; KioskLabel label = cache[cacheKey] as KioskLabel; if ( label != null ) { return label; } else { var file = new BinaryFileService( new RockContext() ).Get( id ); if ( file != null ) { label = new KioskLabel(); label.Guid = file.Guid; label.Url = string.Format( "{0}GetFile.ashx?id={1}", System.Web.VirtualPathUtility.ToAbsolute( "~" ), file.Id ); label.MergeFields = new Dictionary<string, string>(); label.FileContent = System.Text.Encoding.Default.GetString( file.Data.Content ); file.LoadAttributes(); string attributeValue = file.GetAttributeValue( "MergeCodes" ); if ( !string.IsNullOrWhiteSpace( attributeValue ) ) { string[] nameValues = attributeValue.Split( new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries ); foreach ( string nameValue in nameValues ) { string[] nameAndValue = nameValue.Split( new char[] { '^' }, StringSplitOptions.RemoveEmptyEntries ); if ( nameAndValue.Length == 2 && !label.MergeFields.ContainsKey( nameAndValue[0] ) ) { label.MergeFields.Add( nameAndValue[0], nameAndValue[1] ); int definedValueId = int.MinValue; if ( int.TryParse( nameAndValue[1], out definedValueId ) ) { var definedValue = DefinedValueCache.Read( definedValueId ); if ( definedValue != null ) { string mergeField = definedValue.GetAttributeValue( "MergeField" ); if ( mergeField != null ) { label.MergeFields[nameAndValue[0]] = mergeField; } } } } } } var cachePolicy = new CacheItemPolicy(); cachePolicy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds( 60 ); cache.Set( cacheKey, label, cachePolicy ); return label; } } return null; }
/// <summary> /// Reads the specified label by guid. /// </summary> /// <param name="guid">The unique identifier.</param> /// <returns></returns> public static KioskLabel Read( Guid guid ) { string cacheKey = KioskLabel.CacheKey( guid ); RockMemoryCache cache = RockMemoryCache.Default; KioskLabel label = cache[cacheKey] as KioskLabel; if ( label != null ) { return label; } else { using ( var rockContext = new RockContext() ) { var file = new BinaryFileService( rockContext ).Get( guid ); if ( file != null ) { label = new KioskLabel(); label.Guid = file.Guid; label.Url = string.Format( "{0}GetFile.ashx?id={1}", System.Web.VirtualPathUtility.ToAbsolute( "~" ), file.Id ); label.MergeFields = new Dictionary<string, string>(); label.FileContent = file.ContentsToString(); file.LoadAttributes( rockContext ); label.LabelType = file.GetAttributeValue( "core_LabelType" ).ConvertToEnum<KioskLabelType>(); string attributeValue = file.GetAttributeValue( "MergeCodes" ); if ( !string.IsNullOrWhiteSpace( attributeValue ) ) { string[] nameValues = attributeValue.Split( new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries ); foreach ( string nameValue in nameValues ) { string[] nameAndValue = nameValue.Split( new char[] { '^' }, StringSplitOptions.RemoveEmptyEntries ); if ( nameAndValue.Length == 2 && !label.MergeFields.ContainsKey( nameAndValue[0] ) ) { label.MergeFields.Add( nameAndValue[0], nameAndValue[1] ); int definedValueId = int.MinValue; if ( int.TryParse( nameAndValue[1], out definedValueId ) ) { var definedValue = DefinedValueCache.Read( definedValueId ); if ( definedValue != null ) { string mergeField = definedValue.GetAttributeValue( "MergeField" ); if ( mergeField != null ) { label.MergeFields[nameAndValue[0]] = mergeField; } } } } } } cache.Set( cacheKey, label, new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.Date.AddDays( 1 ) } ); return label; } } } return null; }