From f683c40526686251596b1fa1499e9b7a9b4feb02 Mon Sep 17 00:00:00 2001
From: Axel <wiepke@uni-potsdam.de>
Date: Thu, 12 Jul 2018 09:03:11 +0200
Subject: [PATCH] refactor: own class for db communication of quizzes

---
 .../gf/core/management/ManagementImpl.java    | 74 ----------------
 .../controller/service/PeerAssessment.java    |  7 +-
 .../service/QuizDBCommunication.java          | 88 +++++++++++++++++++
 3 files changed, 91 insertions(+), 78 deletions(-)
 create mode 100644 gemeinsamforschen/src/main/java/unipotsdam/gf/modules/assessment/controller/service/QuizDBCommunication.java

diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/ManagementImpl.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/ManagementImpl.java
index b29ba445..1cecac2d 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/ManagementImpl.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/core/management/ManagementImpl.java
@@ -7,7 +7,6 @@ import unipotsdam.gf.core.management.project.Project;
 import unipotsdam.gf.core.management.user.User;
 import unipotsdam.gf.core.management.user.UserInterests;
 import unipotsdam.gf.core.management.user.UserProfile;
-import unipotsdam.gf.modules.assessment.controller.model.Quiz;
 import unipotsdam.gf.modules.assessment.controller.model.StudentIdentifier;
 
 import javax.annotation.ManagedBean;
@@ -200,79 +199,6 @@ public class ManagementImpl implements Management {
         return getUserByField("email", email);
     }
 
