com.epam.ipodromproject.repository.jpa.JPABetRepository.java Source code

Java tutorial

Introduction

Here is the source code for com.epam.ipodromproject.repository.jpa.JPABetRepository.java

Source

/*
 * 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.Bet;
import com.epam.ipodromproject.domain.BetResult;
import com.epam.ipodromproject.repository.BetRepository;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Repository;
import javax.transaction.Transactional;

/**
 *
 * @author Oleh
 */
@Repository("betRepository")
public class JPABetRepository implements BetRepository {

    @PersistenceContext
    EntityManager entityManager;

    public JPABetRepository() {
    }

    @Override
    public Bet getBetById(Long id) {
        return entityManager.find(Bet.class, id);
    }

    @Transactional
    @Override
    public Long save(Bet bet) {
        entityManager.persist(bet);
        return bet.getId();
    }

    @Transactional
    @Override
    public Long update(Bet bet) {
        entityManager.merge(bet);
        return bet.getId();
    }

    @Transactional
    @Override
    public boolean delete(Bet bet) {
        entityManager.remove(bet);
        return true;
    }

    @Transactional
    @Override
    public List<Bet> getActiveBetsMadeByUser(String username, int startingResult, int resultsCount) {
        TypedQuery<Bet> query = entityManager.createNamedQuery("Bet.findByUsernameByBetResult", Bet.class);
        query.setParameter("username", username);
        query.setParameter("betResult", BetResult.NOT_CONSIDERED);
        return query.setMaxResults(resultsCount).setFirstResult(startingResult).getResultList();
    }

    @Override
    public List<Bet> getBetsByCompetition(Long competitionID) {
        TypedQuery<Bet> query = entityManager.createNamedQuery("Bet.findByCompetitionID", Bet.class);
        query.setParameter("competitionID", competitionID);
        query.setParameter("betResult", BetResult.NOT_CONSIDERED);
        return query.getResultList();
    }

    @Transactional
    @Override
    public void cancelBetsByCompetition(Long competitionID) {
        Query query = entityManager.createNamedQuery("Bet.cancelBetsByCompetitionID");
        query.setParameter("competitionID", competitionID);
        query.setParameter("betResult", BetResult.NOT_CONSIDERED);
        query.setParameter("betResultToSet", BetResult.MONEY_RETURNED);
        query.executeUpdate();
    }

    @Override
    public long getTotalArchivedBetsMadeByUser(String username) {
        TypedQuery<Long> query = entityManager.createNamedQuery("Bet.findBetsPagesArchivedByUser", Long.class);
        query.setParameter("username", username);
        query.setParameter("betResult", BetResult.NOT_CONSIDERED);
        long pages = 1;
        try {
            pages = query.getSingleResult();
        } catch (Exception e) {
            pages = (long) 1;
        }
        return pages;
    }

    @Override
    public List<Bet> getArchivedBetsPageMadeByUser(String username, int startingResult, int resultsCount) {
        TypedQuery<Bet> query = entityManager.createNamedQuery("Bet.findArchivedPageByUsername", Bet.class);
        query.setParameter("username", username);
        query.setParameter("betResult", BetResult.NOT_CONSIDERED);
        return query.setMaxResults(resultsCount).setFirstResult(startingResult).getResultList();
    }

    @Override
    public long getTotalActiveBetsMadeByUser(String username) {
        TypedQuery<Long> query = entityManager.createNamedQuery("Bet.findNumberOfActiveBetsByUsername", Long.class);
        query.setParameter("betResult", BetResult.NOT_CONSIDERED);
        query.setParameter("username", username);
        long pages;
        try {
            pages = query.getSingleResult();
        } catch (Exception e) {
            pages = (long) 1;
        }
        return pages;
    }

}