<commit_before>package ems.model

import ems.storage.MongoDBStorage
import ems.storage.BinaryStorage
import ems.{Attachment, URIAttachment}
import org.joda.time.DateTime
import com.mongodb.casbah.Imports._
import java.util.{Date => JDate, Locale, UUID}

case class Abstract(title: String,
                    summary: Option[String] = None,
                    body: Option[String] = None,
                    audience: Option[String] = None,
                    outline: Option[String] = None,
                    equipment: Option[String] = None,
                    language: Locale = new Locale("no"),
                    level: Level = Level.Beginner,
                    format: Format = Format.Presentation
                     ) {
  def withTitle(input: String) = copy(input)

  def withBody(input: String) = copy(body = Some(input))

  def withAudience(input: String) = copy(audience = Some(input))

  def withOutline(input: String) = copy(outline = Some(input))

  def withEquipment(input: String) = copy(equipment = Some(input))

  def withSummary(input: String) = copy(summary = Some(input))

  def withFormat(format: Format) = copy(format = format)

  def withLevel(level: Level) = copy(level = level)


  def toMongo = MongoDBObject(
    "title" -> title.trim.noHtml,
    "body" -> body.map(_.noHtml),
    "summary" -> summary.map(_.noHtml),
    "equipment" -> equipment.map(_.noHtml),
    "outline" -> outline.map(_.noHtml),
    "audience" -> audience.map(_.noHtml),
    "format" -> format.name,
    "level" -> level.name,
    "language" -> language.getLanguage
  )
}

object Abstract {
  def apply(dbo: DBObject): Abstract = {
    val m = wrapDBObj(dbo)
    val asString = m.getAs[String] _
    val format = asString("format").map(Format(_)).getOrElse(Format.Presentation)
    val level = asString("level").map(Level(_)).getOrElse(Level.Beginner)
    Abstract(
      m.getAsOrElse("title", "No Title").noHtml,
      asString("summary").map(_.noHtml),
      asString("body").map(_.noHtml),
      asString("audience").map(_.noHtml),
      asString("outline").map(_.noHtml),
      asString("equipment").map(_.noHtml),
      asString("language").map(l => new Locale(l)).getOrElse(new Locale("no")),
      level,
      format
    )
  }

}

