Changeset 1112 for trunk/grails-app
- Timestamp:
- Nov 10, 2010, 9:37:02 AM (10 years ago)
- Location:
- trunk/grails-app
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/dbnp/exporter/ExporterController.groovy
r1052 r1112 22 22 import org.apache.poi.poifs.filesystem.POIFSFileSystem 23 23 import org.apache.poi.ss.usermodel.DataFormatter 24 import java.util.zip.ZipEntry 25 import java.util.zip.ZipOutputStream 26 import java.util.zip.ZipInputStream 27 import javax.servlet.ServletOutputStream 24 28 25 29 import grails.plugins.springsecurity.Secured … … 30 34 def ImporterService 31 35 32 33 34 35 36 36 /* 37 * List of all studies for selection the study to export 38 * Using the same code as 'list' into StudyController 39 */ 40 def index = { 37 41 38 42 def user = AuthenticationService.getLoggedInUser() … … 70 74 } 71 75 76 def print = { 77 render params 78 render "PRINTING" 79 } 80 81 82 def export = { 83 84 def studies = [] 85 for ( j in dbnp.studycapturing.Study.list() ){ 86 if (params.containsKey(j.title)){ 87 studies.add(j) 88 } 89 } 90 91 if(studies.size()>1){ 92 // Create a ZIP file containing all the SimpleTox files 93 def files = [] 94 for (studyInstance in studies){ 95 downloadFile(studyInstance,false) 96 files.add(new File("web-app/fileuploads/"+studyInstance.title+"_SimpleTox.xls")) 97 } 98 99 response.setContentType( "application/zip" ) ; 100 response.addHeader( "Content-Disposition", "attachment; filename=\"GSCF_SimpleToxStudies.zip\"" ) ; 101 102 // get a ZipOutputStream, so we can zip our files together 103 ZipOutputStream outZip = new ZipOutputStream( response.getOutputStream() ); 104 105 // add SimpleTox files to the zip 106 for (outFiles in files){ 107 108 FileInputStream inStream = null 109 try 110 { 111 // Add ZIP entry to output stream. 112 outZip.putNextEntry( new ZipEntry( outFiles.getName() ) ) ; 113 114 inStream = new FileInputStream( outFiles ) 115 116 // Transfer bytes from the file to the ZIP file 117 byte[] buf = new byte[ 4096 ] ; 118 int len 119 while( ( len = inStream.read( buf ) ) > 0 ) 120 { 121 outZip.write( buf, 0, len ) 122 } 123 } 124 catch( Exception ex ) { } 125 finally 126 { 127 // Complete the entry 128 try{ outZip.closeEntry() } catch( Exception ex ) { } 129 try{ inStream.close() } catch( Exception ex ) { } 130 } 131 outFiles.delete() 132 } 133 outZip.flush() 134 outZip.close() 135 } 136 137 138 else { 139 def studyInstance = studies.getAt(0) 140 // make the file downloadable 141 if ((studyInstance!=null) && (studyInstance.samples.size()>0)){ 142 downloadFile(studyInstance,true) 143 } 144 else { 145 flash.message= "Error while exporting the file, please try again or choose another file" 146 redirect(action:index) 147 } 148 149 } 150 151 } 72 152 /* 73 153 * the export method will create a SimpleTox format for the selected study 74 154 */ 75 def export = { 76 //def studyInstance 77 def studies = [] 78 155 def downloadFile(studyInstance, boolean dl) { 79 156 // the attributes list for the SimpleTox format 80 157 def attributes_list = ["SubjectID","DataFile","HybName","SampleName","ArrayType","Label","StudyTitle","Array_ID", 81 158 "Species"] 82 83 // Get the selected study 84 for ( j in dbnp.studycapturing.Study.list() ){ 85 if (params.containsKey(j.title)){ 86 // studyInstance = j 87 studies.add(j) 88 } 89 } 90 91 // def studies = params*.key.collect{ Study.findByTitle(it) } 92 93 println "STUDIES : "+studies 94 95 for (studyInstance in studies) { 96 97 98 if (studyInstance!=null){ 99 100 println "StudyInstance :" +studyInstance 101 159 println studyInstance.samples.size() 160 println "StudyInstance :" + studyInstance 161 162 // The first row contains the attributes names 102 163 HSSFWorkbook wb = new HSSFWorkbook() 103 println " WORKBOOK : "+wb104 // The first row contains the attributes names105 164 HSSFSheet sheet = wb.createSheet() 106 165 HSSFRow row = sheet.createRow((short)0) … … 111 170 // Adding the next lines 112 171 for (s in 1..studyInstance.samples.size()){ 113 try {114 172 // creating new line for every sample 115 173 HSSFRow sub = sheet.createRow((short)s) … … 118 176 writeMandatoryFields(sub,sample,studyInstance) 119 177 120 // adding the subject domain + template properties 121 writeSubjectProperties(sub,sample,row) 122 123 // adding the samplingEvent domain + template properties 124 writeSamplingEventProperties(sub,sample,row) 178 try { 179 // adding the subject domain + template properties 180 writeSubjectProperties(sub,sample,row) 181 182 // adding the samplingEvent domain + template properties 183 writeSamplingEventProperties(sub,sample,row) 125 184 126 // adding samples domain + template properties 127 TemplateField sf = sample.giveFields().getAt(s) 128 //println studyInstance.samples.getAt(s-1).getFieldValue(sf.name) 129 130 // adding Event domaine + template properties 131 185 // adding EventGroup domain + template properties 186 // writeEventGroupProperties(sub,sample,rows) 187 188 // adding Sample domain + template properties 189 writeSampleProperties(sub,sample,row) 132 190 } 133 191 catch (Exception e){ 134 println "Error creating file" 135 } 136 } 137 138 // Make the file downloadable 139 response.setHeader("Content-disposition", "attachment;filename=\"${studyInstance.title}_SimpleTox.xls\"") 140 response.setContentType("application/octet-stream") 141 wb.write(response.outputStream) 142 response.outputStream.close() 143 } 192 println "Error adding properties" 193 } 194 } 195 196 // Make the file downlodable 197 if(dl) { 198 println "Creation for downloading the file "+studyInstance.title+"_SimpleTox.xls" 199 response.setHeader("Content-disposition", "attachment;filename=\"${studyInstance.title}_SimpleTox.xls\"") 200 response.setContentType("application/octet-stream") 201 wb.write(response.outputStream) 202 response.outputStream.close() 203 } 204 205 // Create the file and save into ZIP 206 if(!dl){ 207 FileOutputStream fileOut = new FileOutputStream("web-app/fileuploads/"+studyInstance.title+"_SimpleTox.xls", true) 208 wb.write(fileOut) 209 fileOut.close() 144 210 } 145 211 } 146 212 147 213 def writeMandatoryFields(sub,sample,study) { 148 149 try {150 214 // adding subject name in row 1 151 s ub.createCell((short)0).setCellValue(sample.parentSubject.name)215 sample.parentSubject.name ? sub.createCell((short)0).setCellValue(sample.parentSubject.name) : "not defined" 152 216 // adding sample in row 4 153 s ub.createCell((short)3).setCellValue(sample.name)217 sample.name!=null ? sub.createCell((short)3).setCellValue(sample.name) : "not defined" 154 218 // adding label (EventGroup) in row 6 155 219 for (ev in EventGroup.list()){ 156 if ( ev.subjects.name.contains(sample.parentSubject.name)) {220 if ( (sample.parentSubject.name) && (ev.subjects.name.contains(sample.parentSubject.name))) { 157 221 sub.createCell((short)5).setCellValue(ev.name) 158 222 break … … 165 229 sub.createCell((short)6).setCellValue(study.title) 166 230 // Species row 9 167 sub.createCell((short)8).setCellValue(sample.parentSubject.species.name) 168 } 169 catch (Exception e){ 170 println "Error during Mandatory Fields" 171 } 231 sample.parentSubject.species.name!=null ? sub.createCell((short)8).setCellValue(sample.parentSubject.species.name) : "not defined" 172 232 } 173 233 174 234 // writing subject properties 175 235 def writeSubjectProperties(sub,sample,row) { 176 try {177 236 for (u in 0..sample.parentSubject.giveFields().unique().size()-1){ 178 237 TemplateField tf = sample.parentSubject.giveFields().getAt(u) 179 row.createCell((short)9+u).setCellValue( tf.name)238 row.createCell((short)9+u).setCellValue("subject-"+tf.name) 180 239 sample.parentSubject.getFieldValue(tf.name) ? sub.createCell((short)9+u).setCellValue(sample.parentSubject.getFieldValue(tf.name).toString()) : "not define" 181 }182 }183 catch (Exception e){184 println "Error during Subject Properties"185 240 } 186 241 } … … 188 243 // writing samplingEvent properties 189 244 def writeSamplingEventProperties(sub,sample,row){ 190 try {191 245 for (t in 0..sample.parentEvent.giveFields().unique().size()-1){ 192 246 TemplateField tf =sample.parentEvent.giveFields().getAt(t) 193 row.createCell((short)9+sample.parentSubject.giveFields().unique().size()+t).setCellValue( tf.name)247 row.createCell((short)9+sample.parentSubject.giveFields().unique().size()+t).setCellValue("samplingEvent-"+tf.name) 194 248 sample.parentEvent.getFieldValue(tf.name) ? sub.createCell((short)9+sample.parentSubject.giveFields().unique().size()+t).setCellValue(sample.parentEvent.getFieldValue(tf.name).toString()) : "not define" 195 249 } 196 } 197 catch (Exception e) { 198 println "Error during Sampling Event properties" 199 } 200 } 201 250 } 251 252 // writing EventGroup properties 253 def writeEventGroupProperties(sub,sample,row){ 254 255 } 256 257 // writing sample properties 258 def writeSampleProperties(sub,sample,row){ 259 for (v in 0..sample.giveFields().unique().size()-1){ 260 TemplateField tf =sample.giveFields().getAt(v) 261 row.createCell((short)9+sample.parentSubject.giveFields().unique().size()+v+sample.parentEvent.giveFields().unique().size()).setCellValue("sample-"+tf.name) 262 sample.getFieldValue(tf.name) ? sub.createCell((short)9+sample.parentSubject.giveFields().unique().size()+v+sample.parentEvent.giveFields().unique().size()).setCellValue(sample.getFieldValue(tf.name).toString()) : "not define" 263 } 264 } 202 265 } -
trunk/grails-app/views/exporter/index.gsp
r1051 r1112 7 7 <g:set var="entityName" value="${message(code: 'study.label', default: 'Study')}" /> 8 8 <title>SimpleTox Exporter</title> 9 10 11 12 9 13 </head> 10 14 <body> 11 15 12 <g:form action="export">16 <g:formRemote url="[controller:'exporter',action:'export']" name="simpleToxForm" onComplete="file://" onFailure="alert('Error while exporting the file');" > 13 17 14 18 <div class="body"> 15 19 <h1>Export as SimpleTox</h1> 16 <br> Select the study you want to export in SimpleTox format.<br><br> 20 <br> Select the study you want to export in SimpleTox format.<br> 21 If you choose multiple studies, a ZIP file will be created. 22 <br><br> 17 23 <g:if test="${flash.message}"> 18 24 <div class="message">${flash.message}</div> … … 35 41 <tr class="${(i % 2) == 0 ? 'odd' : 'even'}"> 36 42 37 <td><input type="checkbox" name="${studyInstance.title}" id=" ${studyInstance.title}"></td>43 <td><input type="checkbox" name="${studyInstance.title}" id="checked_studies"></td> 38 44 39 <td><g:link action="show" id="${studyInstance.id}">${fieldValue(bean: studyInstance, field: "code")}</g:link></td>45 <td><g:link controller="study" action="show" id="${studyInstance.id}">${fieldValue(bean: studyInstance, field: "code")}</g:link></td> 40 46 <td> 41 47 ${fieldValue(bean: studyInstance, field: "title")} … … 71 77 </g:else> 72 78 </td> 73 74 79 </tr> 75 80 </g:each> … … 81 86 <div class="paginateButtons" id="button"> 82 87 </div> 83 84 < INPUT TYPE=submit name=submit Value="Export">88 89 <input type="submit" value="Export"/> 85 90 86 91 </div> 87 <div id="result"> </div>88 92 89 </g:form >93 </g:formRemote> 90 94 91 95 </body>
Note: See TracChangeset
for help on using the changeset viewer.