<commit_before>package models.annotation

import java.io.{BufferedOutputStream, FileOutputStream}

import com.scalableminds.util.geometry.{BoundingBox, Point3D, Scale, Vector3D}
import com.scalableminds.util.io.{NamedEnumeratorStream, ZipIO}
import com.scalableminds.util.reactivemongo.{DBAccessContext, GlobalAccessContext}
import com.scalableminds.util.tools.{BoxImplicits, Fox, FoxImplicits, TextUtils}
import com.scalableminds.webknossos.datastore.SkeletonTracing.{Color, SkeletonTracing, SkeletonTracings, Tree}
import com.scalableminds.webknossos.datastore.VolumeTracing.VolumeTracing
import com.scalableminds.webknossos.datastore.models.datasource.{DataSourceLike => DataSource, SegmentationLayerLike => SegmentationLayer}
import com.scalableminds.webknossos.datastore.tracings._
import com.scalableminds.webknossos.datastore.tracings.skeleton.{NodeDefaults, SkeletonTracingDefaults}
import com.scalableminds.webknossos.datastore.tracings.volume.VolumeTracingDefaults
import com.typesafe.scalalogging.LazyLogging
import models.annotation.AnnotationState._
import models.annotation.AnnotationTypeSQL.AnnotationTypeSQL
import models.annotation.handler.SavedTracingInformationHandler
import models.annotation.nml.NmlWriter
import models.binary.{DataSet, DataSetDAO, DataSetSQLDAO, DataStoreHandlingStrategy}
import models.task.TaskSQL
import models.team.OrganizationSQLDAO
import models.user.User
import utils.ObjectId
import play.api.i18n.Messages
import play.api.Play.current
import play.api.i18n.Messages.Implicits._

import scala.concurrent.Future
import scala.collection.{IterableLike, TraversableLike}
import scala.runtime.Tuple3Zipped
import scala.collection.generic.Growable
import net.liftweb.common.{Box, Full}
import play.api.libs.Files.TemporaryFile
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.iteratee.Enumerator
import reactivemongo.bson.BSONObjectID

object AnnotationService
  extends BoxImplicits
  with FoxImplicits
  with TextUtils
  with ProtoGeometryImplicits
  with LazyLogging {

  private def selectSuitableTeam(user: User, dataSet: DataSet)(implicit ctx: DBAccessContext): Fox[ObjectId] = {
    val selectedTeamOpt = dataSet.allowedTeams.intersect(user.teamIds).headOption
    selectedTeamOpt match {
      case Some(selectedTeam) => Fox.successful(ObjectId.fromBsonId(selectedTeam))
      case None =>
        for {
          _ <- user.isAdmin
          organization <- OrganizationSQLDAO.findOneByName(user.organization)
          organizationTeamId <- OrganizationSQLDAO.findOrganizationTeamId(organization._id)
        } yield organizationTeamId
    }
  }

  private def createVolumeTracing(dataSource: DataSource, withFallback: Boolean): VolumeTracing = {
    val fallbackLayer = if (withFallback) {<commit_msg>allow teammanagers to create explorationals on datasets without allowed teams (#2758)<commit_after>package models.annotation

import java.io.{BufferedOutputStream, FileOutputStream}

import com.scalableminds.util.geometry.{BoundingBox, Point3D, Scale, Vector3D}
import com.scalableminds.util.io.{NamedEnumeratorStream, ZipIO}
import com.scalableminds.util.reactivemongo.{DBAccessContext, GlobalAccessContext}
import com.scalableminds.util.tools.{BoxImplicits, Fox, FoxImplicits, TextUtils}
import com.scalableminds.webknossos.datastore.SkeletonTracing.{Color, SkeletonTracing, SkeletonTracings, Tree}
import com.scalableminds.webknossos.datastore.VolumeTracing.VolumeTracing
import com.scalableminds.webknossos.datastore.models.datasource.{DataSourceLike => DataSource, SegmentationLayerLike => SegmentationLayer}
import com.scalableminds.webknossos.datastore.tracings._
import com.scalableminds.webknossos.datastore.tracings.skeleton.{NodeDefaults, SkeletonTracingDefaults}
import com.scalableminds.webknossos.datastore.tracings.volume.VolumeTracingDefaults
import com.typesafe.scalalogging.LazyLogging
import models.annotation.AnnotationState._
import models.annotation.AnnotationTypeSQL.AnnotationTypeSQL
import models.annotation.handler.SavedTracingInformationHandler
import models.annotation.nml.NmlWriter
import models.binary.{DataSet, DataSetDAO, DataSetSQLDAO, DataStoreHandlingStrategy}
import models.task.TaskSQL
import models.team.OrganizationSQLDAO
import models.user.User
import utils.ObjectId
import play.api.i18n.Messages
import play.api.Play.current
import play.api.i18n.Messages.Implicits._

import scala.concurrent.Future
import scala.collection.{IterableLike, TraversableLike}
import scala.runtime.Tuple3Zipped
import scala.collection.generic.Growable
import net.liftweb.common.{Box, Full}
import play.api.libs.Files.TemporaryFile
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.iteratee.Enumerator
import reactivemongo.bson.BSONObjectID

object AnnotationService
  extends BoxImplicits
  with FoxImplicits
  with TextUtils
  with ProtoGeometryImplicits
  with LazyLogging {

  private def selectSuitableTeam(user: User, dataSet: DataSet)(implicit ctx: DBAccessContext): Fox[ObjectId] = {
    val selectedTeamOpt = dataSet.allowedTeams.intersect(user.teamIds).headOption
    selectedTeamOpt match {
      case Some(selectedTeam) => Fox.successful(ObjectId.fromBsonId(selectedTeam))
      case None =>
        for {
          _ <- (user.isTeamManagerInOrg(user.organization) || user.isAdmin)
          organization <- OrganizationSQLDAO.findOneByName(user.organization)
          organizationTeamId <- OrganizationSQLDAO.findOrganizationTeamId(organization._id)
        } yield organizationTeamId
    }
  }

  private def createVolumeTracing(dataSource: DataSource, withFallback: Boolean): VolumeTracing = {
    val fallbackLayer = if (withFallback) {
{'fraction_non_alphanumeric': 0.08233427362482369, 'fraction_numerical': 0.003173483779971791, 'mean_word_length': 6.245210727969349, 'pattern_counts': {'":': 0, '<': 9, '<?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': '361099', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1716, 'n_words': 289}