<commit_before>package be.cytomine.api.ontology

import be.cytomine.Exception.CytomineException
import be.cytomine.Exception.WrongArgumentException
import be.cytomine.api.RestController
import be.cytomine.api.UrlApi
import be.cytomine.image.ImageInstance
import be.cytomine.ontology.Annotation
import be.cytomine.ontology.AnnotationTerm
import be.cytomine.ontology.Term
import be.cytomine.processing.Job
import be.cytomine.project.Project
import be.cytomine.security.SecUser
import be.cytomine.security.User
import be.cytomine.social.SharedAnnotation
import grails.converters.JSON

import java.awt.image.BufferedImage
import java.text.SimpleDateFormat
import javax.imageio.ImageIO
import groovy.sql.Sql
import be.cytomine.ontology.AlgoAnnotationTerm
import be.cytomine.security.UserJob

class RestAnnotationController extends RestController {

    def exportService
    def grailsApplication
    def annotationService
    def domainService
    def termService
    def imageInstanceService
    def userService
    def projectService
    def cytomineService
    def mailService
    def dataSource



    def list = {
        def annotations = []
        def projects = projectService.list()
        projects.each {
            annotations.addAll(annotationService.listUserAnnotation(it))
        }
        responseSuccess(annotations)
    }

    def listByImage = {
        ImageInstance image = imageInstanceService.read(params.long('id'))
        if (image) responseSuccess(annotationService.list(image))
        else responseNotFound("Image", params.id)
    }

    def listByProject = {
        Project project = projectService.read(params.long('id'), new Project())
        Integer offset = params.offset!=null? params.getInt('offset') : 0
        Integer max = params.max!=null? params.getInt('max') : Integer.MAX_VALUE
        Collection<SecUser> userList = []
        if (params.users != null && params.users != "null") {
            if (params.users != "") userList = userService.list(project, params.users.split("_").collect{ Long.parseLong(it)})
        }
        else {
            userList = userService.list(project)
        }
        Collection<ImageInstance> imageInstanceList = []
        if (params.images != null && params.images != "null") {
            if (params.images != "") imageInstanceList = imageInstanceService.list(project, params.images.split("_").collect{ Long.parseLong(it)})
        } else {
            imageInstanceList = imageInstanceService.list(project)
        }
        def sessionFactory

        if (project) {
            def list = annotationService.list(project, userList, imageInstanceList, (params.noTerm == "true"), (params.multipleTerm == "true"))
            if(params.offset!=null) responseSuccess([size:list.size(),collection:substract(list,offset,max)])
            else responseSuccess(list)
        }
        else responseNotFound("Project", params.id)
    }

    def listByImageAndUser = {
        def image = imageInstanceService.read(params.long('idImage'))
        def user = userService.read(params.idUser)
        if (image && user && params.bbox) {
            responseSuccess(annotationService.list(image, user, (String) params.bbox))
        }
        else if (image && user) responseSuccess(annotationService.list(image, user))
        else if (!user) responseNotFound("User", params.idUser)
        else if (!image) responseNotFound("Image", params.idImage)
    }

    def listAnnotationByTerm = {
        Term term = termService.read(params.long('idterm'))
        if (term) responseSuccess(annotationService.list(term))
        else responseNotFound("Annotation Term", "Term", params.idterm)
    }

    def listAnnotationByProjectAndTerm = {
        log.info "listAnnotationByProjectAndTerm"
        Term term = termService.read(params.long('idterm'))
        Project project = projectService.read(params.long('idproject'), new Project())

        Integer offset = params.offset!=null? params.getInt('offset') : 0
        Integer max = params.max!=null? params.getInt('max') : Integer.MAX_VALUE


        println "offset=$offset max=$max"

        Collection<SecUser> userList = []
        if (params.users != null && params.users != "null") {
            if (params.users != "") userList = userService.list(project, params.users.split("_").collect{ Long.parseLong(it)})
        }
        else {
            userList = userService.list(project)
        }

        log.info "userList="+userList
        Collection<ImageInstance> imageInstanceList = []
        if (params.images != null && params.images != "null") {
            if (params.images != "") imageInstanceList = imageInstanceService.list(project, params.images.split("_").collect{ Long.parseLong(it)})
        } else {
            imageInstanceList = imageInstanceService.list(project)
        }

        if (term == null) responseNotFound("Term", params.idterm)
        else if (project == null) responseNotFound("Project", params.idproject)
        /*else if (userList.isEmpty()) responseNotFound("Users", params.users)
        else if (imageInstanceList.isEmpty()) responseNotFound("ImageInstance", params.images)*/
        else if(!params.suggestTerm) {
            def list = annotationService.list(project, term, userList, imageInstanceList)
            if(params.offset!=null) responseSuccess([size:list.size(),collection:mergeResults(substract(list,offset,max))])
            else responseSuccess(list)
        }
        else {
            Term suggestedTerm = termService.read(params.suggestTerm)
            def list = annotationService.list(project, userList, term, suggestedTerm, Job.read(params.long('job')))
            if(params.offset!=null) responseSuccess([size:list.size(),collection:mergeResults(substract(list,offset,max))])
            else responseSuccess(list)
        }
    }




