Skip to content

Commit

Permalink
Merge pull request #53 from pastore99/merging-43-44-46
Browse files Browse the repository at this point in the history
Merging 43 44 46 to development
  • Loading branch information
r-monti authored Jan 8, 2024
2 parents c1478a2 + 6da5ba4 commit aa277f6
Show file tree
Hide file tree
Showing 39 changed files with 1,305 additions and 281 deletions.
28 changes: 22 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@
</dependency>


<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
Expand Down Expand Up @@ -96,6 +90,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>

<dependency>
<groupId>com.mockrunner</groupId>
<artifactId>mockrunner-servlet</artifactId>
Expand All @@ -119,12 +119,27 @@
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.microsoft.cognitiveservices.speech</groupId>
<artifactId>client-sdk</artifactId>
<version>1.34.0</version>
</dependency>


<dependency>
<groupId>info.debatty</groupId>
<artifactId>java-string-similarity</artifactId>
<version>2.0.0</version>
</dependency>

</dependencies>
<build>
<plugins>
Expand Down Expand Up @@ -213,6 +228,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.1</version>
<configuration>
<source>21</source>
<target>21</target>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ public static void main(String[] args) {
//r.invitePatient(9, "[email protected]", "Raffaele", "Monti");

ScheduleManager manager = new ScheduleManager();
String date = "2024-01-11";
String date = "2024-02-10";
String time = "10:00-11:00";
if(manager.checkData(9, date, time)) {
System.out.println("inif");
manager.createNewSchedule(9, date, time); //funziona
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/controller/ChangeUserInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
} else {
risultato = "Dati personali non aggiornati, email gia' utilizzata";
}
response.sendRedirect("/TalkAID_war_exploded/JSP/Cambio_dati.jsp?risultato=" + risultato);
response.sendRedirect("JSP/userInfoChange.jsp?risultato=" + risultato);
}

private boolean updatePersonalInfo(HttpServletRequest request, int userId, UserRegistry userRegistry) {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/controller/ExerciseController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;


@WebServlet("/exerciseController")
public class ExerciseController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
ExerciseManager em = new ExerciseManager();
String id = request.getParameter("exerciseID");
//TODO: Aggiungi alla sessione insertion date @michele
String insertionDate = request.getParameter("insertionDate");
request.getSession().setAttribute("insertionDate", Date.valueOf(insertionDate));
request.getSession().setAttribute("exerciseID", Integer.parseInt(id));

ExerciseGlossary ex = em.getExercise(Integer.parseInt(id));
request.getSession().setAttribute("exercise", ex);
response.sendRedirect(request.getContextPath() + "/JSP/exercise.jsp");
Expand Down
210 changes: 210 additions & 0 deletions src/main/java/controller/ExerciseEvaluator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
package controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import info.debatty.java.stringsimilarity.Levenshtein;
import model.entity.ExerciseGlossary;
import model.service.exercise.ExerciseManager;
import model.service.exercise.SpeechRecognition;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.*;
import java.lang.reflect.Type;
import java.sql.Blob;
import java.sql.Date;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;


@WebServlet("/exerciseEvaluator")
@MultipartConfig
public class ExerciseEvaluator extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
HttpSession s = request.getSession();
String contentType = request.getContentType();
ExerciseManager em = new ExerciseManager();

int exerciseId = (int) s.getAttribute("exerciseID");
int userId = (int) s.getAttribute("id");
Date d = (Date) s.getAttribute("insertionDate");

int score;

if ("application/json".equals(contentType)) {
score = evaluateNoAudio(exerciseId, userId, d);
} else {
try {
score = evaluateAudio(exerciseId, userId, d);
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
}
em.saveEvaluation(userId, exerciseId, d, score);
}


private int evaluateNoAudio(int exerciseId, int userId, Date date){
Gson gson = new Gson();

ExerciseGlossary baseExercise = new ExerciseManager().getExercise(exerciseId);
String executionJSON = getJSONfromBlob(exerciseId, userId, date);
String type = baseExercise.getType();

int score;

switch (type) {
case "CROSSWORD" -> {
String[][] solution = gson.fromJson(baseExercise.getSolution(), String[][].class);
String[][] execution = gson.fromJson(executionJSON, String[][].class);

score = evaluateCrossword(execution, solution);
}
case "IMAGESTOTEXT", "TEXTTOIMAGES" -> {
Type solutionType = new TypeToken<Map<String, String>>() {}.getType();
Map<String, String> solution = gson.fromJson(baseExercise.getSolution(), solutionType);
Map<String, String> execution = gson.fromJson(executionJSON, solutionType);

score = evaluateITTnTTI(execution, solution);
}
case "RIGHTTEXT" -> {
Type solutionType = new TypeToken<Set<String>>(){}.getType();
Set<String> solution = gson.fromJson(baseExercise.getSolution(), solutionType);
Set<String> execution = gson.fromJson(executionJSON, solutionType);

score = evaluateRightText(execution, solution);
}
default -> throw new IllegalStateException("Unexpected value: " + type);
}

return score;
}

private String getJSONfromBlob(int exerciseId, int userId, Date d){
Blob executionBlob = new ExerciseManager().getExecution(exerciseId, userId, d);
StringBuilder stringBuilder = new StringBuilder();

try (InputStream binaryStream = executionBlob.getBinaryStream();
InputStreamReader inputStreamReader = new InputStreamReader(binaryStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {

String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}

} catch (SQLException | IOException e) {
e.printStackTrace();
}

return stringBuilder.toString();
}

private int evaluateRightText(Set<String> execution, Set<String> solution) {
double right = 0;
int total = solution.size();
for (String s : solution) {
if (execution.contains(s.toUpperCase())){
right++;
}
}
return (int)((right /total)*100);
}

private int evaluateITTnTTI(Map<String, String> execution, Map<String, String> solution) {
double right = 0;
int total = solution.size();

for (Map.Entry<String, String> entry : execution.entrySet()) {
String k = entry.getKey();
String executionValue = entry.getValue();
String solutionValue = solution.get(k);

if (executionValue != null) {
if (executionValue.equals(solutionValue.toUpperCase())) {
right++;
}
}
}
return (int)((right /total)*100);
}

private int evaluateCrossword(String[][] execution, String[][] solution) {
double right = 0;
int total = 0;

for (int i = 0; i < execution.length; i++) {
for (int j = 0; j < execution[0].length; j++) {
if (!execution[i][j].equals("#")){
if (execution[i][j].equals(solution[i][j].toUpperCase())) {
right++;
}
total++;
}
}
}
return (int)((right /total)*100);
}

private int evaluateAudio(int exerciseId, int userId, Date d) throws IOException, ExecutionException, InterruptedException {
InputStream audioExecution = getAudiofromBlob(exerciseId, userId, d);
String audioText = null;
if (audioExecution!=null){
SpeechRecognition s = new SpeechRecognition();
audioText = s.azureSTT(audioExecution);
}

if(audioText != null) {
ExerciseGlossary baseExercise = new ExerciseManager().getExercise(exerciseId);
String exerciseType = baseExercise.getType();
Gson g = new Gson();

if (baseExercise.getType().equals("READTEXT")){
Levenshtein l = new Levenshtein();
String solution = g.fromJson(baseExercise.getInitialState(), String.class);
double distance = l.distance(solution, audioText);
double result = ((solution.length()-distance)/solution.length())*100;

return (int) Math.round(result);

}else if(exerciseType.equals("READIMAGES")){
Type solutionType = new TypeToken<Set<String>>(){}.getType();
Set<String> solution = g.fromJson(baseExercise.getSolution(), solutionType);
String execution = audioText.toLowerCase().replaceAll("[^a-zA-Z0-9\\s]", "");

double right = 0;
double total = solution.size();

String[] wordsArray = execution.split("\\s+");
Set<String> wordSet = new HashSet<>(Arrays.asList(wordsArray));

for(String w: wordSet){
if(solution.contains(w)){
right++;
}
}

double result = ((right/total)*100);

return (int) Math.round(result);
}
}

return 0;
}

private InputStream getAudiofromBlob(int exerciseId, int userId, Date d){
Blob executionBlob = new ExerciseManager().getExecution(exerciseId, userId, d);
try (InputStream audioInputStream = executionBlob.getBinaryStream()) {
return audioInputStream;
} catch (SQLException | IOException e) {
throw new RuntimeException(e);
}
}
}
20 changes: 11 additions & 9 deletions src/main/java/controller/ExerciseLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import model.service.exercise.ExerciseManager;
import org.apache.commons.io.IOUtils;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
Expand All @@ -21,12 +22,13 @@
public class ExerciseLogger extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String contentType = request.getContentType();

if ("application/json".equals(contentType)) {
handleNoAudioExercise(request);
} else {
handleAudioExercise(request);
}
RequestDispatcher d = request.getRequestDispatcher("/exerciseEvaluator");
d.forward(request, response);
}

