/// <summary>
 /// Removes a Solidarity Group from the list of Solidarity Groups
 /// in this Financial Institution
 /// </summary>
 /// <param name="solidarityGroup">Solidarity Group to be removed</param>
 public virtual void RemoveSolidarityGroup(SolidarityGroup solidarityGroup)
 {
     SolidarityGroups.Remove (solidarityGroup);
 }
 /// <summary>
 /// Adds a new Solidarity group to the list of Solidarity Groups
 /// in this Financial Institution
 /// </summary>
 /// <param name="solidarityGroup">Solidarity Group to be added</param>
 public virtual void AddSolidarityGroup(SolidarityGroup solidarityGroup)
 {
     solidarityGroup.FinancialInstitution = this;
     SolidarityGroups.Add (solidarityGroup);
 }
        /// <summary>
        /// Updates a solidarity group with the selected clients ids. This will
        /// associated the clients with the ids contained in the SelectedClientsIds list
        /// to the provided solidarity group
        /// </summary>
        /// <param name="solidarityGroupToUpdate">solidarityGroupToUpdate</param>
        /// <param name="selectedClientIds">selectedClientIds</param>
        /// <exception cref="ZiblerBusinessComponentsException" />
        public void AddClientsToSolidarityGroup(SolidarityGroup solidarityGroupToUpdate,
            IList<int> clientIds)
        {
            try
            {
                IClientDAO clientDAO = _daoFactory.GetClientDAO ();

                bool operationFailed = false;

                //If the client already exists then we won´t worry about it.
                foreach (Client client in solidarityGroupToUpdate.Clients)
                {
                    if (clientIds.Contains (client.Id))
                        clientIds.Remove (client.Id);
                }

                //TODO: Perhaps I should let them know that the Solidarity group
                //		laready contains the client the wish to add??

                //Make sure it is not empty.
                if (clientIds.Count > 0)
                {
                    //Now that we know what clients to add..we go ahead and retrieve
                    //all those clients at once, and as long as the are not already
                    //in another group
                    IList<Client> clients = clientDAO.GetClientsWithNoGroup (clientIds);

                    //If I could not retreive all the selected Ids, it means that
                    //the Client does not exist in the database anymore or
                    //the client has a solidarity group associated already
                    if (clients.Count != clientIds.Count)
                        operationFailed = true;
                    else
                        operationFailed = false;

                    if (operationFailed)
                    {
                        throw new ZiblerBusinessComponentsException (
                            Resources.SolidarityGroupOperationsMsgClientIsAssociatedWithOtherGroup);
                    }
                    else
                    {
                        //Try to add the retrieved clients.
                        foreach (Client client in clients)
                        {
                            solidarityGroupToUpdate.AddClient (client);
                        }
                    }
                }
            }
            /* If the exception was thrown here, just pass it up */
            catch (ZiblerBusinessComponentsException ex)
            {
                throw;
            }
            /* Catch any Data Layer or other exception and throw an unkown exception */
            catch (Exception ex)
            {
                ZiblerBusinessComponentsUnknownException exc
                = new ZiblerBusinessComponentsUnknownException (ex);

                /* Throw the new exception */
                throw exc;
            }
        }
        /// <summary>
        /// Updates a solidarity group and makes it persitant in the database
        /// </summary>
        /// <param name="solidarityGroupToUpdate">SolidarityGroup to update</param>
        /// <exception cref="ZiblerBusinessComponentsException" />
        public void UpdateSolidarityGroup(SolidarityGroup solidarityGroupToUpdate)
        {
            try
            {
                IFinancialInstitutionDAO financialInstitutionDAO = _daoFactory.GetFinancialInstitutionDAO ();
                ISolidarityGroupDAO solidarityGroupDAO = _daoFactory.GetSolidarityGroupDAO ();

                string oldName = solidarityGroupDAO.GetSolidarityGroupName (
                    solidarityGroupToUpdate.Id);

                //If the names are the same then just update
                if (String.Compare (oldName, solidarityGroupToUpdate.Name, true) == 0)
                {
                    solidarityGroupDAO.MakePersistent (solidarityGroupToUpdate);
                }
                else
                {
                    //Verify the name does not exist in the database yet.
                    if (financialInstitutionDAO.SolidarityGroupExists (
                        solidarityGroupToUpdate.FinancialInstitution.Name,
                        solidarityGroupToUpdate.Name))
                    {
                        throw new ZiblerBusinessComponentsException (
                            Resources.SolidarityGroupOperationsMsgSolidarityGroupExists);
                    }
                    else
                    {
                        solidarityGroupDAO.MakePersistent (solidarityGroupToUpdate);
                    }
                }
            }
            /* If the exception was thrown here, just pass it up */
            catch (ZiblerBusinessComponentsException ex)
            {
                throw;
            }
            /* Catch any Data Layer or other exception and throw an unkown exception */
            catch (Exception ex)
            {
                ZiblerBusinessComponentsUnknownException exc
                = new ZiblerBusinessComponentsUnknownException (ex);

                /* Throw the new exception */
                throw exc;
            }
        }
        /// <summary>
        /// Updates a solidarity group with the selected clients ids. This will
        /// remove the clients with the ids contained in the clientIds list
        /// to the provided solidarity group
        /// </summary>
        /// <param name="solidarityGroupToUpdate">solidarityGroupToUpdate</param>
        /// <param name="clientIds">clientIds to remove</param>
        /// <exception cref="ZiblerBusinessComponentsException" />
        public void RemoveClientsFromSolidarityGroup(SolidarityGroup solidarityGroupToUpdate,
            IList<int> clientIds)
        {
            try
            {
                //TODO: Perhaps I should look and see if the clients do not have an associated
                //		Loan request that is active, before I go ahead and remove it from the group.
                IList<Client> clientsToRemove = new List<Client> ();

                //Add the clients to another list so we can remove them later
                foreach (Client client in solidarityGroupToUpdate.Clients)
                {
                    //If the client id belongs to the selected list,
                    //then we go ahead and remove them.
                    if (clientIds.Contains (client.Id))
                        clientsToRemove.Add (client);
                }

                //Now that we know what clients we need to remove.
                //we go ahead and remove them
                foreach (Client client in clientsToRemove)
                {
                    solidarityGroupToUpdate.RemoveClient (client);
                }
            }
            /* If the exception was thrown here, just pass it up */
            catch (ZiblerBusinessComponentsException ex)
            {
                throw;
            }
            /* Catch any Data Layer or other exception and throw an unkown exception */
            catch (Exception ex)
            {
                ZiblerBusinessComponentsUnknownException exc
                = new ZiblerBusinessComponentsUnknownException (ex);

                /* Throw the new exception */
                throw exc;
            }
        }
        /// <summary>
        /// Adds a new Solidarity Group to the system
        /// </summary>
        /// <param name="financialInstitutionName">Financial Institution</param>
        /// <param name="userName">User creating the Solidarity Group</param>
        /// <param name="solidarityGroup">Solidarity Group</param>
        public void CreateNewSolidarityGroup(string financialInstitutionName,
            string userName,
            SolidarityGroup solidarityGroup)
        {
            try
            {

                IFinancialInstitutionDAO financialInstitutionDAO = _daoFactory.GetFinancialInstitutionDAO ();

                //Verify the user does not exist in the system first.
                if (financialInstitutionDAO.SolidarityGroupExists (financialInstitutionName,
                                                                   solidarityGroup.Name))
                {
                    throw new ZiblerBusinessComponentsException (
                        Resources.SolidarityGroupOperationsMsgSolidarityGroupExists);
                }
                else
                {
                    //TODO: Note that this will actually retrieve all the Solidarity Groups
                    //		for the financial institution, which could actually cause performance
                    //		problems if there are a lot of groups.
                    //Get the financial institution, and add the new solidarity group to it.
                    FinancialInstitution financialInstitution = financialInstitutionDAO.GetFinancialInstitutionByName (
                        financialInstitutionName);
                    financialInstitution.AddSolidarityGroup (solidarityGroup);
                }
            }
            /* If the exception was thrown here, just pass it up */
            catch (ZiblerBusinessComponentsException ex)
            {
                throw;
            }
            /* Catch any Data Layer or other exception and throw an unkown exception */
            catch (Exception ex)
            {
                ZiblerBusinessComponentsUnknownException exc
                = new ZiblerBusinessComponentsUnknownException (ex);

                /* Throw the new exception */
                throw exc;
            }
        }