/// <summary> /// Add or update a value for a Defined Type. /// </summary> /// <param name="definedTypeGuidString">A string representing the Guid identifier of the <see cref="Rock.Model.DefinedType"/> to which a new value will be added.</param> /// <param name="value">A string containing the Value of the new <see cref="Rock.Model.DefinedValue"/>.</param> /// <param name="description">A string containing the Description of the new <see cref="Rock.Model.DefinedValue"/>.</param> /// <param name="attributeValues">A dictionary of key-value pairs holding the value of the Attributes associated with the new <see cref="Rock.Model.DefinedValue"/>.</param> public void AddOrUpdateValue(string definedTypeGuidString, string value, string description, Dictionary <string, object> attributeValues) { var dataContext = ( RockContext )this.Context; // Execute this operation in a transaction to allow rollback if there are any problems updating the value attributes. dataContext.WrapTransaction(() => { // Resolve the Defined Type. var definedType = DefinedTypeCache.Get(new Guid(definedTypeGuidString)); if (definedType == null) { throw new Exception($"Defined Type is invalid. Could not map identifier \"{ definedTypeGuidString }\" to an existing Defined Type."); } // Get the existing Defined Value or create a new entry. var definedValueService = new DefinedValueService(dataContext); var definedValue = definedValueService.Queryable().FirstOrDefault(x => x.DefinedTypeId == definedType.Id && x.Value == value); if (definedValue == null) { // Create a new Defined Value. definedValue = new DefinedValue(); definedValueService.Add(definedValue); definedValue.DefinedTypeId = definedType.Id; definedValue.IsSystem = false; definedValue.IsActive = true; } definedValue.Value = value; definedValue.Description = description; dataContext.SaveChanges(); // Set the attributes of the defined value. definedValue.LoadAttributes(); foreach (var keyValuePair in attributeValues) { if (!definedValue.Attributes.ContainsKey(keyValuePair.Key)) { throw new Exception($"Defined Type Attribute is invalid. Could not map key \"{ keyValuePair.Key }\" to an existing Attribute."); } definedValue.SetAttributeValue(keyValuePair.Key, keyValuePair.Value.ToStringSafe()); } definedValue.SaveAttributeValues(dataContext); }); }
/// <summary> /// Handles the Click event of the btnSaveDefinedValue control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void btnSaveValue_Click( object sender, EventArgs e ) { DefinedValue definedValue; DefinedValueService definedValueService = new DefinedValueService(); int definedValueId = hfDefinedValueId.ValueAsInt(); if ( definedValueId.Equals( 0 ) ) { int definedTypeId = hfDefinedTypeId.ValueAsInt(); definedValue = new DefinedValue { Id = 0 }; definedValue.DefinedTypeId = definedTypeId; definedValue.IsSystem = false; var orders = definedValueService.Queryable() .Where( d => d.DefinedTypeId == definedTypeId ) .Select( d => d.Order) .ToList(); definedValue.Order = orders.Any() ? orders.Max() + 1 : 0; } else { definedValue = definedValueService.Get( definedValueId ); } definedValue.Name = tbValueName.Text; definedValue.Description = tbValueDescription.Text; definedValue.LoadAttributes(); Rock.Attribute.Helper.GetEditValues( phDefinedValueAttributes, definedValue ); if ( !Page.IsValid ) { return; } if ( !definedValue.IsValid ) { // Controls will render the error messages return; } RockTransactionScope.WrapTransaction( () => { if ( definedValue.Id.Equals( 0 ) ) { definedValueService.Add( definedValue, CurrentPersonId ); } definedValueService.Save( definedValue, CurrentPersonId ); Rock.Attribute.Helper.SaveAttributeValues( definedValue, CurrentPersonId ); Rock.Web.Cache.DefinedTypeCache.Flush( definedValue.DefinedTypeId ); Rock.Web.Cache.DefinedValueCache.Flush( definedValue.Id ); } ); BindDefinedValuesGrid(); hfDefinedValueId.Value = string.Empty; modalValue.Hide(); }
/// <summary> /// Adds a new defined value to a given DefinedType. /// </summary> /// <param name="stringValue">the string value of the new defined value</param> /// <param name="definedType">a defined type to which the defined value will be added.</param> /// <param name="rockContext"></param> /// <returns></returns> private DefinedValue AddDefinedTypeValue( string stringValue, DefinedType definedType, RockContext rockContext ) { DefinedValueService definedValueService = new DefinedValueService( rockContext ); DefinedTypeService definedTypeService = new DefinedTypeService( rockContext ); DefinedValue definedValue = new DefinedValue { Id = 0, IsSystem = false, Value = stringValue, Description = string.Empty, CreatedDateTime = RockDateTime.Now, DefinedTypeId = definedType.Id }; definedValueService.Add( definedValue ); rockContext.ChangeTracker.DetectChanges(); rockContext.SaveChanges( disablePrePostProcessing: true ); return definedValue; }
/// <summary> /// Adds a new defined value to a given DefinedType. /// </summary> /// <param name="topic">the string value of the new defined value</param> /// <param name="definedTypeCache">a defined type cache to which the defined value will be added.</param> /// <param name="rockContext"></param> /// <returns></returns> private DefinedValueCache AddDefinedTypeValue( string topic, DefinedTypeCache definedTypeCache, RockContext rockContext ) { DefinedValueService definedValueService = new DefinedValueService( rockContext ); DefinedValue definedValue = new DefinedValue { Id = 0, IsSystem = false, Value = topic, Description = "", CreatedDateTime = RockDateTime.Now, DefinedTypeId = definedTypeCache.Id }; definedValueService.Add( definedValue ); rockContext.SaveChanges(); Rock.Web.Cache.DefinedValueCache.Flush( definedValue.Id ); Rock.Web.Cache.DefinedTypeCache.Flush( definedTypeCache.Id ); return DefinedValueCache.Read( definedValue.Id, rockContext ); }
protected void dlgPackage_SaveClick( object sender, EventArgs e ) { int definedValueId = hfDefinedValueId.Value.AsInteger(); var definedType = DefinedTypeCache.Read( Rock.SystemGuid.DefinedType.PROTECT_MY_MINISTRY_PACKAGES.AsGuid() ); if ( definedType != null ) { using ( var rockContext = new RockContext() ) { var service = new DefinedValueService( rockContext ); DefinedValue definedValue = null; if ( !definedValueId.Equals( 0 ) ) { definedValue = service.Get( definedValueId ); } if ( definedValue == null ) { definedValue = new DefinedValue(); definedValue.DefinedTypeId = definedType.Id; service.Add( definedValue ); } definedValue.Value = tbTitle.Text; definedValue.Description = tbDescription.Text; rockContext.SaveChanges(); definedValue.LoadAttributes( rockContext ); Guid? dvJurisdicationCodeGuid = null; int? dvJurisdictionCodeId = ddlMVRJurisdication.SelectedValueAsInt(); if ( dvJurisdictionCodeId.HasValue && dvJurisdictionCodeId.Value > 0 ) { var dvJurisdicationCode = DefinedValueCache.Read( dvJurisdictionCodeId.Value ); if ( dvJurisdicationCode != null ) { dvJurisdicationCodeGuid = dvJurisdicationCode.Guid; } } definedValue.SetAttributeValue( "PMMPackageName", tbPackageName.Text ); definedValue.SetAttributeValue( "DefaultCounty", tbDefaultCounty.Text ); definedValue.SetAttributeValue( "SendHomeCounty", cbSendCounty.Checked.ToString() ); definedValue.SetAttributeValue( "DefaultState", tbDefaultState.Text ); definedValue.SetAttributeValue( "SendHomeState", cbSendState.Checked.ToString() ); definedValue.SetAttributeValue( "MVRJurisdiction", dvJurisdicationCodeGuid.HasValue ? dvJurisdicationCodeGuid.Value.ToString() : string.Empty ); definedValue.SetAttributeValue( "SendHomeStateMVR", cbSendStateMVR.Checked.ToString() ); definedValue.SaveAttributeValues( rockContext ); DefinedTypeCache.Flush( definedType.Id ); DefinedValueCache.Flush( definedValue.Id ); } } BindPackageGrid(); HideDialog(); }
/// <summary> /// Handles the Click event of the btnSaveValue control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void btnSaveValue_Click( object sender, EventArgs e ) { using ( new Rock.Data.UnitOfWorkScope() ) { DefinedValueService valueService = new DefinedValueService(); DefinedValue definedValue; int valueId = ( ( hfIdValue.Value ) != null && hfIdValue.Value != String.Empty ) ? Int32.Parse( hfIdValue.Value ) : 0; if ( valueId == 0 ) { definedValue = new DefinedValue(); definedValue.IsSystem = false; definedValue.DefinedTypeId = Int32.Parse( hfIdType.Value ); valueService.Add( definedValue, CurrentPersonId ); } else { Rock.Web.Cache.AttributeCache.Flush( valueId ); definedValue = valueService.Get( valueId ); } definedValue.Name = tbValueName.Text; definedValue.Description = tbValueDescription.Text; /* Parse attribute values from controls list */ if ( phAttrControl.Controls.Count > 0 ) { } valueService.Save( definedValue, CurrentPersonId ); } rGridValue_Bind( hfIdType.Value ); modalValues.Hide(); pnlValues.Visible = true; }
/// <summary> /// Handles the SaveClick event of the dlgLink control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void dlgLink_SaveClick( object sender, EventArgs e ) { DefinedValue definedValue = null; using ( var rockContext = new RockContext() ) { var service = new DefinedValueService( rockContext ); int? definedValueId = hfDefinedValueId.Value.AsIntegerOrNull(); if ( definedValueId.HasValue ) { definedValue = service.Get( definedValueId.Value ); } if ( definedValue == null ) { definedValue = new DefinedValue { Id = 0 }; definedValue.DefinedTypeId = _definedType.Id; definedValue.IsSystem = false; var orders = service.Queryable() .Where( d => d.DefinedTypeId == _definedType.Id ) .Select( d => d.Order ) .ToList(); definedValue.Order = orders.Any() ? orders.Max() + 1 : 0; } definedValue.Value = tbTitle.Text; definedValue.Description = tbLink.Text; definedValue.LoadAttributes(); definedValue.SetAttributeValue( "IsLink", ( rblLinkType.SelectedValue == "Link" ).ToString() ); rockContext.WrapTransaction( () => { if ( definedValue.Id.Equals( 0 ) ) { service.Add( definedValue ); } rockContext.SaveChanges(); definedValue.SaveAttributeValues( rockContext ); } ); Rock.Web.Cache.DefinedTypeCache.Flush( definedValue.DefinedTypeId ); Rock.Web.Cache.DefinedValueCache.Flush( definedValue.Id ); } HideDialog(); BindGrid(); }