-    public Quiz getQuizByProjectGroupId(String projectId, String quizId){
-        MysqlConnect connect = new MysqlConnect();
-        connect.connect();
-        String mysqlRequest = "SELECT * FROM quiz where projectId=? AND question=?";
-        VereinfachtesResultSet vereinfachtesResultSet =
-                connect.issueSelectStatement(mysqlRequest, projectId,quizId);
-        boolean next = vereinfachtesResultSet.next();
-        String question = "";
-        ArrayList<String> correctAnswers = new ArrayList<String>();
-        ArrayList<String> incorrectAnswers = new ArrayList<String>();
-        String answer;
-        Boolean correct;
-        String mcType = "";
-        while (next) {
-            mcType = vereinfachtesResultSet.getString("mcType");
-            question = vereinfachtesResultSet.getString("question");
-            answer = vereinfachtesResultSet.getString("answer");
-            correct = vereinfachtesResultSet.getBoolean("correct");
-            if (correct){
-                correctAnswers.add(answer);
-            }else{
-                incorrectAnswers.add(answer);
-            }
-            next = vereinfachtesResultSet.next();
-        }
-        Quiz quiz = new Quiz(mcType,question, correctAnswers, incorrectAnswers);
-        connect.close();
-        return quiz;
-    }
-
-    public ArrayList<Quiz> getQuizByProjectId(String projectId) {
-        MysqlConnect connect = new MysqlConnect();
-        ArrayList<Quiz> result= new ArrayList<Quiz>();
-        connect.connect();
-        String mysqlRequest = "SELECT * FROM quiz where projectId= ?";
-        VereinfachtesResultSet vereinfachtesResultSet =
-                connect.issueSelectStatement(mysqlRequest, projectId);
-        boolean next = vereinfachtesResultSet.next();
-        String question = "";
-        ArrayList<String> correctAnswers = new ArrayList<String>();
-        ArrayList<String> incorrectAnswers = new ArrayList<String>();
-        String answer;
-        String oldQuestion="";
-        Boolean correct;
-        String mcType = "";
-        while (next) {
-            mcType = vereinfachtesResultSet.getString("mcType");
-            question = vereinfachtesResultSet.getString("question");
-            answer = vereinfachtesResultSet.getString("answer");
-            correct = vereinfachtesResultSet.getBoolean("correct");
-            if (oldQuestion.equals(question)){
-                if (correct){
-                    correctAnswers.add(answer);
-                }else{
-                    incorrectAnswers.add(answer);
-                }
-            }else{
-                result.add(new Quiz(mcType,question, correctAnswers, incorrectAnswers));
-                correctAnswers.clear();
-                incorrectAnswers.clear();
-                if (correct){
-                    correctAnswers.add(answer);
-                }else{
-                    incorrectAnswers.add(answer);
-                }
-
-            }
-            oldQuestion = question;
-            next = vereinfachtesResultSet.next();
-        }
-        return result;
-    }
-
     private User getUserByField(String field, String value) {
         MysqlConnect connect = new MysqlConnect();
         connect.connect();
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/assessment/controller/service/PeerAssessment.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/assessment/controller/service/PeerAssessment.java
index 5c88e90d..a228065a 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/assessment/controller/service/PeerAssessment.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/assessment/controller/service/PeerAssessment.java
@@ -1,10 +1,9 @@
 package unipotsdam.gf.modules.assessment.controller.service;
 
-import unipotsdam.gf.core.management.ManagementImpl;
-import unipotsdam.gf.core.management.user.User;
 import unipotsdam.gf.interfaces.IPeerAssessment;
 import unipotsdam.gf.modules.assessment.controller.model.*;
 import unipotsdam.gf.core.database.mysql.MysqlConnect;
+import unipotsdam.gf.modules.assessment.controller.service.QuizDBCommunication;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -17,12 +16,12 @@ public class PeerAssessment implements IPeerAssessment {
 
     @Override//returns one quiz
     public Quiz getQuiz(String projectId, String quizId) {
-        return new ManagementImpl().getQuizByProjectGroupId(projectId,quizId);
+        return new QuizDBCommunication().getQuizByProjectGroupId(projectId,quizId);
     }
 
     @Override //returns all quizzes in the course
     public ArrayList<Quiz> getQuiz(String projectId) {
-        return new ManagementImpl().getQuizByProjectId(projectId);
+        return new QuizDBCommunication().getQuizByProjectId(projectId);
     }
 
     @Override
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/assessment/controller/service/QuizDBCommunication.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/assessment/controller/service/QuizDBCommunication.java
new file mode 100644
index 00000000..bd09a9ff
--- /dev/null
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/assessment/controller/service/QuizDBCommunication.java
@@ -0,0 +1,88 @@
+package unipotsdam.gf.modules.assessment.controller.service;
+
+import unipotsdam.gf.core.database.mysql.MysqlConnect;
+import unipotsdam.gf.core.database.mysql.VereinfachtesResultSet;
+import unipotsdam.gf.modules.assessment.controller.model.Quiz;
+
+import javax.annotation.ManagedBean;
+import javax.annotation.Resource;
+import javax.inject.Singleton;
+import java.util.ArrayList;
+
+@ManagedBean
+@Resource
+@Singleton
+public class QuizDBCommunication {
+    public Quiz getQuizByProjectGroupId(String projectId, String quizId){
+        MysqlConnect connect = new MysqlConnect();
+        connect.connect();
+        String mysqlRequest = "SELECT * FROM quiz where projectId=? AND question=?";
+        VereinfachtesResultSet vereinfachtesResultSet =
+                connect.issueSelectStatement(mysqlRequest, projectId,quizId);
+        boolean next = vereinfachtesResultSet.next();
+        String question = "";
+        ArrayList<String> correctAnswers = new ArrayList<String>();
+        ArrayList<String> incorrectAnswers = new ArrayList<String>();
+        String answer;
+        Boolean correct;
+        String mcType = "";
+        while (next) {
+            mcType = vereinfachtesResultSet.getString("mcType");
+            question = vereinfachtesResultSet.getString("question");
+            answer = vereinfachtesResultSet.getString("answer");
+            correct = vereinfachtesResultSet.getBoolean("correct");
+            if (correct){
+                correctAnswers.add(answer);
+            }else{
+                incorrectAnswers.add(answer);
+            }
+            next = vereinfachtesResultSet.next();
+        }
+        Quiz quiz = new Quiz(mcType,question, correctAnswers, incorrectAnswers);
+        connect.close();
+        return quiz;
+    }
+
+    public ArrayList<Quiz> getQuizByProjectId(String projectId) {
+        MysqlConnect connect = new MysqlConnect();
+        ArrayList<Quiz> result= new ArrayList<Quiz>();
+        connect.connect();
+        String mysqlRequest = "SELECT * FROM quiz where projectId= ?";
+        VereinfachtesResultSet vereinfachtesResultSet =
+                connect.issueSelectStatement(mysqlRequest, projectId);
+        boolean next = vereinfachtesResultSet.next();
+        String question = "";
+        ArrayList<String> correctAnswers = new ArrayList<String>();
+        ArrayList<String> incorrectAnswers = new ArrayList<String>();
+        String answer;
+        String oldQuestion="";
+        Boolean correct;
+        String mcType = "";
+        while (next) {
+            mcType = vereinfachtesResultSet.getString("mcType");
+            question = vereinfachtesResultSet.getString("question");
+            answer = vereinfachtesResultSet.getString("answer");
+            correct = vereinfachtesResultSet.getBoolean("correct");
+            if (oldQuestion.equals(question)){
+                if (correct){
+                    correctAnswers.add(answer);
+                }else{
+                    incorrectAnswers.add(answer);
+                }
+            }else{
+                result.add(new Quiz(mcType,question, correctAnswers, incorrectAnswers));
+                correctAnswers.clear();
+                incorrectAnswers.clear();
+                if (correct){
+                    correctAnswers.add(answer);
+                }else{
+                    incorrectAnswers.add(answer);
+                }
+
+            }
+            oldQuestion = question;
+            next = vereinfachtesResultSet.next();
+        }
+        return result;
+    }
+}
-- 
GitLab