/// <summary> /// Sets the value. /// </summary> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <param name="currentPersonId">The current person id.</param> /// <param name="saveValue">if set to <c>true</c> [save value].</param> public void SetValue(string key, string value, int?currentPersonId, bool saveValue) { if (saveValue) { // Save new value var attributeValueService = new AttributeValueService(); var attributeValue = attributeValueService.GetGlobalAttributeValue(key); if (attributeValue == null) { var attributeService = new AttributeService(); var attribute = attributeService.GetGlobalAttribute(key); if (attribute != null) { attributeValue = new AttributeValue(); attributeValue.IsSystem = false; attributeValue.AttributeId = attribute.Id; attributeValue.Value = value; attributeValueService.Save(attributeValue, currentPersonId); } } else { attributeValue.Value = value; attributeValueService.Save(attributeValue, currentPersonId); } } // Update cached value if (AttributeValues != null && AttributeValues.Keys.Contains(key)) { string attributeName = AttributeValues[key].Key; AttributeValues[key] = new KeyValuePair <string, string>(attributeName, value); } }
public void Execute(IJobExecutionContext context) { JobDataMap dataMap = context.JobDetail.JobDataMap; string livePlatformUrl = dataMap.GetString("Address") ?? "http://live.newpointe.org/api/v1/events/current"; //Check ChurchOnline Platform API to see if there is a live event using (WebClient wc = new WebClient()) { LivePlatformUrlJson = wc.DownloadString(livePlatformUrl); } dynamic isServiceLive = JsonConvert.DeserializeObject(LivePlatformUrlJson); string isLive = isServiceLive.response.item.isLive.ToString(); // specify which attribute key we want to work with var attributeKey = "LiveService"; //production var attributeValueService = new AttributeValueService(rockContext); // specify NULL as the EntityId since this is a GlobalAttribute var globalAttributeValue = attributeValueService.GetGlobalAttributeValue(attributeKey); if (globalAttributeValue != null) { // save updated value to database globalAttributeValue.Value = isLive; rockContext.SaveChanges(); } }
/// <summary> /// Sets the value. /// </summary> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <param name="currentPersonId">The current person id.</param> /// <param name="saveValue">if set to <c>true</c> [save value].</param> public void SetValue(string key, string value, int?currentPersonId, bool saveValue) { if (saveValue) { using (new Rock.Data.UnitOfWorkScope()) { // Save new value var attributeValueService = new AttributeValueService(); var attributeValue = attributeValueService.GetGlobalAttributeValue(key); if (attributeValue == null) { var attributeService = new AttributeService(); var attribute = attributeService.GetGlobalAttribute(key); if (attribute == null) { attribute = new Rock.Model.Attribute(); attribute.FieldTypeId = FieldTypeCache.Read(new Guid(SystemGuid.FieldType.TEXT)).Id; attribute.EntityTypeQualifierColumn = string.Empty; attribute.EntityTypeQualifierValue = string.Empty; attribute.Key = key; attribute.Name = key.SplitCase(); attributeService.Add(attribute, currentPersonId); attributeService.Save(attribute, currentPersonId); Attributes.Add(AttributeCache.Read(attribute.Id)); } attributeValue = new AttributeValue(); attributeValueService.Add(attributeValue, currentPersonId); attributeValue.IsSystem = false; attributeValue.AttributeId = attribute.Id; if (!AttributeValues.Keys.Contains(key)) { AttributeValues.Add(key, new KeyValuePair <string, string>(attribute.Name, value)); } } attributeValue.Value = value; attributeValueService.Save(attributeValue, currentPersonId); } } var attributeCache = Attributes.FirstOrDefault(a => a.Key.Equals(key, StringComparison.OrdinalIgnoreCase)); if (attributeCache != null) // (Should never be null) { if (AttributeValues.Keys.Contains(key)) { AttributeValues[key] = new KeyValuePair <string, string>(attributeCache.Name, value); } else { AttributeValues.Add(key, new KeyValuePair <string, string>(attributeCache.Name, value)); } } }
/// <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); }