<commit_before>package no.steria.quizzical;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class AdminServlet extends SecuredServlet {

	private Quiz quiz;
	private MongoQuizDao mongoQuizDao;
	private MongoUserDao mongoUserDao;
	private MongoResponseDao mongoResponseDao;
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ObjectMapper mapper = new ObjectMapper();		 
		JsonNode rootNode = mapper.readTree(req.getReader().readLine());
		
		int quizId = -1;
		String quizName = "", quizDesc = "", submitMsg = "";
		
		List<Question> questions = new ArrayList<Question>();
		
		Iterator<Entry<String,JsonNode>> allEntries = rootNode.getFields();
		
		while(allEntries.hasNext()){
			Entry<String, JsonNode> entry = allEntries.next();
			if(entry.getKey().equals("questions")){
				Iterator<JsonNode> questionEntries = entry.getValue().getElements();
				
				while(questionEntries.hasNext()){
					JsonNode jsonQuestion = questionEntries.next();
					Iterator<Entry<String, JsonNode>> questionFields = jsonQuestion.getFields();
					
					int questionId = 0, correctAnswer = 0;
					String questionText = "";
					
					List<Alternative> alternatives = new ArrayList<Alternative>();
					
					while(questionFields.hasNext()){
						Entry<String, JsonNode> questionField = questionFields.next();
						
						if (questionField.getKey().equals("id")){
							questionId = questionField.getValue().asInt();
						} else if (questionField.getKey().equals("text")){
							questionText = questionField.getValue().asText();
						} else if (questionField.getKey().equals("alternatives")){
							Iterator<JsonNode> jsonAlternatives = questionField.getValue().getElements();
							
							while(jsonAlternatives.hasNext()){
								JsonNode jsonAlternative = jsonAlternatives.next();
								Iterator<Entry<String, JsonNode>> alternativeField = jsonAlternative.getFields();
								
								int alternativeId = 0;
								String alternativeText = "";
								
								while(alternativeField.hasNext()){
									Entry<String, JsonNode> alternativeKeys = alternativeField.next();
									
									if(alternativeKeys.getKey().equals("aid")){
										alternativeId = alternativeKeys.getValue().asInt();
									} else if (alternativeKeys.getKey().equals("atext")){
										alternativeText = alternativeKeys.getValue().asText();
									}	
								}
								Alternative alternative = new Alternative(alternativeId, alternativeText);
								alternatives.add(alternative);
							}
						} else if (questionField.getKey().equals("answer")){
							correctAnswer = questionField.getValue().asInt();
						}
					}
					Question question = new Question(questionId, questionText, alternatives, correctAnswer);
					questions.add(question);
				}
				
			} else if(entry.getKey().equals("quizId")){
				quizId = entry.getValue().asInt();
			} else if(entry.getKey().equals("quizName")){
				quizName = entry.getValue().asText();
			} else if(entry.getKey().equals("quizDesc")){
				quizDesc = entry.getValue().asText();
			} else if(entry.getKey().equals("submitMsg")){
				submitMsg = entry.getValue().asText();
			}
		}
		quiz = new Quiz(quizId, quizName, quizDesc, submitMsg, questions, true);
		mongoQuizDao.insertQuizIntoDB(quiz, mongoUserDao.getUser((String) req.getSession().getAttribute("username")).getUserId());
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		
		ObjectMapper mapper = new ObjectMapper();
		PrintWriter writer = resp.getWriter();
		
		int mode = Integer.parseInt(req.getParameter("mode"));
		
		if(mode == 2){
			retrieveQuizzesByUserId(req, resp, mapper, writer);
		}else if(mode == 3){
			retrieveNumberOfResponsesByQuizId(req, resp, mapper, writer);
		}else if(mode == 4){
			retrieveRandomWinnerOfQuiz(req, resp, mapper, writer);
		}else if(mode == 5){
			deleteQuiz(req);
		}else if(mode == 6){
			changeActiveStatusOfQuiz(req);
		}else if(mode == 7){
			retrieveListOfRespondents(req, resp, mapper, writer);
		}else if(mode == 8){
			retrieveSessionUser(req, resp, mapper, writer);
		}else if(mode == 9){
			req.getSession().invalidate();
		}else if(mode == 12){
			retriveQuizzes(req, resp, mapper, writer);<commit_msg>Made a small bit of adminServlet more readable<commit_after>package no.steria.quizzical;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class AdminServlet extends SecuredServlet {

	private Quiz quiz;
	private MongoQuizDao mongoQuizDao;
	private MongoUserDao mongoUserDao;
	private MongoResponseDao mongoResponseDao;
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ObjectMapper mapper = new ObjectMapper();		 
		JsonNode rootNode = mapper.readTree(req.getReader().readLine());
		
		int quizId = -1;
		String quizName = "", quizDesc = "", submitMsg = "";
		
		List<Question> questions = new ArrayList<Question>();
		
		Iterator<Entry<String,JsonNode>> allEntries = rootNode.getFields();
		
		while(allEntries.hasNext()){
			Entry<String, JsonNode> entry = allEntries.next();
			if(entry.getKey().equals("questions")){
				Iterator<JsonNode> questionEntries = entry.getValue().getElements();
				
				while(questionEntries.hasNext()){
					JsonNode jsonQuestion = questionEntries.next();
					Iterator<Entry<String, JsonNode>> questionFields = jsonQuestion.getFields();
					
					int questionId = 0, correctAnswer = 0;
					String questionText = "";
					
					List<Alternative> alternatives = new ArrayList<Alternative>();
					
					while(questionFields.hasNext()){
						Entry<String, JsonNode> questionField = questionFields.next();
						
						if (questionField.getKey().equals("id")){
							questionId = questionField.getValue().asInt();
						} else if (questionField.getKey().equals("text")){
							questionText = questionField.getValue().asText();
						} else if (questionField.getKey().equals("alternatives")){
							Iterator<JsonNode> jsonAlternatives = questionField.getValue().getElements();
							
							while(jsonAlternatives.hasNext()){
								JsonNode jsonAlternative = jsonAlternatives.next();
								Iterator<Entry<String, JsonNode>> alternativeField = jsonAlternative.getFields();
								
								int alternativeId = 0;
								String alternativeText = "";
								
								while(alternativeField.hasNext()){
									Entry<String, JsonNode> alternativeKeys = alternativeField.next();
									
									if(alternativeKeys.getKey().equals("aid")){
										alternativeId = alternativeKeys.getValue().asInt();
									} else if (alternativeKeys.getKey().equals("atext")){
										alternativeText = alternativeKeys.getValue().asText();
									}	
								}
								Alternative alternative = new Alternative(alternativeId, alternativeText);
								alternatives.add(alternative);
							}
						} else if (questionField.getKey().equals("answer")){
							correctAnswer = questionField.getValue().asInt();
						}
					}
					Question question = new Question(questionId, questionText, alternatives, correctAnswer);
					questions.add(question);
				}
				
			} else if(entry.getKey().equals("quizId")){
				quizId = entry.getValue().asInt();
			} else if(entry.getKey().equals("quizName")){
				quizName = entry.getValue().asText();
			} else if(entry.getKey().equals("quizDesc")){
				quizDesc = entry.getValue().asText();
			} else if(entry.getKey().equals("submitMsg")){
				submitMsg = entry.getValue().asText();
			}
		}
		quiz = new Quiz(quizId, quizName, quizDesc, submitMsg, questions, true);
		String username = (String) req.getSession().getAttribute("username");
		mongoQuizDao.insertQuizIntoDB(quiz, mongoUserDao.getUser(username).getUserId());
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		
		ObjectMapper mapper = new ObjectMapper();
		PrintWriter writer = resp.getWriter();
		
		int mode = Integer.parseInt(req.getParameter("mode"));
		
		if(mode == 2){
			retrieveQuizzesByUserId(req, resp, mapper, writer);
		}else if(mode == 3){
			retrieveNumberOfResponsesByQuizId(req, resp, mapper, writer);
		}else if(mode == 4){
			retrieveRandomWinnerOfQuiz(req, resp, mapper, writer);
		}else if(mode == 5){
			deleteQuiz(req);
		}else if(mode == 6){
			changeActiveStatusOfQuiz(req);
		}else if(mode == 7){
			retrieveListOfRespondents(req, resp, mapper, writer);
		}else if(mode == 8){
			retrieveSessionUser(req, resp, mapper, writer);
		}else if(mode == 9){
			req.getSession().invalidate();
		}else if(mode == 12){
			retriveQuizzes(req, resp, mapper, writer);
{'fraction_non_alphanumeric': 0.1314843348741654, 'fraction_numerical': 0.0030816640986132513, 'mean_word_length': 4.140443505807814, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6714752', 'n_tokens_mistral': 3569, 'n_tokens_neox': 2793, 'n_words': 557}