<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import model.Bill;
import model.Group;
import model.GroupMember;

/**
 *
 * @author Georgi
 */
@Named
@SessionScoped
public class GroupMemberController implements Serializable {

    List<GroupMember> groupMemberList;
    GroupMember currentGroupMember;

    public GroupMemberController() {
        currentGroupMember = new GroupMember();
        refreshFromDB();
    }

    public List<GroupMember> getGroupMemberList() {
        refreshFromDB();
        return groupMemberList;
    }

    public void setGroupMemberList(List<GroupMember> groupMemberList) {
        this.groupMemberList = groupMemberList;
    }

    public GroupMember getCurrentGroupMember() {
        return currentGroupMember;
    }

    public void setCurrentGroupMember(GroupMember currentGroupMember) {
        this.currentGroupMember = currentGroupMember;
    }

    public void addGroupmember() {
        try {

            String sql = "INSERT INTO `group_members` (`group_id`, `user_id`) VALUES (?, ?);";
            Connection conn = DBUtils.getConnection();

            PreparedStatement pst = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            pst.setInt(1, currentGroupMember.getGroup_id());
            pst.setInt(2, currentGroupMember.getUser_id());

            pst.executeUpdate();

            groupMemberList.add(currentGroupMember);
            currentGroupMember = new GroupMember();
        } catch (SQLException ex) {
            Logger.getLogger(BillController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public List<GroupMember> getgroupMembersByGroupId(int groupId) {
        List<GroupMember> refinedGroupMembers = new ArrayList<>();
        for (GroupMember gMember : groupMemberList) {
            if (gMember.getGroup_id() == groupId) {
                refinedGroupMembers.add(gMember);
            }
        }
        return refinedGroupMembers;
    }

    public boolean deleteCurrentGroupmember() {
        try {

            String sql = "DELETE FROM `group_members` WHERE `group_members`.`group_id` = ? AND `group_members`.`user_id` = ?";
            Connection conn = DBUtils.getConnection();

            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setInt(1, currentGroupMember.getGroup_id());
            pst.setInt(2, currentGroupMember.getUser_id());
            pst.executeUpdate();

            for (GroupMember gMember : groupMemberList) {
                if (gMember.getGroup_id() == currentGroupMember.getGroup_id() & gMember.getUser_id() == currentGroupMember.getUser_id()) {
                    groupMemberList.remove(gMember);
                    return true;<commit_msg>Added function to check group members<commit_after>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import model.Bill;
import model.Group;
import model.GroupMember;

/**
 *
 * @author Georgi
 */
@Named
@SessionScoped
public class GroupMemberController implements Serializable {

    List<GroupMember> groupMemberList;
    GroupMember currentGroupMember;

    public GroupMemberController() {
        currentGroupMember = new GroupMember();
        refreshFromDB();
    }

    public List<GroupMember> getGroupMemberList() {
        refreshFromDB();
        return groupMemberList;
    }

    public void setGroupMemberList(List<GroupMember> groupMemberList) {
        this.groupMemberList = groupMemberList;
    }

    public GroupMember getCurrentGroupMember() {
        return currentGroupMember;
    }

    public void setCurrentGroupMember(GroupMember currentGroupMember) {
        this.currentGroupMember = currentGroupMember;
    }

    public void addGroupmember() {
        try {

            String sql = "INSERT INTO `group_members` (`group_id`, `user_id`) VALUES (?, ?);";
            Connection conn = DBUtils.getConnection();

            PreparedStatement pst = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            pst.setInt(1, currentGroupMember.getGroup_id());
            pst.setInt(2, currentGroupMember.getUser_id());

            pst.executeUpdate();

            groupMemberList.add(currentGroupMember);
            currentGroupMember = new GroupMember();
        } catch (SQLException ex) {
            Logger.getLogger(BillController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public boolean hasGroupMemberInGroup(int groupId, int userId) {
        List<GroupMember> refinedGroupMembers = new ArrayList<>();
        for (GroupMember gMember : groupMemberList) {
            if (gMember.getGroup_id() == groupId && gMember.getUser_id() == userId){
                return true;
            }
        }
        return false;
    }
    
    public List<GroupMember> getgroupMembersByGroupId(int groupId) {
        List<GroupMember> refinedGroupMembers = new ArrayList<>();
        for (GroupMember gMember : groupMemberList) {
            if (gMember.getGroup_id() == groupId) {
                refinedGroupMembers.add(gMember);
            }
        }
        return refinedGroupMembers;
    }

    public boolean deleteCurrentGroupmember() {
        try {

            String sql = "DELETE FROM `group_members` WHERE `group_members`.`group_id` = ? AND `group_members`.`user_id` = ?";
            Connection conn = DBUtils.getConnection();

            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setInt(1, currentGroupMember.getGroup_id());
            pst.setInt(2, currentGroupMember.getUser_id());
            pst.executeUpdate();

            for (GroupMember gMember : groupMemberList) {
                if (gMember.getGroup_id() == currentGroupMember.getGroup_id() & gMember.getUser_id() == currentGroupMember.getUser_id()) {
                    groupMemberList.remove(gMember);
                    return true;
{'fraction_non_alphanumeric': 0.09741203838325095, 'fraction_numerical': 0.0011631288165164292, 'mean_word_length': 2.9085227272727274, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3309074', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1820, 'n_words': 405}