private void handleNoAudioExercise(HttpServletRequest request) throws IOException {
Expand All @@ -49,13 +51,13 @@ private void handleNoAudioExercise(HttpServletRequest request) throws IOExceptio
}

private void handleAudioExercise(HttpServletRequest request) throws ServletException, IOException {
InputStream audioInputStream;

try {
Part audioPart = request.getPart("audioFile");
audioInputStream = audioPart.getInputStream();
Blob audioBlob = new SerialBlob(IOUtils.toByteArray(audioInputStream));
saveInDB(request, audioBlob);
try (InputStream audioInputStream = audioPart.getInputStream()) {
Blob audioBlob = new SerialBlob(IOUtils.toByteArray(audioInputStream));
saveInDB(request, audioBlob);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
Expand All @@ -64,10 +66,10 @@ private void handleAudioExercise(HttpServletRequest request) throws ServletExcep
private boolean saveInDB(HttpServletRequest request, Blob execution){
ExerciseManager em = new ExerciseManager();
HttpSession session = request.getSession();
int userId = Integer.parseInt((String) session.getAttribute("userId"));
int exerciseId = Integer.parseInt((String) session.getAttribute("exerciseId"));
Date insertDate = Date.valueOf((String) session.getAttribute("insertDate"));
//TODO: pulisci la sessione @michele
int userId = (int) session.getAttribute("id");
int exerciseId = (int) session.getAttribute("exerciseID");
Date insertDate = (Date) session.getAttribute("insertionDate");

return em.saveExecution(userId, exerciseId, insertDate, execution);
}
}
24 changes: 24 additions & 0 deletions src/main/java/controller/InvitePatient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package controller;

import model.service.condition.ConditionManager;
import model.service.registration.Registration;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebServlet("/invitePatient")
public class InvitePatient extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
model.service.registration.Registration registration=new Registration();
registration.invitePatient((Integer) session.getAttribute("id"), request.getParameter("email"), request.getParameter("nome"), request.getParameter("cognome"));
response.sendRedirect(request.getContextPath() + "/JSP/homepageTherapist.jsp");
}

}
Loading

0 comments on commit aa277f6

Please sign in to comment.