Java tutorial
/* * 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 com.epam.ipodromproject.repository.jpa; import com.epam.ipodromproject.domain.BetResult; import com.epam.ipodromproject.domain.User; import com.epam.ipodromproject.repository.UserRepository; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import javax.persistence.TypedQuery; import javax.transaction.Transactional; import org.springframework.stereotype.Repository; /** * * @author Oleh */ @Repository("userRepository") public class JPAUserRepository implements UserRepository { @PersistenceContext EntityManager entityManager; public JPAUserRepository() { } @Override public User getUserById(Long id) { return entityManager.find(User.class, id); } @Override public Long save(User user) { entityManager.persist(user); return user.getId(); } @Override public Long update(User user) { entityManager.merge(user); return user.getId(); } @Override public boolean delete(User user) { entityManager.remove(user); return true; } @Override public User getUserByUsername(String username) { TypedQuery<User> query = entityManager.createNamedQuery("User.findByUsername", User.class); query.setParameter("username", username); return query.getSingleResult(); } @Override public void cancelBetsByCompetition(Long competitionID) { Query queryForUsers = entityManager.createNamedQuery("User.findMoneyBetByUsersByCompetition"); queryForUsers.setParameter("competitionID", competitionID); queryForUsers.setParameter("betResult", BetResult.NOT_CONSIDERED); List<Object> result = queryForUsers.getResultList(); Query query = entityManager.createNamedQuery("Person.returnMoneyToPerson"); for (Object objects : result) { Object[] resultRow = (Object[]) objects; query.setParameter("id", (Long) resultRow[0]); query.setParameter("moneyReturned", (Double) resultRow[1]); query.executeUpdate(); } } @Override public List<User> getUsersByPartOfName(String partOfName, int startingResult, int resultsCount, boolean enabled, boolean unblocked) { TypedQuery<User> query = entityManager.createNamedQuery("User.getUsersPageByPartOfNameAndEnabled", User.class); query.setParameter("code", "%" + partOfName + "%"); query.setParameter("enabled", enabled); query.setParameter("blocked", !unblocked); return query.setMaxResults(resultsCount).setFirstResult(startingResult).getResultList(); } @Override public double getTotalMoneyLost(User user) { return getTotalMoneyByResult(user, BetResult.LOST); } @Override public double getTotalMoneyWon(User user) { return getTotalMoneyByResult(user, BetResult.WON); } private double getTotalMoneyByResult(User user, BetResult betResult) { TypedQuery<Double> query = entityManager.createNamedQuery("User.findTotalMoneyByUsernameAndResult", Double.class); query.setParameter("user", user); query.setParameter("betResult", betResult); double moneyWon = 0; try { moneyWon = query.getSingleResult(); } catch (Exception e) { } return moneyWon; } @Override public long getTotalUsers(String partOfName, boolean enabled, boolean unblocked) { TypedQuery<Long> query = entityManager.createNamedQuery("User.getTotalUsersByPartOfNameAndEnabled", Long.class); query.setParameter("code", "%" + partOfName + "%"); query.setParameter("enabled", enabled); query.setParameter("blocked", !unblocked); long totalUsers = 0; try { totalUsers = query.getSingleResult(); } catch (Exception e) { } return totalUsers; } }