<commit_before>package controllers

import javax.inject.Inject

import scala.collection.mutable.ListBuffer
import scala.reflect.io.File
import scala.util.matching.Regex
import util.control.Breaks._
import helper.{SpellChecker, Commons, PaperProcessingManager}
import models._
import play.api.Configuration
import play.api.db.Database
import play.api.mvc.{Action, Controller}


/**
  * Created by manuel on 11.04.2016.
  */
class Paper @Inject()(database: Database, configuration: Configuration, papersService: PapersService,
                      questionService: QuestionService, method2AssumptionService: Method2AssumptionService,
                      paperResultService: PaperResultService, answerService: AnswerService,
                      conferenceSettingsService: ConferenceSettingsService, paperMethodService: PaperMethodService,
                      permutationsServcie: PermutationsService
                     ) extends Controller {

  def show(id:Int, secret:String) = Action {
    val paper = papersService.findByIdAndSecret(id,secret)
    if(paper.size > 0) {
      var results = paperResultService.findByPaperId(id)
      results = addMethodsAndAssumptions(id,results)
      val spellCheck = SpellChecker.check(paper.get)
      val filePath = configuration.getString("highlighter.pdfSourceDir").get+"/"+Commons.getSecretHash(secret)+"/log.txt"
      val fileLengh = File(filePath).length
      var log = ""
      if(fileLengh < 9999999 && fileLengh > 0) {
        val source = scala.io.Source.fromFile(filePath)
        log = try source.mkString.replace("\n","\n<br>") finally source.close()
      }
      val answers = answerService.findJsonAnswerByPaperId(id).map(a => {
        val questionIdRegex = new Regex("\"questionId\" : \"([^\"]+)\"")
        val questionId = questionIdRegex.findAllIn(a).matchData.map(r => r.group(1)).toList.head
        var imgPath = questionService.findQuestionImgPathById(questionId.toLong)
        imgPath = imgPath.substring(imgPath.indexOf("tmp")+4)
        val link = "<a href='" + configuration.getString("url.prefix") +
          routes.Paper.getFile("tmp",imgPath).url + "'>Show Question</a>"
        val parsedJSON = a.replace("{\"","").replace("\"}","").replace("\" : \"",": ").replace("\", \"","<br>\n")
        "<br><br>\n\n" + link + "<br>\n" + parsedJSON
      }).mkString("")
      Ok(views.html.paper.showPaper(paper.get,Commons.getSecretHash(secret),results,spellCheck,log,answers))
    } else {
      Unauthorized(views.html.error.unauthorized())
    }
  }

  def addMethodsAndAssumptions(id:Int,results: List[PaperResult]) : List[PaperResult] = {
    var allResults = results
    val paper = papersService.findById(id).get
    val m2aList = answerService.findByPaperId(paper.id.get)
    val conferenceSettings = conferenceSettingsService.findAllByPaperId(paper.id.get,paper.conferenceId).to[ListBuffer]
    m2aList.foreach(m2a => {
      breakable {
        conferenceSettings.zipWithIndex.foreach{case (confSetting,i) => {
          if(confSetting.flag.get != ConferenceSettings.FLAG_IGNORE) {
            if(m2a.method.toLowerCase() == confSetting.methodName.toLowerCase() &&
              m2a.assumption.toLowerCase() == confSetting.assumptionName.toLowerCase()) {
              val m2aDescr = m2a.method+" <span class='glyphicon glyphicon-arrow-right'></span> "+m2a.assumption
              var m2aResult = "Related: <b>"+ (m2a.isRelated > 0.5) + "</b>, " +
                "Checked before: <b>" + (m2a.isCheckedBefore > 0.5) + "</b>"
              var symbol = PaperResult.SYMBOL_ERROR<commit_msg>Processing Log Show Question Image Path Fix<commit_after>package controllers

import javax.inject.Inject

import scala.collection.mutable.ListBuffer
import scala.reflect.io.File
import scala.util.matching.Regex
import util.control.Breaks._
import helper.{SpellChecker, Commons, PaperProcessingManager}
import models._
import play.api.Configuration
import play.api.db.Database
import play.api.mvc.{Action, Controller}


/**
  * Created by manuel on 11.04.2016.
  */
class Paper @Inject()(database: Database, configuration: Configuration, papersService: PapersService,
                      questionService: QuestionService, method2AssumptionService: Method2AssumptionService,
                      paperResultService: PaperResultService, answerService: AnswerService,
                      conferenceSettingsService: ConferenceSettingsService, paperMethodService: PaperMethodService,
                      permutationsServcie: PermutationsService
                     ) extends Controller {

  def show(id:Int, secret:String) = Action {
    val paper = papersService.findByIdAndSecret(id,secret)
    if(paper.size > 0) {
      var results = paperResultService.findByPaperId(id)
      results = addMethodsAndAssumptions(id,results)
      val spellCheck = SpellChecker.check(paper.get)
      val filePath = configuration.getString("highlighter.pdfSourceDir").get+"/"+Commons.getSecretHash(secret)+"/log.txt"
      val fileLengh = File(filePath).length
      var log = ""
      if(fileLengh < 9999999 && fileLengh > 0) {
        val source = scala.io.Source.fromFile(filePath)
        log = try source.mkString.replace("\n","\n<br>") finally source.close()
      }
      val answers = answerService.findJsonAnswerByPaperId(id).map(a => {
        val questionIdRegex = new Regex("\"questionId\" : \"([^\"]+)\"")
        val questionId = questionIdRegex.findAllIn(a).matchData.map(r => r.group(1)).toList.head
        var imgPath = questionService.findQuestionImgPathById(questionId.toLong)
        imgPath = imgPath.substring(imgPath.indexOf("tmp")+4)
        val link = "<a href='" + configuration.getString("hcomp.ballot.baseURL").get +
          configuration.getString("url.prefix").get + routes.Paper.getFile("tmp",imgPath).url + "'>Show Question</a>"
        val parsedJSON = a.replace("{\"","").replace("\"}","").replace("\" : \"",": ").replace("\", \"","<br>\n")
        "<br><br>\n\n" + link + "<br>\n" + parsedJSON
      }).mkString("")
      Ok(views.html.paper.showPaper(paper.get,Commons.getSecretHash(secret),results,spellCheck,log,answers))
    } else {
      Unauthorized(views.html.error.unauthorized())
    }
  }

  def addMethodsAndAssumptions(id:Int,results: List[PaperResult]) : List[PaperResult] = {
    var allResults = results
    val paper = papersService.findById(id).get
    val m2aList = answerService.findByPaperId(paper.id.get)
    val conferenceSettings = conferenceSettingsService.findAllByPaperId(paper.id.get,paper.conferenceId).to[ListBuffer]
    m2aList.foreach(m2a => {
      breakable {
        conferenceSettings.zipWithIndex.foreach{case (confSetting,i) => {
          if(confSetting.flag.get != ConferenceSettings.FLAG_IGNORE) {
            if(m2a.method.toLowerCase() == confSetting.methodName.toLowerCase() &&
              m2a.assumption.toLowerCase() == confSetting.assumptionName.toLowerCase()) {
              val m2aDescr = m2a.method+" <span class='glyphicon glyphicon-arrow-right'></span> "+m2a.assumption
              var m2aResult = "Related: <b>"+ (m2a.isRelated > 0.5) + "</b>, " +
                "Checked before: <b>" + (m2a.isCheckedBefore > 0.5) + "</b>"
              var symbol = PaperResult.SYMBOL_ERROR
{'fraction_non_alphanumeric': 0.136891592920354, 'fraction_numerical': 0.00995575221238938, 'mean_word_length': 3.442874692874693, 'pattern_counts': {'":': 2, '<': 31, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '4862863', 'n_tokens_mistral': 2197, 'n_tokens_neox': 2141, 'n_words': 360}