Newer
Older
CosmosServer / src / main / java / com / example / cosmos_serversb / entities / Group.java
//このクラスは仮です
package com.example.cosmos_serversb.entities;

import com.example.cosmos_serversb.models.SessionManager;
import com.example.cosmos_serversb.models.Users;
import org.hibernate.Session;

import java.util.HashSet;
import java.util.Set;

public class Group {
    private String gId;
    private String uri, name;
    private long requestHash = 0;
    private Set<User> members = new HashSet<>();
    private Set<Request> requests;

    public Group(){

    }

    public Group(String gId, String uri, String name, String uId){
        setgId(gId);
        setUri(uri);
        setName(name);
    }

    public String getgId() {
        return gId;
    }

    private void setgId(String gId) {
        this.gId = gId;
    }

    public String getUri() {
        return uri;
    }

    private void setUri(String uri){
        this.uri = uri;
    }

    public String getUriByGid(String gId){
        return uri;
    }

    private void setName(String name){
        this.name = name;
    }

    public String getName(){
        return name;
    }

    public String getNameByGid(String gId){
        return name;
    }

    public long getRequestHash() {
        return requestHash;
    }

    private void setRequestHash(long hash) {
        this.requestHash = hash;
    }

    public void updateRequestHash(Request request){
        setRequestHash(getRequestHash()+1);
    }

    public Set<Request> getRequests(){
        return requests;
    }

    public void setRequests(Set<Request> requests){
        this.requests = requests;
    }

    public void addRequests(Request request){
        requests.add(request);
    }

    public void setMembers(Set<User> members) {
        this.members = members;
    }

    public Set<User> getMembers() {
        return members;
    }

    public void deleteMember(User user){
        members.remove(user);
    }

    public void clearAllMembersForDeleteGroup(){
        members.clear();
    }

    public void addMember(String uId){
        User user = Users.getInstance().getUserById(uId);
        if(user != null) {
            //userが既に登録されている場合の例外処理は未実装。
            members.add(user);
            user.addGroups(this);
            Session session = SessionManager.getInstance().getSession();
            session.update(user);
        }else{
            //例外処理
        }
    }

    public boolean equals(Group group) {
        return this.gId.equals(group.getgId());
    }

}