models.GroupMember.java Source code

Java tutorial

Introduction

Here is the source code for models.GroupMember.java

Source

/*
 * Copyright (c) 2013, Helome and/or its affiliates. All rights reserved.
 * Helome PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * Created on 201465
 */
package models;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.TypedQuery;

import org.apache.commons.collections.CollectionUtils;

import play.db.jpa.JPA;
import vo.page.Page;

import common.Constants;

/**
 * @ClassName: GroupMember
 * @Description: ?
 * @date 201465 ?10:58:22
 * @author RenYouchao
 * 
 */

@Entity
@Table(name = "tb_group_member")
public class GroupMember {

    /**  */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long id;
    /**id**/
    public Long userId;
    /**??**/
    public String userName;
    /**email**/
    public String email;
    /****/
    @Enumerated(EnumType.ORDINAL)
    @Column(length = 1)
    public Role role;
    /***/
    public Date joinDate;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "groupId")
    public Group group;

    /**
     * @ClassName: Role
     * @Description: 
     * @date 201465 ?11:08:45
     * @author RenYouchao
     */
    public enum Role {
        MEMBER, TRANSLATE;
    }

    public GroupMember() {
    }

    public GroupMember(User user, Role role) {
        this.userId = user.getId();
        this.userName = user.userName;
        this.email = user.getEmail();
        this.role = role;
    }

    public GroupMember(Long userId, String userName, String email, Role role) {
        this.userId = userId;
        this.userName = userName;
        this.email = email;
        this.role = role;
    }

    public GroupMember(Long userId, String userName, String email, Role role, Date joinDate) {
        this.userId = userId;
        this.userName = userName;
        this.email = email;
        this.role = role;
        this.joinDate = joinDate;
    }

    public GroupMember(Long userId, String userName, String email, Role role, Date joinDate, Group group) {
        this.userId = userId;
        this.userName = userName;
        this.email = email;
        this.role = role;
        this.joinDate = joinDate;
        this.group = group;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public Date getJoinDate() {
        return joinDate;
    }

    public void setJoinDate(Date joinDate) {
        this.joinDate = joinDate;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    /**
     * 
     * @return
     */
    public GroupMember saveGroupMember() {
        JPA.em().persist(this);
        return this;
    }

    /**
     * 
     */
    public static void addGropMember(User user, GroupMember.Role role) {
        if (user == null || role == null) {
            throw new IllegalArgumentException("userrole?");
        }
        JPA.em().persist(new GroupMember(user.getId(), user.getName(), user.getEmail(), role));
    }

    /**
     * ?
     * @param userId
     * @return
     */
    public static List<GroupMember> queryGroupMember(Long userId) {
        if (userId == null) {
            throw new IllegalArgumentException("userId?");
        }
        List<GroupMember> list = JPA.em().createQuery("from GroupMember where userId=:userId")
                .setParameter("userId", userId).getResultList();
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<GroupMember>();
        }
        return list;
    }

    /**
     * ?ididGroupMember
     * @return
     */
    public static GroupMember queryGroupMemberByUserIdAndGroupId(Long userId, Long groupId) {
        List<GroupMember> list = JPA.em()
                .createQuery("from GroupMember where userId=:userId and group.id=:groupId", GroupMember.class)
                .setParameter("userId", userId).setParameter("groupId", groupId).getResultList();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    /**
     * id???
     * @param userIdList
     * @return
     */
    public static List<GroupMember> queryGroupMemberByUserIds(List<Long> userIdList) {
        if (CollectionUtils.isNotEmpty(userIdList)) {
            List<GroupMember> gmList = JPA.em()
                    .createQuery("from GroupMember where userId in (:userIds) and group is not null",
                            GroupMember.class)
                    .setParameter("userIds", userIdList).getResultList();
            if (CollectionUtils.isNotEmpty(gmList)) {
                return gmList;
            }
        }
        return new ArrayList<GroupMember>();

    }

    /**
     * ?id
     * @param id
     */
    public static void deleteById(Long id) {
        JPA.em().createQuery("delete from GroupMember where id = :id").setParameter("id", id).executeUpdate();
    }

    /**
     * ?groupId
     */
    public static void deleteByGroupId(Long groupId) {
        JPA.em().createQuery("update Group set owner = null where id = :id").setParameter("id", groupId)
                .executeUpdate();
        JPA.em().createQuery("delete from GroupMember where group.id = :groupId").setParameter("groupId", groupId)
                .executeUpdate();
    }

    /**
     * ?Id???
     */
    public static List<GroupMember> queryByGroupIdWithRandomOrder(Long groupId, Integer num) {
        String hql = "from GroupMember where group.id = :groupId order by RAND()";
        List<GroupMember> resultList = JPA.em().createQuery(hql, GroupMember.class).setParameter("groupId", groupId)
                .setFirstResult(0).setMaxResults(num).getResultList();
        return resultList;
    }

    /**
     * 
     * @param pageIndex 1
     * @param pageSize ??
     * @param groupId Id
     * @param excludeId GroupMember Id,null?
     * @return
     */
    public static Page<GroupMember> queryPageByGroupId(int start, int pageSize, Long groupId, Long excludeId) {
        String hql = " from GroupMember where group.id = :groupId ";
        if (null != excludeId) {
            hql += " and id <> :excludeId";
        }
        String countHql = "select count(id) " + hql;

        TypedQuery<Long> countTypedQuery = JPA.em().createQuery(countHql, Long.class).setParameter("groupId",
                groupId);
        TypedQuery<GroupMember> contentTypedQuery = JPA.em().createQuery(hql, GroupMember.class)
                .setParameter("groupId", groupId).setFirstResult((start - 1) * pageSize).setMaxResults(pageSize);

        if (null != excludeId) {
            countTypedQuery.setParameter("excludeId", excludeId);
            contentTypedQuery.setParameter("excludeId", excludeId);
        }

        Long count = countTypedQuery.getSingleResult();
        List<GroupMember> resultList = contentTypedQuery.getResultList();
        return new Page<GroupMember>(Constants.SUCESS, count, resultList);
    }

    /**
     * ???
     * @param userId
     * @param groupIdList
     * @return Map<GroupId, isJoined>
     */
    public static Map<Long, Boolean> checkJoinGroup(Long userId, List<Long> groupIdList) {
        if (CollectionUtils.isEmpty(groupIdList) || null == userId) {
            return new HashMap<Long, Boolean>();
        }
        String hql = "select group.id from GroupMember where userId = :userId and group.id in (:groupIdList)";
        List<Long> matchList = JPA.em().createQuery(hql, Long.class).setParameter("userId", userId)
                .setParameter("groupIdList", groupIdList).getResultList();

        Map<Long, Boolean> result = new HashMap<Long, Boolean>();
        for (Long groupId : groupIdList) {
            result.put(groupId, matchList.contains(groupId));
        }
        return result;
    }

    public static Map<Long, Boolean> checkJoinGroup(List<Long> userIdList, Long groupId) {
        if (CollectionUtils.isEmpty(userIdList) || null == groupId) {
            return new HashMap<Long, Boolean>();
        }
        String hql = "select userId from GroupMember where userId in (:userIdList) and group.id = :groupId";
        List<Long> matchList = JPA.em().createQuery(hql, Long.class).setParameter("userIdList", userIdList)
                .setParameter("groupId", groupId).getResultList();

        Map<Long, Boolean> result = new HashMap<Long, Boolean>();
        for (Long userId : userIdList) {
            result.put(userId, matchList.contains(userId));
        }
        return result;
    }

    public static void updateUserNameByUserId(String newName, Long userId) {
        String hql = "update GroupMember set userName = :newName where userId = :userId and userName != :newName";
        JPA.em().createQuery(hql).setParameter("newName", newName).setParameter("userId", userId).executeUpdate();
    }

}