<commit_before>package models

import java.util.Date
import javax.inject.{ Inject, Singleton }

import scala.concurrent.Future

import play.api.libs.json.JsValue.jsValueToJsLookup
import play.api.libs.json.{ JsFalse, JsObject }

import models.JsonFormat.taskStatusFormat
import services.AuditedModel

import org.elastic4play.JsonFormat.dateFormat
import org.elastic4play.models.{ AttributeDef, BaseEntity, ChildModelDef, EntityDef, HiveEnumeration, AttributeFormat ⇒ F }
import org.elastic4play.utils.RichJson

object TaskStatus extends Enumeration with HiveEnumeration {
  type Type = Value
  val Waiting, InProgress, Completed, Cancel = Value
}

trait TaskAttributes { _: AttributeDef ⇒
  val title = attribute("title", F.textFmt, "Title of the task")
  val description = optionalAttribute("description", F.textFmt, "Task details")
  val owner = optionalAttribute("owner", F.userFmt, "User who owns the task")
  val status = attribute("status", F.enumFmt(TaskStatus), "Status of the task", TaskStatus.Waiting)
  val flag = attribute("flag", F.booleanFmt, "Flag of the task", false)
  val startDate = optionalAttribute("startDate", F.dateFmt, "Timestamp of the comment start")
  val endDate = optionalAttribute("endDate", F.dateFmt, "Timestamp of the comment end")
  val order = attribute("order", F.numberFmt, "Order of the task", 0L)

}
@Singleton
class TaskModel @Inject() (caseModel: CaseModel) extends ChildModelDef[TaskModel, Task, CaseModel, Case](caseModel, "case_task", "Task", "/case/task") with TaskAttributes with AuditedModel {
  override val defaultSortBy = Seq("-startDate")

  override def updateHook(task: BaseEntity, updateAttrs: JsObject): Future[JsObject] = Future.successful {
    (updateAttrs \ "status").asOpt[TaskStatus.Type] match {
      case Some(TaskStatus.InProgress) ⇒
        updateAttrs
          .setIfAbsent("startDate", new Date)
      case Some(TaskStatus.Completed) ⇒
        updateAttrs
          .setIfAbsent("endDate", new Date) +
          ("flag" → JsFalse)
      case _ ⇒ updateAttrs
    }
  }<commit_msg>#381 Add "dueDate" attribute to task entity<commit_after>package models

import java.util.Date
import javax.inject.{ Inject, Singleton }

import scala.concurrent.Future

import play.api.libs.json.JsValue.jsValueToJsLookup
import play.api.libs.json.{ JsFalse, JsObject }

import models.JsonFormat.taskStatusFormat
import services.AuditedModel

import org.elastic4play.JsonFormat.dateFormat
import org.elastic4play.models.{ AttributeDef, BaseEntity, ChildModelDef, EntityDef, HiveEnumeration, AttributeFormat ⇒ F }
import org.elastic4play.utils.RichJson

object TaskStatus extends Enumeration with HiveEnumeration {
  type Type = Value
  val Waiting, InProgress, Completed, Cancel = Value
}

trait TaskAttributes { _: AttributeDef ⇒
  val title = attribute("title", F.textFmt, "Title of the task")
  val description = optionalAttribute("description", F.textFmt, "Task details")
  val owner = optionalAttribute("owner", F.userFmt, "User who owns the task")
  val status = attribute("status", F.enumFmt(TaskStatus), "Status of the task", TaskStatus.Waiting)
  val flag = attribute("flag", F.booleanFmt, "Flag of the task", false)
  val startDate = optionalAttribute("startDate", F.dateFmt, "Timestamp of the comment start")
  val endDate = optionalAttribute("endDate", F.dateFmt, "Timestamp of the comment end")
  val order = attribute("order", F.numberFmt, "Order of the task", 0L)
  val dueDate = optionalAttribute("dueDate", F.dateFmt, "When this date is passed, Thehive warns users")
}

@Singleton
class TaskModel @Inject() (caseModel: CaseModel) extends ChildModelDef[TaskModel, Task, CaseModel, Case](caseModel, "case_task", "Task", "/case/task") with TaskAttributes with AuditedModel {
  override val defaultSortBy = Seq("-startDate")

  override def updateHook(task: BaseEntity, updateAttrs: JsObject): Future[JsObject] = Future.successful {
    (updateAttrs \ "status").asOpt[TaskStatus.Type] match {
      case Some(TaskStatus.InProgress) ⇒
        updateAttrs
          .setIfAbsent("startDate", new Date)
      case Some(TaskStatus.Completed) ⇒
        updateAttrs
          .setIfAbsent("endDate", new Date) +
          ("flag" → JsFalse)
      case _ ⇒ updateAttrs
    }
  }
{'fraction_non_alphanumeric': 0.10866997401370186, 'fraction_numerical': 0.0025986298133711316, 'mean_word_length': 4.776261937244202, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6121146', 'n_tokens_mistral': 1283, 'n_tokens_neox': 1280, 'n_words': 366}