Skip to content

Commit

Permalink
+ Added the option of to set MEF component attribute values from the …
Browse files Browse the repository at this point in the history
…web.config file.
  • Loading branch information
shivambareria authored and azturner committed Sep 13, 2017
1 parent a73854d commit 930c3e4
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 0 deletions.
122 changes: 122 additions & 0 deletions Rock/Configuration/AttributeValueConfig.cs
@@ -0,0 +1,122 @@
// <copyright>
// Copyright by the Spark Development Network
//
// Licensed under the Rock Community License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.rockrms.com/license
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//
using System.Configuration;

namespace Rock.Configuration
{
/// <summary>
/// Read Attribute Value node in rockConffig
/// </summary>
public class AttributeValueConfig : ConfigurationElement
{
public AttributeValueConfig()
{

}

/// <summary>
/// Gets the attribute key.
/// </summary>
/// <value>
/// The attribute key.
/// </value>
[ConfigurationProperty( "attributeKey", IsRequired = true )]
public string AttributeKey
{
get
{
return this["attributeKey"] as string;
}
}

/// <summary>
/// Gets the entitytype Id.
/// </summary>
/// <value>
/// The entitytype Id.
/// </value>
[ConfigurationProperty( "entityTypeId", IsRequired = true )]
public string EntityTypeId
{
get
{
return this["entityTypeId"] as string;
}
}

/// <summary>
/// Gets the entitytype qualifier column.
/// </summary>
/// <value>
/// The entitytype qualifier column.
/// </value>
[ConfigurationProperty( "entityTypeQualifierColumm" )]
public string EntityTypeQualifierColumm
{
get
{
return this["entityTypeQualifierColumm"] as string;
}
}

/// <summary>
/// Gets the entitytype qualifier value.
/// </summary>
/// <value>
/// The entitytype qualifier value.
/// </value>
[ConfigurationProperty( "entityTypeQualifierValue" )]
public string EntityTypeQualifierValue
{
get
{
return this["entityTypeQualifierValue"] as string;
}
}

/// <summary>
/// Gets the entity Id.
/// </summary>
/// <value>
/// The entity Id.
/// </value>
[ConfigurationProperty( "entityId", IsRequired = true )]
public string EntityId
{
get
{
return this["entityId"] as string;
}
}

/// <summary>
/// Gets the attribute value.
/// </summary>
/// <value>
/// The attribute value.
/// </value>
[ConfigurationProperty( "value", IsRequired = true )]
public string Value
{
get
{
return this["value"] as string;
}
}

}
}
65 changes: 65 additions & 0 deletions Rock/Configuration/AttributeValuesConfig.cs
@@ -0,0 +1,65 @@
// <copyright>
// Copyright by the Spark Development Network
//
// Licensed under the Rock Community License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.rockrms.com/license
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//
using System.Configuration;

namespace Rock.Configuration
{
/// <summary>
/// Attribute Value Collection
/// </summary>
public class AttributeValuesConfig : ConfigurationElementCollection
{
public AttributeValueConfig this[int index]
{
get
{
return base.BaseGet( index ) as AttributeValueConfig;
}
set
{
if ( base.BaseGet( index ) != null )
{
base.BaseRemoveAt( index );
}
this.BaseAdd( index, value );
}
}

public new AttributeValueConfig this[string responseString]
{
get { return ( AttributeValueConfig ) BaseGet( responseString ); }
set
{
if ( BaseGet( responseString ) != null )
{
BaseRemoveAt( BaseIndexOf( BaseGet( responseString ) ) );
}
BaseAdd( value );
}
}

protected override System.Configuration.ConfigurationElement CreateNewElement()
{
return new AttributeValueConfig();
}

protected override object GetElementKey( System.Configuration.ConfigurationElement element )
{
return string.Format("{0}-{1}", ( ( AttributeValueConfig ) element ).AttributeKey, ( ( AttributeValueConfig ) element ).EntityId);
}
}
}
55 changes: 55 additions & 0 deletions Rock/Configuration/RockConfig.cs
@@ -0,0 +1,55 @@
// <copyright>
// Copyright by the Spark Development Network
//
// Licensed under the Rock Community License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.rockrms.com/license
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//
using System;
using System.Configuration;

