From 6860ce3eb4b15ade8f283bcd74759c3186f686f7 Mon Sep 17 00:00:00 2001 From: r-monti <127495690+r-monti@users.noreply.github.com> Date: Tue, 2 Jan 2024 22:25:13 +0100 Subject: [PATCH] Completed the manipolation of the exercise. TODO: -Dynamic CSS loading based on the exercise -CSS of every exercise --- pom.xml | 11 +- .../java/controller/ExerciseController.java | 24 ++++ .../java/model/DAO/DAOExerciseGlossary.java | 10 +- .../java/model/entity/ExerciseGlossary.java | 14 ++- .../service/exercise/ExerciseManager.java | 21 ++++ .../exercise/ExerciseManagerInterface.java | 29 +++++ src/main/webapp/JS/exercise.js | 114 +++++++++++++----- src/main/webapp/JSP/exercise.jsp | 23 +++- src/main/webapp/JSP/exerciseTest.jsp | 11 ++ 9 files changed, 211 insertions(+), 46 deletions(-) create mode 100644 src/main/java/controller/ExerciseController.java create mode 100644 src/main/java/model/service/exercise/ExerciseManager.java create mode 100644 src/main/java/model/service/exercise/ExerciseManagerInterface.java create mode 100644 src/main/webapp/JSP/exerciseTest.jsp diff --git a/pom.xml b/pom.xml index 575663b..5852489 100644 --- a/pom.xml +++ b/pom.xml @@ -20,8 +20,12 @@ central - https://repo1.maven.org/maven2 + Maven Repository Switchboard + https://repo1.maven.org/maven2/ default + + false + @@ -76,6 +80,11 @@ javax.servlet-api 3.1.0 + + org.apache.commons + commons-text + 1.3 + diff --git a/src/main/java/controller/ExerciseController.java b/src/main/java/controller/ExerciseController.java new file mode 100644 index 0000000..99bae21 --- /dev/null +++ b/src/main/java/controller/ExerciseController.java @@ -0,0 +1,24 @@ +package controller; + +import model.entity.ExerciseGlossary; +import model.service.exercise.ExerciseManager; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +import org.apache.commons.text.StringEscapeUtils; + +@WebServlet("/exerciseController") +public class ExerciseController extends HttpServlet { + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + ExerciseManager em = new ExerciseManager(); + String id = request.getParameter("exerciseID"); + ExerciseGlossary ex = em.getExercise(Integer.parseInt(id)); + request.getSession().setAttribute("exercise", ex); + response.sendRedirect(request.getContextPath() + "/JSP/exercise.jsp"); + } +} \ No newline at end of file diff --git a/src/main/java/model/DAO/DAOExerciseGlossary.java b/src/main/java/model/DAO/DAOExerciseGlossary.java index adda940..2e427b9 100644 --- a/src/main/java/model/DAO/DAOExerciseGlossary.java +++ b/src/main/java/model/DAO/DAOExerciseGlossary.java @@ -2,7 +2,6 @@ import model.entity.ExerciseGlossary; -import com.google.gson.Gson; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -21,7 +20,6 @@ public class DAOExerciseGlossary { * @throws SQLException if there is any SQL related error */ private static ExerciseGlossary extractExerciseFromResultSet(ResultSet resultSet) throws SQLException { - Gson g = new Gson(); ExerciseGlossary exercise = new ExerciseGlossary(); exercise.setIdExercise(resultSet.getInt("ID_exercise")); exercise.setExerciseName(resultSet.getString("ExerciseName")); @@ -29,8 +27,8 @@ private static ExerciseGlossary extractExerciseFromResultSet(ResultSet resultSet exercise.setType(resultSet.getString("Type")); exercise.setDifficulty(resultSet.getInt("Difficulty")); exercise.setTarget(resultSet.getString("Target")); - exercise.setInitialState(g.fromJson(resultSet.getString("InitialState"), Object.class)); - exercise.setSolution(g.fromJson(resultSet.getString("Solution"), Object.class)); + exercise.setInitialState(resultSet.getString("InitialState")); + exercise.setSolution(resultSet.getString("Solution")); return exercise; } @@ -41,7 +39,7 @@ private static ExerciseGlossary extractExerciseFromResultSet(ResultSet resultSet * @param code the ExerciseID code to search for. * @return the Exercise if it is found, else null. */ - public ExerciseGlossary getExerciseByCode(String code) { + public ExerciseGlossary getExerciseByCode(int code) { String query = "SELECT * FROM exercise_glossary WHERE ID_exercise = ?"; Connection connection = null; PreparedStatement preparedStatement = null; @@ -50,7 +48,7 @@ public ExerciseGlossary getExerciseByCode(String code) { try { connection = DAOConnection.getConnection(); preparedStatement = connection.prepareStatement(query); - preparedStatement.setString(1, code); + preparedStatement.setInt(1, code); resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { diff --git a/src/main/java/model/entity/ExerciseGlossary.java b/src/main/java/model/entity/ExerciseGlossary.java index cd4e79d..6dabfb4 100644 --- a/src/main/java/model/entity/ExerciseGlossary.java +++ b/src/main/java/model/entity/ExerciseGlossary.java @@ -7,8 +7,8 @@ public class ExerciseGlossary { private String exerciseDescription; private String type; private int difficulty; - private Object initialState; - private Object solution; + private String initialState; + private String solution; private String target; // Getter and Setter methods @@ -53,13 +53,13 @@ public void setDifficulty(int difficulty) { this.difficulty = difficulty; } - public Object getInitialState() { return initialState; } + public String getInitialState() { return initialState; } - public void setInitialState(Object initialState) { this.initialState = initialState; } + public void setInitialState(String initialState) { this.initialState = initialState; } - public Object getSolution() { return solution; } + public String getSolution() { return solution; } - public void setSolution(Object solution) { this.solution = solution; } + public void setSolution(String solution) { this.solution = solution; } public String getTarget() { return target; @@ -68,5 +68,7 @@ public String getTarget() { public void setTarget(String target) { this.target = target; } + + } diff --git a/src/main/java/model/service/exercise/ExerciseManager.java b/src/main/java/model/service/exercise/ExerciseManager.java new file mode 100644 index 0000000..b3f6085 --- /dev/null +++ b/src/main/java/model/service/exercise/ExerciseManager.java @@ -0,0 +1,21 @@ +package model.service.exercise; + +import model.DAO.DAOExerciseGlossary; +import model.entity.ExerciseGlossary; + +import java.sql.Blob; + +public class ExerciseManager implements ExerciseManagerInterface { + private DAOExerciseGlossary dao = new DAOExerciseGlossary(); + public ExerciseGlossary getExercise(int exerciseID) { + return dao.getExerciseByCode(exerciseID); + } + + public Blob getExecution(int exerciseID, int userID) { + return null; + } + + public void saveExecution(int userID) { + + } +} diff --git a/src/main/java/model/service/exercise/ExerciseManagerInterface.java b/src/main/java/model/service/exercise/ExerciseManagerInterface.java new file mode 100644 index 0000000..c958ee2 --- /dev/null +++ b/src/main/java/model/service/exercise/ExerciseManagerInterface.java @@ -0,0 +1,29 @@ +package model.service.exercise; + +import model.entity.ExerciseGlossary; + +import java.sql.Blob; + +public interface ExerciseManagerInterface { + + /** + * Preleva un esercizio dal Database + * @param exerciseID l'id dell'esercizio + * @return l'oggetto esercizio + */ + ExerciseGlossary getExercise(int exerciseID); + + /** + * Preleva un'esecuzione di un esercizio + * @param exerciseID l'id dell'esercizio + * @param userID l'id dell'utente che ha svolto l'esercizio + * @return un BLOB contenente l'esecuzione dell'esercizio + */ + Blob getExecution(int exerciseID, int userID); + + /** + * Salva l'esecuzione di un esercizio + * @param userID l'id dell'utente che ha eseguito l'esercizio + */ + void saveExecution(int userID); +} diff --git a/src/main/webapp/JS/exercise.js b/src/main/webapp/JS/exercise.js index 8b3ba7b..4b64fe7 100644 --- a/src/main/webapp/JS/exercise.js +++ b/src/main/webapp/JS/exercise.js @@ -6,14 +6,35 @@ const CROSSWORD = "CROSSWORD" const COMPLETETEXT = "COMPLETETEXT" const RIGHTTEXT = "RIGHTTEXT" const IMAGESINAROW = 2 +const exercise = $("#exerciseInfo") + +const EXERCISETYPE = exercise.data("type"); const exerciseDiv = $("#exerciseDiv"); -$(document).ready(()=>{ - $("#backDiv").click(() => redirect("home")); - $("#notificationDiv").click(() => redirect("message.jsp")); //TODO: mettere i redirect giusti - loadExercise(/*TODO: mettere qui la tipologia di esercizio*/); -}) +function startUp(exerciseIS){ + $(document).ready(()=>{ + const EXERCISEINITIALSTATE = parseJSON(exerciseIS); + $("#backDiv").click(() => redirect("home")); + $("#notificationDiv").click(() => redirect("message.jsp")); //TODO: mettere i redirect giusti + loadExercise(EXERCISETYPE, EXERCISEINITIALSTATE); + }) +} + +function parseJSON(json) { + try { + const jsonData = JSON.parse(json); + return jsonData; + } catch (jsonError) { + try { + const jsObject = eval('(' + json + ')'); + return jsObject; + } catch (objectError) { + return json; + } + } +} + function redirect(where){ if (where === "home"){ @@ -29,37 +50,55 @@ function redirect(where){ } } -function loadExercise(type){ - switch (type) { +function loadExercise(type, initialState){ + switch (type.toUpperCase()) { case READTEXT: - loadReadText(); + loadReadText(initialState); + //loadCSS("path/al/tuo/readtext.css"); break; case READIMAGES: - loadReadImages(); + loadReadImages(initialState); + //loadCSS("path/al/tuo/readtext.css"); break; case IMAGESTOTEXT: - loadImagesToText(); + loadImagesToText(initialState); + //loadCSS("path/al/tuo/readtext.css"); break; case TEXTTOIMAGES: - loadTextToImages(); + loadTextToImages(initialState); + //loadCSS("path/al/tuo/readtext.css"); break; case CROSSWORD: - loadCrossword(); + loadCrossword(initialState); + //loadCSS("path/al/tuo/readtext.css"); break; case COMPLETETEXT: - loadCompleteText(); + loadCompleteText(initialState); + //loadCSS("path/al/tuo/readtext.css"); break; case RIGHTTEXT: - loadRightText(); + loadRightText(initialState); + //loadCSS("path/al/tuo/readtext.css"); break; default: - //TODO: Errore e torna indietro + setTimeout(()=> { + alert("Ci scusiamo del disagio! Le consigliamo di riprovare tra pochi minuti"); + window.location.href = "homepagepatient" //TODO:mettere la homepage del paziente + }, 3000); } } -function loadReadText(){ +function loadCSS(cssPath) { + let link = document.createElement("link"); + link.rel = "stylesheet"; + link.type = "text/css"; + link.href = cssPath; + document.head.appendChild(link); +} + +function loadReadText(initialState){ - let text = $("

").text(/*TODO: inserire il testo*/); + let text = $("

").text(initialState); let textDiv = $("

").attr("id", "textDiv").append(text); let button = $("