public HashSet<Answer> readPerfectAnswersForDiagnosis(int diagnosisId) {

    HashSet<Answer> answers = new HashSet<Answer>();

    try {

      establishConnection();

      Statement stmt = connection.createStatement();
      String SQL =
          "SELECT answers.* FROM answers, perfect_diagnosis_diagnoses_to_answers WHERE perfect_diagnosis_diagnoses_to_answers.answer = answers.id AND perfect_diagnosis_diagnoses_to_answers.diagnosis = "
              + diagnosisId
              + ";";

      ResultSet set = stmt.executeQuery(SQL);

      Answer answer;
      while (set.next()) {
        answer = new Answer();
        answer.setId(set.getInt("id"));

        answers.add(answer);
      }

    } catch (SQLException ex) {
      System.out.println("SQL Error read perfect answers of diagnosis");
    }

    return answers;
  }
  /**
   * Diese Methode überprüft ob ein Dependency Loop existiert.
   *
   * <p>Dafür wird die Abhängigkeitskette einer Frage Rekursiv durchgegangen und die Fragen jeweils
   * einem Set hinzugefügt.
   *
   * <p>Kommt eine Frage zwei mal vor, dann existiert ein Loop.
   *
   * @param question Die nächste zu überprüfende Frage
   * @param previousQuestions Ein Set mit allen Bisher überprüften Fragen
   * @return true fals ein Loop entsteht, false falls kein Loop entsteht
   */
  private boolean hasDependencyLoop(Question question, HashSet<Question> previousQuestions) {
    if (previousQuestions.contains(question)) {
      return true;
    }
    previousQuestions.add(question);

    final Answer answer = question.getDependsOn();
    if (answer == null) {
      return false;
    } else {
      Question nextQuestion = answer.getAnswerOf();
      if (nextQuestion == null) {
        final Answer fetchedAnswer = answerManager.get(answer.getId());
        nextQuestion = fetchedAnswer.getAnswerOf();
      }
      return hasDependencyLoop(nextQuestion, previousQuestions);
    }
  }
 /**
  * Validates Response object.
  *
  * <p>Returns List of validation errors if the object is not valid. Main rules: if field is
  * required, response answer on that field should not be empty, if field type is date, response
  * answer on that field should match DATE_REGEX.
  *
  * @return List of validation errors or null if Response object is valid
  */
 public List<ValidationError> validate() {
   List<ValidationError> errors = new ArrayList<>();
   if (answers != null) {
     for (Answer answer : answers) {
       if (answer.field.required && answer.isEmptyAnswer()) {
         errors.add(new ValidationError(answer.field.label, "Field is required."));
       }
       if (answer.field.fieldType.equals(FieldType.DATE)
           && !answer.value.isEmpty()
           && !isValidDate(answer.value)) {
         errors.add(
             new ValidationError(
                 answer.field.label, "Wrong date format. Please, use yyyy-mm-dd format."));
       }
     }
   }
   return errors.isEmpty() ? null : errors;
 }
  @Test
  public void shouldCreateAnQuestionWithAddAnswerMethod() {
    assertEquals(2, User.count());
    assertEquals(1, Question.count());
    assertEquals(1, Answer.count());
    assertNotNull(firstQuestion.answers);
    assertEquals(hans, firstQuestion.answers.get(0).author);

    firstQuestion.addAnswer(hans, "It is the moon.");
    assertEquals("It is the moon.", firstQuestion.answers.get(1).content);
  }
  public int create(Answer answer, Diagnosis diagnosis) {

    try {
      establishConnection();

      Statement stmt = connection.createStatement();
      String SQL =
          "SELECT id FROM perfect_diagnosis_diagnoses_to_answers WHERE answer = "
              + answer.getId()
              + " AND diagnosis = "
              + diagnosis.getId()
              + ";";
      ResultSet res = stmt.executeQuery(SQL);

      if (res.next()) System.out.println("Perfect diagnosis diagnoses to answers already exists!");
      else {

        stmt = connection.createStatement();
        SQL =
            "INSERT INTO perfect_diagnosis_diagnoses_to_answers (answer, diagnosis) VALUES("
                + answer.getId()
                + ", "
                + diagnosis.getId()
                + ");";

        int rows = stmt.executeUpdate(SQL, Statement.RETURN_GENERATED_KEYS);
        int id = 0;
        if (rows > 0) {
          ResultSet set = stmt.getGeneratedKeys();
          if (set.next()) id = set.getInt(1);
        }
        return id;
      }
    } catch (SQLException ex) {
      System.out.println("SQL Error create perfect diagnosis to answer");
    }

    return -1;
  }
