<commit_before>package models.project

import com.scalableminds.util.reactivemongo.{DBAccessContext, GlobalAccessContext}
import com.scalableminds.util.tools.{Fox, FoxImplicits}
import com.scalableminds.webknossos.schema.Tables._
import com.typesafe.scalalogging.LazyLogging
import models.annotation.AnnotationState
import models.task.{TaskDAO, TaskService}
import models.team.TeamSQLDAO
import models.user.{User, UserService}
import net.liftweb.common.Full
import play.api.Play.current
import play.api.data.validation.ValidationError
import play.api.i18n.Messages
import play.api.i18n.Messages.Implicits._
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.functional.syntax._
import play.api.libs.json.{Json, _}
import reactivemongo.bson.BSONObjectID
import reactivemongo.play.json.BSONFormats._
import slick.jdbc.PostgresProfile.api._
import slick.lifted.Rep
import utils.{ObjectId, SQLDAO}

import scala.concurrent.Future


case class ProjectSQL(
                     _id: ObjectId,
                     _team: ObjectId,
                     _owner: ObjectId,
                     name: String,
                     priority: Long,
                     paused: Boolean,
                     expectedTime: Option[Long],
                     created: Long = System.currentTimeMillis(),
                     isDeleted: Boolean = false
                     )

object ProjectSQL {
  def fromProject(p: Project)(implicit ctx: DBAccessContext) = {
    for {
      team <- TeamSQLDAO.findOneByName(p.team)
    } yield {
      ProjectSQL(
        ObjectId.fromBsonId(p._id),
        team._id,
        ObjectId.fromBsonId(p._owner),
        p.name,
        p.priority,
        p.paused,
        p.expectedTime.map(_.toLong),
        System.currentTimeMillis(),
        false)
    }
  }
}

object ProjectSQLDAO extends SQLDAO[ProjectSQL, ProjectsRow, Projects] {
  val collection = Projects

  def idColumn(x: Projects): Rep[String] = x._Id
  def isDeletedColumn(x: Projects): Rep[Boolean] = x.isdeleted

  def parse(r: ProjectsRow): Fox[ProjectSQL] =
    Fox.successful(ProjectSQL(
      ObjectId(r._Id),
      ObjectId(r._Team),
      ObjectId(r._Owner),
      r.name,
      r.priority,
      r.paused,
      r.expectedtime,
      r.created.getTime,
      r.isdeleted
    ))

  override def readAccessQ(requestingUserId: ObjectId) = s"(_team in (select _team from webknossos.user_team_roles where _user = '${requestingUserId.id}')) or _owner = '${requestingUserId.id}'"
  override def deleteAccessQ(requestingUserId: ObjectId) = s"_owner = '${requestingUserId.id}"

  // read operations

  override def findOne(id: ObjectId)(implicit ctx: DBAccessContext): Fox[ProjectSQL] =
    for {
      accessQuery <- readAccessQuery
      rList <- run(sql"select * from #${existingCollectionName} where _id = ${id.id} and #${accessQuery}".as[ProjectsRow])
      r <- rList.headOption.toFox ?~> ("Could not find object " + id + " in " + collectionName)
      parsed <- parse(r) ?~> ("SQLDAO Error: Could not parse database row for object " + id + " in " + collectionName)
    } yield parsed

  override def findAll(implicit ctx: DBAccessContext): Fox[List[ProjectSQL]] = {
    for {
      accessQuery <- readAccessQuery
      r <- run(sql"select * from #${existingCollectionName} where #${accessQuery} order by created".as[ProjectsRow])
      parsed <- Fox.combined(r.toList.map(parse))
    } yield parsed
  }

