<commit_before>package dbnp.export

import org.apache.poi.ss.usermodel.Sheet
import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import org.apache.poi.ss.usermodel.DataFormatter

import dbnp.studycapturing.*
import org.dbnp.gdt.*
import dbnp.authentication.SecUser

import grails.util.Holders

/**
 * Exporter to export multiple studies/assay into a single excel file
 * There will be three sheets: one for study data, one for subject data and one for sample data
 */
public class MultipleStudiesExporter implements Exporter {
    @Lazy
    def assayService = {
        Holders.grailsApplication.getMainContext().getBean("assayService")
    }()
    
    @Lazy
    def authenticationService = {
        Holders.grailsApplication.getMainContext().getBean("authenticationService")
    }()
    
    @Lazy
    def apiService = {
        Holders.grailsApplication.getMainContext().getBean("apiService")
    }()
    
    /**
     * SecUser that is used for authorization
     */
    SecUser user

    /**
     * Returns an identifier that describes this export
     */
    public String getIdentifier() { "Combined excel" }
    
    /**
     * Returns the type of entitites to export. Could be Study or Assay
     */
    public String getType() { "Study" }
    
    /**
     * Returns whether this exporter supports exporting multiple entities at once
     * If so, the class should have a proper implementation of the exportMultiple method
     */
    public boolean supportsMultiple() { true }
    
    /**
     * Use the given parameters for exporting
     */
    public void setParameters(def parameters) {
        // Ignore the parameters for now
    }
    
    /**
     * Exports multiple entities to the outputstream
     */
    public void exportMultiple( def studies, OutputStream out ) { 
        
        // Collect data to export for each study
        def assays = Assay.findAll( "FROM Assay WHERE parent IN (:studies)", [ studies: studies ] )
        def sampleData = []
        
        def studyData = this.collectStudyData(studies)
        def subjectsData = this.collectSubjectData(studies)
        
        if(assays) {
            def samples = assays*.samples.flatten().unique()
            sampleData = this.collectSampleData(assays, null, samples, authenticationService.getLoggedInUser())
        }
        
        // Combine everything into the excel workbook
        Workbook wb = new XSSFWorkbook()

        Sheet studySheet = wb.createSheet("Studies")
        Sheet subjectSheet = wb.createSheet("Subjects")<commit_msg>FT-1507 Sort sample names in multiple study export<commit_after>package dbnp.export

import org.apache.poi.ss.usermodel.Sheet
import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import org.apache.poi.ss.usermodel.DataFormatter

import dbnp.studycapturing.*
import org.dbnp.gdt.*
import dbnp.authentication.SecUser

import grails.util.Holders

/**
 * Exporter to export multiple studies/assay into a single excel file
 * There will be three sheets: one for study data, one for subject data and one for sample data
 */
public class MultipleStudiesExporter implements Exporter {
    @Lazy
    def assayService = {
        Holders.grailsApplication.getMainContext().getBean("assayService")
    }()
    
    @Lazy
    def authenticationService = {
        Holders.grailsApplication.getMainContext().getBean("authenticationService")
    }()
    
    @Lazy
    def apiService = {
        Holders.grailsApplication.getMainContext().getBean("apiService")
    }()
    
    /**
     * SecUser that is used for authorization
     */
    SecUser user

    /**
     * Returns an identifier that describes this export
     */
    public String getIdentifier() { "Combined excel" }
    
    /**
     * Returns the type of entitites to export. Could be Study or Assay
     */
    public String getType() { "Study" }
    
    /**
     * Returns whether this exporter supports exporting multiple entities at once
     * If so, the class should have a proper implementation of the exportMultiple method
     */
    public boolean supportsMultiple() { true }
    
    /**
     * Use the given parameters for exporting
     */
    public void setParameters(def parameters) {
        // Ignore the parameters for now
    }
    
    /**
     * Exports multiple entities to the outputstream
     */
    public void exportMultiple( def studies, OutputStream out ) { 
        
        // Collect data to export for each study
        def assays = Assay.findAll( "FROM Assay WHERE parent IN (:studies)", [ studies: studies ] )
        def sampleData = []
        
        def studyData = this.collectStudyData(studies)
        def subjectsData = this.collectSubjectData(studies)

        if(assays) {
            // Get the samples and sort them; this will be the sort order to use for
            // both retrieving the assay data and the measurements
            def samples = assays*.samples.flatten().unique().sort({it.name})
            sampleData = this.collectSampleData(assays, null, samples, authenticationService.getLoggedInUser())
        }
        
        // Combine everything into the excel workbook
        Workbook wb = new XSSFWorkbook()

        Sheet studySheet = wb.createSheet("Studies")
        Sheet subjectSheet = wb.createSheet("Subjects")
{'fraction_non_alphanumeric': 0.08505875769445999, 'fraction_numerical': 0.0007461294534601753, 'mean_word_length': 2.6132075471698113, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1082229', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1426, 'n_words': 439}