package de.ddb.pdc.core;

import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import de.ddb.pdc.core.answerers.AnswererFactory;
import de.ddb.pdc.metadata.DDBItem;

/**
 * Run the PDC for test data defined in DDBItem.
 * This class implements the AnswererService interface
 */
@Service
public class PublicDomainCalculatorImpl implements PublicDomainCalculator {
  private final QuestionnaireFactory questionnaireFactory;
  private final AnswererFactory answererFactory;

  /**
   * Creates a new PublicDomainCalculatorImpl.
   *
   * @param questionnaireFactory {@link QuestionnaireFactory} to use
   * @param answererFactory {@link QuestionnaireFactory}
   */
  @Autowired
  public PublicDomainCalculatorImpl(
      QuestionnaireFactory questionnaireFactory,
      AnswererFactory answererFactory) {
    this.questionnaireFactory = questionnaireFactory;
    this.answererFactory = answererFactory;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public PDCResult calculate(String country, DDBItem metadata) {
    // Check if an item already has a public domain license.
    if (metadata.hasCcLicense()) {
      String cc = metadata.getCcLicense();
      String link = "https://creativecommons.org/licenses/" + cc;
      if (cc.equals("zero")) {
        link += "/1.0/";
      } else {
        link += "/4.0/";
      }
      List<AnsweredQuestion> trace = new LinkedList<AnsweredQuestion>();
      trace.add(new AnsweredQuestion(Question.IS_PUBLIC_DOMAIN, 
          Answer.YES, "Das Werk steht bereits unter einer CC Lizenz"
          + ": CC-" + cc + ". Lizenzinformationen unter: " + link));
      PDCResult result = new PDCResult(true, trace, metadata);
      return result;
    }
    
    // FIXME: Category is hard coded
    Category category = Category.LITERARY_OR_ARTISTIC_WORK;
    Questionnaire questionnaire = questionnaireFactory.build(
        country, category, metadata
    );
    answerQuestions(questionnaire, metadata);
    PDCResult result = questionnaire.getResult();
    
    // If the result is unknown, only deliver one trace entry
    // to the front-end with the reason for the fail.
    if (result.isPublicDomain() == null) {
      List<AnsweredQuestion> trace = result.getTrace();
      List<AnsweredQuestion> actualTrace = new LinkedList<AnsweredQuestion>();
      int index = trace.size() - 1;
      if (trace.get(index).hasNote()) {
        actualTrace.add(new AnsweredQuestion(Question.CALCULATION_FAILED, 
            Answer.UNKNOWN, trace.get(index).getNote()));
      } else {
        actualTrace.add(new AnsweredQuestion(Question.CALCULATION_FAILED, 
            Answer.UNKNOWN, "Der Grund ist unbekannt."));
      }
      PDCResult actualResult = new PDCResult(null, actualTrace, metadata);
      return actualResult;
    }
    
    return result;
  }

  private void answerQuestions(Questionnaire questionnaire, DDBItem metadata) {
    while (questionnaire.hasQuestionsLeft()) {
      Question question = questionnaire.getCurrentQuestion();
      Answerer answerer = answererFactory.getAnswererForQuestion(question);
      Answer answer = answerer.answerQuestionForItem(metadata);
      String assumption = answerer.getNoteForLastQuestion();
      questionnaire.answerCurrentQuestion(answer, assumption);
    }
  }
}

@@ -2115,867 +2115,40 @@
     
-PDCResult result = questionnaire.getResult();%0D%0A    %0D%0A    // If the result is unknown, only deliver one trace entry%0D%0A    // to the front-end with the reason for the fail.%0D%0A    if (result.isPublicDomain() == null) %7B%0D%0A      List%3CAnsweredQuestion%3E trace = result.getTrace();%0D%0A      List%3CAnsweredQuestion%3E actualTrace = new LinkedList%3CAnsweredQuestion%3E();%0D%0A      int index = trace.size() - 1;%0D%0A      if (trace.get(index).hasNote()) %7B%0D%0A        actualTrace.add(new AnsweredQuestion(Question.CALCULATION_FAILED, %0D%0A            Answer.UNKNOWN, trace.get(index).getNote()));%0D%0A      %7D else %7B%0D%0A        actualTrace.add(new AnsweredQuestion(Question.CALCULATION_FAILED, %0D%0A            Answer.UNKNOWN, %22Der Grund ist unbekannt.%22));%0D%0A      %7D%0D%0A      PDCResult actualResult = new PDCResult(null, actualTrace, metadata);%0D%0A      return actualResult;%0D%0A    %7D%0D%0A    %0D%0A    return result
+return questionnaire.getResult()
 ;%0D%0A 

Revert shortening of unknown-status result traces in backend