Search in sources :

Example 11 with UserContext

use of org.mifos.security.util.UserContext in project head by mifos.

the class LoanAccountServiceFacadeWebTier method makeEarlyRepaymentFromSavings.

public void makeEarlyRepaymentFromSavings(RepayLoanInfoDto repayLoanInfoDto, String savingsAccGlobalNum) {
    MifosUser mifosUser = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = new UserContextFactory().create(mifosUser);
    SavingsAccountDetailDto savingsAcc = savingsServiceFacade.retrieveSavingsAccountDetails(savingsAccGlobalNum);
    Long savingsId = savingsAcc.getAccountId().longValue();
    Long customerId = savingsAcc.getCustomerId().longValue();
    LocalDate trxnDate = new LocalDate(repayLoanInfoDto.getDateOfPayment());
    Double amount = Double.parseDouble(repayLoanInfoDto.getEarlyRepayAmount());
    Integer paymentTypeId = Integer.parseInt(repayLoanInfoDto.getPaymentTypeId());
    String receiptId = repayLoanInfoDto.getReceiptNumber();
    LocalDate receiptDate = new LocalDate(repayLoanInfoDto.getReceiptDate());
    Locale preferredLocale = userContext.getPreferredLocale();
    SavingsWithdrawalDto savingsWithdrawalDto = new SavingsWithdrawalDto(savingsId, customerId, trxnDate, amount, paymentTypeId, receiptId, receiptDate, preferredLocale);
    try {
        transactionHelper.startTransaction();
        PaymentDto withdrawal = savingsServiceFacade.withdraw(savingsWithdrawalDto, true);
        repayLoanInfoDto.setSavingsPaymentId(withdrawal.getPaymentId());
        makeEarlyRepayment(repayLoanInfoDto);
        transactionHelper.commitTransaction();
    } catch (BusinessRuleException e) {
        transactionHelper.rollbackTransaction();
        throw new BusinessRuleException(e.getMessageKey(), e);
    } catch (Exception e) {
        transactionHelper.rollbackTransaction();
        throw new MifosRuntimeException(e);
    }
}
Also used : Locale(java.util.Locale) UserContext(org.mifos.security.util.UserContext) MifosUser(org.mifos.security.MifosUser) UserContextFactory(org.mifos.accounts.servicefacade.UserContextFactory) SavingsWithdrawalDto(org.mifos.dto.domain.SavingsWithdrawalDto) LocalDate(org.joda.time.LocalDate) PaymentDto(org.mifos.dto.domain.PaymentDto) AccountPaymentDto(org.mifos.dto.screen.AccountPaymentDto) LoanPaymentDto(org.mifos.dto.domain.LoanPaymentDto) ExpectedPaymentDto(org.mifos.dto.screen.ExpectedPaymentDto) StatesInitializationException(org.mifos.framework.exceptions.StatesInitializationException) BusinessRuleException(org.mifos.service.BusinessRuleException) PersistenceException(org.mifos.framework.exceptions.PersistenceException) ServiceException(org.mifos.framework.exceptions.ServiceException) HibernateSearchException(org.mifos.framework.exceptions.HibernateSearchException) PageExpiredException(org.mifos.framework.exceptions.PageExpiredException) SystemException(org.mifos.framework.exceptions.SystemException) MifosRuntimeException(org.mifos.core.MifosRuntimeException) AccountException(org.mifos.accounts.exceptions.AccountException) PropertyNotFoundException(org.mifos.framework.exceptions.PropertyNotFoundException) ConfigurationException(org.mifos.config.exceptions.ConfigurationException) MeetingException(org.mifos.application.meeting.exceptions.MeetingException) BusinessRuleException(org.mifos.service.BusinessRuleException) SavingsAccountDetailDto(org.mifos.dto.domain.SavingsAccountDetailDto) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 12 with UserContext

use of org.mifos.security.util.UserContext in project head by mifos.

the class LoanAccountServiceFacadeWebTier method makeEarlyRepayment.

