/// <summary> /// Saves the attribute value. /// </summary> /// <param name="context">The context.</param> /// <param name="key">The key.</param> /// <param name="value">The value.</param> private void SaveAttributeValue(RockContext context, string key, string value) { var attributeSvc = new AttributeService(context); var attribute = attributeSvc.GetGlobalAttribute(key); var attributeValueSvc = new AttributeValueService(context); var attributeValue = attributeValueSvc.GetByAttributeIdAndEntityId(attribute.Id, null); if (attributeValue == null && !String.IsNullOrWhiteSpace(value)) { attributeValue = new AttributeValue(); attributeValue.AttributeId = attribute.Id; attributeValue.EntityId = null; attributeValueSvc.Add(attributeValue); } if (attributeValue == null || value.Equals(attributeValue.Value)) { return; } if (String.IsNullOrWhiteSpace(value)) { attributeValueSvc.Delete(attributeValue); } else { attributeValue.Value = value; } context.SaveChanges(); }
/// <summary> /// Job that updates the JobPulse setting with the current date/time. /// This will allow us to notify an admin if the jobs stop running. /// /// Called by the <see cref="IScheduler" /> when a /// <see cref="ITrigger" /> fires that is associated with /// the <see cref="IJob" />. /// </summary> public virtual void Execute(IJobExecutionContext context) { using (new Rock.Data.UnitOfWorkScope()) { AttributeService attribService = new AttributeService(); AttributeValueService attributeValueService = new AttributeValueService(); Rock.Core.Attribute jobPulseAttrib = attribService.GetGlobalAttribute("JobPulse"); Rock.Core.AttributeValue jobPulseAttribValue = jobPulseAttrib.AttributeValues.FirstOrDefault(); // create attribute value if one does not exist if (jobPulseAttribValue == null) { jobPulseAttribValue = new AttributeValue(); jobPulseAttribValue.AttributeId = jobPulseAttrib.Id; attributeValueService.Add(jobPulseAttribValue, null); } // store todays date and time jobPulseAttribValue.Value = DateTime.Now.ToString(); // save attribute attributeValueService.Save(jobPulseAttribValue, null); } }
/// <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); } }
/// <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); }
/// <summary> /// Verifies the attributes. /// </summary> /// <param name="context">The context.</param> /// <param name="categoryId">The category identifier.</param> private void VerifyAttributes(RockContext context, int categoryId) { var attributeService = new AttributeService(context); var category = new CategoryService(context).Get(categoryId); bool hasChanges = false; var serverUrl = attributeService.GetGlobalAttribute(SERVER_URL_KEY); var serverRootPath = attributeService.GetGlobalAttribute(SERVER_ROOT_PATH_KEY); var contentManagerUser = attributeService.GetGlobalAttribute(CONTENT_MANAGER_USER_KEY); var contentManagerPwd = attributeService.GetGlobalAttribute(CONTENT_MANAGER_PWD_KEY); var browserUser = attributeService.GetGlobalAttribute(BROWSER_USER_KEY); var browserPwd = attributeService.GetGlobalAttribute(BROWSER_PWD_KEY); if (serverUrl == null) { serverUrl = new Rock.Model.Attribute(); serverUrl.FieldTypeId = FieldTypeCache.Get(Rock.SystemGuid.FieldType.URL_LINK).Id; serverUrl.IsSystem = false; serverUrl.Name = "Reporting Service URL"; serverUrl.Description = "URL to the SQL Reporting Services Reporting Server endpoint."; serverUrl.Key = SERVER_URL_KEY; serverUrl.IsRequired = false; serverUrl.AllowSearch = false; serverUrl.Categories.Add(category); attributeService.Add(serverUrl); hasChanges = true; } if (serverRootPath == null) { serverRootPath = new Rock.Model.Attribute(); serverRootPath.FieldTypeId = FieldTypeCache.Get(Rock.SystemGuid.FieldType.TEXT).Id; serverRootPath.IsSystem = false; serverRootPath.Name = "Reporting Service Root Folder"; serverRootPath.Key = SERVER_ROOT_PATH_KEY; serverRootPath.Description = "Root/Base folder for Rock reports in reporting services."; serverRootPath.IsRequired = false; serverRootPath.AllowSearch = false; serverRootPath.Categories.Add(category); attributeService.Add(serverRootPath); hasChanges = true; } if (contentManagerUser == null) { contentManagerUser = new Rock.Model.Attribute(); contentManagerUser.FieldTypeId = FieldTypeCache.Get(Rock.SystemGuid.FieldType.TEXT).Id; contentManagerUser.Name = "Reporting Service - Content Manager Username"; contentManagerUser.Key = CONTENT_MANAGER_USER_KEY; contentManagerUser.Description = "The Reporting Server Content Manager (Report Administrator) User Name. (i.e. domain\\user format)"; contentManagerUser.IsRequired = false; contentManagerUser.AllowSearch = false; contentManagerUser.Categories.Add(category); attributeService.Add(contentManagerUser); hasChanges = true; } if (contentManagerPwd == null) { contentManagerPwd = new Rock.Model.Attribute(); contentManagerPwd.FieldTypeId = FieldTypeCache.Get(Rock.SystemGuid.FieldType.ENCRYPTED_TEXT).Id; contentManagerPwd.Name = "Reporting Service - Content Manager Password"; contentManagerPwd.Key = CONTENT_MANAGER_PWD_KEY; contentManagerPwd.Description = "The Content Manager Password."; contentManagerPwd.IsRequired = false; contentManagerPwd.AllowSearch = false; contentManagerPwd.Categories.Add(category); contentManagerPwd.AttributeQualifiers.Add(new AttributeQualifier { IsSystem = false, Key = "ispassword", Value = bool.TrueString }); attributeService.Add(contentManagerPwd); hasChanges = true; } if (browserUser == null) { browserUser = new Rock.Model.Attribute(); browserUser.FieldTypeId = FieldTypeCache.Get(Rock.SystemGuid.FieldType.ENCRYPTED_TEXT).Id; browserUser.Name = "Reporting Service - Browser User"; browserUser.Key = BROWSER_USER_KEY; browserUser.Description = "The Reporting Server Browser (Report Viewer) User Name. (i.e. domain\\user format)"; browserUser.IsRequired = false; browserUser.AllowSearch = false; browserUser.Categories.Add(category); attributeService.Add(browserUser); hasChanges = true; } if (browserPwd == null) { browserPwd = new Rock.Model.Attribute(); browserPwd.FieldTypeId = FieldTypeCache.Get(Rock.SystemGuid.FieldType.ENCRYPTED_TEXT).Id; browserPwd.Name = "Reporting Service - Browser Password"; browserPwd.Key = BROWSER_PWD_KEY; browserPwd.Description = "The Reporting Server Browser Password."; browserPwd.IsRequired = false; browserPwd.AllowSearch = false; browserPwd.Categories.Add(category); browserPwd.AttributeQualifiers.Add(new AttributeQualifier { IsSystem = false, Key = "ispassword", Value = bool.TrueString }); attributeService.Add(browserPwd); hasChanges = true; } if (hasChanges) { context.SaveChanges(); GlobalAttributesCache.Clear(); } }