| @ -0,0 +1,51 @@ | |||||
| <?xml version="1.0" encoding="UTF-8"?> | |||||
| <classpath> | |||||
| <classpathentry kind="src" output="target/classes" path="src/main/java"> | |||||
| <attributes> | |||||
| <attribute name="optional" value="true"/> | |||||
| <attribute name="maven.pomderived" value="true"/> | |||||
| </attributes> | |||||
| </classpathentry> | |||||
| <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"> | |||||
| <attributes> | |||||
| <attribute name="maven.pomderived" value="true"/> | |||||
| </attributes> | |||||
| </classpathentry> | |||||
| <classpathentry kind="src" output="target/test-classes" path="src/test/java"> | |||||
| <attributes> | |||||
| <attribute name="optional" value="true"/> | |||||
| <attribute name="maven.pomderived" value="true"/> | |||||
| <attribute name="test" value="true"/> | |||||
| </attributes> | |||||
| </classpathentry> | |||||
| <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"> | |||||
| <attributes> | |||||
| <attribute name="maven.pomderived" value="true"/> | |||||
| <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-1.8"/> | |||||
| <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> | |||||
| <attributes> | |||||
| <attribute name="maven.pomderived" value="true"/> | |||||
| </attributes> | |||||
| </classpathentry> | |||||
| <classpathentry kind="src" path="target/generated-sources/annotations"> | |||||
| <attributes> | |||||
| <attribute name="optional" value="true"/> | |||||
| <attribute name="maven.pomderived" value="true"/> | |||||
| <attribute name="ignore_optional_problems" value="true"/> | |||||
| <attribute name="m2e-apt" value="true"/> | |||||
| </attributes> | |||||
| </classpathentry> | |||||
| <classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations"> | |||||
| <attributes> | |||||
| <attribute name="optional" value="true"/> | |||||
| <attribute name="maven.pomderived" value="true"/> | |||||
| <attribute name="ignore_optional_problems" value="true"/> | |||||
| <attribute name="m2e-apt" value="true"/> | |||||
| <attribute name="test" value="true"/> | |||||
| </attributes> | |||||
| </classpathentry> | |||||
| <classpathentry kind="output" path="target/classes"/> | |||||
| </classpath> | |||||
| @ -0,0 +1,23 @@ | |||||
| <?xml version="1.0" encoding="UTF-8"?> | |||||
| <projectDescription> | |||||
| <name>PracticaSpringCore</name> | |||||
| <comment></comment> | |||||
| <projects> | |||||
| </projects> | |||||
| <buildSpec> | |||||
| <buildCommand> | |||||
| <name>org.eclipse.jdt.core.javabuilder</name> | |||||
| <arguments> | |||||
| </arguments> | |||||
| </buildCommand> | |||||
| <buildCommand> | |||||
| <name>org.eclipse.m2e.core.maven2Builder</name> | |||||
| <arguments> | |||||
| </arguments> | |||||
| </buildCommand> | |||||
| </buildSpec> | |||||
| <natures> | |||||
| <nature>org.eclipse.jdt.core.javanature</nature> | |||||
| <nature>org.eclipse.m2e.core.maven2Nature</nature> | |||||
| </natures> | |||||
| </projectDescription> | |||||
| @ -0,0 +1,2 @@ | |||||
| eclipse.preferences.version=1 | |||||
| org.eclipse.jdt.apt.aptEnabled=false | |||||
| @ -0,0 +1,16 @@ | |||||
| eclipse.preferences.version=1 | |||||
| org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | |||||
| org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 | |||||
| org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | |||||
| org.eclipse.jdt.core.compiler.compliance=1.8 | |||||
| org.eclipse.jdt.core.compiler.debug.lineNumber=generate | |||||
| org.eclipse.jdt.core.compiler.debug.localVariable=generate | |||||
| org.eclipse.jdt.core.compiler.debug.sourceFile=generate | |||||
| org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | |||||
| org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled | |||||
| org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | |||||
| org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning | |||||
| org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore | |||||
| org.eclipse.jdt.core.compiler.processAnnotations=disabled | |||||
| org.eclipse.jdt.core.compiler.release=disabled | |||||
| org.eclipse.jdt.core.compiler.source=1.8 | |||||
| @ -0,0 +1,4 @@ | |||||
| activeProfiles= | |||||
| eclipse.preferences.version=1 | |||||
| resolveWorkspaceProjects=true | |||||
| version=1 | |||||
| @ -0,0 +1,78 @@ | |||||
| # TRABAJO SPRING CORE - DANI MINGUET | |||||
| He realizado el trabajo sobre videojuegos, aquí muestro el diagrama de clases empleado (Atributos sin "Bold", métodos en "Bold"): | |||||
|  | |||||
| Aquí muestro el fichero csv que utilizo: | |||||
| ``` | |||||
| ID,NOMBRE,DESARROLLADOR,GENERO,FECHA SALIDA,PLATAFORMAS DISPONIBLES | |||||
| 1,God of War: Ragnarok,Santa Monica Studio,Accion-Aventura-Lucha-Rol,09/11/2022,PS5 y PS4 | |||||
| 2,FIFA 23,Electronic Arts,Deportes-Simulacion,27/09/2022,PS5-Nintendo Switch-XBOX y PC | |||||
| 3,Minecraft,Mojang Studios,Supervivencia-Sandbox,17/05/2009,PS-XBOX-Nintendo, Moviles y PC | |||||
| 4,Grand Theft Auto V,Rockstar Games,Mundo abierto,17/09/2013,PS-XBOX y PC | |||||
| 5,CounterStrike: Global Offensive,Valve Corporation,De disparos tactico,21/08/2012,XBOX y PC | |||||
| 6,Elden Ring,FromSoftware,Aventura-Mundo abierto,25/02/2022,PS-XBOX y PC | |||||
| 7,Uncharted 4,Naughty Dog,Accion-Aventura,10/05/2016,PS y PC | |||||
| 8,Ghost of Tsushima,Sucker Punch Productions,Rol-Sigilo-Accion-Aventura-Lucha,17/07/2020,PS5 y PS4 | |||||
| 9,The Witcher 3,CD Projekt,Mundo abierto,19/05/2015,PS-NINTENDO-XBOX y PC | |||||
| 10,Assassins Creed: Origins,Ubisoft,Mundo abierto-Accion-Aventura-Sigilo,27/10/2017,PS-XBOX y PC | |||||
| ``` | |||||
| Al iniciar el programa, si no hay parámetros definidos te debería mostrar una alerta de que no se han indicado, y mostrará el contenido del fichero de ayuda, para saber que parámetros puedes utilizar: | |||||
| ``` | |||||
| No se ha pasado ningún parámetro | |||||
| Sintaxis: java -jar videogameadvisor.jar [OPCIONES] | |||||
| -lg | |||||
| Lista los diferentes géneros de videojuegos de la colección. | |||||
| No se pueden utilizar más parámetros. | |||||
| -ag genero1,genero2,genero... | |||||
| Lista aquellos videojuegos que pertenecen a al menos uno de los géneros | |||||
| que se indican. El listado de géneros no puede incluir espacios. | |||||
| -tg genero1,genero2,genero... | |||||
| Lista aquellos videojuegos que pertenecen a todos y cada uno de los | |||||
| géneros que se indican. El listado de géneros no puede incluir espacios. | |||||
| -y año | |||||
| Lista aquellos videojuegos que se estrenaron en el año indicado. | |||||
| El año debe expresarse con 4 cifras. | |||||
| -b desde,hasta | |||||
| Lista aquellos videojuegos que se estrenaron entre los años DESDE y HASTA. | |||||
| Los años indicados están incluidos. Deben expresarse con 4 cifras. | |||||
| -t titulo | |||||
| Lista aquellos videojuegos cuyo título contiene la cadena proporcionada. | |||||
| Se ignorará el uso de mayúsculas/minúsculas en la comparación. | |||||
| -h | |||||
| Muestra este mensaje de ayuda. | |||||
| EJEMPLOS DE USO | |||||
| java -jar videogameadvisor.jar -y 2018 -ag Accion,Rol | |||||
| Muestra los videojuegos que han salido en el año 2018 y son de acción y/o de comedia. | |||||
| java -jar videogameadvisor.jar -t war | |||||
| Muestra los videojuegos cuyo título contiene la palabra war. | |||||
| java -jar videogameadvisor.jar -td Accion,Aventura -b 2000,2010 -t auto | |||||
| Muestra los videojuegos cuyo título contiene la palabra auto, pertenecen al | |||||
| género de acción y aventura y fueron estrenados entre 2000 y 2010. | |||||
| java -jar videogameadvisor.jar -lg | |||||
| Muestra un listado con todos los géneros. | |||||
| LICENCIAS | |||||
| Los datos utilizados sobre videojuegos utilizados en este ejemplo son | |||||
| reales y creados por mi mismo. Ten en cuenta | |||||
| que solo puede ser usada con fines personales y no comerciales. | |||||
| ``` | |||||
| @ -0,0 +1,22 @@ | |||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | |||||
| <modelVersion>4.0.0</modelVersion> | |||||
| <groupId>com.daniminguet.springcore</groupId> | |||||
| <artifactId>PracticaSpringCore</artifactId> | |||||
| <version>0.0.1-SNAPSHOT</version> | |||||
| <name>PracticaSpringCore</name> | |||||
| <description>Practica Acceso a Datos con Spring Core</description> | |||||
| <dependencies> | |||||
| <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> | |||||
| <dependency> | |||||
| <groupId>org.springframework</groupId> | |||||
| <artifactId>spring-context</artifactId> | |||||
| <version>6.0.0</version> | |||||
| </dependency> | |||||
| <!--https://www.baeldung.com/spring-postconstruct-predestroy --> | |||||
| <dependency> | |||||
| <groupId>javax.annotation</groupId> | |||||
| <artifactId>javax.annotation-api</artifactId> | |||||
| <version>1.3.2</version> | |||||
| </dependency> | |||||
| </dependencies> | |||||
| </project> | |||||
| @ -0,0 +1,19 @@ | |||||
| package com.daniminguet.videogameadvisor; | |||||
| import org.springframework.context.ApplicationContext; | |||||
| import org.springframework.context.annotation.AnnotationConfigApplicationContext; | |||||
| import com.daniminguet.videogameadvisor.config.AppConfig; | |||||
| public class VideogameAdvisorApp { | |||||
| public static void main(String[] args) { | |||||
| ApplicationContext appContext = new AnnotationConfigApplicationContext(AppConfig.class); | |||||
| VideogameAdvisorRunApp runApp = appContext.getBean(VideogameAdvisorRunApp.class); | |||||
| runApp.run(args); | |||||
| ((AnnotationConfigApplicationContext) appContext).close(); | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,33 @@ | |||||
| package com.daniminguet.videogameadvisor; | |||||
| import java.io.IOException; | |||||
| import java.nio.file.Files; | |||||
| import java.nio.file.Paths; | |||||
| import java.util.stream.Collectors; | |||||
| import javax.annotation.PostConstruct; | |||||
| import org.springframework.stereotype.Component; | |||||
| import org.springframework.util.ResourceUtils; | |||||
| @Component | |||||
| public class VideogameAdvisorHelp { | |||||
| private String help; | |||||
| @PostConstruct | |||||
| public void init() { | |||||
| try { | |||||
| //@formatter:off | |||||
| help = Files | |||||
| .lines(Paths.get(ResourceUtils.getFile("classpath:ayuda.txt").toURI())) | |||||
| .collect(Collectors.joining("\n")); | |||||
| //@formatter:on | |||||
| } catch (IOException ioe) { | |||||
| ioe.printStackTrace(); | |||||
| } | |||||
| } | |||||
| public String getHelp() { | |||||
| return help; | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,96 @@ | |||||
| package com.daniminguet.videogameadvisor; | |||||
| import java.util.ArrayList; | |||||
| import java.util.Collection; | |||||
| import java.util.List; | |||||
| import java.util.stream.Collectors; | |||||
| import org.springframework.beans.factory.annotation.Autowired; | |||||
| import org.springframework.stereotype.Component; | |||||
| import com.daniminguet.videogameadvisor.model.Videogame; | |||||
| import com.daniminguet.videogameadvisor.service.VideogameQueryService; | |||||
| import com.daniminguet.videogameadvisor.service.VideogameService; | |||||
| @Component | |||||
| public class VideogameAdvisorRunApp { | |||||
| @Autowired | |||||
| private VideogameService videogameService; | |||||
| @Autowired | |||||
| private VideogameQueryService videogameQueryService; | |||||
| @Autowired | |||||
| private VideogameAdvisorHelp help; | |||||
| public void run(String[] args) { | |||||
| if(args.length < -1) { | |||||
| System.out.println("Error de sintaxis"); | |||||
| System.out.println(help.getHelp()); | |||||
| } else if (args.length == 1){ | |||||
| switch(args[0].toLowerCase()) { | |||||
| case "-lg": | |||||
| videogameService.findAllGenres().forEach(System.out::println); | |||||
| break; | |||||
| case "-h": | |||||
| System.out.println(help.getHelp()); | |||||
| break; | |||||
| default: | |||||
| System.out.println("Error de sintaxis"); | |||||
| System.out.println(help.getHelp()); | |||||
| } | |||||
| } else if (args.length % 2 != 0) { | |||||
| System.out.println("Error de sintaxis"); | |||||
| System.out.println(help.getHelp()); | |||||
| } else if (args.length > 8) { | |||||
| System.out.println("Error de sintaxis"); | |||||
| System.out.println(help.getHelp()); | |||||
| } else if (args.length == 0) { | |||||
| System.out.println("No se ha pasado ningún parámetro"); | |||||
| System.out.println(help.getHelp()); | |||||
| } else { | |||||
| List<String[]> argumentos = new ArrayList<>(); | |||||
| for(int i = 0; i < args.length; i += 2) { | |||||
| argumentos.add(new String[] {args[i], args[i + 1]}); | |||||
| } | |||||
| boolean error = false; | |||||
| for(String[] argumento : argumentos) { | |||||
| switch (argumento[0].toLowerCase()) { | |||||
| case "-ag": | |||||
| videogameQueryService.anyGenre(argumento[1].split(",")); | |||||
| break; | |||||
| case "-tg": | |||||
| videogameQueryService.allGenres(argumento[1].split(",")); | |||||
| break; | |||||
| case "-y": | |||||
| videogameQueryService.year(argumento[1]); | |||||
| break; | |||||
| case "-b": | |||||
| String[] years = argumento[1].split(","); | |||||
| videogameQueryService.betweenYears(years[0], years[1]); | |||||
| break; | |||||
| case "-t": | |||||
| videogameQueryService.titleContains(argumento[1]); | |||||
| break; | |||||
| default: error = true; | |||||
| System.out.println("Error de sintaxis"); | |||||
| System.out.println(help.getHelp()); | |||||
| } | |||||
| } | |||||
| if (!error) { | |||||
| Collection<Videogame> result = videogameQueryService.exec(); | |||||
| System.out.printf("%s\t%-50s\t%s\t%s\t%s\t%s\n","ID","Nombre", "Desarrollador", "Géneros", "Fecha de salida", "Plataformas disonibles"); | |||||
| if (result != null) { | |||||
| result.forEach(vg -> System.out.printf("%s\t%-50s\t%s\t%s\t%s\t%s\n", | |||||
| vg.getId(), vg.getNombre(), vg.getDesarrollador(), | |||||
| vg.getGeneros().stream().collect(Collectors.joining(", ")), | |||||
| vg.getFechaSalida(), vg.getPlataformas())); | |||||
| } else { | |||||
| System.out.println("No hay videojuegos que cumplan esos criterios. Lo sentimos"); | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,32 @@ | |||||
| package com.daniminguet.videogameadvisor.config; | |||||
| import org.springframework.beans.factory.annotation.Value; | |||||
| import org.springframework.context.annotation.ComponentScan; | |||||
| import org.springframework.context.annotation.Configuration; | |||||
| import org.springframework.context.annotation.PropertySource; | |||||
| @Configuration | |||||
| @ComponentScan(basePackages="com.daniminguet.videogameadvisor") | |||||
| @PropertySource("classpath:/videogameadvisor.properties") | |||||
| public class AppConfig { | |||||
| @Value("${file.path}") | |||||
| public String file; | |||||
| @Value("${file.csv.separator}") | |||||
| public String separator; | |||||
| @Value("${file.csv.list_separator}") | |||||
| public String listSeparator; | |||||
| public String getFile() { | |||||
| return file; | |||||
| } | |||||
| public String getSeparator() { | |||||
| return separator; | |||||
| } | |||||
| public String getListSeparator() { | |||||
| return listSeparator; | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,38 @@ | |||||
| package com.daniminguet.videogameadvisor.dao; | |||||
| import java.nio.file.Files; | |||||
| import java.nio.file.Paths; | |||||
| import java.util.ArrayList; | |||||
| import java.util.Arrays; | |||||
| import java.util.List; | |||||
| import java.util.stream.Collectors; | |||||
| import org.springframework.util.ResourceUtils; | |||||
| import com.daniminguet.videogameadvisor.model.Videogame; | |||||
| public class UtilVideogameFileReader { | |||||
| public static List<Videogame> readFile(final String path, final String separator, final String listSeparator) { | |||||
| List<Videogame> result = new ArrayList<Videogame>(); | |||||
| try { | |||||
| // @formatter:off | |||||
| result = Files | |||||
| .lines(Paths.get(ResourceUtils.getFile(path).toURI())) | |||||
| .skip(1) | |||||
| .map(line -> { | |||||
| String[] values = line.split(separator); | |||||
| return new Videogame(Long.parseLong(values[0]), values[1], values[2], | |||||
| Arrays.asList(values[3].split(listSeparator)), values[4], values[5]); | |||||
| }).collect(Collectors.toList()); | |||||
| // @formatter:on | |||||
| } catch (Exception e) { | |||||
| System.err.println("Error de lectura del fichero de datos: imdb_data"); | |||||
| System.exit(-1); | |||||
| } | |||||
| return result; | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,13 @@ | |||||
| package com.daniminguet.videogameadvisor.dao; | |||||
| import java.util.Collection; | |||||
| import com.daniminguet.videogameadvisor.model.Videogame; | |||||
| public interface VideogameDao { | |||||
| public Videogame findById(long id); | |||||
| public Collection<Videogame> findAll(); | |||||
| public void insert(Videogame videogame); | |||||
| public void edit(Videogame videogame); | |||||
| public void delete(long id); | |||||
| } | |||||
| @ -0,0 +1,71 @@ | |||||
| package com.daniminguet.videogameadvisor.dao; | |||||
| import java.util.ArrayList; | |||||
| import java.util.Collection; | |||||
| import java.util.List; | |||||
| import java.util.Optional; | |||||
| import javax.annotation.PostConstruct; | |||||
| import org.springframework.beans.factory.annotation.Autowired; | |||||
| import org.springframework.stereotype.Repository; | |||||
| import com.daniminguet.videogameadvisor.config.AppConfig; | |||||
| import com.daniminguet.videogameadvisor.model.Videogame; | |||||
| @Repository | |||||
| public class VideogameDaoImplMemory implements VideogameDao { | |||||
| public List<Videogame> videogames = new ArrayList<Videogame>(); | |||||
| @Autowired | |||||
| private AppConfig appConfig; | |||||
| @PostConstruct | |||||
| public void init() { | |||||
| videogames = UtilVideogameFileReader.readFile(appConfig.getFile(), appConfig.getSeparator(), appConfig.getListSeparator()); | |||||
| } | |||||
| public Videogame findById(long id) { | |||||
| Optional<Videogame> result = videogames.stream().filter(vg -> vg.getId() == id).findFirst(); | |||||
| return result.orElse(null); | |||||
| } | |||||
| public Collection<Videogame> findAll() { | |||||
| return videogames; | |||||
| } | |||||
| public void insert(Videogame videogame) { | |||||
| videogames.add(videogame); | |||||
| } | |||||
| public void edit(Videogame videogame) { | |||||
| int i = getIndexOf(videogame.getId()); | |||||
| if(i != -1) { | |||||
| videogames.set(i, videogame); | |||||
| } | |||||
| } | |||||
| public void delete(long id) { | |||||
| int i = getIndexOf(id); | |||||
| if(i != -1) { | |||||
| videogames.remove(i); | |||||
| } | |||||
| } | |||||
| private int getIndexOf(long id) { | |||||
| boolean found = false; | |||||
| int i = 0; | |||||
| while(!found && i < videogames.size()) { | |||||
| if(videogames.get(i).getId() == id) { | |||||
| found = true; | |||||
| } else { | |||||
| i++; | |||||
| } | |||||
| } | |||||
| return (found) ? i : -1; | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,80 @@ | |||||
| package com.daniminguet.videogameadvisor.model; | |||||
| import java.util.List; | |||||
| public class Videogame { | |||||
| private long id; | |||||
| private String nombre; | |||||
| private String desarrollador; | |||||
| private List<String> generos; | |||||
| private String fechaSalida; | |||||
| private String plataformas; | |||||
| public Videogame() {} | |||||
| public Videogame(long id, String nombre, String desarrollador, List<String> generos, String fechaSalida, | |||||
| String plataformas) { | |||||
| this.id = id; | |||||
| this.nombre = nombre; | |||||
| this.desarrollador = desarrollador; | |||||
| this.generos = generos; | |||||
| this.fechaSalida = fechaSalida; | |||||
| this.plataformas = plataformas; | |||||
| } | |||||
| public long getId() { | |||||
| return id; | |||||
| } | |||||
| public void setId(long id) { | |||||
| this.id = id; | |||||
| } | |||||
| public String getNombre() { | |||||
| return nombre; | |||||
| } | |||||
| public void setNombre(String nombre) { | |||||
| this.nombre = nombre; | |||||
| } | |||||
| public String getDesarrollador() { | |||||
| return desarrollador; | |||||
| } | |||||
| public void setDesarrollador(String desarrollador) { | |||||
| this.desarrollador = desarrollador; | |||||
| } | |||||
| public List<String> getGeneros() { | |||||
| return generos; | |||||
| } | |||||
| public void setGeneros(List<String> generos) { | |||||
| this.generos = generos; | |||||
| } | |||||
| public String getFechaSalida() { | |||||
| return fechaSalida; | |||||
| } | |||||
| public void setFechaSalida(String fechaSalida) { | |||||
| this.fechaSalida = fechaSalida; | |||||
| } | |||||
| public String getPlataformas() { | |||||
| return plataformas; | |||||
| } | |||||
| public void setPlataformas(String plataformas) { | |||||
| this.plataformas = plataformas; | |||||
| } | |||||
| @Override | |||||
| public String toString() { | |||||
| return "Videogame [id=" + id + ", nombre=" + nombre + ", desarrollador=" + desarrollador + ", generos=" | |||||
| + generos.toString() + ", fechaSalida=" + fechaSalida + ", plataformas=" + plataformas + "]"; | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,14 @@ | |||||
| package com.daniminguet.videogameadvisor.service; | |||||
| import java.util.Collection; | |||||
| import com.daniminguet.videogameadvisor.model.Videogame; | |||||
| public interface VideogameQueryService { | |||||
| public Collection<Videogame> exec(); | |||||
| public VideogameQueryService anyGenre(String... genres); | |||||
| public VideogameQueryService allGenres(String... genres); | |||||
| public VideogameQueryService year(String year); | |||||
| public VideogameQueryService betweenYears(String from, String to); | |||||
| public VideogameQueryService titleContains(String title); | |||||
| } | |||||
| @ -0,0 +1,81 @@ | |||||
| package com.daniminguet.videogameadvisor.service; | |||||
| import java.time.LocalDate; | |||||
| import java.util.Arrays; | |||||
| import java.util.Collection; | |||||
| import java.util.function.Predicate; | |||||
| import java.util.stream.Collectors; | |||||
| import javax.annotation.PostConstruct; | |||||
| import org.springframework.beans.factory.annotation.Autowired; | |||||
| import org.springframework.stereotype.Service; | |||||
| import com.daniminguet.videogameadvisor.dao.VideogameDao; | |||||
| import com.daniminguet.videogameadvisor.model.Videogame; | |||||
| @Service | |||||
| public class VideogameQueryServiceImpl implements VideogameQueryService { | |||||
| @Autowired | |||||
| private VideogameDao dao; | |||||
| private Predicate<Videogame> predicate; | |||||
| @PostConstruct | |||||
| public void init() { | |||||
| predicate = null; | |||||
| } | |||||
| @Override | |||||
| public Collection<Videogame> exec() { | |||||
| //@formatter:off | |||||
| return dao.findAll() | |||||
| .stream() | |||||
| .filter(predicate) | |||||
| .collect(Collectors.toList()); | |||||
| //@formatter:on | |||||
| } | |||||
| @Override | |||||
| public VideogameQueryService anyGenre(String... genres) { | |||||
| Predicate<Videogame> pAnyGenre = (vg -> Arrays.stream(genres).anyMatch(vg.getGeneros()::contains)); | |||||
| predicate = (predicate == null) ? pAnyGenre : predicate.and(pAnyGenre); | |||||
| return this; | |||||
| } | |||||
| @Override | |||||
| public VideogameQueryService allGenres(String... genres) { | |||||
| Predicate<Videogame> pAllGenre = (vg -> Arrays.stream(genres).allMatch(vg.getGeneros()::contains)); | |||||
| predicate = (predicate == null) ? pAllGenre : predicate.and(pAllGenre); | |||||
| return this; | |||||
| } | |||||
| @Override | |||||
| public VideogameQueryService year(String year) { | |||||
| Predicate<Videogame> pYear = (vg -> vg.getFechaSalida().substring(vg.getFechaSalida().length()-4, vg.getFechaSalida().length()-1).equalsIgnoreCase(year)); | |||||
| predicate = (predicate == null) ? pYear : predicate.and(pYear); | |||||
| return this; | |||||
| } | |||||
| @Override | |||||
| public VideogameQueryService betweenYears(String from, String to) { | |||||
| Predicate<Videogame> pBetweenYears = (vg -> { | |||||
| LocalDate fromYear = LocalDate.of(Integer.parseInt(from), 1, 1); | |||||
| LocalDate toYear = LocalDate.of(Integer.parseInt(to), 1, 3); | |||||
| LocalDate videogameYear = LocalDate.of(Integer.parseInt(vg.getFechaSalida().substring(vg.getFechaSalida().length()-4, vg.getFechaSalida().length()-1)), 1, 2); | |||||
| return videogameYear.isAfter(fromYear) && videogameYear.isBefore(toYear); | |||||
| }); | |||||
| predicate = (predicate == null) ? pBetweenYears : predicate.and(pBetweenYears); | |||||
| return this; | |||||
| } | |||||
| @Override | |||||
| public VideogameQueryService titleContains(String title) { | |||||
| Predicate<Videogame> pTitleContains = (vg -> vg.getNombre().toLowerCase().contains(title.toLowerCase())); | |||||
| predicate = (predicate == null) ? pTitleContains : predicate.and(pTitleContains); | |||||
| return this; | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,61 @@ | |||||
| package com.daniminguet.videogameadvisor.service; | |||||
| import java.util.Collection; | |||||
| import java.util.List; | |||||
| import java.util.stream.Collectors; | |||||
| import org.springframework.beans.factory.annotation.Autowired; | |||||
| import org.springframework.stereotype.Service; | |||||
| import com.daniminguet.videogameadvisor.dao.VideogameDao; | |||||
| import com.daniminguet.videogameadvisor.model.Videogame; | |||||
| @Service | |||||
| public class VideogameService { | |||||
| @Autowired | |||||
| VideogameDao videogameDao; | |||||
| @Autowired | |||||
| VideogameQueryService queryService; | |||||
| public Collection<String> findAllGenres() { | |||||
| List<String> result = null; | |||||
| // @formatter:off | |||||
| result = videogameDao.findAll() | |||||
| .stream() | |||||
| .map(vg -> vg.getGeneros()) | |||||
| .flatMap(list -> list.stream()) | |||||
| .distinct() | |||||
| .sorted() | |||||
| .collect(Collectors.toList()); | |||||
| // @formatter:on | |||||
| return result; | |||||
| } | |||||
| public Collection<Videogame> findByAnyGenre(String... genres) { | |||||
| return queryService.anyGenre(genres).exec(); | |||||
| } | |||||
| public Collection<Videogame> findByAllGenres(String... genres) { | |||||
| return queryService.allGenres(genres).exec(); | |||||
| } | |||||
| public Collection<Videogame> findByYear(String year) { | |||||
| return queryService.year(year).exec(); | |||||
| } | |||||
| public Collection<Videogame> findBetweenYears(String from, String to) { | |||||
| return queryService.betweenYears(from, to).exec(); | |||||
| } | |||||
| public Collection<Videogame> findByTitleContains(String title) { | |||||
| return queryService.titleContains(title).exec(); | |||||
| } | |||||
| public Collection<Videogame> findAll() { | |||||
| return videogameDao.findAll(); | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,11 @@ | |||||
| ID,NOMBRE,DESARROLLADOR,GENERO,FECHA SALIDA,PLATAFORMAS DISPONIBLES | |||||
| 1,God of War: Ragnarok,Santa Monica Studio,Accion-Aventura-Lucha-Rol,09/11/2022,PS5 y PS4 | |||||
| 2,FIFA 23,Electronic Arts,Deportes-Simulacion,27/09/2022,PS5-Nintendo Switch-XBOX y PC | |||||
| 3,Minecraft,Mojang Studios,Supervivencia-Sandbox,17/05/2009,PS-XBOX-Nintendo, Moviles y PC | |||||
| 4,Grand Theft Auto V,Rockstar Games,Mundo abierto,17/09/2013,PS-XBOX y PC | |||||
| 5,CounterStrike: Global Offensive,Valve Corporation,De disparos tactico,21/08/2012,XBOX y PC | |||||
| 6,Elden Ring,FromSoftware,Aventura-Mundo abierto,25/02/2022,PS-XBOX y PC | |||||
| 7,Uncharted 4,Naughty Dog,Accion-Aventura,10/05/2016,PS y PC | |||||
| 8,Ghost of Tsushima,Sucker Punch Productions,Rol-Sigilo-Accion-Aventura-Lucha,17/07/2020,PS5 y PS4 | |||||
| 9,The Witcher 3,CD Projekt,Mundo abierto,19/05/2015,PS-NINTENDO-XBOX y PC | |||||
| 10,Assassins Creed: Origins,Ubisoft,Mundo abierto-Accion-Aventura-Sigilo,27/10/2017,PS-XBOX y PC | |||||
| @ -0,0 +1,50 @@ | |||||
| Sintaxis: java -jar videogameadvisor.jar [OPCIONES] | |||||
| -lg | |||||
| Lista los diferentes géneros de videojuegos de la colección. | |||||
| No se pueden utilizar más parámetros. | |||||
| -ag genero1,genero2,genero... | |||||
| Lista aquellos videojuegos que pertenecen a al menos uno de los géneros | |||||
| que se indican. El listado de géneros no puede incluir espacios. | |||||
| -tg genero1,genero2,genero... | |||||
| Lista aquellos videojuegos que pertenecen a todos y cada uno de los | |||||
| géneros que se indican. El listado de géneros no puede incluir espacios. | |||||
| -y año | |||||
| Lista aquellos videojuegos que se estrenaron en el año indicado. | |||||
| El año debe expresarse con 4 cifras. | |||||
| -b desde,hasta | |||||
| Lista aquellos videojuegos que se estrenaron entre los años DESDE y HASTA. | |||||
| Los años indicados están incluidos. Deben expresarse con 4 cifras. | |||||
| -t titulo | |||||
| Lista aquellos videojuegos cuyo título contiene la cadena proporcionada. | |||||
| Se ignorará el uso de mayúsculas/minúsculas en la comparación. | |||||
| -h | |||||
| Muestra este mensaje de ayuda. | |||||
| EJEMPLOS DE USO | |||||
| java -jar videogameadvisor.jar -y 2018 -ag Accion,Rol | |||||
| Muestra los videojuegos que han salido en el año 2018 y son de acción y/o de comedia. | |||||
| java -jar videogameadvisor.jar -t war | |||||
| Muestra los videojuegos cuyo título contiene la palabra war. | |||||
| java -jar videogameadvisor.jar -td Accion,Aventura -b 2000,2010 -t auto | |||||
| Muestra los videojuegos cuyo título contiene la palabra auto, pertenecen al | |||||
| género de acción y aventura y fueron estrenados entre 2000 y 2010. | |||||
| java -jar videogameadvisor.jar -lg | |||||
| Muestra un listado con todos los géneros. | |||||
| LICENCIAS | |||||
| Los datos utilizados sobre videojuegos utilizados en este ejemplo son | |||||
| reales y creados por mi mismo. Ten en cuenta | |||||
| que solo puede ser usada con fines personales y no comerciales. | |||||
| @ -0,0 +1,3 @@ | |||||
| file.path=classpath:VIDEOJUEGOS.csv | |||||
| file.csv.separator=, | |||||
| file.csv.list_separator=- | |||||
| @ -0,0 +1,5 @@ | |||||
| Manifest-Version: 1.0 | |||||
| Built-By: Dani | |||||
| Build-Jdk: 17.0.4 | |||||
| Created-By: Maven Integration for Eclipse | |||||
| @ -0,0 +1,7 @@ | |||||
| #Generated by Maven Integration for Eclipse | |||||
| #Sat Nov 19 18:57:02 CET 2022 | |||||
| m2e.projectLocation=C\:\\Users\\Dani\\Documents\\workspace-spring-tool-suite-4-4.15.3.RELEASE\\PracticaSpringCore | |||||
| m2e.projectName=PracticaSpringCore | |||||
| groupId=com.daniminguet.springcore | |||||
| artifactId=PracticaSpringCore | |||||
| version=0.0.1-SNAPSHOT | |||||
| @ -0,0 +1,22 @@ | |||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | |||||
| <modelVersion>4.0.0</modelVersion> | |||||
| <groupId>com.daniminguet.springcore</groupId> | |||||
| <artifactId>PracticaSpringCore</artifactId> | |||||
| <version>0.0.1-SNAPSHOT</version> | |||||
| <name>PracticaSpringCore</name> | |||||
| <description>Practica Acceso a Datos con Spring Core</description> | |||||
| <dependencies> | |||||
| <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> | |||||
| <dependency> | |||||
| <groupId>org.springframework</groupId> | |||||
| <artifactId>spring-context</artifactId> | |||||
| <version>6.0.0</version> | |||||
| </dependency> | |||||
| <!--https://www.baeldung.com/spring-postconstruct-predestroy --> | |||||
| <dependency> | |||||
| <groupId>javax.annotation</groupId> | |||||
| <artifactId>javax.annotation-api</artifactId> | |||||
| <version>1.3.2</version> | |||||
| </dependency> | |||||
| </dependencies> | |||||
| </project> | |||||
| @ -0,0 +1,11 @@ | |||||
| ID,NOMBRE,DESARROLLADOR,GENERO,FECHA SALIDA,PLATAFORMAS DISPONIBLES | |||||
| 1,God of War: Ragnarok,Santa Monica Studio,Accion-Aventura-Lucha-Rol,09/11/2022,PS5 y PS4 | |||||
| 2,FIFA 23,Electronic Arts,Deportes-Simulacion,27/09/2022,PS5-Nintendo Switch-XBOX y PC | |||||
| 3,Minecraft,Mojang Studios,Supervivencia-Sandbox,17/05/2009,PS-XBOX-Nintendo, Moviles y PC | |||||
| 4,Grand Theft Auto V,Rockstar Games,Mundo abierto,17/09/2013,PS-XBOX y PC | |||||
| 5,CounterStrike: Global Offensive,Valve Corporation,De disparos tactico,21/08/2012,XBOX y PC | |||||
| 6,Elden Ring,FromSoftware,Aventura-Mundo abierto,25/02/2022,PS-XBOX y PC | |||||
| 7,Uncharted 4,Naughty Dog,Accion-Aventura,10/05/2016,PS y PC | |||||
| 8,Ghost of Tsushima,Sucker Punch Productions,Rol-Sigilo-Accion-Aventura-Lucha,17/07/2020,PS5 y PS4 | |||||
| 9,The Witcher 3,CD Projekt,Mundo abierto,19/05/2015,PS-NINTENDO-XBOX y PC | |||||
| 10,Assassins Creed: Origins,Ubisoft,Mundo abierto-Accion-Aventura-Sigilo,27/10/2017,PS-XBOX y PC | |||||
| @ -0,0 +1,50 @@ | |||||
| Sintaxis: java -jar videogameadvisor.jar [OPCIONES] | |||||
| -lg | |||||
| Lista los diferentes géneros de videojuegos de la colección. | |||||
| No se pueden utilizar más parámetros. | |||||
| -ag genero1,genero2,genero... | |||||
| Lista aquellos videojuegos que pertenecen a al menos uno de los géneros | |||||
| que se indican. El listado de géneros no puede incluir espacios. | |||||
| -tg genero1,genero2,genero... | |||||
| Lista aquellos videojuegos que pertenecen a todos y cada uno de los | |||||
| géneros que se indican. El listado de géneros no puede incluir espacios. | |||||
| -y año | |||||
| Lista aquellos videojuegos que se estrenaron en el año indicado. | |||||
| El año debe expresarse con 4 cifras. | |||||
| -b desde,hasta | |||||
| Lista aquellos videojuegos que se estrenaron entre los años DESDE y HASTA. | |||||
| Los años indicados están incluidos. Deben expresarse con 4 cifras. | |||||
| -t titulo | |||||
| Lista aquellos videojuegos cuyo título contiene la cadena proporcionada. | |||||
| Se ignorará el uso de mayúsculas/minúsculas en la comparación. | |||||
| -h | |||||
| Muestra este mensaje de ayuda. | |||||
| EJEMPLOS DE USO | |||||
| java -jar videogameadvisor.jar -y 2018 -ag Accion,Rol | |||||
| Muestra los videojuegos que han salido en el año 2018 y son de acción y/o de comedia. | |||||
| java -jar videogameadvisor.jar -t war | |||||
| Muestra los videojuegos cuyo título contiene la palabra war. | |||||
| java -jar videogameadvisor.jar -td Accion,Aventura -b 2000,2010 -t auto | |||||
| Muestra los videojuegos cuyo título contiene la palabra auto, pertenecen al | |||||
| género de acción y aventura y fueron estrenados entre 2000 y 2010. | |||||
| java -jar videogameadvisor.jar -lg | |||||
| Muestra un listado con todos los géneros. | |||||
| LICENCIAS | |||||
| Los datos utilizados sobre videojuegos utilizados en este ejemplo son | |||||
| reales y creados por mi mismo. Ten en cuenta | |||||
| que solo puede ser usada con fines personales y no comerciales. | |||||
| @ -0,0 +1,3 @@ | |||||
| file.path=classpath:VIDEOJUEGOS.csv | |||||
| file.csv.separator=, | |||||
| file.csv.list_separator=- | |||||