private List<Game> findNoDebugTeamGames() { EntityManager em = this.getEntityManager(); final CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); final CriteriaQuery<Game> query = criteriaBuilder.createQuery(Game.class); Root<Game> e = query.from(Game.class); query.select(e); List<Game> games = em.createQuery(query).getResultList(); List<Game> noDebugTeamGames = new ArrayList<>(); for (Game g : games) { if (!(g instanceof DebugGame)) { List<Team> teams = g.getTeams(); boolean hasDebugTeam = false; for (Team t : teams) { if (t instanceof DebugTeam) { hasDebugTeam = true; break; } } if (!hasDebugTeam) { noDebugTeamGames.add(g); } } } return noDebugTeamGames; }
@GET @Path("RestoreDebugTeams") public String restoreDebugTeams() { List<Game> findNoDebugTeamGames = this.findNoDebugTeamGames(); int counter = 0; for (Game g : findNoDebugTeamGames) { logger.error("Restore Game: " + g.getName() + "/" + g.getId()); DebugTeam dt = new DebugTeam(); g.addTeam(dt); this.getEntityManager().persist(dt); g.getGameModel().propagateDefaultInstance(dt, true); this.getEntityManager().flush(); if (++counter == 25) { break; } } Long remaining = this.countOrphans(); return "OK" + (remaining > 0 ? "(still " + remaining + ")" : ""); }
private String deleteDuplicata() { StringBuilder sb = new StringBuilder(); String sql = "SELECT vi.gameScope, vi.game FROM VariableInstance vi WHERE vi.gameScope IS NOT NULL GROUP BY vi.gameScope.id, vi.game.id HAVING count(vi) > 1"; Query createQuery = this.getEntityManager().createQuery(sql); List resultList = createQuery.getResultList(); int i = 0; for (Object o : resultList) { Object[] array = (Object[]) o; GameScope scope = (GameScope) array[0]; Game game = (Game) array[1]; // VariableInstance variableInstance = scope.getVariableInstance(game); // System.out.println("DELETE: " + variableInstance); sb.append("DELETE: "); sb.append(i++); sb.append(". "); String sql2 = "SELECT vi from VariableInstance vi WHERE vi.gameScope.id = :scopeId and vi.game.id = :gameId"; TypedQuery<VariableInstance> query2 = this.getEntityManager().createQuery(sql2, VariableInstance.class); query2.setHint(QueryHints.CACHE_USAGE, CacheUsage.DoNotCheckCache); // @QueryHint(name = QueryHints.CACHE_USAGE, value = CacheUsage.CheckCacheThenDatabase) query2.setParameter("scopeId", scope.getId()); query2.setParameter("gameId", game.getId()); List<VariableInstance> list = query2.getResultList(); sb.append(list.get(0)); sb.append(" SCOPE - TEAM " + scope.getId() + " " + game.getId()); sb.append(("<br />")); if (list.size() != 2) { sb.append(" -> NOT 2 but " + list.size()); } else { VariableInstance get = list.get(0); VariableInstance get2 = list.get(1); if (get instanceof BooleanInstance) { if (((BooleanInstance) get).getValue() != ((BooleanInstance) get2).getValue()) { sb.append((" -> NOT EQUALS")); } else { this.getEntityManager().remove(get2); } } else if (get instanceof NumberInstance) { if (((NumberInstance) get).getValue() != ((NumberInstance) get2).getValue()) { sb.append((" -> NOT EQUALS")); } else { this.getEntityManager().remove(get2); } } else if (get instanceof StringInstance) { if (!((StringInstance) get).getValue().equals(((StringInstance) get2).getValue())) { sb.append((" -> NOT EQUALS")); } else { this.getEntityManager().remove(get2); } } } sb.append(("<br />")); } return sb.toString(); }