Search in sources :

Example 1 with OfficeSearch

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

the class OfficeServiceFacadeWebTier method createOffice.

@Override
public ListElement createOffice(Short operationMode, OfficeDto officeDto) {
    MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = new UserContextFactory().create(user);
    OfficeLevel level = OfficeLevel.getOfficeLevel(officeDto.getLevelId());
    OfficeBO parentOffice = officeDao.findOfficeById(officeDto.getParentId());
    AddressDto addressDto = officeDto.getAddress();
    Address address = new Address(addressDto.getLine1(), addressDto.getLine2(), addressDto.getLine3(), addressDto.getCity(), addressDto.getState(), addressDto.getCountry(), addressDto.getZip(), addressDto.getPhoneNumber());
    try {
        OfficeBO officeBO = new OfficeBO(userContext, level, parentOffice, officeDto.getCustomFields(), officeDto.getName(), officeDto.getOfficeShortName(), address, OperationMode.fromInt(operationMode.intValue()));
        OfficePersistence officePersistence = new OfficePersistence();
        if (officePersistence.isOfficeNameExist(officeDto.getName())) {
            throw new OfficeValidationException(OfficeConstants.OFFICENAMEEXIST);
        }
        if (officePersistence.isOfficeShortNameExist(officeDto.getOfficeShortName())) {
            throw new OfficeValidationException(OfficeConstants.OFFICESHORTNAMEEXIST);
        }
        String searchId = generateSearchId(parentOffice);
        officeBO.setSearchId(searchId);
        String globalOfficeNum = generateOfficeGlobalNo();
        officeBO.setGlobalOfficeNum(globalOfficeNum);
        StaticHibernateUtil.startTransaction();
        this.officeDao.save(officeBO);
        StaticHibernateUtil.commitTransaction();
        //Shahid - this is hackish solution to return officeId and globalOfficeNum via ListElement, it should be fixed, at least
        //a proper data storage class can be created
        ListElement element = new ListElement(new Integer(officeBO.getOfficeId()), officeBO.getGlobalOfficeNum());
        // if we are here it means office created sucessfully
        // we need to update hierarchy manager cache
        OfficeSearch os = new OfficeSearch(officeBO.getOfficeId(), officeBO.getSearchId(), officeBO.getParentOffice().getOfficeId());
        List<OfficeSearch> osList = new ArrayList<OfficeSearch>();
        osList.add(os);
        EventManger.postEvent(Constants.CREATE, osList, SecurityConstants.OFFICECHANGEEVENT);
        return element;
    } catch (OfficeValidationException e) {
        StaticHibernateUtil.rollbackTransaction();
        throw new BusinessRuleException(e.getMessage());
    } catch (PersistenceException e) {
        StaticHibernateUtil.rollbackTransaction();
        throw new MifosRuntimeException(e);
    } catch (OfficeException e) {
        StaticHibernateUtil.rollbackTransaction();
        throw new BusinessRuleException(e.getKey(), e);
    } finally {
        StaticHibernateUtil.closeSession();
    }
}
Also used : OfficeException(org.mifos.customers.office.exceptions.OfficeException) OfficeValidationException(org.mifos.customers.office.exceptions.OfficeValidationException) Address(org.mifos.framework.business.util.Address) UserContext(org.mifos.security.util.UserContext) ArrayList(java.util.ArrayList) MifosUser(org.mifos.security.MifosUser) UserContextFactory(org.mifos.accounts.servicefacade.UserContextFactory) OfficeSearch(org.mifos.security.util.OfficeSearch) AddressDto(org.mifos.dto.domain.AddressDto) BusinessRuleException(org.mifos.service.BusinessRuleException) OfficeBO(org.mifos.customers.office.business.OfficeBO) ListElement(org.mifos.dto.screen.ListElement) PersistenceException(org.mifos.framework.exceptions.PersistenceException) OfficePersistence(org.mifos.customers.office.persistence.OfficePersistence) OfficeLevel(org.mifos.customers.office.util.helpers.OfficeLevel) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 2 with OfficeSearch

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