Exemple #6
0
  /**
   * Follow notification.
   *
   * @param id the id of the notification.
   */
  public static void followNotification(int id) {
    User user = Session.user();
    Notification notification = user.getNotification(id);
    if (notification != null) {
      notification.unsetNew();
      Entry about = notification.getAbout();

      if (about instanceof Answer) {
        ActionDefinition action = reverse();
        Answer answer = (Answer) about;
        Application.question(answer.getQuestion().id());
        redirect(action.addRef("answer-" + answer.id()).toString());
      } else if (about instanceof Question) {
        Application.question(((Question) about).id());
      } else if (about instanceof Comment) {
        ActionDefinition action = reverse();
        Comment comment = (Comment) about;
        Application.question(comment.getQuestion().id());
        redirect(action.addRef("comment-" + comment.id()).toString());
      }
    } else if (!redirectToCallingPage()) {
      Application.notifications(0);
    }
  }
Exemple #7
0
 /**
  * retrieve all answers corresponding to the given poll
  *
  * @param poll a Poll
  * @return a Collection of Answer
  */
 public static List<Answer> findAnswersOfPoll(Poll poll) {
   return Answer.find("poll = ? and selected = ?", poll, Boolean.TRUE).fetch();
 }
  @Test
  public void setBestAnswerSzenario() {

    User jack = new User("Jack", "*****@*****.**", "password").save();
    User bill = new User("Bill", "*****@*****.**", "secret").save();
    Question question = jack.addQuestion("A title", "My first question");
    Answer firstAnswer = question.answer(jack, "first answer").save();
    Answer secondAnswer = question.answer(bill, "second answer").save();
    Answer thirdAnswer = question.answer(bill, "third answer").save();

    assertFalse(firstAnswer.isBestAnswer());
    assertFalse(secondAnswer.isBestAnswer());
    assertFalse(thirdAnswer.isBestAnswer());

    assertTrue(question.canSetBestAnswer());

    assertFalse(firstAnswer.canBeBestAnswer());
    assertTrue(secondAnswer.canBeBestAnswer());
    assertTrue(thirdAnswer.canBeBestAnswer());

    assertFalse(firstAnswer == null);
    assertFalse(secondAnswer == null);
    assertFalse(thirdAnswer == null);

    question.setBestAnswer(secondAnswer);

    assertFalse(firstAnswer.isBestAnswer());
    assertTrue(secondAnswer.isBestAnswer());
    assertFalse(thirdAnswer.isBestAnswer());

    assertTrue(question.canSetBestAnswer());
    question.resetBestAnswer();

    assertFalse(firstAnswer.isBestAnswer());
    assertFalse(secondAnswer.isBestAnswer());
    assertFalse(thirdAnswer.isBestAnswer());
  }
Exemple #9
0
 /**
  * Gets the answers.
  *
  * @return the answers
  */
 public List<Entry> getAnswers() {
   return Answer.find("owner like ? order by timestamp desc", this).fetch();
 }
Exemple #10
0
 /**
  * Gets the number of answers.
  *
  * @return the number of answers
  */
 public long getNumberOfAnswers() {
   return Answer.count("owner = ?", this);
 }