com.sammyun.service.impl.MemberServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.sammyun.service.impl.MemberServiceImpl.java

Source

/*
 * Copyright 2012-2014 sencloud.com.cn. All rights reserved.
 * Support: http://www.sencloud.com.cn
 * License: http://www.sencloud.com.cn/license
 */
package com.sammyun.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.persistence.LockModeType;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.sammyun.Page;
import com.sammyun.Pageable;
import com.sammyun.Principal;
import com.sammyun.Setting;
import com.sammyun.dao.MemberDao;
import com.sammyun.entity.Admin;
import com.sammyun.entity.Member;
import com.sammyun.entity.Member.MemberType;
import com.sammyun.entity.dict.DictSchool;
import com.sammyun.service.MemberService;
import com.sammyun.util.SettingUtils;

/**
 * Service - 
 * 
 * @author Sencloud Team
 * @version 3.0
 */
@Service("memberServiceImpl")
public class MemberServiceImpl extends BaseServiceImpl<Member, Long> implements MemberService {

    @Resource(name = "memberDaoImpl")
    private MemberDao memberDao;

    @Resource(name = "memberDaoImpl")
    public void setBaseDao(MemberDao memberDao) {
        super.setBaseDao(memberDao);
    }

    /**
     * ???
     * 
     * @param username ??(?)
     * @return ???
     */
    @Transactional(readOnly = true)
    public boolean usernameExists(String username) {
        return memberDao.usernameExists(username);
    }

