<commit_before>/**
 * Importer controller
 *
 * The importer controller handles the uploading of tabular, comma delimited and Excel format
 * based files. When uploaded a preview is shown of the data and the user can adjust the column
 * type. Data in cells which don't correspond to the specified column type will be represented as "#error".
 *
 * The importer controller catches the actions and consecutively performs the
 * logic behind it.
 *
 * @package	importer
 * @author	<EMAIL>
 * @since	20100126
 *
 * Revision information:
 * $Rev$
 * $Author$
 * $Date$
 */

package dbnp.importer

import dbnp.studycapturing.Template
import dbnp.studycapturing.Study
import dbnp.studycapturing.Subject
import dbnp.studycapturing.Event

import dbnp.studycapturing.Sample
import dbnp.studycapturing.TemplateFieldType
import dbnp.studycapturing.TemplateField
import grails.converters.JSON
import org.apache.poi.hssf.usermodel.HSSFWorkbook

class ImporterController {
    def ImporterService    

    /**
     * Default page
     **/
    def index = { 	
    }

    def simpleWizard = {
	render(view:"index_simple", model:[studies:Study.list(), entities: grailsApplication.config.gscf.domain.importableEntities])
    }

    def advancedWizard = {
	render(view:"index_advanced", model:[templates:Template.list()])
    }

    /**
    * This method will move the uploaded file to a temporary path and send the header
    * and the first n rows to the preview
    * @param importfile uploaded file to import
    * @param study.id study identifier
    */
    def upload_advanced = {
	def wb = handleUpload('importfile')

	session.importer_header = ImporterService.getHeader(wb, 0)
	session.importer_study = Study.get(params.study.id.toInteger())
	session.importer_template_id = params.template_id
	session.importer_workbook = wb

        render (view:"step1_advanced", model:[header:session.importer_header, datamatrix:ImporterService.getDatamatrix(wb, session.importer_header, 0, 5)])
    }

    /**
    * This method will move the uploaded file to a temporary path and send the header
    * and the rows to the postview
    *
    * @param importfile uploaded file to import
    * @param entity string representation of the entity chosen
    */
    def upload_simple = {
	def wb = handleUpload('importfile')
	def selectedentities = []
	def entity = grailsApplication.config.gscf.domain.importableEntities.get(params.entity).entity
	def entityClass = Class.forName(entity, true, this.getClass().getClassLoader())

	// Initialize some session variables
	session.importer_workbook = wb
	session.importer_study = Study.get(params.study.id.toInteger())
	session.importer_template_id = params.template_id
	session.importer_sheetindex = params.sheetindex.toInteger() -1 // 0 == first sheet
	session.importer_datamatrix_start = params.datamatrix_start.toInteger() -1 // 0 == first row
	session.importer_headerrow = params.headerrow.toInteger()

	// Get the header from the Excel file using the arguments given in the first step of the wizard
	session.importer_header = ImporterService.getHeader(wb,
							    session.importer_sheetindex,
							    session.importer_headerrow,
							    session.importer_datamatrix_start,
							    entityClass)
	
	// Initialize 'selected entities', used to show entities above the columns
	session.importer_header.each {
	    selectedentities.add([name:params.entity, columnindex:it.key.toInteger()])
	}

	def templates = Template.get(session.importer_template_id)
	
	render(view:"step2_simple", model:[entities: selectedentities,
					   header:session.importer_header,
					   datamatrix:ImporterService.getDatamatrix(
					       wb, session.importer_header,
					       session.importer_sheet,
					       session.importer_datamatrix_start,
					       5),
					   templates:templates])
    }

    /**
     * This method handles a file being uploaded and storing it in a temporary directory
     * and returning a workbook
     *
     * @param formfilename name used for the file field in the form
     * @return workbook object reference
     */
    private HSSFWorkbook handleUpload(formfilename) {

	def downloadedfile = request.getFile(formfilename);
        def tempfile = new File(System.getProperty('java.io.tmpdir') + File.separatorChar + System.currentTimeMillis() + ".nmcdsp")
        downloadedfile.transferTo(tempfile)

	return ImporterService.getWorkbook(new FileInputStream(tempfile))
    }

    /**
     * Method to save the missing properties
     * @param entity entity class we are using (dbnp.studycapturing.Subject etc.)
     */

