Search in sources :

Example 1 with Family

use of org.mx.tools.ffee.dal.entity.Family in project main by JohnPeng739.

the class FamilyManageServiceImpl method createFamily.

/**
 * {@inheritDoc}
 *
 * @see FamilyManageService#createFamily(String, String, String)
 */
@Transactional()
public Family createFamily(String name, String ffeeAccountId, String memberRole) {
    Family family = getFamily(null, name);
    if (family != null) {
        throw new UserInterfaceFfeeErrorException(UserInterfaceFfeeErrorException.FfeeErrors.FAMILY_EXISTED);
    }
    FfeeAccount ffeeAccount = accessor.getById(ffeeAccountId, FfeeAccount.class);
    if (ffeeAccount == null) {
        throw new UserInterfaceFfeeErrorException(UserInterfaceFfeeErrorException.FfeeErrors.ACCOUNT_NOT_EXISTED);
    }
    FamilyMember member = EntityFactory.createEntity(FamilyMember.class);
    member.setFfeeAccount(ffeeAccount);
    member.setMemberRole(memberRole);
    member = accessor.save(member, false);
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Create a new family member, account: %s, family: %s.", ffeeAccount.getAccount().getName(), name));
    }
    family = EntityFactory.createEntity(Family.class);
    family.setName(name);
    family.setOwner(ffeeAccount);
    family.getMembers().add(member);
    family = accessor.save(family, false);
    if (operateLogService != null) {
        operateLogService.writeLog(String.format("成功创建家庭[%s],家主是账户[%s]。", family.getName(), ffeeAccount.getAccount().getName()));
    }
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Create the family[%s] successfully, account: %s.", family.getName(), ffeeAccount.getAccount().getName()));
    }
    return family;
}
Also used : FfeeAccount(org.mx.tools.ffee.dal.entity.FfeeAccount) FamilyMember(org.mx.tools.ffee.dal.entity.FamilyMember) UserInterfaceFfeeErrorException(org.mx.tools.ffee.error.UserInterfaceFfeeErrorException) Family(org.mx.tools.ffee.dal.entity.Family) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with Family

use of org.mx.tools.ffee.dal.entity.Family in project main by JohnPeng739.

the class FamilyManageServiceImpl method joinFamily.

/**
 * {@inheritDoc}
 *
 * @see FamilyManageService#joinFamily(String, String, String)
 */
@Transactional()
public Family joinFamily(String familyId, String ffeeAccountId, String memberRole) {
    Family family = getFamily(familyId, null);
    if (family == null) {
        throw new UserInterfaceFfeeErrorException(UserInterfaceFfeeErrorException.FfeeErrors.FAMILY_NOT_EXISTED);
    }
    FfeeAccount ffeeAccount = accessor.getById(ffeeAccountId, FfeeAccount.class);
    if (ffeeAccount == null) {
        throw new UserInterfaceFfeeErrorException(UserInterfaceFfeeErrorException.FfeeErrors.ACCOUNT_NOT_EXISTED);
    }
    Set<FamilyMember> members = family.getMembers();
    while (members != null && members.iterator().hasNext()) {
        FamilyMember member = members.iterator().next();
        if (member != null && member.getFfeeAccount() != null && ffeeAccountId.equals(member.getFfeeAccount().getId())) {
            throw new UserInterfaceFfeeErrorException(UserInterfaceFfeeErrorException.FfeeErrors.ACCOUNT_IN_MEMBERS);
        }
    }
    FamilyMember member = EntityFactory.createEntity(FamilyMember.class);
    member.setFfeeAccount(ffeeAccount);
    member.setMemberRole(memberRole);
    member = accessor.save(member, false);
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Create a new family member, account: %s, family: %s.", ffeeAccount.getAccount().getName(), family.getName()));
    }
    if (members == null) {
        throw new UserInterfaceFfeeErrorException(UserInterfaceFfeeErrorException.FfeeErrors.FAMILY_MEMBER_SAVE_FAIL);
    }
    members.add(member);
    family = accessor.save(family, false);
    if (operateLogService != null) {
        operateLogService.writeLog(String.format("账户[%s]成功加入家庭[%s]。", ffeeAccount.getAccount().getName(), family.getName()));
    }
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Account[%s] join the family[%s] successfully.", ffeeAccount.getAccount().getName(), family.getName()));
    }
    return family;
}
Also used : FfeeAccount(org.mx.tools.ffee.dal.entity.FfeeAccount) FamilyMember(org.mx.tools.ffee.dal.entity.FamilyMember) UserInterfaceFfeeErrorException(org.mx.tools.ffee.error.UserInterfaceFfeeErrorException) Family(org.mx.tools.ffee.dal.entity.Family) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with Family

use of org.mx.tools.ffee.dal.entity.Family in project main by JohnPeng739.

the class FfeeFamilyManageResource method createFamily.

@Path("family/create")
@POST
public DataVO<FamilyVO> createFamily(@QueryParam("userCode") String userCode, FamilyManageVO familyManageVO) {
    sessionDataStore.setCurrentUserCode(userCode);
    try {
        Family family = familyManageService.createFamily(familyManageVO.getName(), familyManageVO.getFfeeAccountId(), familyManageVO.getMemberRole());
        sessionDataStore.removeCurrentUserCode();
        return new DataVO<>(FamilyVO.transform(family));
    } catch (UserInterfaceException ex) {
        return new DataVO<>(ex);
    }
}
Also used : DataVO(org.mx.service.rest.vo.DataVO) Family(org.mx.tools.ffee.dal.entity.Family) UserInterfaceException(org.mx.error.UserInterfaceException)

Aggregations

Family (org.mx.tools.ffee.dal.entity.Family)3 FamilyMember (org.mx.tools.ffee.dal.entity.FamilyMember)2 FfeeAccount (org.mx.tools.ffee.dal.entity.FfeeAccount)2 UserInterfaceFfeeErrorException (org.mx.tools.ffee.error.UserInterfaceFfeeErrorException)2 Transactional (org.springframework.transaction.annotation.Transactional)2 UserInterfaceException (org.mx.error.UserInterfaceException)1 DataVO (org.mx.service.rest.vo.DataVO)1