private async Task <UpdateResponse> UpdateAttribute(RepresentationAttribute source, RepresentationAttribute target, string resourceType)
        {
            var result        = new UpdateResponse();
            var complexSource = source as ComplexRepresentationAttribute;
            var complexTarget = target as ComplexRepresentationAttribute;

            if (complexTarget != null)
            {
                var schemaAttribute = complexTarget.SchemaAttribute;
                if (schemaAttribute.MultiValued)
                {
                    // Check mutability
                    if (schemaAttribute.Mutability == Common.Constants.SchemaAttributeMutability.Immutable)
                    {
                        if (complexTarget.CompareTo(complexSource) != 0)
                        {
                            result.SetError(_errorResponseFactory.CreateError(string.Format(ErrorMessages.TheImmutableAttributeCannotBeUpdated, schemaAttribute.Name),
                                                                              HttpStatusCode.BadRequest,
                                                                              Common.Constants.ScimTypeValues.Mutability));
                            return(result);
                        }
                    }

                    // Check uniqueness
                    if (schemaAttribute.Uniqueness == Common.Constants.SchemaAttributeUniqueness.Server)
                    {
                        var filter      = _filterParser.Parse(complexTarget.FullPath);
                        var uniqueAttrs = await _representationStore.SearchValues(resourceType, filter);

                        if (uniqueAttrs.Any())
                        {
                            if (uniqueAttrs.Any(a => a.CompareTo(complexTarget) == 0))
                            {
                                result.SetError(_errorResponseFactory.CreateError(
                                                    string.Format(ErrorMessages.TheAttributeMustBeUnique, complexTarget.SchemaAttribute.Name),
                                                    HttpStatusCode.BadRequest,
                                                    Common.Constants.ScimTypeValues.Uniqueness));
                                return(result);
                            }
                        }
                    }
                }

                complexSource.Values = complexTarget.Values;
                return(result);
            }

            // Check mutability
            if (target.SchemaAttribute.Mutability == Common.Constants.SchemaAttributeMutability.Immutable)
            {
                if (source.CompareTo(target) != 0)
                {
                    result.SetError(_errorResponseFactory.CreateError(string.Format(ErrorMessages.TheImmutableAttributeCannotBeUpdated, target.SchemaAttribute.Name),
                                                                      HttpStatusCode.BadRequest,
                                                                      Common.Constants.ScimTypeValues.Mutability));
                    return(result);
                }
            }

            // Check uniqueness
            if (target.SchemaAttribute.Uniqueness == Common.Constants.SchemaAttributeUniqueness.Server)
            {
                var filter      = _filterParser.Parse(target.FullPath);
                var uniqueAttrs = await _representationStore.SearchValues(resourceType, filter);

                if (uniqueAttrs.Any())
                {
                    if (uniqueAttrs.Any(a => a.CompareTo(target) == 0))
                    {
                        result.SetError(_errorResponseFactory.CreateError(
                                            string.Format(ErrorMessages.TheAttributeMustBeUnique, target.SchemaAttribute.Name),
                                            HttpStatusCode.BadRequest,
                                            Common.Constants.ScimTypeValues.Uniqueness));
                        return(result);
                    }
                }
            }

            // Assign the values
            AssignValues(source, target);
            return(result);
        }
 private static bool Equals(RepresentationAttribute source, RepresentationAttribute target)
 {
     return(source.CompareTo(target) == 0);
 }
        private bool UpdateAttribute(RepresentationAttribute source, RepresentationAttribute target, IEnumerable <Representation> allRepresentations, out ErrorResponse error)
        {
            error = null;
            var complexSource = source as ComplexRepresentationAttribute;
            var complexTarget = target as ComplexRepresentationAttribute;

            if (complexTarget != null)
            {
                var schemaAttribute = complexTarget.SchemaAttribute;
                if (schemaAttribute.MultiValued)
                {
                    // Check mutability
                    if (schemaAttribute.Mutability == Common.Constants.SchemaAttributeMutability.Immutable)
                    {
                        if (complexTarget.CompareTo(complexSource) != 0)
                        {
                            error = _errorResponseFactory.CreateError(string.Format(ErrorMessages.TheImmutableAttributeCannotBeUpdated, schemaAttribute.Name),
                                                                      HttpStatusCode.BadRequest,
                                                                      Common.Constants.ScimTypeValues.Mutability);
                            return(false);
                        }
                    }

                    // Check uniqueness
                    if (schemaAttribute.Uniqueness == Common.Constants.SchemaAttributeUniqueness.Server && allRepresentations != null && allRepresentations.Any())
                    {
                        var filter      = _filterParser.Parse(complexTarget.FullPath);
                        var uniqueAttrs = new List <RepresentationAttribute>();
                        foreach (var records in allRepresentations.Select(r => filter.Evaluate(r)))
                        {
                            uniqueAttrs.AddRange(records);
                        }

                        if (uniqueAttrs.Any())
                        {
                            if (uniqueAttrs.Any(a => a.CompareTo(complexTarget) == 0))
                            {
                                error = _errorResponseFactory.CreateError(
                                    string.Format(ErrorMessages.TheAttributeMustBeUnique, complexTarget.SchemaAttribute.Name),
                                    HttpStatusCode.BadRequest,
                                    Common.Constants.ScimTypeValues.Uniqueness);
                                return(false);
                            }
                        }
                    }
                }

                complexSource.Values = complexTarget.Values;
                return(true);
            }

            // Check mutability
            if (target.SchemaAttribute.Mutability == Common.Constants.SchemaAttributeMutability.Immutable)
            {
                if (source.CompareTo(target) != 0)
                {
                    error = _errorResponseFactory.CreateError(string.Format(ErrorMessages.TheImmutableAttributeCannotBeUpdated, target.SchemaAttribute.Name),
                                                              HttpStatusCode.BadRequest,
                                                              Common.Constants.ScimTypeValues.Mutability);
                    return(false);
                }
            }

            // Check uniqueness
            if (target.SchemaAttribute.Uniqueness == Common.Constants.SchemaAttributeUniqueness.Server && allRepresentations != null && allRepresentations.Any())
            {
                var filter      = _filterParser.Parse(target.FullPath);
                var uniqueAttrs = new List <RepresentationAttribute>();
                foreach (var records in allRepresentations.Select(r => filter.Evaluate(r)))
                {
                    uniqueAttrs.AddRange(records);
                }

                if (uniqueAttrs.Any())
                {
                    if (uniqueAttrs.Any(a => a.CompareTo(target) == 0))
                    {
                        error = _errorResponseFactory.CreateError(
                            string.Format(ErrorMessages.TheAttributeMustBeUnique, target.SchemaAttribute.Name),
                            HttpStatusCode.BadRequest,
                            Common.Constants.ScimTypeValues.Uniqueness);
                        return(false);
                    }
                }
            }

            // Assign the values
            return(AssignValues(source, target));
        }