public JSONObject updateTest(JSONRPC2Request req, HttpServletRequest request)
      throws JSONRPC2Error, Exception {
    JSONObject jsonUpdateTest = new JSONObject();

    // get question
    Map<String, Object> params = req.getNamedParams();
    NamedParamsRetriever np = new NamedParamsRetriever(params);
    Map<String, Object> tParams = np.getMap("test");
    NamedParamsRetriever testNp = new NamedParamsRetriever(tParams);
    int testId = testNp.getInt("id");
    String testName = testNp.getString("name");
    int tOwnerId = testNp.getInt("ownerId");

    User u = getCurrentUser(request);

    // check for privileges
    if (u.getId() == tOwnerId) {
      // update question in database
      Test testUpdated = TestController.updateTest(testId, testName);

      // return result
      jsonUpdateTest.put("updatedTest", testUpdated.toJSONObject());

      return jsonUpdateTest;
    } else {
      throw new Exception("no privileges");
    }
  }
  public JSONObject removeTestQuestion(JSONRPC2Request req, HttpServletRequest request)
      throws Exception, SQLException {
    JSONObject jsonResult = new JSONObject();

    // set retrievers
    Map<String, Object> params = req.getNamedParams();
    NamedParamsRetriever np = new NamedParamsRetriever(params);
    Map<String, Object> tParams = np.getMap("test");
    NamedParamsRetriever testNp = new NamedParamsRetriever(tParams);
    Map<String, Object> qParams = np.getMap("question");
    NamedParamsRetriever questNp = new NamedParamsRetriever(qParams);

    MySQLDAO dao = new MySQLDAO();
    Test t = dao.loadTest(testNp.getInt(("id")));
    dao = new MySQLDAO();
    Question q = dao.loadQuestion(questNp.getInt("id"));

    User u = getCurrentUser(request);

    if (u.getId() == t.getOwnerId()) {
      dao = new MySQLDAO();
      t = dao.deleteTestQuestion(t, q);
    } else {
      throw new Exception("no privileges : you do not own this test");
    }

    JSONObject jsonTest = t.toJSONObject();
    JSONObject jsonQuestion = q.toJSONObject();

    jsonResult.put("test", jsonTest);
    jsonResult.put("question", jsonQuestion);

    return jsonResult;
  }
  public JSONObject loadTestAndAllQuestions(JSONRPC2Request req, HttpServletRequest request)
      throws Exception {
    JSONObject jsonResult = new JSONObject();
    JSONArray jsonAllQuestions = new JSONArray();
    JSONObject jsonTest = new JSONObject();

    // retrievers
    Map<String, Object> params = req.getNamedParams();
    NamedParamsRetriever np = new NamedParamsRetriever(params);
    Map<String, Object> tParams = np.getMap("test");
    NamedParamsRetriever testNp = new NamedParamsRetriever(tParams);
    int tid = testNp.getInt("id");

    // get data from DB
    MySQLDAO dao = new MySQLDAO();
    Test t = dao.loadTest(tid);
    QuestionsList allQuestions = QuestionController.loadAllQuestions();

    // set result
    jsonTest = t.toJSONObject();
    jsonAllQuestions = allQuestions.toJSONArray();
    jsonResult.put("test", jsonTest);
    jsonResult.put("questions", jsonAllQuestions);

    return jsonResult;
  }
  public JSONObject addTestQuestion(JSONRPC2Request req, HttpServletRequest request)
      throws Exception, SQLException {
    JSONObject jsonResult = new JSONObject();

    // set retrievers
    Map<String, Object> params = req.getNamedParams();
    NamedParamsRetriever np = new NamedParamsRetriever(params);
    Map<String, Object> tParams = np.getMap("test");
    NamedParamsRetriever testNp = new NamedParamsRetriever(tParams);
    Map<String, Object> qParams = np.getMap("question");
    NamedParamsRetriever questNp = new NamedParamsRetriever(qParams);
    int tid = testNp.getInt(("id"));
    int qid = questNp.getInt(("id"));

    // load test and question
    MySQLDAO dao = new MySQLDAO();
    Test t = dao.loadTest(tid);
    dao = new MySQLDAO();
    Question q = dao.loadQuestion(qid);

    // load current user
    User u = getCurrentUser(request);

    if (u.getId() == t.getOwnerId()) {
      // check for duplicates
      boolean duplicate = false;
      for (Question quest : t.getQuestions()) {
        if (quest.getId() == qid) {
          duplicate = true;
        }
      }
      if (!duplicate) {
        dao = new MySQLDAO();
        t = dao.saveTestQuestion(t, q);
      }
    } else {
      throw new Exception("no privileges : you do not own this test!");
    }

    // set result
    JSONObject jsonTest = t.toJSONObject();
    JSONObject jsonQuestion = q.toJSONObject();

    jsonResult.put("test", jsonTest);
    jsonResult.put("question", jsonQuestion);

    return jsonResult;
  }
  public JSONObject addTest(JSONRPC2Request req, HttpServletRequest request) throws JSONRPC2Error {
    // json object for the result
    JSONObject jsonAddTest = new JSONObject();

    // get test
    Map<String, Object> params = req.getNamedParams();
    NamedParamsRetriever np = new NamedParamsRetriever(params);
    Map<String, Object> tParams = np.getMap("test");
    NamedParamsRetriever testNp = new NamedParamsRetriever(tParams);
    String name = testNp.getString("name");

    // get user
    User u = getCurrentUser(request);
    int userID = (int) u.getId();

    // return test if added successfully
    Test t = TestController.addTest(name, userID);
    System.out.println("add test: " + t.toJSONObject().toString());
    jsonAddTest.put("test", t.toJSONObject());
    return jsonAddTest;
  }