Commit 76ca2c64 authored by Xavier Guerrero's avatar Xavier Guerrero
Browse files

ya se ven los similares

parent 9b3a1c40
......@@ -19,6 +19,13 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="bin/test" path="src/test/resources">
<attributes>
<attribute name="gradle_scope" value="test"/>
<attribute name="gradle_used_by_scope" value="test"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin/default"/>
......
package es.mde.componentes;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.FlowLayout;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import es.mde.entidades.Federado;
import es.mde.entidades.Jugador;
import es.mde.entidades.Principiante;
......@@ -27,8 +31,15 @@ public class VistaJugadorModal extends JDialog {
this.jugador = jugador;
setLayout(new BorderLayout());
// Panel principal con BoxLayout para evitar grandes espacios vacíos
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
// Panel de información del jugador
infoPanel = new JPanel();
infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.Y_AXIS));
infoPanel.setBorder(BorderFactory.createEmptyBorder(10, 100, 10, 100)); // Márgenes: arriba, izquierda, abajo, derecha
// Mostrar información del jugador
infoPanel.add(new JLabel("Nombre: " + jugador.getNombre() + " " + jugador.getApellido1() + " " + jugador.getApellido2()));
......@@ -36,70 +47,74 @@ public class VistaJugadorModal extends JDialog {
infoPanel.add(new JLabel("Teléfono: " + jugador.getTelefono()));
infoPanel.add(new JLabel("Correo Electrónico: " + jugador.getEmail()));
infoPanel.add(new JLabel("Campo: " + jugador.getCampo().getNombre()));
if (jugador instanceof Federado) {
if (jugador.getTipo().equals("Federado")) {
infoPanel.add(new JLabel("Handicap: " + ((Federado) jugador).getHandicap()));
if (((Federado) jugador).isProfesional()) {
infoPanel.add(new JLabel("Profesional: Sí"));
}
} else if (jugador instanceof Principiante) {
} else if (jugador.getTipo().equals("Principiante")) {
infoPanel.add(new JLabel("Puntuación Largo: " + ((Principiante) jugador).getPuntuacionLargo()));
infoPanel.add(new JLabel("Puntuación Corto: " + ((Principiante) jugador).getPuntuacionCorto()));
}
add(infoPanel, BorderLayout.CENTER);
mainPanel.add(infoPanel);
// Panel de jugadores similares
similarPlayersPanel = new JPanel();
similarPlayersPanel.setLayout(new BoxLayout(similarPlayersPanel, BoxLayout.Y_AXIS));
add(similarPlayersPanel, BorderLayout.SOUTH);
similarPlayersPanel = new JPanel(new BorderLayout());
// Crear el modelo de la tabla para los jugadores similares
DefaultTableModel tableModel = new DefaultTableModel();
tableModel.addColumn("Nombre");
tableModel.addColumn("Handicap");
// Cargar jugadores similares en el modelo de la tabla
jugadoresSimilares = new JugadorDAO().getSimilares(jugador);
for (Jugador similar : jugadoresSimilares) {
String nombreCompleto = similar.getNombre() + " " + similar.getApellido1() + " " + similar.getApellido2();
String handicap = String.valueOf(similar.getTipo().equals("Federado") ? ((Federado) similar).getHandicap() : ((Principiante) similar).getHandicap());
tableModel.addRow(new Object[]{nombreCompleto, handicap});
}
// Crear la tabla con el modelo de datos
JTable similarPlayersTable = new JTable(tableModel);
// Asegurar que las celdas de la tabla tengan un fondo blanco
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setBackground(Color.WHITE);
similarPlayersTable.setDefaultRenderer(Object.class, renderer);
// Añadir la tabla al panel de jugadores similares
JScrollPane scrollPane = new JScrollPane(similarPlayersTable);
scrollPane.setBorder(BorderFactory.createTitledBorder("Jugadores de Nivel Similar"));
similarPlayersPanel.add(scrollPane, BorderLayout.CENTER);
mainPanel.add(similarPlayersPanel);
add(mainPanel, BorderLayout.CENTER);
JButton closeButton = new JButton("Cerrar");
closeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
add(closeButton, BorderLayout.SOUTH);
closeButton.addActionListener(e -> dispose());
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.add(closeButton);
add(buttonPanel, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(parent);
// Cargar jugadores similares
cargarJugadoresSimilares();
}
private void cargarJugadoresSimilares() {
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
if (jugador instanceof Federado) {
jugadoresSimilares = new JugadorDAO().getFederadosSimilares(getId(jugador.getJugadorUrl()));
} else if (jugador instanceof Principiante) {
jugadoresSimilares = new JugadorDAO().getJugadoresSimilares(getId(jugador.getJugadorUrl()));
}
return null;
}
jugadoresSimilares = new JugadorDAO().getSimilares(jugador);
for (Jugador similar : jugadoresSimilares) {
System.out.println(similar.getNombre() + ", " + similar.getTipo());
@Override
protected void done() {
similarPlayersPanel.removeAll();
for (Jugador similar : jugadoresSimilares) {
JPanel jugadorPanel = new JPanel();
jugadorPanel.setLayout(new BoxLayout(jugadorPanel, BoxLayout.Y_AXIS));
jugadorPanel.add(new JLabel("Nombre: " + similar.getNombre() + " " + similar.getApellido1() + " " + similar.getApellido2()));
jugadorPanel.add(new JLabel("Handicap: " + (similar instanceof Federado ? ((Federado) similar).getHandicap() : ((Principiante) similar).getHandicap())));
similarPlayersPanel.add(jugadorPanel);
}
similarPlayersPanel.revalidate();
similarPlayersPanel.repaint();
}
};
worker.execute();
}
private Long getId(String url) {
return Long.parseLong(url.substring(url.lastIndexOf('/') + 1));
JPanel jugadorPanel = new JPanel();
jugadorPanel.setLayout(new BoxLayout(jugadorPanel, BoxLayout.Y_AXIS));
jugadorPanel.add(new JLabel("Nombre: " + similar.getNombre() + " " + similar.getApellido1() + " " + similar.getApellido2()));
jugadorPanel.add(new JLabel("Handicap: " + (similar.getTipo().equals("Federado") ? ((Federado) similar).getHandicap() : ((Principiante) similar).getHandicap())));
similarPlayersPanel.add(jugadorPanel);
}
similarPlayersPanel.revalidate();
similarPlayersPanel.repaint();
}
}
}
\ No newline at end of file
......@@ -12,18 +12,20 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import es.mde.entidades.Campo;
import es.mde.entidades.Federado;
import es.mde.entidades.Jugador;
import es.mde.entidades.Principiante;
public abstract class GocourtAbstractDAO<T> {
private ObjectMapper mapper;
private HttpClient client;
protected String getApiUrl() {
private String getApiUrl() {
return "https://gocourtapitest.manabo.org/api/";
}
protected ObjectMapper getMapper() {
private ObjectMapper getMapper() {
return mapper;
}
......@@ -169,4 +171,44 @@ public abstract class GocourtAbstractDAO<T> {
throw new IOException(e);
}
}
public List<Jugador> getJugadoresSimilares(Jugador jugador) {
String apiEndPoint = jugador.getTipo().equals("Federado")
? "jugadores/search/jugadoresNivelSimilar"
: "jugadores/search/federadosNivelSimilar";
long jugadorId = Long.parseLong(jugador.getJugadorUrl().substring(jugador.getJugadorUrl().lastIndexOf('/') + 1));
URI uri = URI.create(getApiUrl() + apiEndPoint + "?id=" + jugadorId);
List<Jugador> similares = new ArrayList<>();
try {
HttpRequest request = HttpRequest.newBuilder(uri)
.GET()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
JsonNode nodoFederados = getMapper().readTree(response.body()).findValue("federados");
if (nodoFederados != null && nodoFederados.isArray()) {
for (JsonNode nodo : nodoFederados) {
Jugador similar;
String tipo = nodo.path("tipo").asText();
similar = getMapper().readValue(nodo.traverse(), Federado.class);
similares.add(similar);
}
}
JsonNode nodoPrincipiantes = getMapper().readTree(response.body()).findValue("principiantes");
if (nodoPrincipiantes != null && nodoPrincipiantes.isArray()) {
for (JsonNode nodo : nodoPrincipiantes) {
Jugador similar;
String tipo = nodo.path("tipo").asText();
similar = getMapper().readValue(nodo.traverse(), Principiante.class);
similares.add(similar);
}
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return similares;
}
}
\ No newline at end of file
package es.mde.repositorios;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
import es.mde.entidades.Federado;
import es.mde.entidades.Jugador;
import es.mde.entidades.Principiante;
public class JugadorDAO extends GocourtAbstractDAO<Jugador> {
private static final String API_JUGADORESSIMILARES = "jugadores/similares";
private static final String API_FEDERADOSSIMILARES = "federados/similares";
public List<Jugador> getJugadores() {
List<Jugador> jugadores = new ArrayList<>();
List<Federado> federados = new FederadoDAO().getFederados();
......@@ -28,32 +19,8 @@ public class JugadorDAO extends GocourtAbstractDAO<Jugador> {
return jugadores;
}
public List<Jugador> getFederadosSimilares(Long id) throws IOException, InterruptedException {
return getJugadoresSimilares(API_FEDERADOSSIMILARES, id);
public List<Jugador> getSimilares(Jugador jugador) {
return getJugadoresSimilares(jugador);
}
public List<Jugador> getJugadoresSimilares(Long id) throws IOException, InterruptedException {
return getJugadoresSimilares(API_JUGADORESSIMILARES, id);
}
private List<Jugador> getJugadoresSimilares(String apiEndpoint, Long id) throws IOException, InterruptedException {
List<Jugador> similares = new ArrayList<>();
URI uri = URI.create(getApiUrl() + apiEndpoint + "?id=" + id);
HttpRequest request = HttpRequest.newBuilder(uri)
.GET()
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
JsonNode nodoElementos = getMapper().readTree(response.body()).findValue("jugadoresSimilares");
for (JsonNode nodo : nodoElementos) {
Jugador similar = getMapper().readValue(nodo.traverse(), Jugador.class);
similares.add(similar);
}
}
return similares;
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment