/// <summary>
        /// Creates a new financial institution in the system.
        /// </summary>
        /// <param name="newFinancialInst">New financial institution</param>
        public void CreateNewFinancialInstitution(FinancialInstitution newFinancialInst)
        {
            try
            {
                IFinancialInstitutionDAO financialInstitutionDAO = _daoFactory.GetFinancialInstitutionDAO ();

                /* Check the name is not repeated */
                FinancialInstitution finInstExists = financialInstitutionDAO.GetFinancialInstitutionByName (
                    newFinancialInst.Name);

                if (finInstExists != null)
                    throw new ZiblerBusinessComponentsException (
                        Resources.FinancialInstitutionOperationsMsgFinancialInstitutionExists);

                /* Add default miscellaneous parameters */
                newFinancialInst.MiscellaneousParameters = new MiscellaneousParameters ();
                newFinancialInst.MiscellaneousParameters.ContractFirstClause = "ESTA ES LA PRIMERA CLAUSULA DEL CONTRATO";
                newFinancialInst.MiscellaneousParameters.EnforceLoanBracketsInRequests = false;
                newFinancialInst.MiscellaneousParameters.LogUserActions = false;
                newFinancialInst.MiscellaneousParameters.RejectionLetterText = "Los factores que determinaron el rechazo de la " +
                                                                               "solicitud de crédito son, que no cumple con " +
                                                                               "los parámetros establecidos por nuestro " +
                                                                               "sistema crediticio";

                /* Persist the financial institution */
                financialInstitutionDAO.MakePersistent (newFinancialInst);
            }
            /* 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 the Financial Institution in the system
        /// along with its memberships for applications.
        /// </summary>
        /// <param name="finInstToUpdate">finInstToUpdate</param>
        /// <param name="appMemberships">Fin Inst memberships</param>
        public void UpdateFinancialInstitution(FinancialInstitution finInstToUpdate,
            IList<ApplicationFinancialInstitutionMembership> appMemberships)
        {
            try
            {
                IFinancialInstitutionDAO financialInstitutionDAO = _daoFactory.GetFinancialInstitutionDAO ();

                IApplicationDAO applicationDAO = _daoFactory.GetApplicationDAO ();

                string oldName = financialInstitutionDAO.GetFinancialInstName (finInstToUpdate.Id);

                /* If the old name is not in the system, then the Financial
                * Institution must have been deleted */
                if (oldName == "")
                    throw new ZiblerBusinessComponentsException (Resources.RecordNotFound);

                //Determine if we should check for the user name.
                bool checkForUserName = true;

                if (oldName == finInstToUpdate.Name)
                    checkForUserName = false;

                //If the name has been changed, then we verified that the new one
                //is not already in the system.
                if (checkForUserName)
                {
                    FinancialInstitution finInstExists = financialInstitutionDAO.GetFinancialInstitutionByName (
                        finInstToUpdate.Name);

                    if (finInstExists != null)
                        throw new ZiblerBusinessComponentsException (
                            Resources.FinancialInstitutionOperationsMsgFinancialInstitutionExists);
                }

                //Remove all membershipts.
                finInstToUpdate.ApplicationFinancialInstitutionMemberships.Clear ();

                //Get all the applications.
                //TODO: There are not too many applications, however this may cause issues
                //		in future releases when applications are many
                IList<Application> applications = applicationDAO.FindAll ();

                //Add the memberships since we did not throw any exception
                foreach (Application application in applications)
                {
                    bool found = false;
                    int i = 0;

                    while (found == false && i < appMemberships.Count)
                    {
                        /* If the selected membership contains the current
                        * application, then add it as the membership.
                        * Note that the passed membership objects only contain
                        * the Name of the application
                        */
                        if (application.Name == appMemberships[i].Application.Name)
                        {
                            /* Change the application to the current and
                            * then add the membership */
                            appMemberships[i].Application = application;
                            finInstToUpdate.AddApplicationFinancialInstitutionMembership (
                                appMemberships[i]);

                            /* Exit the loop and try the next app */
                            found = true;
                        }
                        i++;
                    }
                }

                DateTime? startDate = null;
                DateTime? endDate = null;

                /* To set the Main start and end Date, we will use the earliest
                * and the latest dates of all the memberships. */
                foreach (ApplicationFinancialInstitutionMembership membership in
                    finInstToUpdate.ApplicationFinancialInstitutionMemberships)
                {
                    /* If we have a date, then we compare to see which one is earliest */
                    if (startDate != null)
                    {
                        bool keepDate = true;

                        /* the Main membership range dates will be null,
                        * so check for it */
                        if (membership.ValidFrom != null)
                        {
                            keepDate = DateUtilities.IsFirstDateGreaterOrEqualThanSecondDate (
                                membership.ValidFrom.Value,
                                startDate.Value);
                        }

                        if (keepDate)
                        {
                            /* Do nothing, keep the date */
                        }
                        else
                        {
                            /* Replace it */
                            startDate = membership.ValidFrom;
                        }
                    }
                    else
                    {
                        startDate = membership.ValidFrom;
                    }

                    if (endDate != null)
                    {
                        bool keepDate = true;

                        /* the Main membership range dates will be null,
                        * so check for it */
                        if (membership.ValidUntil != null)
                        {
                            keepDate = DateUtilities.IsFirstDateGreaterOrEqualThanSecondDate (
                                endDate.Value,
                                membership.ValidUntil.Value);
                        }

                        if (keepDate)
                        {
                            /* Do nothing, keep the date */
                        }
                        else
                        {
                            /* Replace it */
                            endDate = membership.ValidUntil;
                        }
                    }
                    else
                    {
                        endDate = membership.ValidUntil;
                    }
                }

                /* Only do this if the dates where found, otherwise
                * rise an exception */
                if (startDate != null && endDate != null)
                {
                    /* Now that we have a start date and an end date, assign them to
                    * the Main app membership */
                    foreach (ApplicationFinancialInstitutionMembership membership in
                        finInstToUpdate.ApplicationFinancialInstitutionMemberships)
                    {
                        /* Assign the dates */
                        if (membership.Application.Name == "Main")
                        {
                            membership.ValidFrom = startDate;
                            membership.ValidUntil = endDate;
                        }
                    }
                }
                else
                {
                    /* Something really bad must have happened if we got here.
                    * Add the error data*/
                    ZiblerBusinessComponentsUnknownException ex
                    = new ZiblerBusinessComponentsUnknownException ();
                    ex.Data.Add ("StartDate", startDate);
                    ex.Data.Add ("EndDate", endDate);

                    throw ex;
                }
            }
            /* 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 the Financial Institution in the system
        /// </summary>
        /// <param name="finInstToUpdate">finInstToUpdate</param>
        public void UpdateFinancialInstitution(FinancialInstitution finInstToUpdate)
        {
            try
            {
                IFinancialInstitutionDAO financialInstitutionDAO = _daoFactory.GetFinancialInstitutionDAO ();

                /* Just persist in the DB */
                financialInstitutionDAO.MakePersistent (finInstToUpdate);
            }
            /* 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;
            }
        }