Example #1
0
    /// <summary>
    /// Add or Update group information.
    /// </summary>
    /// <param name="context">Zentity Context object</param>
    /// <param name="isAdded">Flag for the specify group is added or updated</param>
    /// <returns></returns>
    private bool SaveResource(ZentityContext context, out bool isAdded)
    {
        bool result = false;

        isAdded = true;
        GetGroupInformation(groupInformation);

        List <String> seletcedList = groupAssignment.GetSelectGroup();

        _resourceName = groupInformation.GroupName;
        using (ResourceDataAccess dataAccess = new ResourceDataAccess(context))
        {
            if (string.IsNullOrEmpty(Id))
            {
                result = dataAccess.CreateGroup(groupInformation, seletcedList, userToken);

                foreach (String identityId in seletcedList)
                {
                    Identity identity = (Identity)dataAccess.GetResource(new Guid(identityId));
                    result = dataAccess.AddIdentityToGroup(groupInformation, identity, userToken);
                }
                isAdded = true;
            }
            else
            {
                if (groupInfoPanel.Enabled)
                {
                    result = dataAccess.UpdateGroup(groupInformation, userToken);
                }


                //result = dataAccess.RemoveIdentityFromGroup(new Guid(Id));
                if (groupAssignment.IsEnable)
                {
                    Group group = (Group)dataAccess.GetResource(new Guid(Id));
                    group.Identities.Load();

                    List <string> existingIdentities = group.Identities
                                                       .Select(identity => identity.Id.ToString())
                                                       .ToList();

                    List <String> allSearchList = groupAssignment.GetSearchList();
                    foreach (string exsitingId in existingIdentities)
                    {
                        if (!seletcedList.Contains(exsitingId))
                        {
                            if (allSearchList.Contains(exsitingId))
                            {
                                Identity identity = (Identity)dataAccess.GetResource(new Guid(exsitingId));
                                result = dataAccess.RemoveIdentityFromGroup(identity, group, userToken);
                            }
                        }
                    }

                    foreach (string selectedId in seletcedList)
                    {
                        if (!existingIdentities.Contains(selectedId))
                        {
                            Identity identity = (Identity)dataAccess.GetResource(new Guid(selectedId));
                            result = dataAccess.AddIdentityToGroup(groupInformation, identity, userToken);
                        }
                    }
                }
                isAdded = false;
            }
        }

        return(result);
    }