/// <summary>
 /// Validates details of a matter and returns the validation status.
 /// </summary>
 /// <param name="matter">Matter object containing Matter data</param>
 /// <param name="clientContext">Client context object for SharePoint</param>
 /// <param name="methodNumber">Number indicating which method needs to be validated</param>
 /// <returns>A string value indicating whether validations passed or fail</returns>
 internal static string MatterDetailsValidation(Matter matter, ClientContext clientContext, int methodNumber, MatterConfigurations matterConfigurations)
 {
     if (matterConfigurations.IsMatterDescriptionMandatory)
     {
         if (string.IsNullOrWhiteSpace(matter.Description))
         {
             return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputMatterDescriptionCode, TextConstants.IncorrectInputMatterDescriptionMessage));
         }
         else
         {
             var matterDescription = Regex.Match(matter.Description, ConstantStrings.SpecialCharacterExpressionMatterDescription, RegexOptions.IgnoreCase);
             if (int.Parse(ServiceConstantStrings.MatterDescriptionLength, CultureInfo.InvariantCulture) < matter.Description.Length || !matterDescription.Success)
             {
                 return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputMatterDescriptionCode, TextConstants.IncorrectInputMatterDescriptionMessage));
             }
         }
     }
     if (matterConfigurations.IsConflictCheck)
     {
         DateTime conflictCheckedOnDate;
         bool     isValidDate = DateTime.TryParse(matter.Conflict.CheckOn, out conflictCheckedOnDate);
         if (!isValidDate || 0 > DateTime.Compare(DateTime.Now, conflictCheckedOnDate))
         {
             return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputConflictDateCode, TextConstants.IncorrectInputConflictDateMessage));
         }
         if (string.IsNullOrWhiteSpace(matter.Conflict.Identified))
         {
             return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputConflictIdentifiedCode, TextConstants.IncorrectInputConflictIdentifiedMessage));
         }
         else
         {
             try
             {
                 if (0 > string.Compare(ConstantStrings.FALSE, matter.Conflict.Identified, StringComparison.OrdinalIgnoreCase))
                 {
                     if (0 >= matter.BlockUserNames.Count())
                     {
                         return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputBlockUserNamesCode, TextConstants.IncorrectInputBlockUserNamesMessage));
                     }
                     else
                     {
                         SharePointHelper.ResolveUserNames(clientContext, matter.BlockUserNames).FirstOrDefault();
                     }
                 }
             }
             catch (Exception)
             {
                 return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputBlockUserNamesCode, TextConstants.IncorrectInputBlockUserNamesMessage));
             }
         }
         if (string.IsNullOrWhiteSpace(matter.Conflict.CheckBy))
         {
             return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputConflictCheckByCode, TextConstants.IncorrectInputConflictCheckByMessage));
         }
         else
         {
             try
             {
                 SharePointHelper.ResolveUserNames(clientContext, new List <string>()
                 {
                     matter.Conflict.CheckBy
                 }).FirstOrDefault();
             }
             catch (Exception)
             {
                 return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputConflictCheckByCode, TextConstants.IncorrectInputConflictCheckByMessage));
             }
         }
     }
     if (int.Parse(ConstantStrings.ProvisionMatterCreateMatter, CultureInfo.InvariantCulture) == methodNumber && 0 >= matter.Roles.Count())
     {
         return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputUserRolesCode, TextConstants.IncorrectInputUserRolesMessage));
     }
     return(string.Empty);
 }
 /// <summary>
 /// Validates the inputs for matter provision app and returns the validation status (success/failure).
 /// </summary>
 /// <param name="requestObject">Request Object containing SharePoint App Token</param>
 /// <param name="client">Client object containing Client data</param>
 /// <param name="clientContext">Client context object for SharePoint</param>
 /// <param name="matter">Matter object containing Matter data</param>
 /// <param name="matterDetails">Matter details object which has data of properties to be stamped</param>
 /// <param name="methodNumber">Number indicating which method needs to be validated</param>
 /// <returns>A string value indicating whether validations passed or fail</returns>
 internal static string ProvisionMatterValidation(RequestObject requestObject, Client client, ClientContext clientContext, Matter matter, MatterDetails matterDetails, int methodNumber, MatterConfigurations matterConfigurations)
 {
     if (int.Parse(ConstantStrings.ProvisionMatterCreateMatter, CultureInfo.InvariantCulture) <= methodNumber && int.Parse(ConstantStrings.EditMatterPermission, CultureInfo.InvariantCulture) >= methodNumber && !Lists.CheckPermissionOnList(ServiceUtility.GetClientContext(null, new Uri(ConstantStrings.ProvisionMatterAppURL), requestObject.RefreshToken), ConstantStrings.SendMailListName, PermissionKind.EditListItems))
     {
         return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, ServiceConstantStrings.IncorrectInputUserAccessCode, ServiceConstantStrings.IncorrectInputUserAccessMessage));
     }
     else
     {
         if (null != requestObject)
         {
             if (string.IsNullOrWhiteSpace(requestObject.RefreshToken) && string.IsNullOrWhiteSpace(requestObject.SPAppToken))
             {
                 return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputRequestObjectCode, TextConstants.IncorrectInputRequestObjectMessage));
             }
         }
         if (null != client)
         {
             string result = ValidateClientInformation(client, methodNumber);
             if (!string.IsNullOrEmpty(result))
             {
                 return(result);
             }
         }
         if (null != matter)
         {
             string MatterMetadataValidationResponse = MatterMetadataValidation(matter, clientContext, methodNumber, matterConfigurations);
             if (!string.IsNullOrEmpty(MatterMetadataValidationResponse))
             {
                 return(MatterMetadataValidationResponse);
             }
             if (int.Parse(ConstantStrings.EditMatterPermission, CultureInfo.InvariantCulture) == methodNumber)
             {
                 string roleCheck = ValidationHelperFunctions.RoleCheck(requestObject, matter, client);
                 if (!string.IsNullOrEmpty(roleCheck))
                 {
                     return(roleCheck);
                 }
             }
             if (null != matter.Permissions)
             {
                 bool isFullControlPresent = EditMatterHelperFunctions.ValidateFullControlPermission(matter);
                 if (!isFullControlPresent)
                 {
                     return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, ServiceConstantStrings.IncorrectInputUserAccessCode, ServiceConstantStrings.ErrorEditMatterMandatoryPermission));
                 }
             }
         }
         if (null != matterDetails && !(int.Parse(ConstantStrings.EditMatterPermission, CultureInfo.InvariantCulture) == methodNumber))
         {
             if (string.IsNullOrWhiteSpace(matterDetails.PracticeGroup))
             {
                 return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputPracticeGroupCode, TextConstants.IncorrectInputPracticeGroupMessage));
             }
             if (string.IsNullOrWhiteSpace(matterDetails.AreaOfLaw))
             {
                 return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputAreaOfLawCode, TextConstants.IncorrectInputAreaOfLawMessage));
             }
             if (string.IsNullOrWhiteSpace(matterDetails.SubareaOfLaw))
             {
                 return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputSubareaOfLawCode, TextConstants.IncorrectInputSubareaOfLawMessage));
             }
             try
             {
                 if (string.IsNullOrWhiteSpace(matterDetails.ResponsibleAttorney))
                 {
                     return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputResponsibleAttorneyCode, TextConstants.IncorrectInputResponsibleAttorneyMessage));
                 }
                 else
                 {
                     IList <string> userNames = matterDetails.ResponsibleAttorney.Split(';').ToList <string>();
                     SharePointHelper.ResolveUserNames(clientContext, userNames).FirstOrDefault();
                 }
             }
             catch (Exception)
             {
                 return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputResponsibleAttorneyCode, TextConstants.IncorrectInputResponsibleAttorneyMessage));
             }
         }
     }
     return(string.Empty);
 }
        /// <summary>
        /// Validates meta-data of a matter and returns the validation status (success/failure).
        /// </summary>
        /// <param name="matter">Matter object containing Matter data</param>
        /// <param name="clientContext">Client context object for SharePoint</param>
        /// <param name="methodNumber">Number indicating which method needs to be validated</param>
        /// <returns>A string value indicating whether validations passed or fail</returns>
        internal static string MatterMetadataValidation(Matter matter, ClientContext clientContext, int methodNumber, MatterConfigurations matterConfigurations)
        {
            string matterNameValidation = MatterNameValidation(matter);

            if (!string.IsNullOrWhiteSpace(matterNameValidation))
            {
                return(matterNameValidation);
            }
            if (int.Parse(ConstantStrings.ProvisionMatterCreateMatter, CultureInfo.InvariantCulture) == methodNumber || int.Parse(ConstantStrings.ProvisionMatterAssignContentType, CultureInfo.InvariantCulture) == methodNumber || int.Parse(ConstantStrings.ProvisionMatterUpdateMetadataForList, CultureInfo.InvariantCulture) == methodNumber)
            {
                if (string.IsNullOrWhiteSpace(matter.Id))
                {
                    return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputMatterIdCode, TextConstants.IncorrectInputMatterIdMessage));
                }
                else
                {
                    var matterId = Regex.Match(matter.Id, ConstantStrings.SpecialCharacterExpressionMatterId, RegexOptions.IgnoreCase);
                    if (int.Parse(ServiceConstantStrings.MatterIdLength, CultureInfo.InvariantCulture) < matter.Id.Length || !matterId.Success)
                    {
                        return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputMatterIdCode, TextConstants.IncorrectInputMatterIdMessage));
                    }
                }
            }
            if (int.Parse(ConstantStrings.ProvisionMatterCreateMatter, CultureInfo.InvariantCulture) == methodNumber || int.Parse(ConstantStrings.ProvisionMatterShareMatter, CultureInfo.InvariantCulture) == methodNumber || int.Parse(ConstantStrings.ProvisionMatterMatterLandingPage, CultureInfo.InvariantCulture) == methodNumber)
            {
                string matterDetailsValidationResponse = MatterDetailsValidation(matter, clientContext, methodNumber, matterConfigurations);
                if (!string.IsNullOrEmpty(matterDetailsValidationResponse))
                {
                    return(matterDetailsValidationResponse);
                }
            }
            try
            {
                if (!(int.Parse(ConstantStrings.ProvisionMatterCheckMatterExists, CultureInfo.InvariantCulture) == methodNumber) && !(int.Parse(ConstantStrings.ProvisionMatterAssignContentType, CultureInfo.InvariantCulture) == methodNumber))
                {
                    if (0 >= matter.AssignUserEmails.Count())
                    {
                        return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputUserNamesCode, TextConstants.IncorrectInputUserNamesMessage));
                    }
                    else
                    {
                        IList <string> userList = matter.AssignUserEmails.SelectMany(x => x).Distinct().ToList();
                        SharePointHelper.ResolveUserNames(clientContext, userList).FirstOrDefault();
                    }
                }
            }
            catch (Exception)
            {
                return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputUserNamesCode, TextConstants.IncorrectInputUserNamesMessage));
            }

            if (int.Parse(ConstantStrings.ProvisionMatterAssignUserPermissions, CultureInfo.InvariantCulture) == methodNumber || int.Parse(ConstantStrings.ProvisionMatterMatterLandingPage, CultureInfo.InvariantCulture) == methodNumber || int.Parse(ConstantStrings.EditMatterPermission, CultureInfo.InvariantCulture) == methodNumber)
            {
                string CheckUserPermissionResponse = CheckUserPermission(matter);
                if (!string.IsNullOrEmpty(CheckUserPermissionResponse))
                {
                    return(CheckUserPermissionResponse);
                }
            }
            if (int.Parse(ConstantStrings.ProvisionMatterAssignContentType, CultureInfo.InvariantCulture) == methodNumber || int.Parse(ConstantStrings.ProvisionMatterShareMatter, CultureInfo.InvariantCulture) == methodNumber)
            {
                string validateContentTypeResponse = ValidateContentType(matter);
                if (!string.IsNullOrEmpty(validateContentTypeResponse))
                {
                    return(validateContentTypeResponse);
                }
            }
            return(string.Empty);
        }