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;
}