@Override
public void makeEarlyRepayment(RepayLoanInfoDto repayLoanInfoDto) {
    MifosUser mifosUser = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = new UserContextFactory().create(mifosUser);
    LoanBO loan = this.loanDao.findByGlobalAccountNum(repayLoanInfoDto.getGlobalAccountNum());
    try {
        personnelDao.checkAccessPermission(userContext, loan.getOfficeId(), loan.getCustomer().getLoanOfficerId());
    } catch (AccountException e) {
        throw new MifosRuntimeException(e.getMessage(), e);
    }
    monthClosingServiceFacade.validateTransactionDate(repayLoanInfoDto.getDateOfPayment());
    if (!isTrxnDateValid(loan.getAccountId(), repayLoanInfoDto.getDateOfPayment())) {
        throw new BusinessRuleException("errors.invalidTxndate");
    }
    try {
        if (repayLoanInfoDto.isWaiveInterest() && !loan.isInterestWaived()) {
            throw new BusinessRuleException(LoanConstants.WAIVER_INTEREST_NOT_CONFIGURED);
        }
        BigDecimal interestDueForCurrentInstallment = calculateInterestDueForCurrentInstalmanet(repayLoanInfoDto);
        org.mifos.dto.domain.AccountPaymentDto paymentDto = new org.mifos.dto.domain.AccountPaymentDto(Double.valueOf(repayLoanInfoDto.getEarlyRepayAmount()), repayLoanInfoDto.getDateOfPayment(), repayLoanInfoDto.getReceiptNumber(), repayLoanInfoDto.getReceiptDate(), repayLoanInfoDto.getId());
        paymentDto.setPaymentTypeId(Short.valueOf(repayLoanInfoDto.getPaymentTypeId()));
        if (repayLoanInfoDto.getSavingsPaymentId() != null) {
            paymentDto.setMemberNumWithAmount(generateAmountWithInterest(null == repayLoanInfoDto.getMembersValue() ? new HashMap<String, Double>() : repayLoanInfoDto.getMembersValue(), repayLoanInfoDto));
            loan.makeEarlyRepayment(paymentDto, repayLoanInfoDto.getId(), repayLoanInfoDto.isWaiveInterest(), new Money(loan.getCurrency(), interestDueForCurrentInstallment), repayLoanInfoDto.getSavingsPaymentId(), null);
        } else {
            loan.makeEarlyRepayment(paymentDto, repayLoanInfoDto.getId(), repayLoanInfoDto.isWaiveInterest(), new Money(loan.getCurrency(), interestDueForCurrentInstallment));
        }
    } catch (AccountException e) {
        throw new BusinessRuleException(e.getKey(), e);
    }
}
Also used : UserContext(org.mifos.security.util.UserContext) LoanBO(org.mifos.accounts.loan.business.LoanBO) MifosUser(org.mifos.security.MifosUser) UserContextFactory(org.mifos.accounts.servicefacade.UserContextFactory) BigDecimal(java.math.BigDecimal) Money(org.mifos.framework.util.helpers.Money) BusinessRuleException(org.mifos.service.BusinessRuleException) AccountException(org.mifos.accounts.exceptions.AccountException) AccountPaymentDto(org.mifos.dto.screen.AccountPaymentDto) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 13 with UserContext

use of org.mifos.security.util.UserContext in project head by mifos.

the class LoanAccountServiceFacadeWebTier method retrieveLoanOfficerDetailsForBranch.

@Override
public ChangeAccountStatusDto retrieveLoanOfficerDetailsForBranch(Short officeId) {
    MifosUser mifosUser = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = new UserContextFactory().create(mifosUser);
    CenterCreation officeDetails = new CenterCreation(officeId, userContext.getId(), userContext.getLevelId(), userContext.getPreferredLocale());
    List<PersonnelDto> loanOfficers = this.personnelDao.findActiveLoanOfficersForOffice(officeDetails);
    boolean loanPendingApprovalStateEnabled = ProcessFlowRules.isLoanPendingApprovalStateEnabled();
    Short accountState = AccountState.LOAN_PARTIAL_APPLICATION.getValue();
    if (loanPendingApprovalStateEnabled) {
        accountState = AccountState.LOAN_PENDING_APPROVAL.getValue();
    }
    boolean centerHierarchyExists = ClientRules.getCenterHierarchyExists();
    return new ChangeAccountStatusDto(new ArrayList<OfficeDetailsDto>(), loanOfficers, loanPendingApprovalStateEnabled, accountState, centerHierarchyExists);
}
Also used : ChangeAccountStatusDto(org.mifos.dto.screen.ChangeAccountStatusDto) UserContext(org.mifos.security.util.UserContext) CenterCreation(org.mifos.dto.domain.CenterCreation) PersonnelDto(org.mifos.dto.domain.PersonnelDto) MifosUser(org.mifos.security.MifosUser) UserContextFactory(org.mifos.accounts.servicefacade.UserContextFactory) OfficeDetailsDto(org.mifos.dto.domain.OfficeDetailsDto)

Example 14 with UserContext

use of org.mifos.security.util.UserContext in project head by mifos.

the class LoanAccountServiceFacadeWebTier method reverseLoanDisbursal.

@Override
public void reverseLoanDisbursal(String globalAccountNum, String note) {
    MifosUser mifosUser = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = new UserContextFactory().create(mifosUser);
    LoanBO loan = this.loanDao.findByGlobalAccountNum(globalAccountNum);
    PersonnelBO personnel = this.personnelDao.findPersonnelById(userContext.getId());
    loan.updateDetails(userContext);
    try {
        this.transactionHelper.startTransaction();
        loan.reverseLoanDisbursal(personnel, note);
        this.loanDao.save(loan);
        this.transactionHelper.commitTransaction();
    } catch (BusinessRuleException e) {
        this.transactionHelper.rollbackTransaction();
        throw new BusinessRuleException(e.getMessageKey(), e);
    } catch (AccountException e) {
        this.transactionHelper.rollbackTransaction();
        throw new BusinessRuleException(e.getKey(), e);
    } finally {
        this.transactionHelper.closeSession();
    }
}
Also used : BusinessRuleException(org.mifos.service.BusinessRuleException) AccountException(org.mifos.accounts.exceptions.AccountException) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) UserContext(org.mifos.security.util.UserContext) LoanBO(org.mifos.accounts.loan.business.LoanBO) MifosUser(org.mifos.security.MifosUser) UserContextFactory(org.mifos.accounts.servicefacade.UserContextFactory)