  def findOneByName(name: String)(implicit ctx: DBAccessContext): Fox[ProjectSQL] =
    for {
      accessQuery <- readAccessQuery
      rList <- run(sql"select * from #${existingCollectionName} where name = ${name} and #${accessQuery}".as[ProjectsRow])
      r <- rList.headOption.toFox<commit_msg>fix sql syntax in project deletion access query<commit_after>package models.project

import com.scalableminds.util.reactivemongo.{DBAccessContext, GlobalAccessContext}
import com.scalableminds.util.tools.{Fox, FoxImplicits}
import com.scalableminds.webknossos.schema.Tables._
import com.typesafe.scalalogging.LazyLogging
import models.annotation.AnnotationState
import models.task.{TaskDAO, TaskService}
import models.team.TeamSQLDAO
import models.user.{User, UserService}
import net.liftweb.common.Full
import play.api.Play.current
import play.api.data.validation.ValidationError
import play.api.i18n.Messages
import play.api.i18n.Messages.Implicits._
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.functional.syntax._
import play.api.libs.json.{Json, _}
import reactivemongo.bson.BSONObjectID
import reactivemongo.play.json.BSONFormats._
import slick.jdbc.PostgresProfile.api._
import slick.lifted.Rep
import utils.{ObjectId, SQLDAO}

import scala.concurrent.Future


case class ProjectSQL(
                     _id: ObjectId,
                     _team: ObjectId,
                     _owner: ObjectId,
                     name: String,
                     priority: Long,
                     paused: Boolean,
                     expectedTime: Option[Long],
                     created: Long = System.currentTimeMillis(),
                     isDeleted: Boolean = false
                     )

object ProjectSQL {
  def fromProject(p: Project)(implicit ctx: DBAccessContext) = {
    for {
      team <- TeamSQLDAO.findOneByName(p.team)
    } yield {
      ProjectSQL(
        ObjectId.fromBsonId(p._id),
        team._id,
        ObjectId.fromBsonId(p._owner),
        p.name,
        p.priority,
        p.paused,
        p.expectedTime.map(_.toLong),
        System.currentTimeMillis(),
        false)
    }
  }
}

object ProjectSQLDAO extends SQLDAO[ProjectSQL, ProjectsRow, Projects] {
  val collection = Projects

  def idColumn(x: Projects): Rep[String] = x._Id
  def isDeletedColumn(x: Projects): Rep[Boolean] = x.isdeleted

  def parse(r: ProjectsRow): Fox[ProjectSQL] =
    Fox.successful(ProjectSQL(
      ObjectId(r._Id),
      ObjectId(r._Team),
      ObjectId(r._Owner),
      r.name,
      r.priority,
      r.paused,
      r.expectedtime,
      r.created.getTime,
      r.isdeleted
    ))

  override def readAccessQ(requestingUserId: ObjectId) = s"(_team in (select _team from webknossos.user_team_roles where _user = '${requestingUserId.id}')) or _owner = '${requestingUserId.id}'"
  override def deleteAccessQ(requestingUserId: ObjectId) = s"_owner = '${requestingUserId.id}'"

  // read operations

  override def findOne(id: ObjectId)(implicit ctx: DBAccessContext): Fox[ProjectSQL] =
    for {
      accessQuery <- readAccessQuery
      rList <- run(sql"select * from #${existingCollectionName} where _id = ${id.id} and #${accessQuery}".as[ProjectsRow])
      r <- rList.headOption.toFox ?~> ("Could not find object " + id + " in " + collectionName)
      parsed <- parse(r) ?~> ("SQLDAO Error: Could not parse database row for object " + id + " in " + collectionName)
    } yield parsed

  override def findAll(implicit ctx: DBAccessContext): Fox[List[ProjectSQL]] = {
    for {
      accessQuery <- readAccessQuery
      r <- run(sql"select * from #${existingCollectionName} where #${accessQuery} order by created".as[ProjectsRow])
      parsed <- Fox.combined(r.toList.map(parse))
    } yield parsed
  }

  def findOneByName(name: String)(implicit ctx: DBAccessContext): Fox[ProjectSQL] =
    for {
      accessQuery <- readAccessQuery
      rList <- run(sql"select * from #${existingCollectionName} where name = ${name} and #${accessQuery}".as[ProjectsRow])
      r <- rList.headOption.toFox
{'fraction_non_alphanumeric': 0.11233096800107109, 'fraction_numerical': 0.0010710938545990091, 'mean_word_length': 3.0509761388286334, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 7, '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': '341290', 'n_tokens_mistral': 2366, 'n_tokens_neox': 2288, 'n_words': 482}