       //return a list of annotation (if list = [[annotation1,rate1, term1, expectedTerm1],..], add rate value in annotation]
       private def mergeResults(def list) {
           //list = [ [a,b],...,[x,y]]  => [a.rate = b, x.rate = y...]
           if(list.isEmpty() || list[0] instanceof Annotation) return list
           def result = []
           list.each {
               Annotation annotation = it[0]
               annotation.rate = it[1]
               annotation.idTerm = it[2]
               annotation.idExpectedTerm = it[3]
               result << annotation
           }
           return result
       }


    def downloadDocumentByProject = {  //and filter by users and terms !
        // Export service provided by Export plugin

        Project project = projectService.read(params.long('id'),new Project())
        if (!project) responseNotFound("Project", params.long('id'))

        projectService.checkAuthorization(project)
        def users = []
        if (params.users != null && params.users != "") {
            params.users.split(",").eacdataSourceh { id ->
                users << Long.parseLong(id)
            }
        }
        def terms = []
        if (params.terms != null && params.terms != "") {
            params.terms.split(",").each {  id ->
                terms << Long.parseLong(id)
            }
        }
        def images = []
        if (params.images != null && params.images != "") {
            params.images.split(",").each {  id ->
                images << Long.parseLong(id)
            }
        }
        def termsName = Term.findAllByIdInList(terms).collect{ it.toString() }
        def usersName = SecUser.findAllByIdInList(users).collect{ it.toString() }
        def imageInstances = ImageInstance.findAllByIdInList(images)

        if (params?.format && params.format != "html") {
            def exporterIdentifier = params.format;
            if (exporterIdentifier == "xls") exporterIdentifier = "excel"
            response.contentType = grailsApplication.config.grails.mime.types[params.format]
            SimpleDateFormat  simpleFormat = new SimpleDateFormat("yyyyMMdd_hhmmss");
            String datePrefix = simpleFormat.format(new Date())<commit_msg>Fix error in annotation listing<commit_after>package be.cytomine.api.ontology

import be.cytomine.Exception.CytomineException
import be.cytomine.Exception.WrongArgumentException
import be.cytomine.api.RestController
import be.cytomine.api.UrlApi
import be.cytomine.image.ImageInstance
import be.cytomine.ontology.Annotation
import be.cytomine.ontology.AnnotationTerm
import be.cytomine.ontology.Term
import be.cytomine.processing.Job
import be.cytomine.project.Project
import be.cytomine.security.SecUser
import be.cytomine.security.User
import be.cytomine.social.SharedAnnotation
import grails.converters.JSON

import java.awt.image.BufferedImage
import java.text.SimpleDateFormat
import javax.imageio.ImageIO
import groovy.sql.Sql
import be.cytomine.ontology.AlgoAnnotationTerm
import be.cytomine.security.UserJob

class RestAnnotationController extends RestController {

    def exportService
    def grailsApplication
    def annotationService
    def domainService
    def termService
    def imageInstanceService
    def userService
    def projectService
    def cytomineService
    def mailService
    def dataSource



    def list = {
        def annotations = []
        def projects = projectService.list()
        projects.each {
            annotations.addAll(annotationService.listUserAnnotation(it))
        }
        responseSuccess(annotations)
    }

    def listByImage = {
        ImageInstance image = imageInstanceService.read(params.long('id'))
        if (image) responseSuccess(annotationService.list(image))
        else responseNotFound("Image", params.id)
    }

    def listByProject = {
        Project project = projectService.read(params.long('id'), new Project())
        Integer offset = params.offset!=null? params.getInt('offset') : 0
        Integer max = params.max!=null? params.getInt('max') : Integer.MAX_VALUE
        Collection<SecUser> userList = []
        if (params.users != null && params.users != "null") {
            if (params.users != "") userList = userService.list(project, params.users.split("_").collect{ Long.parseLong(it)})
        }
        else {
            userList = userService.list(project)
        }
        Collection<ImageInstance> imageInstanceList = []
        if (params.images != null && params.images != "null") {
            if (params.images != "") imageInstanceList = imageInstanceService.list(project, params.images.split("_").collect{ Long.parseLong(it)})
        } else {
            imageInstanceList = imageInstanceService.list(project)
        }
        def sessionFactory

        if (project) {
            def list = annotationService.list(project, userList, imageInstanceList, (params.noTerm == "true"), (params.multipleTerm == "true"))
            if(params.offset!=null) responseSuccess([size:list.size(),collection:substract(list,offset,max)])
            else responseSuccess(list)
        }
        else responseNotFound("Project", params.id)
    }

    def listByImageAndUser = {
        def image = imageInstanceService.read(params.long('idImage'))
        def user = userService.read(params.idUser)
        if (image && user && params.bbox) {
            responseSuccess(annotationService.list(image, user, (String) params.bbox))
        }
        else if (image && user) responseSuccess(annotationService.list(image, user))
        else if (!user) responseNotFound("User", params.idUser)
        else if (!image) responseNotFound("Image", params.idImage)
    }

    def listAnnotationByTerm = {
        Term term = termService.read(params.long('idterm'))
        if (term) responseSuccess(annotationService.list(term))
        else responseNotFound("Annotation Term", "Term", params.idterm)
    }

    def listAnnotationByProjectAndTerm = {
        log.info "listAnnotationByProjectAndTerm"
        Term term = termService.read(params.long('idterm'))
        Project project = projectService.read(params.long('idproject'), new Project())

        Integer offset = params.offset!=null? params.getInt('offset') : 0
        Integer max = params.max!=null? params.getInt('max') : Integer.MAX_VALUE


        println "offset=$offset max=$max"

        Collection<SecUser> userList = []
        if (params.users != null && params.users != "null") {
            if (params.users != "") userList = userService.list(project, params.users.split("_").collect{ Long.parseLong(it)})
        }
        else {
            userList = userService.list(project)
        }

        log.info "userList="+userList
        Collection<ImageInstance> imageInstanceList = []
        if (params.images != null && params.images != "null") {
            if (params.images != "") imageInstanceList = imageInstanceService.list(project, params.images.split("_").collect{ Long.parseLong(it)})
        } else {
            imageInstanceList = imageInstanceService.list(project)
        }

        if (term == null) responseNotFound("Term", params.idterm)
        else if (project == null) responseNotFound("Project", params.idproject)
        /*else if (userList.isEmpty()) responseNotFound("Users", params.users)
        else if (imageInstanceList.isEmpty()) responseNotFound("ImageInstance", params.images)*/
        else if(!params.suggestTerm) {
            def list = annotationService.list(project, term, userList, imageInstanceList)
            if(params.offset!=null) responseSuccess([size:list.size(),collection:mergeResults(substract(list,offset,max))])
            else responseSuccess(list)
        }
        else {
            Term suggestedTerm = termService.read(params.suggestTerm)
            def list = annotationService.list(project, userList, term, suggestedTerm, Job.read(params.long('job')))
            if(params.offset!=null) responseSuccess([size:list.size(),collection:mergeResults(substract(list,offset,max))])
            else responseSuccess(list)
        }
    }




       //return a list of annotation (if list = [[annotation1,rate1, term1, expectedTerm1],..], add rate value in annotation]
       private def mergeResults(def list) {
           //list = [ [a,b],...,[x,y]]  => [a.rate = b, x.rate = y...]
           if(list.isEmpty() || list[0] instanceof Annotation) return list
           def result = []
           list.each {
               Annotation annotation = it[0]
               annotation.rate = it[1]
               annotation.idTerm = it[2]
               annotation.idExpectedTerm = it[3]
               result << annotation
           }
           return result
       }


    def downloadDocumentByProject = {  //and filter by users and terms !
        // Export service provided by Export plugin

        Project project = projectService.read(params.long('id'),new Project())
        if (!project) responseNotFound("Project", params.long('id'))

        projectService.checkAuthorization(project)
        def users = []
        if (params.users != null && params.users != "") {
            params.users.split(",").each { id ->
                users << Long.parseLong(id)
            }
        }
        def terms = []
        if (params.terms != null && params.terms != "") {
            params.terms.split(",").each {  id ->
                terms << Long.parseLong(id)
            }
        }
        def images = []
        if (params.images != null && params.images != "") {
            params.images.split(",").each {  id ->
                images << Long.parseLong(id)
            }
        }
        def termsName = Term.findAllByIdInList(terms).collect{ it.toString() }
        def usersName = SecUser.findAllByIdInList(users).collect{ it.toString() }
        def imageInstances = ImageInstance.findAllByIdInList(images)

        if (params?.format && params.format != "html") {
            def exporterIdentifier = params.format;
            if (exporterIdentifier == "xls") exporterIdentifier = "excel"
            response.contentType = grailsApplication.config.grails.mime.types[params.format]
            SimpleDateFormat  simpleFormat = new SimpleDateFormat("yyyyMMdd_hhmmss");
            String datePrefix = simpleFormat.format(new Date())
{'fraction_non_alphanumeric': 0.11940483570985741, 'fraction_numerical': 0.0013639181649101053, 'mean_word_length': 2.727987058007858, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6443123', 'n_tokens_mistral': 4366, 'n_tokens_neox': 4256, 'n_words': 889}