@Test public void AddAndCountTest() { EntityCommon teamStage = new TeamStage("team1", "stage1", 1); EMF.add(teamStage); assertNull(TeamStage.getLastTeamStage("team2")); teamStage = new TeamStage("team2", "stage2", 1); EMF.add(teamStage); assertEquals("stage2", TeamStage.getLastTeamStage("team2").getStageName()); teamStage = new TeamStage("team2", "stage3", 3); EMF.add(teamStage); assertEquals("stage3", TeamStage.getLastTeamStage("team2").getStageName()); teamStage = new TeamStage("team2", "stage3", 3); EMF.add(teamStage); teamStage = new TeamStage("team1", "stage1", 1); EMF.add(teamStage); teamStage = new TeamStage(); assertEquals(3, teamStage.count()); List<? extends EntityCommon> allTeamsAndStages = teamStage.getList(); for (EntityCommon teamStageForPrint : allTeamsAndStages) { System.out.println(teamStageForPrint); } }
/* (non-Javadoc) * @see cz.plsi.webInfo.shared.dataStore.entities.EntityCommon#exists() */ @Override public boolean exists() { EntityManager em = EMF.getInstance().createEntityManager(); Query query = em.createQuery( "SELECT ts FROM " + TeamStage.class.getName() + " ts " + "where ts.teamName=:teamName " + "and ts.stageName=:stageName"); query.setParameter("teamName", this.getTeamName()); query.setParameter("stageName", this.getStageName()); query.setMaxResults(1); return !query.getResultList().isEmpty(); }
public static TeamStage getTeamStage(Key id) { EntityManager em = EMF.getInstance().createEntityManager(); TypedQuery<TeamStage> query = em.createQuery( "SELECT ts FROM " + TeamStage.class.getName() + " ts " + "where ts.id=:id", TeamStage.class); query.setParameter("id", id); query.setMaxResults(1); List<TeamStage> resultList = query.getResultList(); if (resultList.isEmpty()) { return null; } return resultList.get(0); }
@Override public List<TeamStage> getList() { EntityManager em = EMF.getInstance().createEntityManager(); CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); CriteriaQuery<TeamStage> cq = criteriaBuilder.createQuery(TeamStage.class); Root<TeamStage> teamStage = cq.from(TeamStage.class); Predicate criteria = null; if (this.teamName != null) { criteria = criteriaBuilder.equal(teamStage.get("teamName"), this.teamName); } if (this.stageName != null) { criteria = criteriaBuilder.and( criteria, criteriaBuilder.equal(teamStage.get("stageName"), this.stageName)); } if (this.stageBranch != null) { criteria = criteriaBuilder.and( criteria, criteriaBuilder.equal(teamStage.get("stageBranch"), this.stageBranch)); } if (this.stageOrder == TEAM_ENDED_GAME) { Predicate equalEnded = criteriaBuilder.equal(teamStage.get("stageOrder"), this.stageOrder); criteria = criteria != null ? criteriaBuilder.and(criteria, equalEnded) : equalEnded; } else { Predicate notEqualOrder = criteriaBuilder.notEqual(teamStage.get("stageOrder"), TEAM_ENDED_GAME); criteria = criteria != null ? criteriaBuilder.and(criteria, notEqualOrder) : notEqualOrder; } if (criteria != null) { cq.where(criteria); } cq.select(teamStage); cq.orderBy( criteriaBuilder.desc(teamStage.get("stageOrder")), criteriaBuilder.asc(teamStage.get("stageDate"))); em.getTransaction().begin(); List<TeamStage> resultList = em.createQuery(cq).getResultList(); em.getTransaction().commit(); return resultList; }
/* (non-Javadoc) * @see cz.plsi.webInfo.shared.dataStore.entities.EntityCommon#count() */ @Override public long count() { EntityManager em = EMF.getInstance().createEntityManager(); StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append("SELECT COUNT(ts) FROM "); queryBuilder.append(TeamStage.class.getName()); queryBuilder.append(" ts"); if (this.teamName != null) { queryBuilder.append(" where ts.teamName=:teamName"); } TypedQuery<Long> query = em.createQuery(queryBuilder.toString(), Long.class); if (this.teamName != null) { query.setParameter("teamName", this.teamName); } return query.getSingleResult(); }
// TODO refaktor z team name na team code public static TeamStage getLastTeamStage(String team) { EntityManager em = EMF.getInstance().createEntityManager(); TypedQuery<TeamStage> query = em.createQuery( "SELECT ts FROM " + TeamStage.class.getName() + " ts " + "where ts.teamName=:teamName " + "ORDER BY ts.stageDate DESC", TeamStage.class); query.setParameter("teamName", team); query.setMaxResults(1); List<TeamStage> resultList = query.getResultList(); if (resultList.isEmpty()) { return null; } return resultList.get(0); }