the class OfficePersistence method getPersonnelOffices.

/**
     * This function is used to get the list of the offices under the given
     * personnel office under any user at any time
     *
     * @param officeid
     *            office id of the person
     * @return List list of the offices under him
     * @throws HibernateProcessException
     */
public List<OfficeSearch> getPersonnelOffices(Short officeid) throws SystemException, ApplicationException {
    HierarchyManager hm = HierarchyManager.getInstance();
    String pattern = hm.getSearchId(officeid) + "%";
    List<OfficeSearch> lst = null;
    try {
        Session session = StaticHibernateUtil.getSessionTL();
        Query officeSearch = session.getNamedQuery(NamedQueryConstants.GETOFFICESEARCH);
        officeSearch.setString(SecurityConstants.PATTERN, pattern);
        lst = officeSearch.list();
    } catch (HibernateException he) {
        throw new SecurityException(SecurityConstants.GENERALERROR, he);
    }
    return lst;
}
Also used : HierarchyManager(org.mifos.security.authorization.HierarchyManager) Query(org.hibernate.Query) HibernateException(org.hibernate.HibernateException) OfficeSearch(org.mifos.security.util.OfficeSearch) SecurityException(org.mifos.framework.exceptions.SecurityException) Session(org.hibernate.Session)

Example 3 with OfficeSearch

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

the class OfficePersistence method getOffices.

/**
     * This function is used to initialise the the hirerchy manager which is
     * paert of the security module which keeps the cache of officeid to office
     * search id so that it can find office under given person without going to
     * database every time
     *
     * @return List of OfficeSearch objects which contains office is and
     *         associated searchid
     * @throws HibernateProcessException
     */
public List<OfficeSearch> getOffices() throws SystemException, ApplicationException {
    List<OfficeSearch> lst = null;
    try {
        Session session = StaticHibernateUtil.getSessionTL();
        Query queryOfficeSearchList = session.getNamedQuery(NamedQueryConstants.GETOFFICESEARCHLIST);
        lst = queryOfficeSearchList.list();
    } catch (HibernateException he) {
        throw new SecurityException(SecurityConstants.GENERALERROR, he);
    }
    return lst;
}
Also used : Query(org.hibernate.Query) HibernateException(org.hibernate.HibernateException) OfficeSearch(org.mifos.security.util.OfficeSearch) SecurityException(org.mifos.framework.exceptions.SecurityException) Session(org.hibernate.Session)

Aggregations

OfficeSearch (org.mifos.security.util.OfficeSearch)3 HibernateException (org.hibernate.HibernateException)2 Query (org.hibernate.Query)2 Session (org.hibernate.Session)2 SecurityException (org.mifos.framework.exceptions.SecurityException)2 ArrayList (java.util.ArrayList)1 UserContextFactory (org.mifos.accounts.servicefacade.UserContextFactory)1 MifosRuntimeException (org.mifos.core.MifosRuntimeException)1 OfficeBO (org.mifos.customers.office.business.OfficeBO)1 OfficeException (org.mifos.customers.office.exceptions.OfficeException)1 OfficeValidationException (org.mifos.customers.office.exceptions.OfficeValidationException)1 OfficePersistence (org.mifos.customers.office.persistence.OfficePersistence)1 OfficeLevel (org.mifos.customers.office.util.helpers.OfficeLevel)1 AddressDto (org.mifos.dto.domain.AddressDto)1 ListElement (org.mifos.dto.screen.ListElement)1 Address (org.mifos.framework.business.util.Address)1 PersistenceException (org.mifos.framework.exceptions.PersistenceException)1 MifosUser (org.mifos.security.MifosUser)1 HierarchyManager (org.mifos.security.authorization.HierarchyManager)1 UserContext (org.mifos.security.util.UserContext)1