private void SaveRequestRoles(IList <PushRequestRole> saveRequestRoles, IObject obj, PushResponse pushResponse, Dictionary <string, IObject> objectByNewId)
        {
            foreach (var saveRequestRole in saveRequestRoles)
            {
                var composite = (Composite)obj.Strategy.Class;
                var roleTypes = composite.RoleTypesByGroup[this.@group];
                var acl       = new AccessControlList(obj, this.user);

                var roleTypeName = saveRequestRole.T;
                var roleType     = roleTypes.FirstOrDefault(v => v.PropertyName.Equals(roleTypeName));

                if (roleType != null)
                {
                    if (acl.CanWrite(roleType))
                    {
                        if (roleType.ObjectType.IsUnit)
                        {
                            var unitType = (IUnit)roleType.ObjectType;
                            var role     = saveRequestRole.S;
                            if (role is string)
                            {
                                role = Serialization.ReadString((string)role, unitType.UnitTag);
                            }

                            obj.Strategy.SetUnitRole(roleType.RelationType, role);
                        }
                        else
                        {
                            if (roleType.IsOne)
                            {
                                var roleId = (string)saveRequestRole.S;
                                if (string.IsNullOrEmpty(roleId))
                                {
                                    obj.Strategy.RemoveCompositeRole(roleType.RelationType);
                                }
                                else
                                {
                                    var role = this.GetRole(roleId, objectByNewId);
                                    if (role == null)
                                    {
                                        pushResponse.AddMissingError(roleId);
                                    }
                                    else
                                    {
                                        obj.Strategy.SetCompositeRole(roleType.RelationType, role);
                                    }
                                }
                            }
                            else
                            {
                                // Add
                                if (saveRequestRole.A != null)
                                {
                                    var roleIds = saveRequestRole.A;
                                    if (roleIds.Length != 0)
                                    {
                                        var roles = this.GetRoles(roleIds, objectByNewId);
                                        if (roles.Length != roleIds.Length)
                                        {
                                            AddMissingRoles(roles, roleIds, pushResponse);
                                        }
                                        else
                                        {
                                            foreach (var role in roles)
                                            {
                                                obj.Strategy.AddCompositeRole(roleType.RelationType, role);
                                            }
                                        }
                                    }
                                }

                                // Remove
                                if (saveRequestRole.R != null)
                                {
                                    var roleIds = saveRequestRole.R;
                                    if (roleIds.Length != 0)
                                    {
                                        var roles = this.GetRoles(roleIds, objectByNewId);
                                        if (roles.Length != roleIds.Length)
                                        {
                                            AddMissingRoles(roles, roleIds, pushResponse);
                                        }
                                        else
                                        {
                                            foreach (var role in roles)
                                            {
                                                obj.Strategy.RemoveCompositeRole(roleType.RelationType, role);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        pushResponse.AddAccessError(obj);
                    }
                }
            }
        }
        public PushResponse Build()
        {
            var saveResponse = new PushResponse();

            Dictionary <string, IObject> objectByNewId = null;

            if (this.pushRequest.NewObjects != null && this.pushRequest.NewObjects.Length > 0)
            {
                objectByNewId = this.pushRequest.NewObjects.ToDictionary(
                    x => x.NI,
                    x =>
                {
                    var cls = this.session.Database.MetaPopulation.FindClassByName(x.T);
                    return((IObject)Allors.ObjectBuilder.Build(this.session, cls));
                });
            }

            if (this.pushRequest.Objects != null && this.pushRequest.Objects.Length > 0)
            {
                // bulk load all objects
                var objectIds = this.pushRequest.Objects.Select(v => v.I).ToArray();
                this.session.Instantiate(objectIds);

                foreach (var saveRequestObject in this.pushRequest.Objects)
                {
                    var obj = this.session.Instantiate(saveRequestObject.I);

                    if (!saveRequestObject.V.Equals(obj.Strategy.ObjectVersion.ToString()))
                    {
                        saveResponse.AddVersionError(obj);
                    }
                    else
                    {
                        var saveRequestRoles = saveRequestObject.Roles;
                        this.SaveRequestRoles(saveRequestRoles, obj, saveResponse, objectByNewId);
                    }
                }
            }

            if (objectByNewId != null)
            {
                foreach (var saveRequestNewObject in this.pushRequest.NewObjects)
                {
                    var obj = objectByNewId[saveRequestNewObject.NI];
                    var saveRequestRoles = saveRequestNewObject.Roles;
                    if (saveRequestRoles != null)
                    {
                        this.SaveRequestRoles(saveRequestRoles, obj, saveResponse, objectByNewId);
                    }
                }
            }

            var validation = this.session.Derive();

            if (validation.HasErrors)
            {
                saveResponse.AddDerivationErrors(validation);
            }

            if (!saveResponse.HasErrors)
            {
                if (objectByNewId != null)
                {
                    saveResponse.NewObjects = objectByNewId.Select(dictionaryEntry => new PushResponseNewObject
                    {
                        I  = dictionaryEntry.Value.Id.ToString(),
                        NI = dictionaryEntry.Key
                    }).ToArray();
                }

                this.session.Commit();
            }

            return(saveResponse);
        }
        private static void AddMissingRoles(IObject[] actualRoles, string[] requestedRoleIds, PushResponse pushResponse)
        {
            var actualRoleIds  = actualRoles.Select(x => x.Id.ToString());
            var missingRoleIds = requestedRoleIds.Except(actualRoleIds);

            foreach (var missingRoleId in missingRoleIds)
            {
                pushResponse.AddMissingError(missingRoleId);
            }
        }