Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • s3206149/mod4-wp-2023-2024-pokemon
  • s3281744/mod4-wp-2023-2024-pokemon
  • s3128989/mod4-wp-2023-2024-pokemon
  • s3263797/m-4-pokemon
  • s3203255/mod4-wp-2023-2024-pokemon
  • s2854961/mod-4-wp-2023-2024-pokemons
  • s3192016/pokemon-practical-sessions
  • s3126080/mod4-wp-2023-2024-pokemon
  • s3165442/poke-mon
  • s3187152/mod4-wp-2023-2024-pokemon
  • s3209105/mod4-wp-2023-2024-pokemon
  • s3362779/mod4-wp-2023-2024-pokemon
  • s3173011/mod-4-wp-3-2023-2024-pokemon
  • s3174506/mod4-wp-2023-2024-pokemon
  • s3216926/mod-4-wp-2023-2024-pokemon-2nd
  • s3211770/mod4-wp-2023-2024-pokemon
  • s2936011/mod4-wp-2023-2024-pokemon
  • s3234312/mod4-wp-2023-2024-pokemon
  • s3362264/mod4-wp-2023-2024-pokemon
  • s3204766/mod4-wp-2023-2024-pokemon
  • s3363775/mod4-wp-2023-2024-pokemon
  • s3142906/mod4-wp-2023-2024-pokemon
  • s3298892/mod4-wp-2023-2024-pokemon
  • s3125866/mod-4-wp-2023-2024-pokemon-update
  • s2900556/mod-4-wp-2023-2024-pokemontut-3
  • s3196801/mod4-wp-2023-2024-pokemon
  • s2507919/mod4-wp-2023-2024-pokemon
  • Marius/mod-4-pokemon
  • s2948486/mod4-wp-2023-2024-pokemon
  • s3193748/mod4-wp-2023-2024-pokemon
  • s3199800/mod4-wp-2023-2024-pokemon
  • s3152340/mod4-wp-2023-2024-pokemon
  • s3194108/mod4-wp-2023-2024-pokemon
  • s3364518/mod4-wp-2023-2024-pokemon
  • s3200205/mod4-wp-2023-2024-pokemon
  • s3187667/pokemon-server
  • s3259633/mod4-wp-2023-2024-pokemon
  • s3137279/mod4-wp-2023-2024-pokemon
  • s3213552/mod4-wp-2023-2024-pokemon
  • s3200779/mod4-wp-2023-2024-pokemon
  • s3148157/mod4-wp-2023-2024-pokemon
  • s3196372/mod4-wp-2023-2024-pokemon
  • s3126714/mod4-wp-2023-2024-pokemon
  • s3286029/pracitcal-2
  • s3262499/mod4-wp-2023-2024-pokemon
  • s3290387/mod4-wp-2023-2024-pokemon
  • s3202798/mod4-wp-2023-2024-pokemon
  • s3210715/mod4-wp-2023-2024-pokemon
  • s3198308/mod4-wp-2023-2024-pokemon
  • s3211614/mod4-wp-2023-2024-pokemon
  • s3193691/mod4-wp-2023-2024-pokemon
  • s3273733/mod4-wp-2023-2024-pokemon
  • s3210898/mod4-wp-2023-2024-pokemon
  • s3133931/mod4-wp-2023-2024-pokemon
  • s3199479/mod4-wp-2023-2024-pokemon
  • s3190285/mod4-wp-2023-2024-pokemon
  • s3002896/mod4-wp-2023-2024-pokemon
  • s3166244/mod4-wp-2023-2024-pokemon
  • s3165582/mod4-wp-2023-2024-pokemon
  • s3147851/mod-4-wp-2023-2024-pokemon
  • web-programming-sessions/mod4-wp-2023-2024-pokemon
  • s3309630/mod4-wp-2023-2024-pokemon
  • s2914980/mod4-wp-2023-2024-pokemon
  • s3000885/mod4-wp-2023-2024-pokemon
  • s3209539/mod-4-pokemon-project
  • s3203573/kai-poke-mon
  • s3138127/mod-4-wp-2023-2024-pokemon-daniel
  • s3106586/mod4-wp-2023-2024-pokemon
  • s3183394/mod4-wp-2023-2024-pokemon
  • s3245748/mod4-wp-2023-2024-pokemon
  • s3252159/dm-mod-4-wp-2023-2024-pokemon
  • s2695154/mod4-wp-2023-2024-pokemon
  • s3171825/mod4-wp-2023-2024-pokemon
  • s3200183/mod4-wp-2023-2024-pokemon
  • s3000087/mod4-wp-2023-2024-pokemon
  • s3127168/mod4-wp-2023-2024-pokemon
  • s3137333/mod4-wp-2023-2024-pokemon
  • s3128830/mod4-wp-2023-2024-pokemon
  • s3102378/mod4-wp-2023-2024-pokemon
  • s3202224/mod4-wp-2023-2024-pokemon
  • s2590654/mod4-wp-2023-2024-pokemon
  • s3154882/mod4-wp-2023-2024-pokemon
  • s2958732/mod4-wp-2023-2024-pokemon
  • s3161293/mod4-wp-2023-2024-pokemon
  • s3133656/mod4-wp-2023-2024-pokemon
  • s3157385/mod-4-wp-2023-2024-pokemon-ali
  • s3192393/mod4-wp-2023-2024-pokemon
  • s3192016/mod4-wp-2023-2024-pokemon
  • s3211347/mod4-wp-2023-2024-pokemon
  • s3152790/mod4-wp-2023-2024-pokemon
  • s2959380/mod4-wp-2023-2024-pokemon
  • s3362906/mod4-wp-2023-2024-pokemon
  • s2964546/mod4-wp-2023-2024-pokemon
  • s3214915/mod4-wp-2023-2024-pokemon
  • s2219174/mod4-wp-2023-2024-pokemon
  • s3161595/mod4-wp-2023-2024-pokemon
  • s3207803/mod4-wp-2023-2024-pokemon
  • s3209334/mod4-wp-2023-2024-pokemon
  • s3191915/mod4-wp-2023-2024-pokemon
  • s3192253/mod4-wp-2023-2024-pokemon
  • s3137090/mod4-wp-2023-2024-pokemon
  • s3190536/mod4-wp-2023-2024-pokemon2
  • s3138585/mod4-wp-2023-2024-pokemon
  • s3186792/mod4-wp-2023-2024-pokemon
  • s2998491/mod4-wp-2023-2024-pokemon
  • s3145786/mod4-wp-2023-2024-pokemon
  • s3231046/mod4-wp-2023-2024-pokemon
  • s3125866/mod4-wp-2023-2024-pokemon
  • s3140458/mod4-wp-2023-2024-pokemon
  • s3133346/mod4-wp-2023-2024-pokemon
  • s3274519/mod4-wp-2023-2024-pokemon
  • s3216551/mod4-wp-2023-2024-pokemon
  • s3237478/mod4-wp-2023-2024-pokemon
  • s2985063/mod4-wp-2023-2024-pokemon
  • s3251942/mod4-wp-2023-2024-pokemon
  • s3305155/mod-4-wp-2023-2024-pokemon-david-sastre-madueno
  • s2675072/mod4-wp-2023-2024-pokemon
  • s3136027/mod4-wp-2023-2024-pokemon
  • s3186156/mod4-wp-2023-2024-pokemon
  • s3277305/mod4-wp-2023-2024-pokemon
  • s3242404/mod4-wp-2023-2024-pokemon
  • s3205967/mod4-wp-2023-2024-pokemon
  • s3173011/mod4-wp-2023-2024-pokemon
  • s3159736/mod4-wp-2023-2024-pokemon
  • s3207269/wplab-2
  • s3195945/mod4-wp-2023-2024-pokemon
  • s3151913/mod4-wp-2023-2024-pokemon
  • s3135136/mod4-wp-2023-2024-pokemon
  • s3216926/mod4-wp-2023-2024-pokemon
  • s3161218/mod4-wp-2023-2024-pokemon
  • s2921103/mod4-wp-2023-2024-pokemon
  • s3175561/mod4-wp-2023-2024-pokemon
  • SimeonNikolov/mod4-wp-2023-2024-pokemon
  • s3200329/mod4-wp-2023-2024-pokemon
  • s3148726/mod4-wp-2023-2024-pokemon
  • s2960796/mod4-wp-2023-2024-pokemon
  • s3098117/mod4-wp-2023-2024-pokemon
  • s3180794/mod4-wp-2023-2024-pokemon
  • s3179176/mod4-wp-2023-2024-pokemon
  • s3170268/mod4-wp-2023-2024-pokemon
  • s3126188/mod4-wp-2023-2024-pokemon
  • s3166449/pokemon-sarah-vonk
  • s3191567/mod4-wp-2023-2024-pokemon
  • s3271498/mod4-wp-2023-2024-pokemon
  • s3173089/mod4-wp-2023-2024-pokemon
  • s3172716/mod4-wp-2023-2024-pokemon
  • s3188981/mod4-wp-2023-2024-pokemon
  • s3184609/mod-4-lab-1-pokemon
  • s3144569/mod4-wp-2023-2024-pokemon
  • s3181340/poke-app-lab-06-05
  • s2900556/mod4-wp-2023-2024-pokemon
  • s3177858/mod4-wp-2023-2024-pokemon
  • s3209180/mod4-wp-2023-2024-pokemon
  • s3199495/mod4-wp-2023-2024-pokemon
  • s3163660/mod4-wp-2023-2024-pokemon
  • s3172708/mod4-wp-2023-2024-pokemon
  • s3206068/mod4-wp-2023-2024-pokemon
  • s3210774/mod4-wp-2023-2024-pokemon
  • s3200175/mod4-wp-2023-2024-pokemon
  • s3127141/mod4-wp-2023-2024-pokemon
  • s2995522/pokemon-practical-2-module-4
  • s3156621/mod4-wp-2023-2024-pokemon
  • s3126099/mod4-wp-2023-2024-pokemon
  • s3128121/mod4-wp-2023-2024-pokemon
  • s3233405/mod4-wp-2023-2024-pokemon
  • s3087921/mod4-wp-2023-2024-pokemon
  • s3202119/mod4-wp-2023-2024-pokemon
  • claudenirmf/mod4-wp-2023-2024-pokemon
