<commit_before>package whocame

import camp.Camper
import camp.Counselor
import camp.CounselorTeam
import camp.Grade
import demographic.Address;
import whoCame.GroovyExcelParser

class ImporterController {

    def index() { }
	
	
	def counselorImporter(){
		boolean importProcess = true
		
		
		//String filename = this.getServletConfig().getServletContext().getResourceAsStream("/files/counselor.xlsx");
	//	println("HOLA::::::"+System.getenv('OPENSHIFT_DATA_DIR'))
		String filename= g.resource(dir: 'files', file: 'counselor.xlsx', absolute: true)
		println("---------->"+filename)
		boolean append = params.append?:false
		String particularGrade = params.grade?:null
		if(!append){
			Counselor.findAll().each {
				//it?.team?.counselors?.remove(it)
				it?.team?.removeFromCounselors(it)
				it.delete()
			}
			
		}
		GroovyExcelParser parser = new GroovyExcelParser()
		def (headers, rows) = parser.parse(filename)
	
		rows.eachWithIndex { row, i ->
			Counselor counselor
			try {
			def mapRecords = parser.toMap(headers, row)
			if(!particularGrade || (particularGrade && particularGrade == mapRecords['gradeName'])){
				
			counselor = new Counselor(mapRecords)
			counselor.setPersonAddress(new Address(mapRecords))
		
			
			Grade grade = Grade.findByGradeName(mapRecords['gradeName'])
			if(grade){
			
				CounselorTeam team = CounselorTeam.findByGrade(grade)
				team.addToCounselors(counselor)
				
			}
				if(!counselor.save()){
					
					importProcess = false
					throw new Exception ("Counselor save failed at row"+ i+1)
				}
			}
			} catch(Exception e) {
				println "***********---------***********"
				println e.toString()
				counselor.errors.allErrors.each {error ->
					println error.toString()
				}
				println "***********---------***********"
			}
		}
		
		if(importProcess){
			render(contentType: "application/json") {
				result(operation: 'ok')
			}
		}else{
			render(contentType: "application/json") {
				result(operation: 'error', description:'import error')
				}
		}
		
	}
	def camperImporter(){
		boolean importProcess = true
		String filename= g.resource(dir: 'files', file: 'campers.xlsx', absolute: true)
		//def filename = 'web-app/files/campers.xlsx'
		boolean append = params.append?:false
		
		if(!append){
			Camper.findAll().each {
				it?.camperGrade?.removeFromCampers(it)
				it.delete()
			}
			
		}
		GroovyExcelParser parser = new GroovyExcelParser()
		def (headers, rows) = parser.parse(filename)
		rows.eachWithIndex { row, i ->
			Camper camper
			try {
			def mapRecords = parser.toMap(headers, row)
			camper = new Camper(mapRecords)
			
				Grade grade = Grade.findByGradeName(mapRecords['gradeName'])
				if(grade){
					grade.addToCampers(camper)
					
				}else{
					throw new Exception ("No grade found at row"+ i+1)
				}
				
				if(!camper.save()){
					importProcess = false
					throw new Exception ("Camper save failed at row"+ i+1)
				}
			} catch(Exception e) {<commit_msg>-Append campers importer<commit_after>package whocame

import camp.Camper
import camp.Counselor
import camp.CounselorTeam
import camp.Grade
import demographic.Address;
import whoCame.GroovyExcelParser

class ImporterController {

    def index() { }
	
	
	def counselorImporter(){
		boolean importProcess = true
		
		
		//String filename = this.getServletConfig().getServletContext().getResourceAsStream("/files/counselor.xlsx");
	//	println("HOLA::::::"+System.getenv('OPENSHIFT_DATA_DIR'))
		String filename= g.resource(dir: 'files', file: 'counselor.xlsx', absolute: true)
		println("---------->"+filename)
		boolean append = params.append?:false
		String particularGrade = params.grade?:null
		if(!append){
			Counselor.findAll().each {
				//it?.team?.counselors?.remove(it)
				it?.team?.removeFromCounselors(it)
				it.delete()
			}
			
		}
		GroovyExcelParser parser = new GroovyExcelParser()
		def (headers, rows) = parser.parse(filename)
	
		rows.eachWithIndex { row, i ->
			Counselor counselor
			try {
			def mapRecords = parser.toMap(headers, row)
			if(!particularGrade || (particularGrade && particularGrade == mapRecords['gradeName'])){
				
			counselor = new Counselor(mapRecords)
			counselor.setPersonAddress(new Address(mapRecords))
		
			
			Grade grade = Grade.findByGradeName(mapRecords['gradeName'])
			if(grade){
			
				CounselorTeam team = CounselorTeam.findByGrade(grade)
				team.addToCounselors(counselor)
				
			}
				if(!counselor.save()){
					
					importProcess = false
					throw new Exception ("Counselor save failed at row"+ i+1)
				}
			}
			} catch(Exception e) {
				println "***********---------***********"
				println e.toString()
				counselor.errors.allErrors.each {error ->
					println error.toString()
				}
				println "***********---------***********"
			}
		}
		
		if(importProcess){
			render(contentType: "application/json") {
				result(operation: 'ok')
			}
		}else{
			render(contentType: "application/json") {
				result(operation: 'error', description:'import error')
				}
		}
		
	}
	def camperImporter(){
		boolean importProcess = true
		String filename
		//def filename = 'web-app/files/campers.xlsx'
		boolean append = params.append?:false
		
		if(!append){
			filename= g.resource(dir: 'files', file: 'campers.xlsx', absolute: true)
			Camper.findAll().each {
				it?.camperGrade?.removeFromCampers(it)
				it.delete()
			}
			
		}else{
		filename= g.resource(dir: 'files', file: 'extraCampers.xlsx', absolute: true)
		}
		GroovyExcelParser parser = new GroovyExcelParser()
		def (headers, rows) = parser.parse(filename)
		rows.eachWithIndex { row, i ->
			Camper camper
			try {
			def mapRecords = parser.toMap(headers, row)
			camper = new Camper(mapRecords)
			
				Grade grade = Grade.findByGradeName(mapRecords['gradeName'])
				if(grade){
					grade.addToCampers(camper)
					
				}else{
					throw new Exception ("No grade found at row"+ i+1)
				}
				
				if(!camper.save()){
					importProcess = false
					throw new Exception ("Camper save failed at row"+ i+1)
				}
			} catch(Exception e) {
{'fraction_non_alphanumeric': 0.1494195688225539, 'fraction_numerical': 0.0009950248756218905, 'mean_word_length': 3.4021897810218977, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5828174', 'n_tokens_mistral': 2442, 'n_tokens_neox': 1872, 'n_words': 389}