Ejemplo n.º 1
0
        public virtual HttpResponseMessage SetAttributeValue(int id, string attributeKey, string attributeValue)
        {
            T model;

            if (!Service.TryGet(id, out model))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            CheckCanEdit(model);

            IHasAttributes modelWithAttributes = model as IHasAttributes;

            if (modelWithAttributes != null)
            {
                using (var rockContext = new RockContext())
                {
                    modelWithAttributes.LoadAttributes(rockContext);
                    Rock.Web.Cache.AttributeCache attributeCache = modelWithAttributes.Attributes.ContainsKey(attributeKey) ? modelWithAttributes.Attributes[attributeKey] : null;

                    if (attributeCache != null)
                    {
                        if (!attributeCache.IsAuthorized(Rock.Security.Authorization.EDIT, this.GetPerson()))
                        {
                            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden)
                            {
                                ReasonPhrase = string.Format("Not authorized to edit {0} on {1}", modelWithAttributes.GetType().GetFriendlyTypeName(), attributeKey)
                            });
                        }

                        Rock.Attribute.Helper.SaveAttributeValue(modelWithAttributes, attributeCache, attributeValue, rockContext);
                        var response = ControllerContext.Request.CreateResponse(HttpStatusCode.Accepted, modelWithAttributes.Id);
                        return(response);
                    }
                    else
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                        {
                            ReasonPhrase = string.Format("{0} does not have a {1} attribute", modelWithAttributes.GetType().GetFriendlyTypeName(), attributeKey)
                        });
                    }
                }
            }
            else
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    ReasonPhrase = "specified item does not have attributes"
                });
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Saves an attribute value.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="attribute">The attribute.</param>
        /// <param name="newValues">The new values.</param>
        /// <param name="personId">The person id.</param>
        public static void SaveAttributeValues(IHasAttributes model, Rock.Web.Cache.AttributeCache attribute, List <Rock.Model.AttributeValueDto> newValues, int?personId)
        {
            Model.AttributeValueService attributeValueService = new Model.AttributeValueService();

            var attributeValues = attributeValueService.GetByAttributeIdAndEntityId(attribute.Id, model.Id).ToList();
            int i = 0;

            while (i < attributeValues.Count || i < newValues.Count)
            {
                Rock.Model.AttributeValue attributeValue;

                if (i < attributeValues.Count)
                {
                    attributeValue = attributeValues[i];
                }
                else
                {
                    attributeValue             = new Rock.Model.AttributeValue();
                    attributeValue.AttributeId = attribute.Id;
                    attributeValue.EntityId    = model.Id;
                    attributeValue.Order       = i;
                    attributeValueService.Add(attributeValue, personId);
                }

                if (i >= newValues.Count)
                {
                    attributeValueService.Delete(attributeValue, personId);
                }
                else
                {
                    if (attributeValue.Value != newValues[i].Value)
                    {
                        attributeValue.Value = newValues[i].Value;
                    }
                    newValues[i] = new Rock.Model.AttributeValueDto(attributeValue);
                }

                attributeValueService.Save(attributeValue, personId);


                i++;
            }

            model.AttributeValues[attribute.Key] = newValues;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Saves an attribute value.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="attribute">The attribute.</param>
        /// <param name="newValue">The new value.</param>
        /// <param name="personId">The person id.</param>
        public static void SaveAttributeValue(IHasAttributes model, Rock.Web.Cache.AttributeCache attribute, string newValue, int?personId)
        {
            Model.AttributeValueService attributeValueService = new Model.AttributeValueService();

            var attributeValue = attributeValueService.GetByAttributeIdAndEntityId(attribute.Id, model.Id).FirstOrDefault();

            if (attributeValue == null)
            {
                attributeValue             = new Rock.Model.AttributeValue();
                attributeValue.AttributeId = attribute.Id;
                attributeValue.EntityId    = model.Id;
                attributeValue.Order       = 0;
                attributeValueService.Add(attributeValue, personId);
            }

            attributeValue.Value = newValue;

            attributeValueService.Save(attributeValue, personId);

            model.AttributeValues[attribute.Key] = new List <Rock.Model.AttributeValueDto>()
            {
                new Rock.Model.AttributeValueDto(attributeValue)
            };
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets the Attribute Query Expression to be used with an Entity Query
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="qry">The qry.</param>
        /// <param name="filterControl">The filter control.</param>
        /// <param name="attribute">The attribute.</param>
        /// <param name="serviceInstance"></param>
        /// <param name="filterMode">The filter mode.</param>
        /// <returns></returns>
        public virtual IQueryable <T> ApplyAttributeQueryFilter <T>(IQueryable <T> qry, Control filterControl, Rock.Web.Cache.AttributeCache attribute, IService serviceInstance, Rock.Reporting.FilterMode filterMode) where T : Rock.Data.Entity <T>, new()
        {
            if (filterControl == null)
            {
                return(qry);
            }

            var filterValues = GetFilterValues(filterControl, attribute.QualifierValues, filterMode);
            var entityFields = EntityHelper.GetEntityFields(typeof(T));
            var entityField  = entityFields.Where(a => a.FieldKind == FieldKind.Attribute && a.AttributeGuid == attribute.Guid).FirstOrDefault();

            if (entityField == null)
            {
                entityField = EntityHelper.GetEntityFieldForAttribute(attribute, false);
            }

            var parameterExpression = serviceInstance.ParameterExpression;
            var attributeExpression = Rock.Utility.ExpressionHelper.GetAttributeExpression(serviceInstance, parameterExpression, entityField, filterValues);

            qry = qry.Where(parameterExpression, attributeExpression);

            return(qry);
        }
 public AttributeInstanceValues(Rock.Attribute.IHasAttributes model, Rock.Web.Cache.AttributeCache attribute, int?currentPersonId)
 {
     _model           = model;
     _attribute       = attribute;
     _currentPersonId = currentPersonId;
 }
 public AttributeInstanceValues( Rock.Attribute.IHasAttributes model, Rock.Web.Cache.AttributeCache attribute, int? currentPersonId )
 {
     _model = model;
     _attribute = attribute;
     _currentPersonId = currentPersonId;
 }