com.greenline.hrs.admin.user.service.UserInfoService.java Source code

Java tutorial

Introduction

Here is the source code for com.greenline.hrs.admin.user.service.UserInfoService.java

Source

/*
 * Project: admin-parent
 * 
 * File Created at 2014-04-16
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package com.greenline.hrs.admin.user.service;

import com.greenline.hrs.admin.user.biz.UserBasicInfoBiz;
import com.greenline.hrs.admin.user.biz.UserNickNameBiz;
import com.greenline.hrs.admin.user.biz.UserPassportBiz;
import com.greenline.hrs.admin.user.bo.UserStatus;
import com.greenline.hrs.admin.user.po.UserBasicInfo;
import com.greenline.hrs.admin.user.po.UserNickName;
import com.greenline.hrs.admin.user.po.UserPassport;
import com.greenline.hrs.admin.user.vo.UserNormalInfo;
import com.greenline.hrs.admin.util.encrypt.EncryptType;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author July
 * @version V1.0
 * @type UserInfoService
 * @desc ??
 * @date 2014-04-16
 */
@Service
public class UserInfoService {

    private static final Log LOG = LogFactory.getLog(UserInfoService.class);

    @Autowired
    private UserPassportBiz userPassportBiz;

    @Autowired
    private UserBasicInfoBiz userBasicInfoBiz;

    @Autowired
    private UserNickNameBiz userNickNameBiz;

    /**
     * 
     *
     * @param userNormalInfo
     * @return ?Longuid?null
     */
    public Long addUser(UserNormalInfo userNormalInfo) {
        if (userNormalInfo == null) {
            return null;
        }
        UserPassport userPassport = new UserPassport();
        UserNickName userNickName = new UserNickName();
        UserBasicInfo userBasicInfo = new UserBasicInfo();
        BeanUtils.copyProperties(userNormalInfo, userNickName);
        BeanUtils.copyProperties(userNormalInfo, userBasicInfo);
        BeanUtils.copyProperties(userNormalInfo, userPassport);
        //TODO ?salt?,?
        userPassport.setSalt(System.currentTimeMillis() + "");
        // 
        userPassport.setAuthType(EncryptType.DES.name());
        // 
        userBasicInfo.setUserStatus(UserStatus.ENABLE.name());
        Long uid = null;
        try {
            uid = userNickNameBiz.save(userNickName);
            //nickName?
            if (uid != null) {
                userBasicInfo.setUid(uid);
                userPassport.setUid(uid);
                userPassportBiz.save(userPassport);
                userBasicInfoBiz.saveUser(userBasicInfo);
            }
        } catch (DataIntegrityViolationException e) {
            LOG.error("Add user Exception", e);
            // ,uid?,?
            if (uid != null) {
                userNickNameBiz.delete(uid);
                userPassportBiz.delete(uid);
            }
            uid = null;
        }
        return uid;
    }

    /**
     * ?????
     *
     * @param offset ???
     * @param size   ??
     * @return , ??list.
     */
    public List<UserNormalInfo> getUserInfoList(int offset, int size) {

        List<UserNickName> userNickNames = userNickNameBiz.getList(offset, size);
        if (userNickNames.isEmpty()) {
            return Collections.EMPTY_LIST;
        }
        Map<Long, UserNormalInfo> userNormalInfoMap = new HashMap<Long, UserNormalInfo>(userNickNames.size() * 2);
        List<Long> userIds = new ArrayList<Long>(userNickNames.size() * 2);
        List<UserNormalInfo> userNormalInfos = new ArrayList<UserNormalInfo>(userNickNames.size() * 2);
        UserNormalInfo userNormalInfo = null;
        for (UserNickName userNickName : userNickNames) {
            //?nickName?normalInfo
            userNormalInfo = new UserNormalInfo();
            userNormalInfo.setUid(userNickName.getUid());
            userNormalInfo.setNickName(userNickName.getNickName());
            userIds.add(userNickName.getUid());
            //normalInfoMap
            userNormalInfoMap.put(userNickName.getUid(), userNormalInfo);
        }
        //??,GC
        userNickNames.clear();

        List<UserBasicInfo> userBasicInfos = userBasicInfoBiz.getUserList(userIds);

        for (UserBasicInfo userBasicInfo : userBasicInfos) {
            //?uid???
            userNormalInfo = userNormalInfoMap.get(userBasicInfo.getUid());
            BeanUtils.copyProperties(userBasicInfo, userNormalInfo);
            userNormalInfos.add(userNormalInfo);
        }
        //??,GC
        userBasicInfos.clear();
        userNormalInfoMap.clear();

        return userNormalInfos;
    }

    /**
     * ??normalInfo?
     *
     * @param uid ??id
     * @return , , ?null.
     */
    public UserNormalInfo getUserNomalInfo(long uid) {
        UserNickName userNickName = userNickNameBiz.get(uid);
        UserBasicInfo userBasicInfo = userBasicInfoBiz.getUserBasicInfo(uid);
        UserNormalInfo userNormalInfo = null;
        //??null,
        if (userNickName != null && userBasicInfo != null) {
            userNormalInfo = new UserNormalInfo();
            BeanUtils.copyProperties(userNickName, userNormalInfo);
            BeanUtils.copyProperties(userBasicInfo, userNormalInfo);
        }
        return userNormalInfo;
    }

    /**
     * ??
     *
     * @param uid ?id
     * @return ???.
     */
    public boolean delUserNomalInfo(long uid) {
        boolean flag = false;
        try {
            userNickNameBiz.delete(uid);
            userPassportBiz.delete(uid);
            userBasicInfoBiz.delete(uid);
            flag = true;
        } catch (Exception e) {
            LOG.error(":" + uid, e);
        }
        return flag;
    }
}