case class Session(id: Option[String],
                   eventId: String,
                   slug: String,
                   room: Option[Room],
                   slot: Option[Slot],
                   abs: Abstract,
                   state: State,
                   published: Boolean,
                   tags: Set[Tag],
                   keywords: Set[Keyword],
                   speakers: Seq[Speaker],
                   attachments: Seq[URIAttachment] = Nil,
                   lastModified: DateTime = new DateTime()) extends Entity[Session] {


  type T = Session

  def addAttachment(attachment: URIAttachment) = copy(attachments = attachments ++ Seq(attachment))

  def addKeyword(word: String) = copy(keywords = keywords + Keyword(word))

  def addTag(word: String) = copy(tags = tags + Tag(word))

  def withTitle(input: String) = withAbstract(abs.withTitle(input))

  def withBody(input: String) = withAbstract(abs.withBody(input))

  def withSummary(input: String) = withAbstract(abs.withSummary(input))

  def withRoom(room: Room) = copy(room = Some(room))

  def withSlot(slot: Slot) = copy(slot = Some(slot))

  def publish = if (published) this else copy(published = true)

  def withFormat(format: Format) = copy(abs = abs.withFormat(format))

  def withLevel(level: Level) = withAbstract(abs.withLevel(level))

  def withTags(tags: Set[Tag]) = copy(tags = tags)

  def addOrUpdateSpeaker(speaker: Speaker) = {
    val speakers = Vector(this.speakers : _*)
    val index = speakers.indexWhere(_.id == speaker.id)
    if (index != -1) {
      withSpeakers(speakers.updated(index, speaker))
    }
    else {
      addSpeaker(speaker)
    }
  }

  def addSpeaker(speaker: Speaker) = copy(speakers = speakers ++ Seq(speaker))

  def withSpeakers(speakers: Seq[Speaker]) = copy(speakers = speakers)

  private def withAbstract(abs: Abstract) = copy(abs = abs)

  def withId(id: String) = copy(id = Some(id))

  def toMongo(update: Boolean): DBObject = {
    val base = MongoDBObject(
      "slug" -> slug,
      "published" -> published,
      "tags" -> tags.map(_.name.noHtml),
      "keywords" -> keywords.map(_.name.noHtml),
      "state" -> state.name,
      "last-modified" -> DateTime.now().toDate
    ) ++ abs.toMongo

    if (update) {
      MongoDBObject(
        "$set" -> base
      )
    }
    else {
      val obj = MongoDBObject(
        "_id" -> id.getOrElse(UUID.randomUUID().toString),
        "eventId" -> eventId,
        "roomId" -> room.flatMap(_.id),
        "slotId" -> slot.flatMap(_.id),
        "attachments" -> attachments.map(_.toMongo),
        "speakers" -> speakers.map(_.toMongo)
      ) ++ base<commit_msg>Do not reset publish flag if updated.<commit_after>package ems.model

import ems.storage.MongoDBStorage
import ems.storage.BinaryStorage
import ems.{Attachment, URIAttachment}
import org.joda.time.DateTime
import com.mongodb.casbah.Imports._
import java.util.{Date => JDate, Locale, UUID}

case class Abstract(title: String,
                    summary: Option[String] = None,
                    body: Option[String] = None,
                    audience: Option[String] = None,
                    outline: Option[String] = None,
                    equipment: Option[String] = None,
                    language: Locale = new Locale("no"),
                    level: Level = Level.Beginner,
                    format: Format = Format.Presentation
                     ) {
  def withTitle(input: String) = copy(input)

  def withBody(input: String) = copy(body = Some(input))

  def withAudience(input: String) = copy(audience = Some(input))

  def withOutline(input: String) = copy(outline = Some(input))

  def withEquipment(input: String) = copy(equipment = Some(input))

  def withSummary(input: String) = copy(summary = Some(input))

  def withFormat(format: Format) = copy(format = format)

  def withLevel(level: Level) = copy(level = level)


  def toMongo = MongoDBObject(
    "title" -> title.trim.noHtml,
    "body" -> body.map(_.noHtml),
    "summary" -> summary.map(_.noHtml),
    "equipment" -> equipment.map(_.noHtml),
    "outline" -> outline.map(_.noHtml),
    "audience" -> audience.map(_.noHtml),
    "format" -> format.name,
    "level" -> level.name,
    "language" -> language.getLanguage
  )
}

object Abstract {
  def apply(dbo: DBObject): Abstract = {
    val m = wrapDBObj(dbo)
    val asString = m.getAs[String] _
    val format = asString("format").map(Format(_)).getOrElse(Format.Presentation)
    val level = asString("level").map(Level(_)).getOrElse(Level.Beginner)
    Abstract(
      m.getAsOrElse("title", "No Title").noHtml,
      asString("summary").map(_.noHtml),
      asString("body").map(_.noHtml),
      asString("audience").map(_.noHtml),
      asString("outline").map(_.noHtml),
      asString("equipment").map(_.noHtml),
      asString("language").map(l => new Locale(l)).getOrElse(new Locale("no")),
      level,
      format
    )
  }

}

case class Session(id: Option[String],
                   eventId: String,
                   slug: String,
                   room: Option[Room],
                   slot: Option[Slot],
                   abs: Abstract,
                   state: State,
                   published: Boolean,
                   tags: Set[Tag],
                   keywords: Set[Keyword],
                   speakers: Seq[Speaker],
                   attachments: Seq[URIAttachment] = Nil,
                   lastModified: DateTime = new DateTime()) extends Entity[Session] {


  type T = Session

  def addAttachment(attachment: URIAttachment) = copy(attachments = attachments ++ Seq(attachment))

  def addKeyword(word: String) = copy(keywords = keywords + Keyword(word))

  def addTag(word: String) = copy(tags = tags + Tag(word))

  def withTitle(input: String) = withAbstract(abs.withTitle(input))

  def withBody(input: String) = withAbstract(abs.withBody(input))

  def withSummary(input: String) = withAbstract(abs.withSummary(input))

  def withRoom(room: Room) = copy(room = Some(room))

  def withSlot(slot: Slot) = copy(slot = Some(slot))

  def withFormat(format: Format) = copy(abs = abs.withFormat(format))

  def withLevel(level: Level) = withAbstract(abs.withLevel(level))

  def withTags(tags: Set[Tag]) = copy(tags = tags)

  def addOrUpdateSpeaker(speaker: Speaker) = {
    val speakers = Vector(this.speakers : _*)
    val index = speakers.indexWhere(_.id == speaker.id)
    if (index != -1) {
      withSpeakers(speakers.updated(index, speaker))
    }
    else {
      addSpeaker(speaker)
    }
  }

  def addSpeaker(speaker: Speaker) = copy(speakers = speakers ++ Seq(speaker))

  def withSpeakers(speakers: Seq[Speaker]) = copy(speakers = speakers)

  private def withAbstract(abs: Abstract) = copy(abs = abs)

  def withId(id: String) = copy(id = Some(id))

  def toMongo(update: Boolean): DBObject = {
    val base = MongoDBObject(
      "slug" -> slug,
      "tags" -> tags.map(_.name.noHtml),
      "keywords" -> keywords.map(_.name.noHtml),
      "state" -> state.name,
      "last-modified" -> DateTime.now().toDate
    ) ++ abs.toMongo

    if (update) {
      MongoDBObject(
        "$set" -> base
      )
    }
    else {
      val obj = MongoDBObject(
        "_id" -> id.getOrElse(UUID.randomUUID().toString),
        "eventId" -> eventId,
        "published" -> published,
        "roomId" -> room.flatMap(_.id),
        "slotId" -> slot.flatMap(_.id),
        "attachments" -> attachments.map(_.toMongo),
        "speakers" -> speakers.map(_.toMongo)
      ) ++ base
{'fraction_non_alphanumeric': 0.14016971679787343, 'fraction_numerical': 0.00020447806972702178, 'mean_word_length': 2.6432029795158285, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 51, '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': '4220583', 'n_tokens_mistral': 3115, 'n_tokens_neox': 3003, 'n_words': 635}