    def saveMissingProperties = {
<commit_msg>- fixed sheetindex argument<commit_after>/**
 * Importer controller
 *
 * The importer controller handles the uploading of tabular, comma delimited and Excel format
 * based files. When uploaded a preview is shown of the data and the user can adjust the column
 * type. Data in cells which don't correspond to the specified column type will be represented as "#error".
 *
 * The importer controller catches the actions and consecutively performs the
 * logic behind it.
 *
 * @package	importer
 * @author	<EMAIL>
 * @since	20100126
 *
 * Revision information:
 * $Rev$
 * $Author$
 * $Date$
 */

package dbnp.importer

import dbnp.studycapturing.Template
import dbnp.studycapturing.Study
import dbnp.studycapturing.Subject
import dbnp.studycapturing.Event

import dbnp.studycapturing.Sample
import dbnp.studycapturing.TemplateFieldType
import dbnp.studycapturing.TemplateField
import grails.converters.JSON
import org.apache.poi.hssf.usermodel.HSSFWorkbook

class ImporterController {
    def ImporterService    

    /**
     * Default page
     **/
    def index = { 	
    }

    def simpleWizard = {
	render(view:"index_simple", model:[studies:Study.list(), entities: grailsApplication.config.gscf.domain.importableEntities])
    }

    def advancedWizard = {
	render(view:"index_advanced", model:[templates:Template.list()])
    }

    /**
    * This method will move the uploaded file to a temporary path and send the header
    * and the first n rows to the preview
    * @param importfile uploaded file to import
    * @param study.id study identifier
    */
    def upload_advanced = {
	def wb = handleUpload('importfile')

	session.importer_header = ImporterService.getHeader(wb, 0)
	session.importer_study = Study.get(params.study.id.toInteger())
	session.importer_template_id = params.template_id
	session.importer_workbook = wb

        render (view:"step1_advanced", model:[header:session.importer_header, datamatrix:ImporterService.getDatamatrix(wb, session.importer_header, 0, 5)])
    }

    /**
    * This method will move the uploaded file to a temporary path and send the header
    * and the rows to the postview
    *
    * @param importfile uploaded file to import
    * @param entity string representation of the entity chosen
    */
    def upload_simple = {
	def wb = handleUpload('importfile')
	def selectedentities = []
	def entity = grailsApplication.config.gscf.domain.importableEntities.get(params.entity).entity
	def entityClass = Class.forName(entity, true, this.getClass().getClassLoader())

	// Initialize some session variables
	session.importer_workbook = wb
	session.importer_study = Study.get(params.study.id.toInteger())
	session.importer_template_id = params.template_id
	session.importer_sheetindex = params.sheetindex.toInteger() -1 // 0 == first sheet
	session.importer_datamatrix_start = params.datamatrix_start.toInteger() -1 // 0 == first row
	session.importer_headerrow = params.headerrow.toInteger()

	// Get the header from the Excel file using the arguments given in the first step of the wizard
	session.importer_header = ImporterService.getHeader(wb,
							    session.importer_sheetindex,
							    session.importer_headerrow,
							    session.importer_datamatrix_start,
							    entityClass)
	
	// Initialize 'selected entities', used to show entities above the columns
	session.importer_header.each {
	    selectedentities.add([name:params.entity, columnindex:it.key.toInteger()])
	}

	def templates = Template.get(session.importer_template_id)
	
	render(view:"step2_simple", model:[entities: selectedentities,
					   header:session.importer_header,
					   datamatrix:ImporterService.getDatamatrix(
					       wb, session.importer_header,
					       session.importer_sheetindex,
					       session.importer_datamatrix_start,
					       5),
					   templates:templates])
    }

    /**
     * This method handles a file being uploaded and storing it in a temporary directory
     * and returning a workbook
     *
     * @param formfilename name used for the file field in the form
     * @return workbook object reference
     */
    private HSSFWorkbook handleUpload(formfilename) {

	def downloadedfile = request.getFile(formfilename);
        def tempfile = new File(System.getProperty('java.io.tmpdir') + File.separatorChar + System.currentTimeMillis() + ".nmcdsp")
        downloadedfile.transferTo(tempfile)

	return ImporterService.getWorkbook(new FileInputStream(tempfile))
    }

    /**
     * Method to save the missing properties
     * @param entity entity class we are using (dbnp.studycapturing.Subject etc.)
     */

    def saveMissingProperties = {

{'fraction_non_alphanumeric': 0.08660395718456049, 'fraction_numerical': 0.003892312682452157, 'mean_word_length': 3.7217968351199593, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5324280', 'n_tokens_mistral': 2870, 'n_tokens_neox': 2604, 'n_words': 721}