| @ -0,0 +1,55 @@ | |||
| <?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"> | |||
| <attributes> | |||
| <attribute name="maven.pomderived" value="true"/> | |||
| </attributes> | |||
| </classpathentry> | |||
| <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>mmaplayers</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,20 @@ | |||
| <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>danielkuperus</groupId> | |||
| <artifactId>mmaplayers</artifactId> | |||
| <version>0.0.1-SNAPSHOT</version> | |||
| <name>MMAPlayers</name> | |||
| <description>Este programa muestra los 25 mejores luchadores de la MMA</description> | |||
| <dependencies> | |||
| <dependency> | |||
| <groupId>org.springframework</groupId> | |||
| <artifactId>spring-context</artifactId> | |||
| <version>6.0.0</version> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>javax.annotation</groupId> | |||
| <artifactId>javax.annotation-api</artifactId> | |||
| <version>1.3.2</version> | |||
| </dependency> | |||
| </dependencies> | |||
| </project> | |||
| @ -0,0 +1,133 @@ | |||
| package mmaplayers; | |||
| import java.util.List; | |||
| public class Fighter { | |||
| private static int AUTO_ID = 0; | |||
| private int id; | |||
| private String name; | |||
| private List<String> categories; | |||
| private String bestDiscipline; | |||
| private int victories; | |||
| private int loses; | |||
| public Fighter(int id, String name, String bestDiscipline, List<String> categories, int victories, int loses) { | |||
| this.id = ++AUTO_ID; | |||
| this.name = name; | |||
| this.categories = categories; | |||
| this.bestDiscipline = bestDiscipline; | |||
| this.victories = victories; | |||
| this.loses = loses; | |||
| } | |||
| public long getId() { | |||
| return id; | |||
| } | |||
| public void setId(long id) { | |||
| this.id = id; | |||
| } | |||
| public String getName() { | |||
| return title; | |||
| } | |||
| public void setTitle(String title) { | |||
| this.name = name; | |||
| } | |||
| public List<String> getCategories() { | |||
| return categories; | |||
| } | |||
| public void setCategories(List<String> categories) { | |||
| this.categories = categories; | |||
| } | |||
| public String getBestDiscipline() { | |||
| return bestDiscipline; | |||
| } | |||
| public void setBestDiscipline(String year) { | |||
| this.bestDiscipline = bestDiscipline; | |||
| } | |||
| public int getVictories() { | |||
| return victories; | |||
| } | |||
| public void setBestDiscipline(String year) { | |||
| this.victories = victories; | |||
| } | |||
| public int getLoses() { | |||
| return loses; | |||
| } | |||
| public void setBestDiscipline(String year) { | |||
| this.loses = loses; | |||
| } | |||
| @Override | |||
| public int hashCode() { | |||
| final int prime = 31; | |||
| int result = 1; | |||
| result = prime * result + ((categories == null) ? 0 : categories.hashCode()); | |||
| result = prime * result + (int) (id ^ (id >>> 32)); | |||
| result = prime * result + ((name == null) ? 0 : name.hashCode()); | |||
| result = prime * result + ((bestDiscipline == null) ? 0 : bestDiscipline.hashCode()); | |||
| return result; //revisar mas adelante | |||
| } | |||
| @Override | |||
| public boolean equals(Object obj) { | |||
| if (this == obj) | |||
| return true; | |||
| if (obj == null) | |||
| return false; | |||
| if (getClass() != obj.getClass()) | |||
| return false; | |||
| Film other = (Film) obj; | |||
| if (genres == null) { | |||
| if (other.genres != null) | |||
| return false; | |||
| } else if (!categories.equals(other.genres)) | |||
| return false; | |||
| if (id != other.id) | |||
| return false; | |||
| if (name == null) { | |||
| if (other.name != null) | |||
| return false; | |||
| } else if (!name.equals(other.name)) | |||
| return false; | |||
| if (bestDiscipline == null) { | |||
| if (other.bestDiscipline != null) | |||
| return false; | |||
| } else if (!bestDiscipline.equals(other.bestDiscipline)) | |||
| return false; | |||
| if (victories != other.victories) | |||
| return false; | |||
| if (loses != other.loses) | |||
| return false; | |||
| return true; | |||
| } | |||
| @Override | |||
| public String toString() { | |||
| return "Luchador [id=" + id + ", nombre=" + name + ", categoria=" + categories + ", mejor disciplina=" + bestDiscipline + "victorias "+victories+" derrotas"+loses"]"; | |||
| } | |||
| } | |||
| @ -0,0 +1,32 @@ | |||
| package mmaplayers.fightersearch.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="mmaplayers") | |||
| @PropertySource("classpath:/booksearch.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 mmaplayers.fightersearch.controller; | |||
| import mmaplayers.mmaplayers.model.Fighter; | |||
| import org.springframework.util.ResourceUtils; | |||
| 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; | |||
| public class CsvParser { | |||
| public static List<Fighter> readFile(final String path, final String separator, final String listSeparator) { | |||
| List<Fighter> result = new ArrayList<>(); | |||
| try { | |||
| result = Files | |||
| .lines(Paths.get(ResourceUtils.getFile(path).toURI())) | |||
| .skip(1) | |||
| .map(line -> { | |||
| String[] values = line.split(separator); | |||
| if (values.length >= 5) { | |||
| return new Fighter(values[0], Arrays.asList(values[1].split(listSeparator)), values[2], values[3], values[4] ,values[5]); | |||
| }).collect(Collectors.toList()); | |||
| } catch (Exception e) { | |||
| e.printStackTrace(); | |||
| System.err.println("Error de lectura del fichero de datos: MMA_DanielKuperus.csv"); | |||
| System.exit(-1); | |||
| } | |||
| return result; | |||
| } | |||
| } | |||
| @ -0,0 +1,60 @@ | |||
| package mmaplayers.fightersearch.dao; | |||
| public class FighterDao implements IFighterDao{ | |||
| public Fighter<Fighter> fighters = new ArrayList<>(); | |||
| @Autowired | |||
| private AppConfig appConfig; | |||
| @PostConstruct | |||
| public void init() { | |||
| fighters = CsvParser.readFile(appConfig.getFile(), appConfig.getSeparator(), appConfig.getListSeparator()); | |||
| } | |||
| @Override | |||
| public Fighter findById(int id) { | |||
| Optional<Fighter> result = fighters | |||
| .stream() | |||
| .filter(f -> f.getId() == id) | |||
| .findFirst(); | |||
| return result.orElse(null); | |||
| } | |||
| @Override | |||
| public Collection<Fighter> findAll() { | |||
| return fighters; | |||
| } | |||
| @Override | |||
| public void insert(Fighter fighter) { | |||
| fighters.add(fighter); | |||
| } | |||
| @Override | |||
| public void edit(Fighter fighter) { | |||
| int index = getIndexOf(fighter.getId()); | |||
| if (index != -1) | |||
| fighter.set(index, fighter); | |||
| } | |||
| @Override | |||
| public void delete(int id) { | |||
| int index = getIndexOf(id); | |||
| if (index != -1) | |||
| fighters.remove(index); | |||
| } | |||
| private int getIndexOf(int id) { | |||
| boolean find = false; | |||
| int index = 0; | |||
| while (!find && index < fighters.size()) { | |||
| if (fighters.get(index).getId() == id) | |||
| find = true; | |||
| else | |||
| index++; | |||
| } | |||
| return (find) ? index : -1; | |||
| } | |||
| } | |||
| @ -0,0 +1,10 @@ | |||
| package mmaplayers.fightersearch.dao; | |||
| public interface IFighterDao { | |||
| public Fighter findById(int id); | |||
| public Collection<Fighter> findAll(); | |||
| public void insert(Fighter fighter); | |||
| public void edit(Fighter fighter); | |||
| public void delete(int id); | |||
| } | |||
| @ -0,0 +1,55 @@ | |||
| package mmaplayers.fightersearch.service; | |||
| import mmaplayers.fightersearch.dao.IFighterDao; | |||
| import mmaplayers.fightersearch.model.Fighter; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| import javax.annotation.PostConstruct; | |||
| import java.util.Arrays; | |||
| import java.util.Collection; | |||
| import java.util.function.Predicate; | |||
| import java.util.stream.Collectors; | |||
| public class FighterQueryService implements IFightQueryService { | |||
| @Autowired | |||
| IFighterDao iFighterDao; | |||
| private Predicate<Fighter> predicate; | |||
| @PostConstruct | |||
| public void init() { | |||
| predicate = null; | |||
| } | |||
| @Override | |||
| public Collection<Book> exec() { | |||
| return iFighterDao.findAll() | |||
| .stream() | |||
| .filter(predicate) | |||
| .collect(Collectors.toList()); | |||
| } | |||
| @Override | |||
| public IFighterQueryService anyCategorie(String... categorie) { | |||
| Predicate<Fighter> pAnyCategorie = (fighter -> Arrays.stream(categories).anyMatch(fighter.getCategorie()::contains)); | |||
| predicate = (predicate == null) ? pAnyCategorie : predicate.and(pAnyCategorie); | |||
| return this; | |||
| } | |||
| @Override | |||
| public IFighterQueryService allCategories(String... categories) { | |||
| Predicate<Fighter> pAnyCategorie = (fighter -> Arrays.stream(categories).allMatch(book.getCategorie()::contains)); | |||
| predicate = (predicate == null) ? pAnyCategorie : predicate.and(pAnyCategorie); | |||
| return this; | |||
| } | |||
| @Override | |||
| public IFighterQueryService anyName(String name) { | |||
| Predicate<Fighter> pNameContains = (fighter -> fighter.getName().toLowerCase().contains(name.toLowerCase())); | |||
| predicate = (predicate == null) ? pNameContains : predicate.and(pNameContains); | |||
| return this; | |||
| } | |||
| } | |||
| @ -0,0 +1,54 @@ | |||
| package mmaplayers.fightersearch.service; | |||
| import mmaplayers.fightersearch.dao.IFighterDao; | |||
| import mmaplayers.fightersearch.model.Fighter; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| import java.util.Collection; | |||
| import java.util.List; | |||
| import java.util.stream.Collectors; | |||
| public class FighterService implements IFightService{ | |||
| @Autowired | |||
| IFighterDao iFighterDao; | |||
| @Autowired | |||
| IFighterQueryService queryService; | |||
| @Override | |||
| public Collection<String> findAllCategories() { | |||
| List<String> result = null; | |||
| result = iFighterDao.findAll() | |||
| .stream() | |||
| .map(book -> book.getCategorie()) | |||
| .distinct() | |||
| .sorted() | |||
| .collect(Collectors.toList()); | |||
| return result; | |||
| } | |||
| @Override | |||
| public Collection<Fighter> findByAnyCategorie(String... categories) { | |||
| return queryService.anyCategorie(categories).exec(); | |||
| } | |||
| @Override | |||
| public Collection<Fighter> findByAllCategories(String... categories) { | |||
| return queryService.allCategories(categories).exec(); | |||
| } | |||
| @Override | |||
| public Collection<Fighter> findByNameContains(String name) { | |||
| return queryService.anyName(name).exec(); | |||
| } | |||
| @Override | |||
| public Collection<Fighter> findAll() { | |||
| return iFighterDao.findAll(); | |||
| } | |||
| } | |||
| @ -0,0 +1,13 @@ | |||
| package mmaplayers.fightersearch.service; | |||
| import mmaplayers.fightersearch.model.Fighter; | |||
| import java.util.Collection; | |||
| public interface IFightQueryService { | |||
| public Collection<String> findAllCategories(); | |||
| public Collection<Fighter> findByAnyCategorie(String... categories); | |||
| public Collection<Fighter> findByAllCategories(String... categories); | |||
| public Collection<Fighter> findByNameContains(String name); | |||
| public Collection<Fighter> findAll(); | |||
| } | |||
| @ -0,0 +1,13 @@ | |||
| package mmaplayers.fightersearch.service; | |||
| import mmaplayers.fightersearch.model.Fight; | |||
| public interface IFightService { | |||
| public Collection<String> findAllCategories(); | |||
| public Collection<Fighter> findByAnyCategorie(String... categories); | |||
| public Collection<Fighter> findByAllCategories(String... categories); | |||
| public Collection<Fighter> findByNameContains(String name); | |||
| public Collection<Fighter> findAll(); | |||
| } | |||
| @ -0,0 +1,5 @@ | |||
| Manifest-Version: 1.0 | |||
| Built-By: Daniel | |||
| Build-Jdk: 17.0.4.1 | |||
| Created-By: Maven Integration for Eclipse | |||
| @ -0,0 +1,7 @@ | |||
| #Generated by Maven Integration for Eclipse | |||
| #Thu Nov 24 01:08:48 CET 2022 | |||
| m2e.projectLocation=C\:\\Users\\Daniel\\Documents\\workspace-spring-tool-suite-4-4.16.1.RELEASE\\mmaplayers | |||
| m2e.projectName=mmaplayers | |||
| groupId=danielkuperus | |||
| artifactId=mmaplayers | |||
| version=0.0.1-SNAPSHOT | |||
| @ -0,0 +1,20 @@ | |||
| <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>danielkuperus</groupId> | |||
| <artifactId>mmaplayers</artifactId> | |||
| <version>0.0.1-SNAPSHOT</version> | |||
| <name>MMAPlayers</name> | |||
| <description>Este programa muestra los 25 mejores luchadores de la MMA</description> | |||
| <dependencies> | |||
| <dependency> | |||
| <groupId>org.springframework</groupId> | |||
| <artifactId>spring-context</artifactId> | |||
| <version>6.0.0</version> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>javax.annotation</groupId> | |||
| <artifactId>javax.annotation-api</artifactId> | |||
| <version>1.3.2</version> | |||
| </dependency> | |||
| </dependencies> | |||
| </project> | |||