168 results
Show changes
Showing
with 20177 additions and 0 deletions
@REM ----------------------------------------------------------------------------
@REM Licensed to the Apache Software Foundation (ASF) under one
@REM or more contributor license agreements. See the NOTICE file
@REM distributed with this work for additional information
@REM regarding copyright ownership. The ASF licenses this file
@REM to you under the Apache License, Version 2.0 (the
@REM "License"); you may not use this file except in compliance
@REM with the License. You may obtain a copy of the License at
@REM
@REM https://www.apache.org/licenses/LICENSE-2.0
@REM
@REM Unless required by applicable law or agreed to in writing,
@REM software distributed under the License is distributed on an
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@REM KIND, either express or implied. See the License for the
@REM specific language governing permissions and limitations
@REM under the License.
@REM ----------------------------------------------------------------------------
@REM ----------------------------------------------------------------------------
@REM Maven Start Up Batch script
@REM
@REM Required ENV vars:
@REM JAVA_HOME - location of a JDK home dir
@REM
@REM Optional ENV vars
@REM M2_HOME - location of maven2's installed home dir
@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending
@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
@REM e.g. to debug Maven itself, use
@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
@REM ----------------------------------------------------------------------------
@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
@echo off
@REM set title of command window
title %0
@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on'
@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO%
@REM set %HOME% to equivalent of $HOME
if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
@REM Execute a user defined script before this one
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
@REM check for pre script, once with legacy .bat ending and once with .cmd ending
if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %*
if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %*
:skipRcPre
@setlocal
set ERROR_CODE=0
@REM To isolate internal variables from possible post scripts, we use another setlocal
@setlocal
@REM ==== START VALIDATION ====
if not "%JAVA_HOME%" == "" goto OkJHome
echo.
echo Error: JAVA_HOME not found in your environment. >&2
echo Please set the JAVA_HOME variable in your environment to match the >&2
echo location of your Java installation. >&2
echo.
goto error
:OkJHome
if exist "%JAVA_HOME%\bin\java.exe" goto init
echo.
echo Error: JAVA_HOME is set to an invalid directory. >&2
echo JAVA_HOME = "%JAVA_HOME%" >&2
echo Please set the JAVA_HOME variable in your environment to match the >&2
echo location of your Java installation. >&2
echo.
goto error
@REM ==== END VALIDATION ====
:init
@REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
@REM Fallback to current working directory if not found.
set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
set EXEC_DIR=%CD%
set WDIR=%EXEC_DIR%
:findBaseDir
IF EXIST "%WDIR%"\.mvn goto baseDirFound
cd ..
IF "%WDIR%"=="%CD%" goto baseDirNotFound
set WDIR=%CD%
goto findBaseDir
:baseDirFound
set MAVEN_PROJECTBASEDIR=%WDIR%
cd "%EXEC_DIR%"
goto endDetectBaseDir
:baseDirNotFound
set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
cd "%EXEC_DIR%"
:endDetectBaseDir
IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
@setlocal EnableExtensions EnableDelayedExpansion
for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
:endReadAdditionalConfig
SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar"
FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO (
IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B
)
@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
@REM This allows using the maven wrapper in projects that prohibit checking in binary data.
if exist %WRAPPER_JAR% (
if "%MVNW_VERBOSE%" == "true" (
echo Found %WRAPPER_JAR%
)
) else (
if not "%MVNW_REPOURL%" == "" (
SET DOWNLOAD_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar"
)
if "%MVNW_VERBOSE%" == "true" (
echo Couldn't find %WRAPPER_JAR%, downloading it ...
echo Downloading from: %DOWNLOAD_URL%
)
powershell -Command "&{"^
"$webclient = new-object System.Net.WebClient;"^
"if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^
"$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^
"}"^
"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^
"}"
if "%MVNW_VERBOSE%" == "true" (
echo Finished downloading %WRAPPER_JAR%
)
)
@REM End of extension
@REM Provide a "standardized" way to retrieve the CLI args that will
@REM work with both Windows and non-Windows executions.
set MAVEN_CMD_LINE_ARGS=%*
%MAVEN_JAVA_EXE% ^
%JVM_CONFIG_MAVEN_PROPS% ^
%MAVEN_OPTS% ^
%MAVEN_DEBUG_OPTS% ^
-classpath %WRAPPER_JAR% ^
"-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^
%WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
if ERRORLEVEL 1 goto error
goto end
:error
set ERROR_CODE=1
:end
@endlocal & set ERROR_CODE=%ERROR_CODE%
if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost
@REM check for post script, once with legacy .bat ending and once with .cmd ending
if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat"
if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd"
:skipRcPost
@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
if "%MAVEN_BATCH_PAUSE%"=="on" pause
if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE%
cmd /C exit /B %ERROR_CODE%
<?xml version="1.0" encoding="UTF-8"?>
<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>nl.utwente.mod4</groupId>
<artifactId>pokemon</artifactId>
<version>1.0-SNAPSHOT</version>
<name>pokemon</name>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<junit.version>5.9.1</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.0</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package nl.utwente.mod4.pokemon;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import jakarta.servlet.annotation.WebListener;
import nl.utwente.mod4.pokemon.dao.PokemonTypeDao;
import nl.utwente.mod4.pokemon.dao.TrainerDao;
import java.io.IOException;
@WebListener
public class InitializationListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Initializing PokeApp...");
try {
PokemonTypeDao.INSTANCE.load();
TrainerDao.INSTANCE.load();
} catch (IOException e) {
System.err.println("Error while loading data.");
e.printStackTrace();
}
System.out.println("PokeApp initialized.");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("Shutting down PokeApp...");
try {
PokemonTypeDao.INSTANCE.save();
TrainerDao.INSTANCE.save();
} catch (IOException e) {
System.err.println("Error while saving data.");
e.printStackTrace();
}
System.out.println("PokeApp shutdown.");
}
}
package nl.utwente.mod4.pokemon;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;
@ApplicationPath("/api")
public class PokeApp extends Application {
}
\ No newline at end of file
package nl.utwente.mod4.pokemon;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.ext.Provider;
@Provider
public class RequestFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) {
System.out.println("Incoming Request:");
System.out.println("Method: " + requestContext.getMethod());
System.out.println("URI: " + requestContext.getUriInfo().getRequestUri());
System.out.println("Headers: " + requestContext.getHeaders());
}
}
package nl.utwente.mod4.pokemon;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.List;
public class Utils {
public static String getAbsolutePathToResources() {
var path = Utils.class.getClassLoader().getResource("").getPath();
return URLDecoder.decode(path, StandardCharsets.UTF_8);
}
public static <T extends Comparable<T>> int compare(T o1, T o2) {
if (o1 == null && o2 == null)
return 0;
if (o1 == null)
return -1;
if (o2 == null)
return 1;
return o1.compareTo(o2);
}
public static List<?> pageSlice(List<?> list, int pageSize, int pageNumber) {
int total = list.size();
int firstIndex = (pageNumber - 1) * pageSize;
int lastIndex = Math.min(pageNumber * pageSize, total);
return list.subList(firstIndex,lastIndex);
}
}
package nl.utwente.mod4.pokemon.dao;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.NotSupportedException;
import nl.utwente.mod4.pokemon.Utils;
import nl.utwente.mod4.pokemon.model.PokemonType;
import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.util.*;
public enum PokemonTypeDao {
INSTANCE;
private static final String ORIGINAL_POKEMON_TYPES = Utils.getAbsolutePathToResources() + "/aggregated-and-filtered-pokemon-dataset.json";
private static final String POKEMON_TYPES = Utils.getAbsolutePathToResources() + "/pokemon-types.json";
private HashMap<String, PokemonType> pokemonTypes = new HashMap<>();
public void delete(String id) {
if(pokemonTypes.containsKey(id)) {
pokemonTypes.remove(id);
} else {
throw new NotFoundException("Pokemon type '" + id + "' not found.");
}
}
public List<PokemonType> getPokemonTypes(int pageSize, int pageNumber, String sortBy) {
List<PokemonType> list = new ArrayList<>(pokemonTypes.values());
if (sortBy == null || sortBy.isEmpty() || "id".equals(sortBy))
list.sort((pt1, pt2) -> Utils.compare(Integer.parseInt(pt1.id), Integer.parseInt(pt2.id)));
else if ("pokedexNumber".equals(sortBy))
list.sort((pt1, pt2) -> Utils.compare(pt1.pokedexNumber, pt2.pokedexNumber));
else if ("lastUpDate".equals(sortBy))
list.sort((pt1, pt2) -> Utils.compare(pt1.lastUpDate, pt2.lastUpDate));
else
throw new NotSupportedException("Sort field not supported");
return (List<PokemonType>) Utils.pageSlice(list,pageSize,pageNumber);
}
public PokemonType getPokemonType(String id) {
var pt = pokemonTypes.get(id);
if (pt == null) {
throw new NotFoundException("Pokemon '" + id + "' not found!");
}
return pt;
}
public void load() throws IOException {
ObjectMapper mapper = new ObjectMapper();
File source = existsPokemonTypes() ?
new File(POKEMON_TYPES) :
new File(ORIGINAL_POKEMON_TYPES);
PokemonType[] arr = mapper.readValue(source, PokemonType[].class);
Arrays.stream(arr).forEach(pt -> pokemonTypes.put(pt.id, pt));
}
private boolean existsPokemonTypes() {
File f = new File(POKEMON_TYPES);
return f.exists() && !f.isDirectory();
}
public void save() throws IOException {
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
File destination = new File(POKEMON_TYPES);
writer.writeValue(destination, pokemonTypes.values());
}
public PokemonType create(PokemonType newPokemonType) {
String nextId = "" + (getMaxId() + 1);
newPokemonType.id = nextId;
newPokemonType.created = Instant.now().toString();
newPokemonType.lastUpDate = Instant.now().toString();
pokemonTypes.put(nextId,newPokemonType);
return newPokemonType;
}
private int getMaxId() {
Set<String> ids = pokemonTypes.keySet();
return ids.isEmpty() ? 0 : ids.stream()
.map(Integer::parseInt)
.max(Integer::compareTo)
.get();
}
public PokemonType replace(PokemonType updated) {
if(!updated.checkIsValid())
throw new BadRequestException("Invalid pokemon type.");
if(pokemonTypes.get(updated.id) == null)
throw new NotFoundException("Pokemon type id '" + updated.id + "' not found.");
updated.lastUpDate = Instant.now().toString();
pokemonTypes.put(updated.id,updated);
return updated;
}
public int getTotalPokemonTypes() {
return pokemonTypes.keySet().size();
}
}
package nl.utwente.mod4.pokemon.dao;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.NotFoundException;
import nl.utwente.mod4.pokemon.Utils;
import nl.utwente.mod4.pokemon.model.Trainer;
import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.util.*;
public enum TrainerDao {
INSTANCE;
private static final String ORIGINAL_TRAINERS = Utils.getAbsolutePathToResources() + "/default-trainers-dataset.json";
private static final String TRAINERS = Utils.getAbsolutePathToResources() + "/trainers.json";
private HashMap<String, Trainer> trainers = new HashMap<>();
public void delete(String id) {
if(trainers.containsKey(id)) {
trainers.remove(id);
} else {
throw new NotFoundException("Trainer '" + id + "' not found.");
}
}
public List<Trainer> getTrainers(int pageSize, int pageNumber) {
var list = new ArrayList<>(trainers.values());
return (List<Trainer>) Utils.pageSlice(list,pageSize,pageNumber);
}
public Trainer getTrainer(String id) {
var pt = trainers.get(id);
if (pt == null) {
throw new NotFoundException("Pokemon '" + id + "' not found!");
}
return pt;
}
public void load() throws IOException {
ObjectMapper mapper = new ObjectMapper();
File source = existsTrainers() ?
new File(TRAINERS) :
new File(ORIGINAL_TRAINERS);
Trainer[] arr = mapper.readValue(source, Trainer[].class);
Arrays.stream(arr).forEach(trainer -> trainers.put(trainer.id, trainer));
}
public void save() throws IOException {
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
File destination = new File(TRAINERS);
writer.writeValue(destination, trainers.values());
}
private boolean existsTrainers() {
File f = new File(TRAINERS);
return f.exists() && !f.isDirectory();
}
private int getMaxId() {
Set<String> ids = trainers.keySet();
return ids.isEmpty() ? 0 : ids.stream()
.map(Integer::parseInt)
.max(Integer::compareTo)
.get();
}
public Trainer create(Trainer newTrainer) {
String nextId = "" + (getMaxId() + 1);
newTrainer.id = nextId;
newTrainer.created = Instant.now().toString();
newTrainer.lastUpDate = Instant.now().toString();
trainers.put(nextId,newTrainer);
return newTrainer;
}
public Trainer replace(Trainer updated) {
if(!updated.checkIsValid())
throw new BadRequestException("Invalid trainer.");
if(trainers.get(updated.id) == null)
throw new NotFoundException("Trainer id '" + updated.id + "' not found.");
updated.lastUpDate = Instant.now().toString();
trainers.put(updated.id,updated);
return updated;
}
public int getTotalTrainers() {
return trainers.keySet().size();
}
}
package nl.utwente.mod4.pokemon.model;
public class NamedEntity {
public String id;
public String name;
public String created;
public String lastUpDate;
public NamedEntity() {
id = null;
name = null;
created = null;
lastUpDate = null;
}
public boolean checkIsValid() {
return id != null && !id.isEmpty();
}
}
package nl.utwente.mod4.pokemon.model;
public class Pokemon extends NamedEntity {
public String trainerId;
public String pokemonType;
public Pokemon() {}
}
package nl.utwente.mod4.pokemon.model;
import java.util.ArrayList;
import java.util.List;
public class PokemonType extends NamedEntity {
public int pokedexNumber;
public int generation;
public String japaneseName;
public String classification;
public List<String> abilities;
public float baseHeight;
public float baseWeight;
public int baseHp;
public int baseAttack;
public int baseSpAttack;
public int baseDefense;
public int baseSpDefense;
public int baseSpeed;
public int captureRate;
public boolean isLegendary;
public String imgUrl;
public String primaryType;
public String secondaryType;
public PokemonType() {
super();
pokedexNumber = 0;
generation = 0;
japaneseName = null;
classification = null;
abilities = null;
baseHeight = 0;
baseWeight = 0;
baseHp = 0;
baseAttack = 0;
baseSpAttack = 0;
baseDefense = 0;
baseSpDefense = 0;
baseSpeed = 0;
captureRate = 0;
isLegendary = false;
imgUrl = null;
}
}
package nl.utwente.mod4.pokemon.model;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
public class ResourceCollection<T extends NamedEntity> {
public HashMap<String,Object> meta;
// public HashMap<String,Object> links;
public T[] data;
public ResourceCollection() {
data = null;
meta = new HashMap<>();
// links = new HashMap<>();
}
public ResourceCollection(T[] resources, int pageSize, int pageNumber, int total) {
meta = new HashMap<>();
// links = new HashMap<>();
meta.put("total", total);
meta.put("pageNumber", pageNumber);
meta.put("pageSize", pageSize);
data = resources;
}
}
package nl.utwente.mod4.pokemon.model;
public class Trainer extends NamedEntity {
public String profileUrl;
public Trainer() {
super();
profileUrl = null;
}
}
package nl.utwente.mod4.pokemon.routes;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import nl.utwente.mod4.pokemon.model.Pokemon;
import java.util.ArrayList;
import java.util.List;
@Path("/pokemon")
public class PokemonRoute {
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Pokemon> getPokemon() {
var list = new ArrayList<Pokemon> ();
var bolt = new Pokemon();
bolt.id = "1";
bolt.name = "Bolt";
bolt.trainerId = "1";
bolt.pokemonType = "513";
list.add(bolt);
return list;
}
@Path("/{id}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Pokemon getPokemon(@PathParam("id") String id) {
var bolt = new Pokemon();
bolt.id = id;
bolt.name = "Bolt";
bolt.trainerId = "1";
bolt.pokemonType = "513";
return bolt;
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Pokemon createPokemon(Pokemon pokemon) {
return pokemon;
}
@Path("/{id}")
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Pokemon updatePokemon(@PathParam("id") String id, Pokemon pokemon) {
return pokemon;
}
@Path("/{id}")
@DELETE
public void deletePokemon(@PathParam("id") String id) {
System.out.println("Pokemon '"+id+"' deleted");
}
}
package nl.utwente.mod4.pokemon.routes;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import nl.utwente.mod4.pokemon.dao.PokemonTypeDao;
import nl.utwente.mod4.pokemon.model.PokemonType;
import nl.utwente.mod4.pokemon.model.ResourceCollection;
@Path("/pokemonTypes")
public class PokemonTypeRoute {
@GET
@Produces(MediaType.APPLICATION_JSON)
public ResourceCollection<PokemonType> getPokemonTypes(
@QueryParam("sortBy") String sortBy,
@QueryParam("pageSize") int pageSize,
@QueryParam("pageNumber") int pageNumber
) {
// initializes page size and page number for request
int ps = pageSize > 0 ? pageSize : Integer.MAX_VALUE;
int pn = pageNumber > 0 ? pageNumber : 1;
// request the desired page and sorting to the Data Access Object
PokemonType[] resources = PokemonTypeDao.INSTANCE.getPokemonTypes(ps, pn, sortBy).toArray(new PokemonType[0]);
// updates the total and page size for the response
int total = PokemonTypeDao.INSTANCE.getTotalPokemonTypes();
ps = resources == null ? 0 : resources.length;
// returns a collection object containing the requested resources
return new ResourceCollection<>(resources, ps, pn, total);
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public PokemonType createPokemonType(PokemonType pokemonType) {
// creates a resource based on to the JSON object sent by the client
return PokemonTypeDao.INSTANCE.create(pokemonType);
}
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public PokemonType getPokemonType(@PathParam("id") String id) {
// returns to the client the resource with the desired id
return PokemonTypeDao.INSTANCE.getPokemonType(id);
}
@PUT
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public PokemonType updatePokemonType(@PathParam("id") String id, PokemonType toReplace) {
// check if the desired id and the id of the resource to be replaced match
if (id == null || !id.equals(toReplace.id))
throw new BadRequestException("Id mismatch.");
// return the object replaced
return PokemonTypeDao.INSTANCE.replace(toReplace);
}
@DELETE
@Path("/{id}")
public void deletePokemonType(@PathParam("id") String id) {
// delete the resource with the desired id
PokemonTypeDao.INSTANCE.delete(id);
}
}
\ No newline at end of file
package nl.utwente.mod4.pokemon.routes;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import nl.utwente.mod4.pokemon.dao.TrainerDao;
import nl.utwente.mod4.pokemon.model.ResourceCollection;
import nl.utwente.mod4.pokemon.model.Trainer;
@Path("/trainers")
public class TrainerRoute {
@GET
@Produces(MediaType.APPLICATION_JSON)
public ResourceCollection<Trainer> getTrainers(
@QueryParam("pageSize") int pageSize,
@QueryParam("pageNumber") int pageNumber
) {
// initializes page size and page number for request
int ps = pageSize > 0 ? pageSize : Integer.MAX_VALUE;
int pn = pageNumber > 0 ? pageNumber : 1;
// request the desired page to the Data Access Object
Trainer[] resources = TrainerDao.INSTANCE.getTrainers(ps, pn).toArray(new Trainer[0]);
// updates the total and page size for the response
int total = TrainerDao.INSTANCE.getTotalTrainers();
ps = resources == null ? 0 : resources.length;
// returns a collection object containing the requested resources
return new ResourceCollection<>(resources, ps, pn, total);
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Trainer createTrainer(Trainer pokemonType) {
// creates a resource based on to the JSON object sent by the client
return TrainerDao.INSTANCE.create(pokemonType);
}
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Trainer getTrainer(@PathParam("id") String id) {
// returns to the client the resource with the desired id
return TrainerDao.INSTANCE.getTrainer(id);
}
@PUT
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Trainer updateTrainer(@PathParam("id") String id, Trainer toReplace) {
// check if the desired id and the id of the resource to be replaced match
if (id == null || !id.equals(toReplace.id))
throw new BadRequestException("Id mismatch.");
// return the object replaced
return TrainerDao.INSTANCE.replace(toReplace);
}
@DELETE
@Path("/{id}")
public void deleteTrainer(@PathParam("id") String id) {
// delete the resource with the desired id
TrainerDao.INSTANCE.delete(id);
}
}
[
{
"name": "Red",
"id": "1",
"created": "24/04/2024",
"lastUpDate": "24/04/2024",
"profileUrl": "https://archives.bulbagarden.net/media/upload/thumb/d/d3/Lets_Go_Pikachu_Eevee_Red.png/500px-Lets_Go_Pikachu_Eevee_Red.png"
},
{
"name": "Blue",
"id": "2",
"created": "24/04/2024",
"lastUpDate": "24/04/2024",
"profileUrl": "https://archives.bulbagarden.net/media/upload/thumb/1/1a/Lets_Go_Pikachu_Eevee_Blue.png/320px-Lets_Go_Pikachu_Eevee_Blue.png"
},
{
"name": "Misty",
"id": "3",
"created": "24/04/2024",
"lastUpDate": "24/04/2024",
"profileUrl": "https://archives.bulbagarden.net/media/upload/thumb/f/f6/Lets_Go_Pikachu_Eevee_Misty.png/367px-Lets_Go_Pikachu_Eevee_Misty.png"
},
{
"name": "Brock",
"id": "4",
"created": "24/04/2024",
"lastUpDate": "24/04/2024",
"profileUrl": "https://archives.bulbagarden.net/media/upload/thumb/a/a6/Lets_Go_Pikachu_Eevee_Brock.png/243px-Lets_Go_Pikachu_Eevee_Brock.png"
}
]
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<welcome-file-list>
<welcome-file>trainers.html</welcome-file>
<!-- <welcome-file>pokemonTypes.html</welcome-file>-->
</welcome-file-list>
</web-app>
\ No newline at end of file
src/main/webapp/favicon.ico

33.7 KiB