/// <summary> /// Sets the value. /// </summary> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <param name="saveValue">if set to <c>true</c> [save value].</param> /// <param name="rockContext">The rock context.</param> public void SetValue(string key, string value, bool saveValue, RockContext rockContext) { if (saveValue) { // Save new value rockContext = rockContext ?? new RockContext(); var attributeValueService = new AttributeValueService(rockContext); var attributeValue = attributeValueService.GetGlobalAttributeValue(key); if (attributeValue == null) { var attributeService = new AttributeService(rockContext); var attribute = attributeService.GetGlobalAttribute(key); if (attribute == null) { attribute = new Model.Attribute { FieldTypeId = FieldTypeCache.Get(new Guid(SystemGuid.FieldType.TEXT)).Id, EntityTypeQualifierColumn = string.Empty, EntityTypeQualifierValue = string.Empty, Key = key, Name = key.SplitCase() }; attributeService.Add(attribute); rockContext.SaveChanges(); } attributeValue = new AttributeValue { IsSystem = false, AttributeId = attribute.Id }; attributeValueService.Add(attributeValue); } attributeValue.Value = value; rockContext.SaveChanges(); } lock ( _obj ) { _attributeIds = null; } AttributeValues.AddOrUpdate(key, value, (k, v) => value); var attributeCache = Attributes.FirstOrDefault(a => a.Key.Equals(key, StringComparison.OrdinalIgnoreCase)); if (attributeCache != null) { value = attributeCache.FieldType.Field.FormatValue(null, value, attributeCache.QualifierValues, false); } AttributeValuesFormatted.AddOrUpdate(key, value, (k, v) => value); }
/// <summary> /// Saves a <see cref="Rock.Model.Person">Person's</see> user preference setting by key. /// </summary> /// <param name="person">The <see cref="Rock.Model.Person"/> who the preference value belongs to.</param> /// <param name="key">A <see cref="System.String"/> representing the key (name) of the preference setting. </param> /// <param name="values">A list of <see cref="System.String"/> values representing the value of the preference setting.</param> /// <param name="personId">A <see cref="System.Int32"/> representing the Id of the <see cref="Rock.Model.Person"/> saving the setting.</param> public void SaveUserPreference(Person person, string key, List <string> values, int?personId) { int?PersonEntityTypeId = Rock.Web.Cache.EntityTypeCache.Read(Person.USER_VALUE_ENTITY).Id; var attributeService = new Model.AttributeService(); var attribute = attributeService.Get(PersonEntityTypeId, string.Empty, string.Empty, key); if (attribute == null) { var fieldTypeService = new Model.FieldTypeService(); var fieldType = fieldTypeService.GetByGuid(new Guid(Rock.SystemGuid.FieldType.TEXT)); attribute = new Model.Attribute(); attribute.IsSystem = false; attribute.EntityTypeId = PersonEntityTypeId; attribute.EntityTypeQualifierColumn = string.Empty; attribute.EntityTypeQualifierValue = string.Empty; attribute.Key = key; attribute.Name = key; attribute.DefaultValue = string.Empty; attribute.IsMultiValue = false; attribute.IsRequired = false; attribute.Description = string.Empty; attribute.FieldTypeId = fieldType.Id; attribute.Order = 0; attributeService.Add(attribute, personId); attributeService.Save(attribute, personId); } var attributeValueService = new Model.AttributeValueService(); // Delete existing values var attributeValues = attributeValueService.GetByAttributeIdAndEntityId(attribute.Id, person.Id).ToList(); foreach (var attributeValue in attributeValues) { attributeValueService.Delete(attributeValue, personId); attributeValueService.Save(attributeValue, personId); } // Save new values foreach (var value in values.Where(v => !string.IsNullOrWhiteSpace(v))) { var attributeValue = new Model.AttributeValue(); attributeValue.AttributeId = attribute.Id; attributeValue.EntityId = person.Id; attributeValue.Value = value; attributeValueService.Add(attributeValue, personId); attributeValueService.Save(attributeValue, personId); } }
/// <summary> /// Copys the current Token's attributes in the target entity /// </summary> /// <param name="token">The Token to copy attributes</param> /// <param name="entity">The entity into which attributes will be copied.</param> private static void CopyTokenAttributes(SaxToken?token, AttributedEntity entity) { if (token != null && token.Value.attributes != null) { foreach (var e in token.Value.attributes) { Model.Attribute attr = new Model.Attribute(); attr.Name = e.Key.ToLower(); attr.Value = e.Value.Trim(); entity.Attributes[e.Key] = attr; } } }
public IHttpActionResult GetByID(int idAttribute = 0) { var item = ServiceContext.AttributeService.GetById(idAttribute); if (item == null) { item = new Model.Attribute() { Status = 1 } } ; item.Culture = ServiceContext.AttributeService.ListAttributeCulture(idAttribute); return(Ok(item)); }
public override HttpResponseMessage Post([FromBody] Model.Attribute value) { // if any Categories are included in the Post, we'll need to fetch them from the database so that that EF inserts them into AttributeCategory correct if (value.Categories != null && value.Categories.Any()) { var fetchedCategories = new CategoryService(Service.Context as Rock.Data.RockContext).GetByIds(value.Categories.Select(a => a.Id).ToList()).ToList(); value.Categories.Clear(); foreach (var cat in fetchedCategories) { value.Categories.Add(cat); } } var result = base.Post(value); return(result); }
public int GetInsert(string attributeName) { string acronym = attributeName.Replace(' ', '_').ToUpper().Trim(); int idAttribute = 0; using (var context = new ImobeNetContext()) { var attribute = (from ac in context.Attribute where ac.Acronym == acronym select ac).FirstOrDefault(); if (attribute == null) { var attr = new Model.Attribute() { Acronym = acronym, ModifyDate = DateTime.Now, CreateDate = DateTime.Now, CreatedBy = "AUTO", ModifiedBy = "AUTO", Status = (short)Enums.StatusType.Pending, IDIcon = 6, //Blank Required = false }; this.Insert(attr); idAttribute = attr.IDAttribute.Value; foreach (var culture in ServiceContext.CultureService.GetAll()) { ServiceContext.AttributeCultureService.Insert(new AttributeCulture() { Name = attributeName, IDCulture = culture.IDCulture, IDAttribute = attr.IDAttribute.Value }); } } else { idAttribute = attribute.IDAttribute.Value; } } return(idAttribute); }
public IHttpActionResult Save([FromBody] Model.Attribute record) { string message = string.Empty; if (!ModelState.IsValid) { return(BadRequest(ModelState)); } else { ICollection <AttributeCulture> attributeCulture = null; if (record.Culture != null) { attributeCulture = (from c in record.Culture select new AttributeCulture { IDAttribute = (record.IDAttribute.HasValue) ? record.IDAttribute.Value : 0, Name = c.Name, Value = c.Value, IDCulture = c.IDCulture }).ToList(); } if (record.IDAttribute.HasValue) { record.ModifyDate = DateTime.Now; record.ModifiedBy = SiteContext.ActiveUserName; Services.ServiceContext.AttributeService.Update(record, attributeCulture); message = Internationalization.Message.Record_Updated_Successfully; } else { record.CreateDate = DateTime.Now; record.CreatedBy = SiteContext.ActiveUserName; record.ModifyDate = DateTime.Now; record.ModifiedBy = SiteContext.ActiveUserName; Services.ServiceContext.AttributeService.Insert(record, attributeCulture); message = Internationalization.Message.Record_Inserted_Successfully; } return(Ok(new { Message = message })); } }
public override HttpResponseMessage Post([FromBody] Model.Attribute value) { // if any Categories are included in the Post, we'll need to fetch them from the database so that that EF inserts them into AttributeCategory correct if (value.Categories != null && value.Categories.Any()) { var fetchedCategories = new CategoryService(Service.Context as Rock.Data.RockContext).GetByIds(value.Categories.Select(a => a.Id).ToList()).ToList(); value.Categories.Clear(); foreach (var cat in fetchedCategories) { value.Categories.Add(cat); } // Since changes to Categories isn't tracked by ChangeTracker, set the ModifiedDateTime just in case Categories changed value.ModifiedDateTime = RockDateTime.Now; } var result = base.Post(value); return(result); }
public Node Visit(Condition that, Dictionary <string, Node> data) { Node targeNode = null; Model.Attribute nameAttribute = that.Attributes[AttributeNames.Node]; string node = nameAttribute.Value.ToString(); if (!data.ContainsKey(node)) {//We must create a new node. Node n = new Node(); n.Attributes[AttributeNames.Node] = nameAttribute; data[node] = n; targeNode = n; } else { targeNode = data[node]; } targeNode.AddPatternWithGuard(CurrentPattern, that, CurrentSkeleton); return(targeNode); }
public override void Put(int id, [FromBody] Model.Attribute value) { base.Put(id, value); }
/// <summary> /// Saves a <see cref="Rock.Model.Person">Person's</see> user preference setting by key. /// </summary> /// <param name="person">The <see cref="Rock.Model.Person"/> who the preference value belongs to.</param> /// <param name="key">A <see cref="System.String"/> representing the key (name) of the preference setting. </param> /// <param name="values">A list of <see cref="System.String"/> values representing the value of the preference setting.</param> /// <param name="personId">A <see cref="System.Int32"/> representing the Id of the <see cref="Rock.Model.Person"/> saving the setting.</param> public void SaveUserPreference(Person person, string key, List<string> values, int? personId) { int? PersonEntityTypeId = Rock.Web.Cache.EntityTypeCache.Read( Person.USER_VALUE_ENTITY ).Id; var attributeService = new Model.AttributeService(); var attribute = attributeService.Get( PersonEntityTypeId, string.Empty, string.Empty, key ); if ( attribute == null ) { var fieldTypeService = new Model.FieldTypeService(); var fieldType = fieldTypeService.GetByGuid( new Guid( Rock.SystemGuid.FieldType.TEXT ) ); attribute = new Model.Attribute(); attribute.IsSystem = false; attribute.EntityTypeId = PersonEntityTypeId; attribute.EntityTypeQualifierColumn = string.Empty; attribute.EntityTypeQualifierValue = string.Empty; attribute.Key = key; attribute.Name = key; attribute.DefaultValue = string.Empty; attribute.IsMultiValue = false; attribute.IsRequired = false; attribute.Description = string.Empty; attribute.FieldTypeId = fieldType.Id; attribute.Order = 0; attributeService.Add( attribute, personId ); attributeService.Save( attribute, personId ); } var attributeValueService = new Model.AttributeValueService(); // Delete existing values var attributeValues = attributeValueService.GetByAttributeIdAndEntityId( attribute.Id, person.Id ).ToList(); foreach ( var attributeValue in attributeValues ) { attributeValueService.Delete( attributeValue, personId ); attributeValueService.Save( attributeValue, personId ); } // Save new values foreach ( var value in values.Where( v => !string.IsNullOrWhiteSpace( v ) ) ) { var attributeValue = new Model.AttributeValue(); attributeValue.AttributeId = attribute.Id; attributeValue.EntityId = person.Id; attributeValue.Value = value; attributeValueService.Add( attributeValue, personId ); attributeValueService.Save( attributeValue, personId ); } }
/// <summary> /// Saves a <see cref="Rock.Model.Person">Person's</see> user preference setting by key. /// </summary> /// <param name="person">The <see cref="Rock.Model.Person"/> who the preference value belongs to.</param> /// <param name="key">A <see cref="System.String"/> representing the key (name) of the preference setting.</param> /// <param name="value">The value.</param> public static void SaveUserPreference( Person person, string key, string value ) { int? PersonEntityTypeId = Rock.Web.Cache.EntityTypeCache.Read( Person.USER_VALUE_ENTITY ).Id; using ( var rockContext = new RockContext() ) { var attributeService = new Model.AttributeService( rockContext ); var attribute = attributeService.Get( PersonEntityTypeId, string.Empty, string.Empty, key ); if ( attribute == null ) { var fieldTypeService = new Model.FieldTypeService( rockContext ); var fieldType = FieldTypeCache.Read( Rock.SystemGuid.FieldType.TEXT.AsGuid() ); attribute = new Model.Attribute(); attribute.IsSystem = false; attribute.EntityTypeId = PersonEntityTypeId; attribute.EntityTypeQualifierColumn = string.Empty; attribute.EntityTypeQualifierValue = string.Empty; attribute.Key = key; attribute.Name = key; attribute.IconCssClass = string.Empty; attribute.DefaultValue = string.Empty; attribute.IsMultiValue = false; attribute.IsRequired = false; attribute.Description = string.Empty; attribute.FieldTypeId = fieldType.Id; attribute.Order = 0; attributeService.Add( attribute ); rockContext.SaveChanges(); } var attributeValueService = new Model.AttributeValueService( rockContext ); var attributeValue = attributeValueService.GetByAttributeIdAndEntityId( attribute.Id, person.Id ); if ( string.IsNullOrWhiteSpace( value ) ) { // Delete existing value if no existing value if ( attributeValue != null ) { attributeValueService.Delete( attributeValue ); } } else { if ( attributeValue == null ) { attributeValue = new Model.AttributeValue(); attributeValue.AttributeId = attribute.Id; attributeValue.EntityId = person.Id; attributeValueService.Add( attributeValue ); } attributeValue.Value = value; } rockContext.SaveChanges(); } }
public override void Put(int id, [FromBody] Model.Attribute value) { base.Put(id, value); AttributeCache.Flush(id); AttributeCache.FlushEntityAttributes(); }
public void InsertUpdate(Model.Attribute entity, ICollection <AttributeCulture> attributeCulture, Enums.ActionType actionType) { using (var context = new ImobeNetContext()) { //using (TransactionScope scopeOfTransaction = new TransactionScope()) using (var transaction = context.Database.BeginTransaction()) { try { var attributeRepository = new BaseRepository <Model.Attribute>(context); var AttributeCultureRepository = new BaseRepository <AttributeCulture>(context); if (actionType == Enums.ActionType.Insert) { attributeRepository.Insert(entity); } else if (actionType == Enums.ActionType.Update) { attributeRepository.Update(entity); } context.SaveChanges(); var Attributes = (from a in context.AttributeCulture where a.IDAttribute == entity.IDAttribute select a); //Deletando os idiomas if (Attributes != null && Attributes.Count() > 0) { foreach (var Attribute in Attributes) { AttributeCultureRepository.Delete(Attribute); } context.SaveChanges(); } // inserindo novos foreach (var Attribute in attributeCulture) { Attribute.IDAttribute = entity.IDAttribute.Value; AttributeCultureRepository.Insert(Attribute); } context.SaveChanges(); transaction.Commit(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } finally { transaction.Dispose(); } } } }
public Task <Result <Model.Attribute, Error.ExceptionalError> > UpdateAttribute(Guid id, Model.Attribute attribute) { var request = RequestAdapter <ResponsesAdapter.AttributeResponse, Model.Attribute> .Build( "mutation UpdateAttribute($attribute: AttributeInput!) { updateAttribute(attribute: $attribute) { id } }", response => response.Attribute !, new { attribute = SerializeAttribute(attribute) }, "UpdateAttribute" ); return(_client.SendQueryAsync(request)); }
/// <summary> /// Adds or Updates a <see cref="Rock.Model.Attribute" /> item for the attribute. /// </summary> /// <param name="property">The property.</param> /// <param name="entityTypeId">The entity type id.</param> /// <param name="entityQualifierColumn">The entity qualifier column.</param> /// <param name="entityQualifierValue">The entity qualifier value.</param> /// <param name="rockContext">The rock context.</param> /// <returns></returns> /// <remarks> /// If a rockContext value is included, this method will save any previous changes made to the context /// </remarks> private static bool UpdateAttribute(FieldAttribute property, int?entityTypeId, string entityQualifierColumn, string entityQualifierValue, RockContext rockContext = null) { bool updated = false; rockContext = rockContext ?? new RockContext(); var attributeService = new AttributeService(rockContext); var attributeQualifierService = new AttributeQualifierService(rockContext); var fieldTypeService = new FieldTypeService(rockContext); var categoryService = new CategoryService(rockContext); var propertyCategories = property.Category.SplitDelimitedValues(false).ToList(); // Look for an existing attribute record based on the entity, entityQualifierColumn and entityQualifierValue Model.Attribute attribute = attributeService.Get(entityTypeId, entityQualifierColumn, entityQualifierValue, property.Key); if (attribute == null) { // If an existing attribute record doesn't exist, create a new one updated = true; attribute = new Model.Attribute(); attribute.EntityTypeId = entityTypeId; attribute.EntityTypeQualifierColumn = entityQualifierColumn; attribute.EntityTypeQualifierValue = entityQualifierValue; attribute.Key = property.Key; attribute.IconCssClass = string.Empty; attribute.IsGridColumn = false; } else { // Check to see if the existing attribute record needs to be updated if (attribute.Name != property.Name || attribute.DefaultValue != property.DefaultValue || attribute.Description != property.Description || attribute.Order != property.Order || attribute.FieldType.Assembly != property.FieldTypeAssembly || attribute.FieldType.Class != property.FieldTypeClass || attribute.IsRequired != property.IsRequired) { updated = true; } // Check category else if (attribute.Categories.Select(c => c.Name).Except(propertyCategories).Any() || propertyCategories.Except(attribute.Categories.Select(c => c.Name)).Any()) { updated = true; } // Check the qualifier values else if (attribute.AttributeQualifiers.Select(q => q.Key).Except(property.FieldConfigurationValues.Select(c => c.Key)).Any() || property.FieldConfigurationValues.Select(c => c.Key).Except(attribute.AttributeQualifiers.Select(q => q.Key)).Any()) { updated = true; } else { foreach (var attributeQualifier in attribute.AttributeQualifiers) { if (!property.FieldConfigurationValues.ContainsKey(attributeQualifier.Key) || property.FieldConfigurationValues[attributeQualifier.Key].Value != attributeQualifier.Value) { updated = true; break; } } } } if (updated) { // Update the attribute attribute.Name = property.Name; attribute.Description = property.Description; attribute.DefaultValue = property.DefaultValue; attribute.Order = property.Order; attribute.IsRequired = property.IsRequired; attribute.Categories.Clear(); if (propertyCategories.Any()) { foreach (string propertyCategory in propertyCategories) { int attributeEntityTypeId = EntityTypeCache.Read(typeof(Rock.Model.Attribute)).Id; var category = categoryService.Get(propertyCategory, attributeEntityTypeId, "EntityTypeId", entityTypeId.ToString()).FirstOrDefault(); if (category == null) { category = new Category(); category.Name = propertyCategory; category.EntityTypeId = attributeEntityTypeId; category.EntityTypeQualifierColumn = "EntityTypeId"; category.EntityTypeQualifierValue = entityTypeId.ToString(); category.Order = 0; } attribute.Categories.Add(category); } } foreach (var qualifier in attribute.AttributeQualifiers.ToList()) { attributeQualifierService.Delete(qualifier); } attribute.AttributeQualifiers.Clear(); foreach (var configValue in property.FieldConfigurationValues) { var qualifier = new Model.AttributeQualifier(); qualifier.Key = configValue.Key; qualifier.Value = configValue.Value.Value; attribute.AttributeQualifiers.Add(qualifier); } // Try to set the field type by searching for an existing field type with the same assembly and class name if (attribute.FieldType == null || attribute.FieldType.Assembly != property.FieldTypeAssembly || attribute.FieldType.Class != property.FieldTypeClass) { attribute.FieldType = fieldTypeService.Queryable().FirstOrDefault(f => f.Assembly == property.FieldTypeAssembly && f.Class == property.FieldTypeClass); } // If this is a new attribute, add it, otherwise remove the exiting one from the cache if (attribute.Id == 0) { attributeService.Add(attribute); } else { AttributeCache.Flush(attribute.Id); } rockContext.SaveChanges(); return(true); } else { return(false); } }
public void Insert(Model.Attribute entity, ICollection <AttributeCulture> attributeCulture) { this.InsertUpdate(entity, attributeCulture, Enums.ActionType.Insert); }
/// <summary> /// Adds or Updates a <see cref="Rock.Model.Attribute" /> item for the attribute. /// </summary> /// <param name="property">The property.</param> /// <param name="entityTypeId">The entity type id.</param> /// <param name="entityQualifierColumn">The entity qualifier column.</param> /// <param name="entityQualifierValue">The entity qualifier value.</param> /// <param name="rockContext">The rock context.</param> /// <returns></returns> /// <remarks> /// If a rockContext value is included, this method will save any previous changes made to the context /// </remarks> private static bool UpdateAttribute( FieldAttribute property, int? entityTypeId, string entityQualifierColumn, string entityQualifierValue, RockContext rockContext = null ) { bool updated = false; rockContext = rockContext ?? new RockContext(); var attributeService = new AttributeService( rockContext ); var attributeQualifierService = new AttributeQualifierService( rockContext ); var fieldTypeService = new FieldTypeService(rockContext); var categoryService = new CategoryService( rockContext ); var propertyCategories = property.Category.SplitDelimitedValues( false ).ToList(); // Look for an existing attribute record based on the entity, entityQualifierColumn and entityQualifierValue Model.Attribute attribute = attributeService.Get( entityTypeId, entityQualifierColumn, entityQualifierValue, property.Key ); if ( attribute == null ) { // If an existing attribute record doesn't exist, create a new one updated = true; attribute = new Model.Attribute(); attribute.EntityTypeId = entityTypeId; attribute.EntityTypeQualifierColumn = entityQualifierColumn; attribute.EntityTypeQualifierValue = entityQualifierValue; attribute.Key = property.Key; attribute.IconCssClass = string.Empty; attribute.IsGridColumn = false; } else { // Check to see if the existing attribute record needs to be updated if ( attribute.Name != property.Name || attribute.DefaultValue != property.DefaultValue || attribute.Description != property.Description || attribute.Order != property.Order || attribute.FieldType.Assembly != property.FieldTypeAssembly || attribute.FieldType.Class != property.FieldTypeClass || attribute.IsRequired != property.IsRequired ) { updated = true; } // Check category else if ( attribute.Categories.Select( c => c.Name ).Except( propertyCategories ).Any() || propertyCategories.Except( attribute.Categories.Select( c => c.Name ) ).Any() ) { updated = true; } // Check the qualifier values else if ( attribute.AttributeQualifiers.Select( q => q.Key ).Except( property.FieldConfigurationValues.Select( c => c.Key ) ).Any() || property.FieldConfigurationValues.Select( c => c.Key ).Except( attribute.AttributeQualifiers.Select( q => q.Key ) ).Any() ) { updated = true; } else { foreach ( var attributeQualifier in attribute.AttributeQualifiers ) { if ( !property.FieldConfigurationValues.ContainsKey( attributeQualifier.Key ) || property.FieldConfigurationValues[attributeQualifier.Key].Value != attributeQualifier.Value ) { updated = true; break; } } } } if ( updated ) { // Update the attribute attribute.Name = property.Name; attribute.Description = property.Description; attribute.DefaultValue = property.DefaultValue; attribute.Order = property.Order; attribute.IsRequired = property.IsRequired; attribute.Categories.Clear(); if ( propertyCategories.Any() ) { foreach ( string propertyCategory in propertyCategories ) { int attributeEntityTypeId = EntityTypeCache.Read( typeof( Rock.Model.Attribute ) ).Id; var category = categoryService.Get( propertyCategory, attributeEntityTypeId, "EntityTypeId", entityTypeId.ToString() ).FirstOrDefault(); if ( category == null ) { category = new Category(); category.Name = propertyCategory; category.EntityTypeId = attributeEntityTypeId; category.EntityTypeQualifierColumn = "EntityTypeId"; category.EntityTypeQualifierValue = entityTypeId.ToString(); category.Order = 0; } attribute.Categories.Add( category ); } } foreach ( var qualifier in attribute.AttributeQualifiers.ToList() ) { attributeQualifierService.Delete( qualifier ); } attribute.AttributeQualifiers.Clear(); foreach ( var configValue in property.FieldConfigurationValues ) { var qualifier = new Model.AttributeQualifier(); qualifier.Key = configValue.Key; qualifier.Value = configValue.Value.Value; attribute.AttributeQualifiers.Add( qualifier ); } // Try to set the field type by searching for an existing field type with the same assembly and class name if ( attribute.FieldType == null || attribute.FieldType.Assembly != property.FieldTypeAssembly || attribute.FieldType.Class != property.FieldTypeClass ) { attribute.FieldType = fieldTypeService.Queryable().FirstOrDefault( f => f.Assembly == property.FieldTypeAssembly && f.Class == property.FieldTypeClass ); } // If this is a new attribute, add it, otherwise remove the exiting one from the cache if ( attribute.Id == 0 ) { attributeService.Add( attribute ); } else { AttributeCache.Flush( attribute.Id ); } rockContext.SaveChanges(); return true; } else { return false; } }
/// <summary> /// Saves the user preferences. /// </summary> /// <param name="person">The person.</param> /// <param name="preferences">The preferences.</param> public static void SaveUserPreferences( Person person, Dictionary<string, string> preferences ) { if ( preferences != null ) { int? personEntityTypeId = Rock.Web.Cache.EntityTypeCache.Read( Person.USER_VALUE_ENTITY ).Id; using ( var rockContext = new RockContext() ) { var attributeService = new Model.AttributeService( rockContext ); var attributes = attributeService .Get( personEntityTypeId, string.Empty, string.Empty ) .Where( a => preferences.Keys.Contains( a.Key ) ) .ToList(); bool wasUpdated = false; foreach ( var attributeKeyValue in preferences ) { var attribute = attributes.FirstOrDefault( a => a.Key == attributeKeyValue.Key ); if ( attribute == null ) { var fieldTypeService = new Model.FieldTypeService( rockContext ); var fieldType = FieldTypeCache.Read( Rock.SystemGuid.FieldType.TEXT.AsGuid() ); attribute = new Model.Attribute(); attribute.IsSystem = false; attribute.EntityTypeId = personEntityTypeId; attribute.EntityTypeQualifierColumn = string.Empty; attribute.EntityTypeQualifierValue = string.Empty; attribute.Key = attributeKeyValue.Key; attribute.Name = attributeKeyValue.Key; attribute.IconCssClass = string.Empty; attribute.DefaultValue = string.Empty; attribute.IsMultiValue = false; attribute.IsRequired = false; attribute.Description = string.Empty; attribute.FieldTypeId = fieldType.Id; attribute.Order = 0; attributeService.Add( attribute ); wasUpdated = true; } } if ( wasUpdated ) { // Save any new attributes rockContext.SaveChanges(); // Requery attributes ( so they all have ids ) attributes = attributeService .Get( personEntityTypeId, string.Empty, string.Empty ) .Where( a => preferences.Keys.Contains( a.Key ) ) .ToList(); } var attributeIds = attributes.Select( a => a.Id ).ToList(); var attributeValueService = new Model.AttributeValueService( rockContext ); var attributeValues = attributeValueService.Queryable( "Attribute" ) .Where( v => attributeIds.Contains( v.AttributeId ) && v.EntityId.HasValue && v.EntityId.Value == person.Id ) .ToList(); wasUpdated = false; foreach ( var attributeKeyValue in preferences ) { if ( string.IsNullOrWhiteSpace( attributeKeyValue.Value ) ) { foreach ( var attributeValue in attributeValues .Where( v => v.Attribute != null && v.Attribute.Key == attributeKeyValue.Key ) .ToList() ) { attributeValueService.Delete( attributeValue ); attributeValues.Remove( attributeValue ); wasUpdated = true; } } else { var attributeValue = attributeValues .Where( v => v.Attribute != null && v.Attribute.Key == attributeKeyValue.Key ) .FirstOrDefault(); if ( attributeValue == null ) { var attribute = attributes .Where( a => a.Key == attributeKeyValue.Key ) .FirstOrDefault(); if ( attribute != null ) { attributeValue = new Model.AttributeValue(); attributeValue.AttributeId = attribute.Id; attributeValue.EntityId = person.Id; attributeValueService.Add( attributeValue ); } } wasUpdated = wasUpdated || ( attributeValue.Value != attributeKeyValue.Value ); attributeValue.Value = attributeKeyValue.Value; } } if ( wasUpdated ) { rockContext.SaveChanges(); } } } }
/// <summary> /// Adds or Updates a <see cref="Rock.Model.Attribute" /> item for the attribute. /// </summary> /// <param name="property">The property.</param> /// <param name="entityTypeId">The entity type id.</param> /// <param name="entityQualifierColumn">The entity qualifier column.</param> /// <param name="entityQualifierValue">The entity qualifier value.</param> /// <param name="currentPersonId">The current person id.</param> /// <returns></returns> private static bool UpdateAttribute(FieldAttribute property, int?entityTypeId, string entityQualifierColumn, string entityQualifierValue, int?currentPersonId) { bool updated = false; Model.AttributeService attributeService = new Model.AttributeService(); Model.FieldTypeService fieldTypeService = new Model.FieldTypeService(); // Look for an existing attribute record based on the entity, entityQualifierColumn and entityQualifierValue Model.Attribute attribute = attributeService.Get( entityTypeId, entityQualifierColumn, entityQualifierValue, property.Key); if (attribute == null) { // If an existing attribute record doesn't exist, create a new one updated = true; attribute = new Model.Attribute(); attribute.EntityTypeId = entityTypeId; attribute.EntityTypeQualifierColumn = entityQualifierColumn; attribute.EntityTypeQualifierValue = entityQualifierValue; attribute.Key = property.Key; attribute.IsGridColumn = false; } else { // Check to see if the existing attribute record needs to be updated if (attribute.Name != property.Name || attribute.Category != property.Category || attribute.DefaultValue != property.DefaultValue || attribute.Description != property.Description || attribute.Order != property.Order || attribute.FieldType.Assembly != property.FieldTypeAssembly || attribute.FieldType.Class != property.FieldTypeClass || attribute.IsRequired != property.IsRequired) { updated = true; } } if (updated) { // Update the attribute attribute.Name = property.Name; attribute.Category = property.Category; attribute.Description = property.Description; attribute.DefaultValue = property.DefaultValue; attribute.Order = property.Order; attribute.IsRequired = property.IsRequired; // Try to set the field type by searching for an existing field type with the same assembly and class name if (attribute.FieldType == null || attribute.FieldType.Assembly != property.FieldTypeAssembly || attribute.FieldType.Class != property.FieldTypeClass) { attribute.FieldType = fieldTypeService.Queryable().FirstOrDefault(f => f.Assembly == property.FieldTypeAssembly && f.Class == property.FieldTypeClass); } // If this is a new attribute, add it, otherwise remove the exiting one from the cache if (attribute.Id == 0) { attributeService.Add(attribute, currentPersonId); } else { Rock.Web.Cache.AttributeCache.Flush(attribute.Id); } attributeService.Save(attribute, currentPersonId); return(true); } else { return(false); } }
static object SerializeAttribute(Model.Attribute attribute) => new { id = attribute.Id, name = attribute.Name };
protected void Add(Model.Attribute toMove) { Product.Descriptions.Add(new AttributeDescription(string.Empty, attributeId: toMove.Id, attribute: toMove)); Attributes.Remove(toMove); EditContext.NotifyFieldChanged(FieldIdentifier.Create(() => Product.Descriptions)); }