namespace Rock.Configuration
{
/// <summary>
/// Read RockConfig section from Web.Config
/// </summary>
public class RockConfig : ConfigurationSection
{
private static RockConfig config
= ConfigurationManager.GetSection( "rockConfig" ) as RockConfig;

public static RockConfig Config
{
get
{
return config;
}
}

/// <summary>
/// Gets the attribute Value Collection.
/// </summary>
/// <value>
/// The attribute values.
/// </value>
[System.Configuration.ConfigurationProperty( "attributeValues" )]
[ConfigurationCollection( typeof( AttributeValuesConfig ), AddItemName = "attributeValue" )]
public AttributeValuesConfig AttributeValues
{
get
{
object o = this["attributeValues"];
return o as AttributeValuesConfig;
}
}
}
}
3 changes: 3 additions & 0 deletions Rock/Rock.csproj
Expand Up @@ -476,6 +476,9 @@
<Compile Include="Communication\Transport\Firebase.cs" />
<Compile Include="Communication\Transport\SMTP.cs" />
<Compile Include="Communication\Transport\Twilio.cs" />
<Compile Include="Configuration\AttributeValueConfig.cs" />
<Compile Include="Configuration\AttributeValuesConfig.cs" />
<Compile Include="Configuration\RockConfig.cs" />
<Compile Include="Constants\DisplayStrings.cs" />
<Compile Include="Constants\SystemSettingKeys.cs" />
<Compile Include="Data\BoundFieldTypeAttribute.cs" />
Expand Down
50 changes: 50 additions & 0 deletions RockWeb/App_Code/Global.asax.cs
Expand Up @@ -39,6 +39,7 @@

using Rock;
using Rock.Communication;
using Rock.Configuration;
using Rock.Data;
using Rock.Jobs;
using Rock.Model;
Expand Down Expand Up @@ -176,6 +177,7 @@ protected void Application_Start( object sender, EventArgs e )

// Preload the commonly used objects
stopwatch.Restart();
LoadComponenetData( rockContext );
LoadCacheObjects( rockContext );
if ( System.Web.Hosting.HostingEnvironment.IsDevelopmentEnvironment )
{
Expand Down Expand Up @@ -718,6 +720,54 @@ public bool MigratePlugins( RockContext rockContext )
return result;
}

/// <summary>
/// Loads the Component Data from Web.config.
/// </summary>
private void LoadComponenetData( RockContext rockContext )
{
var rockConfig = RockConfig.Config;
if ( rockConfig.AttributeValues.Count > 0 )
{
foreach ( AttributeValueConfig attributeValueConfig in rockConfig.AttributeValues )
{
AttributeService attributeService = new AttributeService( rockContext );
AttributeValueService attributeValueService = new AttributeValueService( rockContext );
var attribute = attributeService.Get( attributeValueConfig.EntityTypeId.AsInteger(),
attributeValueConfig.EntityTypeQualifierColumm,
attributeValueConfig.EntityTypeQualifierValue,
attributeValueConfig.AttributeKey );
if ( attribute == null )
{
attribute = new Rock.Model.Attribute();
attribute.FieldTypeId = FieldTypeCache.Read( new Guid( Rock.SystemGuid.FieldType.TEXT ) ).Id;
attribute.EntityTypeQualifierColumn = attributeValueConfig.EntityTypeQualifierColumm;
attribute.EntityTypeQualifierValue = attributeValueConfig.EntityTypeQualifierValue;
attribute.Key = attributeValueConfig.AttributeKey;
attribute.Name = attributeValueConfig.AttributeKey.SplitCase();
attributeService.Add( attribute );
rockContext.SaveChanges();
}


var attributeValue = attributeValueService.GetByAttributeIdAndEntityId( attribute.Id, attributeValueConfig.EntityId.AsInteger() );
if ( attributeValue == null && !string.IsNullOrWhiteSpace( attributeValueConfig.Value ) )
{

attributeValue = new Rock.Model.AttributeValue();
attributeValue.AttributeId = attribute.Id;
attributeValue.EntityId = attributeValueConfig.EntityId.AsInteger();
attributeValueService.Add( attributeValue );
}
if ( attributeValue.Value != attributeValueConfig.Value )
{
attributeValue.Value = attributeValueConfig.Value;
rockContext.SaveChanges();
}

}
}
}

/// <summary>
/// Adds the call back.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions RockWeb/web.config
Expand Up @@ -3,6 +3,7 @@
<configSections>
<section name="resizer" type="ImageResizer.ResizerSection,ImageResizer" requirePermission="false" />
<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="rockConfig" type="Rock.Configuration.RockConfig, Rock" />
</configSections>
<quartz>
<add key="quartz.scheduler.instanceName" value="RockSchedulerIIS" />
Expand All @@ -17,6 +18,11 @@
<resizer>
<sizelimits totalWidth="3600" totalHeight="3600" totalBehavior="throwexception" />
</resizer>
<rockConfig>
<attributeValues>
<!--<attributeValue attributeKey="NodeUrl" entityTypeId="406" entityId="16" value="value"/>-->
</attributeValues>
</rockConfig>
<connectionStrings configSource="web.ConnectionStrings.config" />
<system.web>
<customErrors mode="On" defaultRedirect="/Error.aspx" redirectMode="ResponseRewrite" />
Expand Down

0 comments on commit 930c3e4

Please sign in to comment.