1 | /** |
---|
2 | * ExporterController Controller |
---|
3 | * |
---|
4 | * Description of my controller |
---|
5 | * |
---|
6 | * @author your email (+name?) |
---|
7 | * @since 2010mmdd |
---|
8 | * @package ??? |
---|
9 | * |
---|
10 | * Revision information: |
---|
11 | * $Rev: 1921 $ |
---|
12 | * $Author: t.w.abma@umcutrecht.nl $ |
---|
13 | * $Date: 2011-06-09 10:48:15 +0000 (do, 09 jun 2011) $ |
---|
14 | */ |
---|
15 | package dbnp.exporter |
---|
16 | |
---|
17 | import dbnp.studycapturing.* |
---|
18 | import org.dbnp.gdt.* |
---|
19 | |
---|
20 | import org.apache.poi.hssf.util.HSSFColor |
---|
21 | import org.apache.poi.* |
---|
22 | import org.apache.poi.hssf.usermodel.* |
---|
23 | import org.apache.poi.poifs.filesystem.POIFSFileSystem |
---|
24 | import org.apache.poi.ss.usermodel.DataFormatter |
---|
25 | import java.util.zip.ZipEntry |
---|
26 | import java.util.zip.ZipOutputStream |
---|
27 | import java.util.zip.ZipInputStream |
---|
28 | import javax.servlet.ServletOutputStream |
---|
29 | |
---|
30 | import grails.plugins.springsecurity.Secured |
---|
31 | |
---|
32 | @Secured(['IS_AUTHENTICATED_REMEMBERED']) |
---|
33 | class ExporterController { |
---|
34 | |
---|
35 | def authenticationService |
---|
36 | |
---|
37 | /* |
---|
38 | * List of all studies for selection the study to export |
---|
39 | * Using the same code as 'list' into StudyController |
---|
40 | */ |
---|
41 | def index = { |
---|
42 | |
---|
43 | def user = authenticationService.getLoggedInUser() |
---|
44 | def max = Math.min(params.max ? params.int('max') : 10, 100) |
---|
45 | |
---|
46 | def c = dbnp.studycapturing.Study.createCriteria() |
---|
47 | |
---|
48 | def studies = Study.giveReadableStudies(user, max); |
---|
49 | [studyInstanceList: studies, studyInstanceTotal: studies.count()] |
---|
50 | } |
---|
51 | |
---|
52 | def export = { |
---|
53 | def ids = params.list( 'ids' ); |
---|
54 | def tokens = params.list( 'tokens' ); |
---|
55 | def studies = [] |
---|
56 | |
---|
57 | ids.each { |
---|
58 | if( it.toString().isLong() ) { |
---|
59 | def study = Study.get( Long.valueOf( it ) ); |
---|
60 | if( study ) |
---|
61 | studies << study |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | // Also accept tokens for defining studies |
---|
66 | tokens.each { |
---|
67 | def study = Study.findByStudyUUID( it ); |
---|
68 | if( study ) |
---|
69 | studies << study; |
---|
70 | } |
---|
71 | |
---|
72 | if(studies.size()>1){ |
---|
73 | |
---|
74 | // Send the right headers for the zip file to be downloaded |
---|
75 | response.setContentType( "application/zip" ) ; |
---|
76 | response.addHeader( "Content-Disposition", "attachment; filename=\"GSCF_SimpleToxStudies.zip\"" ) ; |
---|
77 | |
---|
78 | // Create a ZIP file containing all the SimpleTox files |
---|
79 | ZipOutputStream zipFile = new ZipOutputStream( new BufferedOutputStream( response.getOutputStream() ) ); |
---|
80 | BufferedWriter zipWriter = new BufferedWriter( new OutputStreamWriter( zipFile ) ); |
---|
81 | |
---|
82 | // Loop through the given studies and export them |
---|
83 | for (studyInstance in studies){ |
---|
84 | if( studyInstance.samples?.size() ) { |
---|
85 | try { |
---|
86 | zipFile.putNextEntry( new ZipEntry( studyInstance.title + "_SimpleTox.xls" )); |
---|
87 | downloadFile(studyInstance, zipFile); |
---|
88 | zipWriter.flush(); |
---|
89 | zipFile.closeEntry(); |
---|
90 | } catch( Exception e ) { |
---|
91 | log.error "Error while writing excelfile for zip for study " + studyInstance?.title + ": " + e.getMessage(); |
---|
92 | } finally { |
---|
93 | // Always close zip entry |
---|
94 | try { |
---|
95 | zipWriter.flush(); |
---|
96 | zipFile.closeEntry(); |
---|
97 | } catch( Exception e ) { |
---|
98 | log.error "Error while closing excelfile for zip for study: " + e.getMessage(); |
---|
99 | } |
---|
100 | } |
---|
101 | } else { |
---|
102 | log.trace "Study " + studyInstance?.title + " doesn't contain any samples, so is not exported to simpleTox" |
---|
103 | |
---|
104 | // Add a text file with explanation in the zip file |
---|
105 | zipFile.putNextEntry(new ZipEntry( studyInstance.title + "_contains_no_samples.txt" ) ); |
---|
106 | zipFile.closeEntry(); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | // Close zipfile and flush to the user |
---|
111 | zipFile.close(); |
---|
112 | response.outputStream.flush(); |
---|
113 | |
---|
114 | } else { |
---|
115 | def studyInstance = studies.getAt(0) |
---|
116 | // make the file downloadable |
---|
117 | if ((studyInstance!=null) && (studyInstance.samples.size()>0)){ |
---|
118 | response.setHeader("Content-disposition", "attachment;filename=\"${studyInstance.title}_SimpleTox.xls\"") |
---|
119 | response.setContentType("application/octet-stream") |
---|
120 | downloadFile(studyInstance, response.getOutputStream()) |
---|
121 | response.getOutputStream().close() |
---|
122 | } else if( studyInstance.samples.size() == 0 ) { |
---|
123 | flash.message = "Given study doesn't contain any samples, so no excel file is created. Please choose another study."; |
---|
124 | redirect( action: 'index' ); |
---|
125 | } |
---|
126 | else { |
---|
127 | flash.message= "Error while exporting the file, please try again or choose another study." |
---|
128 | redirect( action: 'index' ) |
---|
129 | } |
---|
130 | |
---|
131 | } |
---|
132 | |
---|
133 | } |
---|
134 | /* |
---|
135 | * the export method will create a SimpleTox format for the selected study |
---|
136 | * and write the file to the given output stream |
---|
137 | */ |
---|
138 | def downloadFile(studyInstance, OutputStream outStream) { |
---|
139 | // the attributes list for the SimpleTox format |
---|
140 | def attributes_list = ["SubjectID","DataFile","HybName","SampleName","ArrayType","Label","StudyTitle","Array_ID", |
---|
141 | "Species"] |
---|
142 | //println studyInstance.samples.size() |
---|
143 | //println "StudyInstance :" + studyInstance |
---|
144 | |
---|
145 | // The first row contains the attributes names |
---|
146 | HSSFWorkbook wb = new HSSFWorkbook() |
---|
147 | HSSFSheet sheet = wb.createSheet() |
---|
148 | HSSFRow row = sheet.createRow((short)0) |
---|
149 | for (i in 0..attributes_list.size()){ |
---|
150 | row.createCell((short)i).setCellValue(attributes_list[i]) |
---|
151 | } |
---|
152 | |
---|
153 | // Adding the next lines |
---|
154 | for (s in 1..studyInstance.samples.size()){ |
---|
155 | // creating new line for every sample |
---|
156 | HSSFRow sub = sheet.createRow((short)s) |
---|
157 | def sample = studyInstance.samples.getAt(s-1) |
---|
158 | |
---|
159 | writeMandatoryFields(sub,sample,studyInstance) |
---|
160 | |
---|
161 | try { |
---|
162 | // adding the subject domain + template properties |
---|
163 | writeSubjectProperties(sub,sample,row) |
---|
164 | |
---|
165 | // adding the samplingEvent domain + template properties |
---|
166 | writeSamplingEventProperties(sub,sample,row) |
---|
167 | |
---|
168 | // adding EventGroup domain + template properties |
---|
169 | // writeEventGroupProperties(sub,sample,rows) |
---|
170 | |
---|
171 | // adding Sample domain + template properties |
---|
172 | writeSampleProperties(sub,sample,row) |
---|
173 | } |
---|
174 | catch (Exception e){ |
---|
175 | //println "Error adding properties" |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | wb.write( outStream ); |
---|
180 | } |
---|
181 | |
---|
182 | def writeMandatoryFields(sub,sample,study) { |
---|
183 | // adding subject name in row 1 |
---|
184 | sample.parentSubject ? sub.createCell((short)0).setCellValue(sample.parentSubject.name) : "not defined" |
---|
185 | // adding sample in row 4 |
---|
186 | sample.name!=null ? sub.createCell((short)3).setCellValue(sample.name) : "not defined" |
---|
187 | // adding label (EventGroup) in row 6 |
---|
188 | for (ev in EventGroup.list()){ |
---|
189 | if(sample.parentSubject){ |
---|
190 | if ( (sample.parentSubject.name) && (ev.subjects.name.contains(sample.parentSubject.name))) { |
---|
191 | sub.createCell((short)5).setCellValue(ev.name) |
---|
192 | break |
---|
193 | } |
---|
194 | else { |
---|
195 | sub.createCell((short)5).setCellValue(" ") |
---|
196 | }} |
---|
197 | else { |
---|
198 | sub.createCell((short)5).setCellValue(" ") |
---|
199 | } |
---|
200 | } |
---|
201 | // adding study title in row 7 |
---|
202 | sub.createCell((short)6).setCellValue(study.title) |
---|
203 | // Species row 9 |
---|
204 | // sample.parentSubject.species.name!=null ? sub.createCell((short)8).setCellValue(sample.parentSubject.species.name) : "not defined" |
---|
205 | sample.parentSubject ? sub.createCell((short)8).setCellValue(sample.parentSubject.species.name) : "not defined" |
---|
206 | } |
---|
207 | |
---|
208 | // writing subject properties |
---|
209 | def writeSubjectProperties(sub,sample,row) { |
---|
210 | if( sample.parentSubject ) { |
---|
211 | log.trace "----- SUBJECT -----" |
---|
212 | for (u in 0..sample.parentSubject.giveFields().unique().size()-1){ |
---|
213 | TemplateField tf = sample.parentSubject.giveFields().getAt(u) |
---|
214 | log.trace tf.name |
---|
215 | row.createCell((short)9+u).setCellValue(tf.name) |
---|
216 | sample.parentSubject.getFieldValue(tf.name) ? sub.createCell((short)9+u).setCellValue(sample.parentSubject.getFieldValue(tf.name).toString()) : "not define" |
---|
217 | } |
---|
218 | } else { |
---|
219 | log.trace "------ NO SUBJECT FOR SAMPLE " + sample.name + "-----"; |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | // writing samplingEvent properties |
---|
224 | def writeSamplingEventProperties(sub,sample,row){ |
---|
225 | if( sample.parentEvent ) { |
---|
226 | log.trace "----- SAMPLING EVENT -----" |
---|
227 | for (t in 0..sample.parentEvent.giveFields().unique().size()-1){ |
---|
228 | TemplateField tf =sample.parentEvent.giveFields().getAt(t) |
---|
229 | log.trace tf.name |
---|
230 | row.createCell((short)9+sample.parentSubject.giveFields().unique().size()+t).setCellValue("samplingEvent-"+tf.name) |
---|
231 | sample.parentEvent.getFieldValue(tf.name) ? sub.createCell((short)9+sample.parentSubject.giveFields().unique().size()+t).setCellValue(sample.parentEvent.getFieldValue(tf.name).toString()) : "not define" |
---|
232 | } |
---|
233 | } else { |
---|
234 | log.trace "------ NO SAMPLING EVENT FOR SAMPLE " + sample.name + "-----"; |
---|
235 | } |
---|
236 | } |
---|
237 | |
---|
238 | // writing EventGroup properties |
---|
239 | def writeEventGroupProperties(sub,sample,row){ |
---|
240 | |
---|
241 | } |
---|
242 | |
---|
243 | // writing sample properties |
---|
244 | def writeSampleProperties(sub,sample,row){ |
---|
245 | log.trace "----- SAMPLE -----" |
---|
246 | for (v in 0..sample.giveFields().unique().size()-1){ |
---|
247 | TemplateField tf =sample.giveFields().getAt(v) |
---|
248 | log.trace tf.name |
---|
249 | row.createCell((short)9+sample.parentSubject.giveFields().unique().size()+v+sample.parentEvent.giveFields().unique().size()).setCellValue("sample-"+tf.name) |
---|
250 | 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" |
---|
251 | } |
---|
252 | } |
---|
253 | } |
---|