public static JObject ToResponse(this SCIMRepresentation representation, string location, bool isGetRequest = false, bool includeStandardAttributes = true, bool addEmptyArray = false)
        {
            var jObj = new JObject
            {
                { StandardSCIMRepresentationAttributes.Id, representation.Id }
            };

            if (includeStandardAttributes)
            {
                representation.AddStandardAttributes(location, new List <string> {
                }, ignore: true);
            }

            if (addEmptyArray)
            {
                representation.ApplyEmptyArray();
            }

            var attributes = representation.HierarchicalAttributes.Select(a =>
            {
                var schema = representation.GetSchema(a);
                var order  = 1;
                if (schema != null && schema.IsRootSchema)
                {
                    order = 0;
                }

                return(new EnrichParameter(schema, order, a));
            });

            EnrichResponse(attributes, jObj, isGetRequest);
            return(jObj);
        }
        public static JObject ToResponse(this SCIMRepresentation representation, string location, bool isGetRequest = false)
        {
            var meta = new JObject
            {
                { SCIMConstants.StandardSCIMMetaAttributes.ResourceType, representation.ResourceType },
                { SCIMConstants.StandardSCIMMetaAttributes.Created, representation.Created },
                { SCIMConstants.StandardSCIMMetaAttributes.LastModified, representation.LastModified },
                { SCIMConstants.StandardSCIMMetaAttributes.Version, representation.Version }
            };

            if (!string.IsNullOrWhiteSpace(location))
            {
                meta.Add(SCIMConstants.StandardSCIMMetaAttributes.Location, location);
            }

            var jObj = new JObject
            {
                { SCIMConstants.StandardSCIMRepresentationAttributes.Id, representation.Id },
                { SCIMConstants.StandardSCIMRepresentationAttributes.Schemas, new JArray(representation.Schemas.Select(s => s.Id)) },
                { SCIMConstants.StandardSCIMRepresentationAttributes.Meta, meta }
            };

            if (!string.IsNullOrWhiteSpace(representation.ExternalId))
            {
                jObj.Add(SCIMConstants.StandardSCIMRepresentationAttributes.ExternalId, representation.ExternalId);
            }

            var attributes = representation.Attributes.Select(a =>
            {
                var schema = representation.GetSchema(a);
                int order  = 1;
                if (schema != null && schema.IsRootSchema)
                {
                    order = 0;
                }

                return(new EnrichParameter(schema, order, a));
            });

            EnrichResponse(attributes, jObj, isGetRequest);
            return(jObj);
        }
        private static void IncludeAttribute(SCIMRepresentation scimRepresentation, SCIMAttributeExpression scimExpression, IQueryable <SCIMRepresentationAttribute> attributes, JObject result)
        {
            var filteredAttributes = GetAttributes(scimExpression, attributes);

            if (!filteredAttributes.Any())
            {
                return;
            }

            if (scimExpression.Child != null)
            {
                foreach (var kvp in filteredAttributes.GroupBy(f => f.SchemaAttribute.Name))
                {
                    var representationAttr = kvp.First();
                    if (representationAttr.SchemaAttribute.MultiValued == false)
                    {
                        var jObjVal = new JObject();
                        IncludeAttribute(scimRepresentation, scimExpression.Child, representationAttr.Values.AsQueryable(), jObjVal);
                        result.Add(representationAttr.SchemaAttribute.Name, jObjVal);
                    }
                    else
                    {
                        var jArr = new JArray();
                        foreach (var attr in kvp)
                        {
                            var jObjVal = new JObject();
                            IncludeAttribute(scimRepresentation, scimExpression.Child, attr.Values.AsQueryable(), jObjVal);
                            jArr.Add(jObjVal);
                        }

                        result.Add(representationAttr.SchemaAttribute.Name, jArr);
                    }
                }

                return;
            }

            var enrichParameters = filteredAttributes.Select(at => new EnrichParameter(scimRepresentation.GetSchema(at), 0, at));

            EnrichResponse(enrichParameters, result);
        }