public bool UpdateMatterStampedProperties()

in tree/master/cloud/src/solution/Microsoft.Legal.MatterCenter.Service/Microsoft.Legal.MatterCenter.Repository/Matter/MatterRepository.cs [1124:1500]


        public bool UpdateMatterStampedProperties(ClientContext clientContext, MatterDetails matterDetails,
            Matter matter, PropertyValues matterStampedProperties, bool isEditMode, List<string> usersToRemove)
        {

            try
            {
                if (null != clientContext && null != matter && null != matterDetails && (0 < matterStampedProperties.FieldValues.Count))
                {
                    var index = 0;
                    List<string> arrRoles = new List<string>();
                    List<string> arrPermissions = new List<string>();
                    string stampedUsers = GetStampPropertyValue(matterStampedProperties.FieldValues, matterSettings.StampedPropertyMatterCenterUsers);
                    var stampedUserList = stampedUsers.Replace("$|$", "$").Split('$').ToList();
                    string stampedUserEmails = GetStampPropertyValue(matterStampedProperties.FieldValues, matterSettings.StampedPropertyMatterCenterUserEmails);
                    var stampedUserEmailsList = stampedUserEmails.Replace("$|$", "$").Split('$').ToList();
                    string stampedPermissions = GetStampPropertyValue(matterStampedProperties.FieldValues, matterSettings.StampedPropertyMatterCenterPermissions);
                    var stampedPermissionsList = stampedPermissions.Replace("$|$", "$").Split('$').ToList();
                    string stampedRoles = GetStampPropertyValue(matterStampedProperties.FieldValues, matterSettings.StampedPropertyMatterCenterRoles);
                    var stampedRolesList = stampedRoles.Replace("$|$", "$").Split('$').ToList();
                    stampedRolesList = stampedRolesList.Where(rolesList => rolesList != string.Empty).ToList();

                    string stampedTeamMembers = GetStampPropertyValue(matterStampedProperties.FieldValues, matterSettings.StampedPropertyTeamMembers);
                    var stampedTeamMembersList = stampedTeamMembers.Replace(";", "$").Split('$').ToList();
                    stampedTeamMembersList = stampedTeamMembersList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                    
                    List<int> itemsToRemoveStampedUserProp = new List<int>();
                    foreach (IList<string> userNames in matter.AssignUserEmails)
                    {
                        IList<string> userNamesListTemp = userNames.Where(user => !string.IsNullOrWhiteSpace(user)).ToList();
                        foreach (var userTem in userNamesListTemp)
                        {
                            arrRoles.Add(matter.Roles[index]);
                            arrPermissions.Add(matter.Permissions[index]);
                            if (stampedUserEmailsList.Contains(userTem))
                            {
                                itemsToRemoveStampedUserProp.Add(stampedUserEmailsList.IndexOf(userTem));
                            }
                        }

                        index++;
                    }

                    matter.Roles = arrRoles;
                    matter.Permissions = arrPermissions;
                    Dictionary<string, string> propertyList = new Dictionary<string, string>();
                    usersToRemove = usersToRemove.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();

                    // Get existing stamped properties
                    foreach (var userNameToRemove in usersToRemove)
                    {
                        if (stampedUserEmailsList.Contains(userNameToRemove))
                        {
                            itemsToRemoveStampedUserProp.Add(stampedUserEmailsList.IndexOf(userNameToRemove));
                        }

                    }

                    if (itemsToRemoveStampedUserProp.Count > 0)
                    {
                        for (int i = 0; i < itemsToRemoveStampedUserProp.Count; i++)
                        {
                            stampedUserList[itemsToRemoveStampedUserProp[i]] = string.Empty;
                            stampedUserEmailsList[itemsToRemoveStampedUserProp[i]] = string.Empty;

                            if (stampedRolesList != null && stampedRolesList.Count > 0)
                            {
                                stampedRolesList[itemsToRemoveStampedUserProp[i]] = string.Empty;
                            }
                            stampedPermissionsList[itemsToRemoveStampedUserProp[i]] = string.Empty;
                            stampedTeamMembersList[itemsToRemoveStampedUserProp[i]] = string.Empty;
                        }
                    }


                    stampedUserList = stampedUserList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                    stampedUsers = string.Join("$|$", stampedUserList);
                    stampedUserEmailsList = stampedUserEmailsList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                    stampedUserEmails = string.Join("$|$", stampedUserEmailsList);
                    stampedRolesList = stampedRolesList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                    stampedRoles = string.Join("$|$", stampedRolesList);
                    stampedPermissionsList = stampedPermissionsList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                    stampedPermissions = string.Join("$|$", stampedPermissionsList);
                    stampedTeamMembersList = stampedTeamMembersList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                    stampedTeamMembers = string.Join(";", stampedTeamMembersList);

                    string stampedResponsibleAttorneys = GetStampPropertyValue(matterStampedProperties.FieldValues, matterSettings.StampedPropertyResponsibleAttorney);
                    var stampedResponsibleAttorneysList = stampedResponsibleAttorneys.Split(';').ToList();
                    stampedResponsibleAttorneysList = stampedResponsibleAttorneysList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                    string stampedResponsibleAttorneysEmail = GetStampPropertyValue(matterStampedProperties.FieldValues, matterSettings.StampedPropertyResponsibleAttorneyEmail);
                    var stampedResponsibleAttorneysEmailList = stampedResponsibleAttorneysEmail.Split(';').ToList();
                    stampedResponsibleAttorneysEmailList = stampedResponsibleAttorneysEmailList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();

                    string currentPermissions = string.Join(ServiceConstants.DOLLAR + ServiceConstants.PIPE + ServiceConstants.DOLLAR, matter.Permissions.Where(user => !string.IsNullOrWhiteSpace(user)));
                    string currentRoles = string.Join(ServiceConstants.DOLLAR + ServiceConstants.PIPE + ServiceConstants.DOLLAR, matter.Roles.Where(user => !string.IsNullOrWhiteSpace(user)));
                    string currentBlockedUploadUsers = string.Join(ServiceConstants.SEMICOLON, matterDetails.UploadBlockedUsers.Where(user => !string.IsNullOrWhiteSpace(user)));
                    string currentUsers = GetMatterAssignedUsers(matter);
                    currentUsers = currentUsers.Replace(";", "$|$").ToString();
                    string currentUserEmails = spList.GetMatterAssignedUsersEmail(clientContext, matter);
                    currentUserEmails = currentUserEmails.Replace(";", "$|$").ToString();

                    string currentResponsibleAttorneys = matterDetails.ResponsibleAttorney;
                    var currentResponsibleAttorneysList = currentResponsibleAttorneys.Split(';').ToList();

                    string currentResponsibleAttorneysEmail = matterDetails.ResponsibleAttorneyEmail;
                    var currentResponsibleAttorneysEmailList = currentResponsibleAttorneysEmail.Split(';').ToList();

                    List<int> itemsToRemoveStampedResponsibleAttroneyProp = new List<int>();
                    foreach (var respAttroneyUser in currentResponsibleAttorneysList)
                    {
                        if (respAttroneyUser != "")
                        {
                            if (stampedResponsibleAttorneysList.Contains(respAttroneyUser))
                            {
                                itemsToRemoveStampedResponsibleAttroneyProp.Add(stampedResponsibleAttorneysList.IndexOf(respAttroneyUser));
                            }
                        }
                    }
                    foreach (var userNameToRemove in usersToRemove)
                    {
                        if (stampedResponsibleAttorneysEmailList.Contains(userNameToRemove))
                        {
                            itemsToRemoveStampedResponsibleAttroneyProp.Add(stampedResponsibleAttorneysEmailList.IndexOf(userNameToRemove));
                        }

                    }

                    if (itemsToRemoveStampedResponsibleAttroneyProp.Count > 0)
                    {
                        for (int i = 0; i < itemsToRemoveStampedResponsibleAttroneyProp.Count; i++)
                        {
                            stampedResponsibleAttorneysList[itemsToRemoveStampedResponsibleAttroneyProp[i]] = string.Empty;
                            stampedResponsibleAttorneysEmailList[itemsToRemoveStampedResponsibleAttroneyProp[i]] = string.Empty;
                        }
                    }

                    stampedResponsibleAttorneysList = stampedResponsibleAttorneysList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                    stampedResponsibleAttorneys = string.Join(";", stampedResponsibleAttorneysList);
                    stampedResponsibleAttorneysEmailList = stampedResponsibleAttorneysEmailList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                    stampedResponsibleAttorneysEmail = string.Join(";", stampedResponsibleAttorneysEmailList);




                    string finalBlockedUploadUsers = string.Empty;
                    string finalMatterPermissions = string.IsNullOrWhiteSpace(stampedPermissions) || isEditMode ? currentPermissions : string.Concat(stampedPermissions, ServiceConstants.DOLLAR + ServiceConstants.PIPE + ServiceConstants.DOLLAR, currentPermissions);
                    string finalMatterRoles = string.IsNullOrWhiteSpace(stampedRoles) || isEditMode ? currentRoles : string.Concat(stampedRoles, ServiceConstants.DOLLAR + ServiceConstants.PIPE + ServiceConstants.DOLLAR, currentRoles);
                    string finalResponsibleAttorneys = string.IsNullOrWhiteSpace(stampedResponsibleAttorneys) || isEditMode ? matterDetails.ResponsibleAttorney : string.Concat(stampedResponsibleAttorneys, ServiceConstants.SEMICOLON, matterDetails.ResponsibleAttorney);
                    string finalTeamMembers = string.IsNullOrWhiteSpace(stampedTeamMembers) || isEditMode ? matterDetails.TeamMembers : string.Concat(stampedTeamMembers, ServiceConstants.SEMICOLON, matterDetails.TeamMembers);
                    string finalMatterCenterUsers = string.IsNullOrWhiteSpace(stampedUsers) || isEditMode ? currentUsers : string.Concat(stampedUsers, ServiceConstants.DOLLAR + ServiceConstants.PIPE + ServiceConstants.DOLLAR, currentUsers);
                    
                    string finalMatterCenterUserEmails = string.IsNullOrWhiteSpace(stampedUserEmails) || isEditMode ? currentUserEmails : string.Concat(stampedUserEmails, ServiceConstants.DOLLAR + ServiceConstants.PIPE + ServiceConstants.DOLLAR, currentUserEmails);
                    string finalResponsibleAttorneysEmail = string.IsNullOrWhiteSpace(stampedResponsibleAttorneysEmail) || isEditMode ? matterDetails.ResponsibleAttorneyEmail : string.Concat(stampedResponsibleAttorneysEmail, ServiceConstants.SEMICOLON, matterDetails.ResponsibleAttorneyEmail);
                    var finalMatterUsers = finalMatterCenterUsers.Replace("$|$", ";").ToString();
                    var finalMatterUsersList = finalMatterUsers.Replace(";", "$").Split('$').ToList();
                    finalMatterUsersList = finalMatterUsersList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();


                    var finalTeamMembersList = finalTeamMembers.Replace(";", "$").Split('$').ToList();
                    finalTeamMembersList = finalTeamMembersList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();

                    var finalMatterPermissionsList = finalMatterPermissions.Replace("$|$", "$").Split('$').ToList();
                    finalMatterPermissionsList = finalMatterPermissionsList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                    var finalMatterRolesList = finalMatterRoles.Replace("$|$", "$").Split('$').ToList();
                    finalMatterRolesList = finalMatterRolesList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();

                    var finalMatterCenterUserEmailsString = finalMatterCenterUserEmails.Replace("$|$", ";");
                    var finalMatterCenterUserEmailsList = finalMatterCenterUserEmailsString.Replace(";", "$").Split('$').ToList();
                    finalMatterCenterUserEmailsList = finalMatterCenterUserEmailsList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();

                    var finalResponsibleAttorneysEmailListString = finalResponsibleAttorneysEmail.Replace("$|$", ";");
                    var finalResponsibleAttorneysEmailList = finalResponsibleAttorneysEmailListString.Replace(";", "$").Split('$').ToList();
                    finalResponsibleAttorneysEmailList = finalResponsibleAttorneysEmailList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();

                    var finalResponsibleAttorneysUsersListString = finalResponsibleAttorneys.Replace(";", "$");
                    var finalResponsibleAttorneysUsersList = finalResponsibleAttorneysUsersListString.Split('$').ToList();
                    finalResponsibleAttorneysUsersList = finalResponsibleAttorneysUsersList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();


                    #region Remove all the external users from the request object so that only internal users are added to the matter
                    //Once the external users accepted the invitation, those external users will be added to the matter by azure web app job
                    List<int> itemsToRemove = new List<int>();
                    ///Below changes are required for the matters where no roles are available in the stamped properties. By default we are adding Responsible Attorney
                    if (generalSettings.IsBackwardCompatible == true)
                    {
                        if (finalMatterRolesList != null && finalMatterRolesList.Count == 0)
                        {
                            for (int i = 0; i < finalMatterPermissionsList.Count; i++)
                            {
                                finalMatterRolesList.Add("Responsible Attorney");
                            }
                        }
                    } 

                    //If the user permissions has been changed to read, need to add that user to blocked users list, so that, read only users can't upload 
                    //documents to matters
                    var finalBlockedUploadUsersList = new List<string>();
                    for (int i = 0; i < finalMatterPermissionsList.Count; i++)
                    {
                        if (finalMatterPermissionsList[i].ToLower() == "read")
                        {
                            finalBlockedUploadUsersList.Add(finalMatterCenterUserEmailsList[i]);
                        }
                    }
                    finalBlockedUploadUsers = string.Join(";", finalBlockedUploadUsersList);


                    for (int i = 0; i < finalMatterCenterUserEmailsList.Count; i++)
                    {
                        if (userdetails.CheckUserPresentInMatterCenter(clientContext, finalMatterCenterUserEmailsList[i]) == false)
                        {
                            itemsToRemove.Add(i);
                        }
                    }

                    if (itemsToRemove.Count > 0)
                    {
                        for (int i = 0; i < itemsToRemove.Count; i++)
                        {
                            finalMatterUsersList[itemsToRemove[i]] = string.Empty;
                            finalTeamMembersList[itemsToRemove[i]] = string.Empty;
                            finalMatterPermissionsList[itemsToRemove[i]] = string.Empty;
                            finalMatterRolesList[itemsToRemove[i]] = string.Empty;
                            finalMatterCenterUserEmailsList[itemsToRemove[i]] = string.Empty;
                            if (finalResponsibleAttorneysEmailList.Count > itemsToRemove[i] && finalResponsibleAttorneysEmailList[itemsToRemove[i]] != null)
                            {
                                finalResponsibleAttorneysEmailList[itemsToRemove[i]] = string.Empty;
                                finalResponsibleAttorneysUsersList[itemsToRemove[i]] = string.Empty;
                            }
                        }
                        finalMatterUsersList = finalMatterUsersList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                        finalTeamMembersList = finalTeamMembersList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                        finalMatterPermissionsList = finalMatterPermissionsList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                        finalMatterRolesList = finalMatterRolesList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                        finalResponsibleAttorneysEmailList = finalResponsibleAttorneysEmailList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                        finalResponsibleAttorneysUsersList = finalResponsibleAttorneysUsersList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                        finalMatterCenterUserEmailsList = finalMatterCenterUserEmailsList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                        var finalMatterUsersArray = finalMatterUsersList.ToArray();
                        var finalTeamMembersArray = finalTeamMembersList.ToArray();
                        var finalMatterPermissionsArray = finalMatterPermissionsList.ToArray();
                        var finalMatterRolesArray = finalMatterRolesList.ToArray();
                        var finalMatterCenterUserEmailsArray = finalMatterCenterUserEmailsList.ToArray();
                        var finalResponsibleAttorneysEmailArray = finalResponsibleAttorneysEmailList.ToArray();
                        var finalResponsibleAttorneysUsersArray = finalResponsibleAttorneysUsersList.ToArray();

                        finalMatterCenterUsers = string.Join("$|$", finalMatterUsersArray);
                        finalTeamMembers = string.Join(";;", finalTeamMembersArray);
                        finalMatterCenterUserEmails = string.Join("$|$", finalMatterCenterUserEmailsArray);
                        finalMatterPermissions = string.Join("$|$", finalMatterPermissionsArray);
                        finalMatterRoles = string.Join("$|$", finalMatterRolesArray);
                        finalResponsibleAttorneysEmail = string.Join(";", finalResponsibleAttorneysEmailArray);
                        finalResponsibleAttorneys = string.Join(";", finalResponsibleAttorneysUsersArray);
                    }
                    #endregion

                    propertyList.Add(matterSettings.StampedPropertyResponsibleAttorney, WebUtility.HtmlEncode(finalResponsibleAttorneys));
                    propertyList.Add(matterSettings.StampedPropertyResponsibleAttorneyEmail, WebUtility.HtmlEncode(finalResponsibleAttorneysEmail));
                    propertyList.Add(matterSettings.StampedPropertyTeamMembers, WebUtility.HtmlEncode(finalTeamMembers));
                    propertyList.Add(matterSettings.StampedPropertyBlockedUploadUsers, WebUtility.HtmlEncode(finalBlockedUploadUsers));
                    propertyList.Add(matterSettings.StampedPropertyMatterCenterRoles, WebUtility.HtmlEncode(finalMatterRoles));
                    propertyList.Add(matterSettings.StampedPropertyMatterCenterPermissions, WebUtility.HtmlEncode(finalMatterPermissions));
                    propertyList.Add(matterSettings.StampedPropertyMatterCenterUsers, WebUtility.HtmlEncode(finalMatterCenterUsers));
                    propertyList.Add(matterSettings.StampedPropertyMatterCenterUserEmails, WebUtility.HtmlEncode(finalMatterCenterUserEmails));
                    spList.SetPropertBagValuesForList(clientContext, matterStampedProperties, matter.Name, propertyList);

                    #region Update matter and matterdetails object to have only external users so that notification can be send to those external users
                    //For each external user, an entry will be created in the azure table storage and that entry will contain only the information related to the
                    //external user, such as his role, his permission etc
                    itemsToRemove.Clear();
                    List<int> itemsToRemoveAttorneys = new List<int>();

                    int l = 0;
                    finalResponsibleAttorneysEmailList = matterDetails.ResponsibleAttorneyEmail.Split(';').ToList();
                    finalResponsibleAttorneysUsersList = matterDetails.ResponsibleAttorney.Split(';').ToList();
                    foreach (string userName in finalResponsibleAttorneysUsersList)
                    {
                        if (!string.IsNullOrWhiteSpace(userName) && userdetails.CheckUserPresentInMatterCenter(clientContext, userName) == true)
                        {
                            itemsToRemoveAttorneys.Add(l);
                        }
                        l = l + 1;
                    }
                    if (itemsToRemoveAttorneys.Count > 0)
                    {
                        for (int k = 0; k < itemsToRemoveAttorneys.Count; k++)
                        {
                            if (finalResponsibleAttorneysEmailList.Count > itemsToRemoveAttorneys[k] &&
                                finalResponsibleAttorneysEmailList[itemsToRemoveAttorneys[k]] != null)
                            {
                                finalResponsibleAttorneysUsersList[itemsToRemoveAttorneys[k]] = string.Empty;
                                finalResponsibleAttorneysEmailList[itemsToRemoveAttorneys[k]] = string.Empty;
                            }
                        }
                        finalResponsibleAttorneysUsersList = finalResponsibleAttorneysUsersList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                        finalResponsibleAttorneysEmailList = finalResponsibleAttorneysEmailList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                    }
                    l = 0;

                    //Check if any of the assigned team member is not an external user?



                    //setting the internal users to null and adding the index to remove the roles and permissions in matter.Permissions and matter.roles
                    for (int z = 0; z < matter.AssignUserNames.Count; z++)
                    {
                        for (int y = 0; y < matter.AssignUserNames[z].Count; y++)
                        {
                            string userName = matter.AssignUserNames[z][y].ToString();
                            if (userName != "")
                            {
                                if (userdetails.CheckUserPresentInMatterCenter(clientContext, userName) == true)
                                {
                                    itemsToRemove.Add(l);
                                    matter.AssignUserNames[z][y] = null;
                                    matter.AssignUserEmails[z][y] = null;
                                }
                                l = l + 1;
                            }
                        }
                    }



                    for (int x = 0; x < matter.AssignUserNames.Count; x++)
                    {
                        var matterAssignedNames = matter.AssignUserNames[x].Where(user => !string.IsNullOrWhiteSpace(user));
                        var matterAssignedUserEmails = matter.AssignUserEmails[x].Where(user => !string.IsNullOrWhiteSpace(user));
                        if (matterAssignedNames.Count() > 0)
                        {
                            matter.AssignUserNames[x] = matterAssignedNames.ToList();
                            matter.AssignUserEmails[x] = matterAssignedUserEmails.ToList();
                        }
                        else
                        {
                            matter.AssignUserNames[x] = null;
                            matter.AssignUserEmails[x] = null;
                        }
                    }



                    //  finalTeamMembersList = matterDetails.TeamMembers.Replace(";", "$").Split('$').ToList();
                    finalTeamMembersList = matterDetails.TeamMembers.Split(';').Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                    if (itemsToRemove.Count > 0)
                    {
                        for (int k = 0; k < itemsToRemove.Count; k++)
                        {
                            matter.Permissions[itemsToRemove[k]] = string.Empty;
                            matter.Roles[itemsToRemove[k]] = string.Empty;
                            //matter.AssignUserEmails[itemsToRemove[k]] = null;
                            //matter.AssignUserNames[itemsToRemove[k]] = null;
                            finalTeamMembersList[itemsToRemove[k]] = string.Empty;
                        }
                    }

                    matter.Permissions = matter.Permissions.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                    matter.Roles = matter.Roles.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                    finalTeamMembersList = finalTeamMembersList.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
                    matter.AssignUserEmails = matter.AssignUserEmails.Where(s => s != null).ToList();
                    matter.AssignUserNames = matter.AssignUserNames.Where(s => s != null).ToList();


                    matterDetails.ResponsibleAttorneyEmail = string.Empty;
                    matterDetails.ResponsibleAttorney = string.Empty;
                    matterDetails.TeamMembers = string.Join(";", finalTeamMembersList.ToArray());
                    matterDetails.ResponsibleAttorneyEmail = string.Join(";", finalResponsibleAttorneysEmailList.ToArray());
                    matterDetails.ResponsibleAttorney = string.Join(";", finalResponsibleAttorneysUsersList.ToArray());
                    #endregion

                    return true;
                }
            }
            catch (Exception ex)
            {
                throw; //// This will transfer control to catch block of parent function.
            }
            return false;
        }