1 | /** |
---|
2 | * ExporterController Controler |
---|
3 | * |
---|
4 | * Description of my controller |
---|
5 | * |
---|
6 | * @author your email (+name?) |
---|
7 | * @since 2010mmdd |
---|
8 | * @package ??? |
---|
9 | * |
---|
10 | * Revision information: |
---|
11 | * $Rev$ |
---|
12 | * $Author$ |
---|
13 | * $Date$ |
---|
14 | */ |
---|
15 | package dbnp.exporter |
---|
16 | |
---|
17 | import dbnp.studycapturing.* |
---|
18 | |
---|
19 | import org.apache.poi.hssf.util.HSSFColor |
---|
20 | import org.apache.poi.* |
---|
21 | import org.apache.poi.hssf.usermodel.* |
---|
22 | import org.apache.poi.poifs.filesystem.POIFSFileSystem |
---|
23 | import org.apache.poi.ss.usermodel.DataFormatter |
---|
24 | |
---|
25 | import grails.plugins.springsecurity.Secured |
---|
26 | |
---|
27 | class ExporterController { |
---|
28 | |
---|
29 | def AuthenticationService |
---|
30 | def ImporterService |
---|
31 | |
---|
32 | /* |
---|
33 | * List of all studies for selection the study to export |
---|
34 | * Using the same code as 'list' into StudyController |
---|
35 | */ |
---|
36 | def index = { |
---|
37 | |
---|
38 | def user = AuthenticationService.getLoggedInUser() |
---|
39 | def max = Math.min(params.max ? params.int('max') : 10, 100) |
---|
40 | |
---|
41 | def c = dbnp.studycapturing.Study.createCriteria() |
---|
42 | |
---|
43 | def studies |
---|
44 | if( user == null ) { |
---|
45 | studies = c.list { |
---|
46 | maxResults(max) |
---|
47 | and { |
---|
48 | eq( "published", true ) |
---|
49 | eq( "publicstudy", true ) |
---|
50 | } |
---|
51 | } |
---|
52 | } else { |
---|
53 | studies = c.list { |
---|
54 | maxResults(max) |
---|
55 | or { |
---|
56 | eq( "owner", user ) |
---|
57 | writers { |
---|
58 | eq( "id", user.id ) |
---|
59 | } |
---|
60 | and { |
---|
61 | readers { |
---|
62 | eq( "id", user.id ) |
---|
63 | } |
---|
64 | eq( "published", true ) |
---|
65 | } |
---|
66 | } |
---|
67 | } |
---|
68 | } |
---|
69 | [studyInstanceList: studies, studyInstanceTotal: studies.count()] |
---|
70 | } |
---|
71 | |
---|
72 | /* |
---|
73 | * the export method will create a SimpleTox format for the selected study |
---|
74 | */ |
---|
75 | def export = { |
---|
76 | //def studyInstance |
---|
77 | def studies = [] |
---|
78 | |
---|
79 | // the attributes list for the SimpleTox format |
---|
80 | def attributes_list = ["SubjectID","DataFile","HybName","SampleName","ArrayType","Label","StudyTitle","Array_ID", |
---|
81 | "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 | |
---|
102 | HSSFWorkbook wb = new HSSFWorkbook() |
---|
103 | println " WORKBOOK : "+wb |
---|
104 | // The first row contains the attributes names |
---|
105 | HSSFSheet sheet = wb.createSheet() |
---|
106 | HSSFRow row = sheet.createRow((short)0) |
---|
107 | for (i in 0..attributes_list.size()){ |
---|
108 | row.createCell((short)i).setCellValue(attributes_list[i]) |
---|
109 | } |
---|
110 | |
---|
111 | // Adding the next lines |
---|
112 | for (s in 1..studyInstance.samples.size()){ |
---|
113 | try { |
---|
114 | // creating new line for every sample |
---|
115 | HSSFRow sub = sheet.createRow((short)s) |
---|
116 | def sample = studyInstance.samples.getAt(s-1) |
---|
117 | |
---|
118 | writeMandatoryFields(sub,sample,studyInstance) |
---|
119 | |
---|
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) |
---|
125 | |
---|
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 | |
---|
132 | } |
---|
133 | 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 | } |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | def writeMandatoryFields(sub,sample,study) { |
---|
148 | |
---|
149 | try { |
---|
150 | // adding subject name in row 1 |
---|
151 | sub.createCell((short)0).setCellValue(sample.parentSubject.name) |
---|
152 | // adding sample in row 4 |
---|
153 | sub.createCell((short)3).setCellValue(sample.name) |
---|
154 | // adding label (EventGroup) in row 6 |
---|
155 | for (ev in EventGroup.list()){ |
---|
156 | if (ev.subjects.name.contains(sample.parentSubject.name)) { |
---|
157 | sub.createCell((short)5).setCellValue(ev.name) |
---|
158 | break |
---|
159 | } |
---|
160 | else { |
---|
161 | sub.createCell((short)5).setCellValue(" ") |
---|
162 | } |
---|
163 | } |
---|
164 | // adding study title in row 7 |
---|
165 | sub.createCell((short)6).setCellValue(study.title) |
---|
166 | // Species row 9 |
---|
167 | sub.createCell((short)8).setCellValue(sample.parentSubject.species.name) |
---|
168 | } |
---|
169 | catct (Exception e){ |
---|
170 | println "Error during Mandatory Fields" |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | // writing subject properties |
---|
175 | def writeSubjectProperties(sub,sample,row) { |
---|
176 | try { |
---|
177 | for (u in 0..sample.parentSubject.giveFields().unique().size()-1){ |
---|
178 | TemplateField tf = sample.parentSubject.giveFields().getAt(u) |
---|
179 | row.createCell((short)9+u).setCellValue(tf.name) |
---|
180 | 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 | } |
---|
186 | } |
---|
187 | |
---|
188 | // writing samplingEvent properties |
---|
189 | def writeSamplingEventProperties(sub,sample,row){ |
---|
190 | try { |
---|
191 | for (t in 0..sample.parentEvent.giveFields().unique().size()-1){ |
---|
192 | TemplateField tf =sample.parentEvent.giveFields().getAt(t) |
---|
193 | row.createCell((short)9+sample.parentSubject.giveFields().unique().size()+t).setCellValue(tf.name) |
---|
194 | 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 | } |
---|
196 | } |
---|
197 | catch (Exception e) { |
---|
198 | println "Error during Sampling Event properties" |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | } |
---|