<commit_before>package models

import util.plugins.Cache

import play.api.Logger
import org.squeryl.{KeyedEntity, Query, Schema, Table}
import org.squeryl.dsl.QueryDsl
import org.squeryl.dsl.ast.LogicalBoolean
import org.squeryl.internals.PosoLifecycleEvent
import scala.transient
import util.Config

trait ValidatedEntity[T] extends KeyedEntity[T] {
  def validate(): Unit
  def asJson: String
  @transient private var persisted: Option[Boolean] = None
  // KeyedEntity overrides hashCode/equals with respect to isPersisted. Without the
  // forComparison/isPersisted code, two models with the same primary key are considered identical
  // (according to their PK) even if those models have different attributes. This behavior breaks
  // callbacks that need to compare an 'old' and 'new' model. forComparison provides a short-hand
  // for forcing KeyedEntity to use the class hashCode/equals instead of the custom one. This is
  // horrible and will likely break since it depends on some squeryl internals.
  def forComparison {
    persisted = Some(false)
  }
  override def isPersisted: Boolean = persisted.getOrElse(super.isPersisted)
}

trait BasicModel[T <: AnyRef] { self: Schema =>
  private[this] val logger = Logger.logger

  import org.squeryl.PrimitiveTypeMode

  protected def tableDef: Table[T] // Override
  protected def createEventName: Option[String] = None
  protected def deleteEventName: Option[String] = None

  def inTransaction[A](f: => A): A = PrimitiveTypeMode.inTransaction(f)
  protected def cacheKeys(t: T): Seq[String] = Seq()
  override def callbacks = Seq(
    afterDelete(tableDef) call(loggedInvalidation("afterDelete", _)),
    afterUpdate(tableDef) call(loggedInvalidation("afterUpdate", _)),
    afterInsert(tableDef) call(loggedInvalidation("afterInsert", _))
  )

  protected def log[A](a: => A): A = {
    if (Config.getBoolean("querylog.enabled").getOrElse(false)) {
      org.squeryl.Session.currentSession.setLogger(query => 
        if (Config.getBoolean("querylog.includeResults").getOrElse(false) || !(query startsWith "ResultSetRow")) {
          Logger.logger.debug(Config.getString("querylog.prefix", "") + query)
        }
      )<commit_msg>switch query log level from DEBUG to INFO for production logging<commit_after>package models

import util.plugins.Cache

import play.api.Logger
import org.squeryl.{KeyedEntity, Query, Schema, Table}
import org.squeryl.dsl.QueryDsl
import org.squeryl.dsl.ast.LogicalBoolean
import org.squeryl.internals.PosoLifecycleEvent
import scala.transient
import util.Config

trait ValidatedEntity[T] extends KeyedEntity[T] {
  def validate(): Unit
  def asJson: String
  @transient private var persisted: Option[Boolean] = None
  // KeyedEntity overrides hashCode/equals with respect to isPersisted. Without the
  // forComparison/isPersisted code, two models with the same primary key are considered identical
  // (according to their PK) even if those models have different attributes. This behavior breaks
  // callbacks that need to compare an 'old' and 'new' model. forComparison provides a short-hand
  // for forcing KeyedEntity to use the class hashCode/equals instead of the custom one. This is
  // horrible and will likely break since it depends on some squeryl internals.
  def forComparison {
    persisted = Some(false)
  }
  override def isPersisted: Boolean = persisted.getOrElse(super.isPersisted)
}

trait BasicModel[T <: AnyRef] { self: Schema =>
  private[this] val logger = Logger.logger

  import org.squeryl.PrimitiveTypeMode

  protected def tableDef: Table[T] // Override
  protected def createEventName: Option[String] = None
  protected def deleteEventName: Option[String] = None

  def inTransaction[A](f: => A): A = PrimitiveTypeMode.inTransaction(f)
  protected def cacheKeys(t: T): Seq[String] = Seq()
  override def callbacks = Seq(
    afterDelete(tableDef) call(loggedInvalidation("afterDelete", _)),
    afterUpdate(tableDef) call(loggedInvalidation("afterUpdate", _)),
    afterInsert(tableDef) call(loggedInvalidation("afterInsert", _))
  )

  protected def log[A](a: => A): A = {
    if (Config.getBoolean("querylog.enabled").getOrElse(false)) {
      org.squeryl.Session.currentSession.setLogger(query => 
        if (Config.getBoolean("querylog.includeResults").getOrElse(false) || !(query startsWith "ResultSetRow")) {
          Logger.logger.info(Config.getString("querylog.prefix", "") + query)
        }
      )
{'fraction_non_alphanumeric': 0.10094637223974763, 'fraction_numerical': 0.0, 'mean_word_length': 4.555694618272841, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '2262491', 'n_tokens_mistral': 1305, 'n_tokens_neox': 1295, 'n_words': 378}