    /**
     * ????
     * 
     * @param username ??(?)
     * @return ????
     */
    @Transactional(readOnly = true)
    public boolean usernameDisabled(String username) {
        Assert.hasText(username);
        Setting setting = SettingUtils.get();
        if (setting.getDisabledUsernames() != null) {
            for (String disabledUsername : setting.getDisabledUsernames()) {
                if (StringUtils.containsIgnoreCase(username, disabledUsername)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * E-mail?
     * 
     * @param email E-mail(?)
     * @return E-mail?
     */
    @Transactional(readOnly = true)
    public boolean emailExists(String email) {
        return memberDao.emailExists(email);
    }

    /**
     * E-mail?
     * 
     * @param previousEmail ?E-mail(?)
     * @param currentEmail ?E-mail(?)
     * @return E-mail?
     */
    @Transactional(readOnly = true)
    public boolean emailUnique(String previousEmail, String currentEmail) {
        if (StringUtils.equalsIgnoreCase(previousEmail, currentEmail)) {
            return true;
        } else {
            if (memberDao.emailExists(currentEmail)) {
                return true;
            } else {
                return false;
            }
        }
    }

    /**
     * mobile?
     * 
     * @param mobile
     * @return
     */
    @Transactional(readOnly = true)
    public boolean mobileUnique(String mobile) {
        if (memberDao.mobileUnique(mobile)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * ?
     * 
     * @param member 
     * @param operator ?
     */
    public void save(Member member, Admin operator) {
        Assert.notNull(member);
        memberDao.persist(member);
    }

    /**
     * 
     * 
     * @param member 
     * @param modifyPoint 
     * @param modifyBalance ?
     * @param depositMemo ?
     * @param operator ?
     */
    public void update(Member member, Integer modifyPoint, Admin operator) {
        Assert.notNull(member);

        memberDao.lock(member, LockModeType.PESSIMISTIC_WRITE);

        if (modifyPoint != null && modifyPoint != 0 && member.getPoint() + modifyPoint >= 0) {
            member.setPoint(member.getPoint() + modifyPoint);
        }

        memberDao.merge(member);
    }

    /**
     * ???
     * 
     * @param username ??(?)
     * @return ?null
     */
    @Transactional(readOnly = true)
    public Member findByUsername(String username) {
        return memberDao.findByUsername(username);
    }

    /**
     * ?E-mail
     * 
     * @param email E-mail(?)
     * @return ?null
     */
    @Transactional(readOnly = true)
    public List<Member> findListByEmail(String email) {
        return memberDao.findListByEmail(email);
    }

    /**
     * ?
     * 
     * @return ?
     */
    @Transactional(readOnly = true)
    public boolean isAuthenticated() {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            Principal principal = (Principal) request.getSession().getAttribute(Member.PRINCIPAL_ATTRIBUTE_NAME);
            if (principal != null) {
                return true;
            }
        }
        return false;
    }

    /**
     * ??
     * 
     * @return ??null
     */
    @Transactional(readOnly = true)
    public Member getCurrent() {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            Principal principal = (Principal) request.getSession().getAttribute(Member.PRINCIPAL_ATTRIBUTE_NAME);
            if (principal != null) {
                return memberDao.find(principal.getId());
            }
        }
        return null;
    }

    /**
     * ????
     * 
     * @return ????null
     */
    @Transactional(readOnly = true)
    public String getCurrentUsername() {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            Principal principal = (Principal) request.getSession().getAttribute(Member.PRINCIPAL_ATTRIBUTE_NAME);
            if (principal != null) {
                return principal.getUsername();
            }
        }
        return null;
    }

    /**
     * ??
     * 
     * @param previousVipCodel
     * @param vipCode
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public boolean vipCodeUnique(String previousVipCodel, String vipCode) {
        if (StringUtils.equalsIgnoreCase(previousVipCodel, vipCode)) {
            return true;
        } else {
            if (memberDao.vipCodeUnique(vipCode)) {
                return false;
            } else {
                return true;
            }
        }
    }

    /**
     * ???
     * 
     * @param username ??
     * @return ?null
     */
    @Override
    @Transactional(readOnly = true)
    public List<Member> findByUserID(String username) {
        return memberDao.findByUserID(username);
    }

    /**
     * ?mobile
     * 
     * @param mobile mobile(?)
     * @return ?null
     */
    @Override
    public List<Member> findListByMobile(String mobile) {
        return memberDao.findListByMobile(mobile);
    }

    /**
     * mobile?
     * 
     * @param previousMobile ?mobile(?)
     * @param mobile ?mobile(?)
     * @return mobile?
     */
    @Override
    public boolean mobileUnique(String previousMobile, String mobile) {
        if (StringUtils.equalsIgnoreCase(previousMobile, mobile)) {
            return true;
        } else {
            if (memberDao.mobileUnique(mobile)) {
                return true;
            } else {
                return false;
            }
        }
    }

    /**
     * 
     */
    @Override
    public List<String> findAllMember() {
        return memberDao.findAllMember();
    }

    /**
     * ??
     * 
     * @param idCard
     * @return
     */
    @Override
    public boolean idCardUnique(String idCard) {
        if (memberDao.idCardUnique(idCard)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<Member> findBySchoolAndType(DictSchool dictSchool, MemberType memberType) {
        // TODO Auto-generated method stub
        return memberDao.findBySchoolAndType(dictSchool, memberType);
    }

    @Override
    public List<Member> findActiveTeachersBySchool(DictSchool dictSchool) {
        if (dictSchool == null) {
            return null;
        }
        Set<Member> members = dictSchool.getMembers();
        if (members == null) {
            return null;
        }
        if (members.size() == 0) {
            return null;
        }
        List<Member> activeTeachers = new ArrayList<Member>();
        for (Member member : members) {
            if (member.getIsEnabled() == true && member.getMemberType() == MemberType.teacher) {
                activeTeachers.add(member);
            }
        }
        if (activeTeachers.size() == 0) {
            return null;
        } else {
            return activeTeachers;
        }
    }

    @Override
    public List<Member> findSystemMember(MemberType memberType, DictSchool dictSchool) {
        return memberDao.findSystemMember(memberType, dictSchool);
    }

    @Override
    public List<Member> findByRealName(String realName) {
        return memberDao.findByRealName(realName);
    }

    @Override
    public Page<Member> findPage(DictSchool dictSchool, Pageable pageable, Member.MemberType memberType,
            Boolean isEnabled) {
        return memberDao.findPage(dictSchool, pageable, memberType, isEnabled);
    }

    @Override
    public List<Member> findTeacherByNameAndSchoolInLike(String realName, DictSchool dictSchool) {
        return memberDao.findTeacherByNameAndSchoolInLike(realName, dictSchool);
    }
}