<commit_before>package se.kth.hopsworks.users;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import se.kth.hopsworks.user.model.Users;
import se.kth.kthfsdashboard.user.AbstractFacade;


/**
 *
 * @author <NAME><<EMAIL>>
 *
 */
@Stateless
public class UserFacade extends AbstractFacade<Users> {
    
    private final int STARTING_USER = 1000;

    @PersistenceContext(unitName = "kthfsPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UserFacade() {
        super(Users.class);
    }

    @Override
    public List<Users> findAll() {
        TypedQuery<Users> query = em.createNamedQuery("Users.findAll",
                Users.class);
        return query.getResultList();
    }

    public List<Users> findAllByName() {
        TypedQuery<Users> query = em.createNamedQuery("Users.findAllByName",
                Users.class);
        return query.getResultList();
    }

    public List<Users> findAllUsers() {
        Query query = em.createNativeQuery("SELECT * FROM USERS", Users.class);
        return query.getResultList();
    }

    public List<Users> filterUsersBasedOnStudy(String name) {

        Query query = em.createNativeQuery(
                "SELECT * FROM USERS WHERE email NOT IN (SELECT team_member FROM StudyTeam WHERE name=?)",
                Users.class).setParameter(1, name);
        return query.getResultList();
    }

    public void persist(Users user) {
        em.persist(user);
    }

    public int lastUserID() {
        Query query = em.createNativeQuery("SELECT MAX(p.uid) FROM users p");
        Object obj = query.getSingleResult();

        if (obj == null) {
            return STARTING_USER;
        }
        return (Integer) obj;
    }

    public void update(Users user) {
        em.merge(user);
    }

    public void removeByEmail(String email) {
        Users user = findByEmail(email);
        if (user != null) {
            em.remove(user);
        }
    }

    @Override
    public void remove(Users user) {
        if (user != null && user.getEmail() != null && em.contains(user)) {
            em.remove(user);
        }
    }

    public Users findByEmail(String email) {
        return em.createNamedQuery("Users.findByEmail", Users.class).setParameter("email", email)
                .getSingleResult();
    }

    public void detach(Users user) {
        em.detach(user);
    }

    /**
     * Get all users with STATUS = status.
     *
     * @param status
     * @return
     */
    public List<Users> findAllByStatus(int status) {
        TypedQuery<Users> query = em.createNamedQuery("Users.findByStatus",
                Users.class);
        query.setParameter("status", status);
        return query.getResultList();
    }

}<commit_msg>Fixes #10, had problem with creating a new user due to exception that wasnt handled in the facade.<commit_after>package se.kth.hopsworks.users;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import se.kth.hopsworks.user.model.Users;
import se.kth.kthfsdashboard.user.AbstractFacade;

/**
 *
 * @author <NAME><<EMAIL>>
 *
 */
@Stateless
public class UserFacade extends AbstractFacade<Users> {

  private final int STARTING_USER = 1000;

  @PersistenceContext(unitName = "kthfsPU")
  private EntityManager em;

  @Override
  protected EntityManager getEntityManager() {
    return em;
  }

  public UserFacade() {
    super(Users.class);
  }

  @Override
  public List<Users> findAll() {
    TypedQuery<Users> query = em.createNamedQuery("Users.findAll",
            Users.class);
    return query.getResultList();
  }

  public List<Users> findAllByName() {
    TypedQuery<Users> query = em.createNamedQuery("Users.findAllByName",
            Users.class);
    return query.getResultList();
  }

  public List<Users> findAllUsers() {
    Query query = em.createNativeQuery("SELECT * FROM USERS", Users.class);
    return query.getResultList();
  }

  public List<Users> filterUsersBasedOnStudy(String name) {

    Query query = em.createNativeQuery(
            "SELECT * FROM USERS WHERE email NOT IN (SELECT team_member FROM StudyTeam WHERE name=?)",
            Users.class).setParameter(1, name);
    return query.getResultList();
  }

  public void persist(Users user) {
    em.persist(user);
  }

  public int lastUserID() {
    Query query = em.createNativeQuery("SELECT MAX(p.uid) FROM users p");
    Object obj = query.getSingleResult();

    if (obj == null) {
      return STARTING_USER;
    }
    return (Integer) obj;
  }

  public void update(Users user) {
    em.merge(user);
  }

  public void removeByEmail(String email) {
    Users user = findByEmail(email);
    if (user != null) {
      em.remove(user);
    }
  }

  @Override
  public void remove(Users user) {
    if (user != null && user.getEmail() != null && em.contains(user)) {
      em.remove(user);
    }
  }

  public Users findByEmail(String email) {
    try {
      return em.createNamedQuery("Users.findByEmail", Users.class).setParameter(
              "email", email)
              .getSingleResult();
    } catch (Exception e) {
      return null;
    }
  }

  public void detach(Users user) {
    em.detach(user);
  }

  /**
   * Get all users with STATUS = status.
   *
   * @param status
   * @return
   */
  public List<Users> findAllByStatus(int status) {
    TypedQuery<Users> query = em.createNamedQuery("Users.findByStatus",
            Users.class);
    query.setParameter("status", status);
    return query.getResultList();
  }

}
{'fraction_non_alphanumeric': 0.11617546264564771, 'fraction_numerical': 0.00205620287868403, 'mean_word_length': 2.758531873792659, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '7482475', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1739, 'n_words': 400}