Example 15 with UserContext

use of org.mifos.security.util.UserContext in project head by mifos.

the class LoginServiceFacadeWebTier method login.

@Override
public LoginDto login(String username, String password) {
    PersonnelBO user = this.personnelDao.findPersonnelByUsername(username);
    if (user == null) {
        throw new UsernameNotFoundException(LoginConstants.KEYINVALIDUSER);
    }
    MifosConfigurationManager configMgr = MifosConfigurationManager.getInstance();
    List<ValueListElement> localeList = personnelServiceFacade.getDisplayLocaleList();
    Locale preferredLocale = new Locale(configMgr.getString(LANGUAGE_CODE), configMgr.getString(COUNTRY_CODE));
    String listElement = "[" + preferredLocale.toString() + "]";
    Short localeId = user.getPreferredLocale();
    for (ValueListElement element : localeList) {
        if (element.getName().contains(listElement)) {
            localeId = element.getId().shortValue();
            break;
        }
    }
    user.setPreferredLocale(localeId);
    UserContext userContext = new UserContext();
    userContext.setPreferredLocale(preferredLocale);
    userContext.setLocaleId(localeId);
    userContext.setId(user.getPersonnelId());
    userContext.setName(user.getDisplayName());
    userContext.setLevel(user.getLevelEnum());
    userContext.setRoles(user.getRoles());
    userContext.setLastLogin(user.getLastLogin());
    userContext.setPasswordChanged(user.getPasswordChanged());
    userContext.setBranchId(user.getOffice().getOfficeId());
    userContext.setBranchGlobalNum(user.getOffice().getGlobalOfficeNum());
    userContext.setOfficeLevelId(user.getOffice().getLevel().getId());
    user.updateDetails(userContext);
    try {
        this.transactionHelper.startTransaction();
        this.transactionHelper.beginAuditLoggingFor(user);
        user.login(password);
        this.personnelDao.save(user);
        this.transactionHelper.commitTransaction();
        boolean isPasswordExpired = new LocalDate(user.getPasswordExpirationDate()).isBefore(new LocalDate());
        return new LoginDto(user.getPersonnelId(), user.getOffice().getOfficeId(), user.isPasswordChanged(), isPasswordExpired);
    } catch (ApplicationException e) {
        this.transactionHelper.rollbackTransaction();
        throw new BusinessRuleException(e.getKey(), e);
    } catch (Exception e) {
        this.transactionHelper.rollbackTransaction();
        throw new MifosRuntimeException(e);
    } finally {
        this.transactionHelper.closeSession();
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) Locale(java.util.Locale) UserContext(org.mifos.security.util.UserContext) LocalDate(org.joda.time.LocalDate) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) BusinessRuleException(org.mifos.service.BusinessRuleException) MifosRuntimeException(org.mifos.core.MifosRuntimeException) PersonnelException(org.mifos.customers.personnel.exceptions.PersonnelException) ApplicationException(org.mifos.framework.exceptions.ApplicationException) MifosConfigurationManager(org.mifos.config.business.MifosConfigurationManager) BusinessRuleException(org.mifos.service.BusinessRuleException) ApplicationException(org.mifos.framework.exceptions.ApplicationException) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) LoginDto(org.mifos.dto.domain.LoginDto) ValueListElement(org.mifos.dto.domain.ValueListElement) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Aggregations

UserContext (org.mifos.security.util.UserContext)369 MifosUser (org.mifos.security.MifosUser)134 MifosRuntimeException (org.mifos.core.MifosRuntimeException)102 ArrayList (java.util.ArrayList)97 Test (org.junit.Test)81 BusinessRuleException (org.mifos.service.BusinessRuleException)75 TransactionDemarcate (org.mifos.framework.util.helpers.TransactionDemarcate)72 PersonnelBO (org.mifos.customers.personnel.business.PersonnelBO)65 AccountException (org.mifos.accounts.exceptions.AccountException)55 UserContextFactory (org.mifos.accounts.servicefacade.UserContextFactory)49 Money (org.mifos.framework.util.helpers.Money)46 LoanBO (org.mifos.accounts.loan.business.LoanBO)45 LocalDate (org.joda.time.LocalDate)44 SavingsBO (org.mifos.accounts.savings.business.SavingsBO)43 ServiceException (org.mifos.framework.exceptions.ServiceException)43 PersistenceException (org.mifos.framework.exceptions.PersistenceException)38 CustomerBO (org.mifos.customers.business.CustomerBO)37 MeetingBO (org.mifos.application.meeting.business.MeetingBO)34 Date (java.util.Date)31 CustomerException (org.mifos.customers.exceptions.CustomerException)31