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 | |
---|
78 | // the attributes list for the SimpleTox format |
---|
79 | def attributes_list = ["SubjectID","DataFile","HybName","SampleName","ArrayType","Label","StudyTitle","Array_ID", |
---|
80 | "Species"] |
---|
81 | |
---|
82 | // Get the selected study |
---|
83 | for ( j in dbnp.studycapturing.Study.list() ){ |
---|
84 | if (params.containsKey(j.title)){ |
---|
85 | studyInstance = j |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | HSSFWorkbook wb = new HSSFWorkbook() |
---|
90 | FileOutputStream fileOut = new FileOutputStream(studyInstance.title+"_SimpleTox.xls") |
---|
91 | // HSSFCellStyle style = wb.createCellStyle() |
---|
92 | // style.setFillForegroundColor((short) HSSFColor.RED.index) |
---|
93 | |
---|
94 | |
---|
95 | // The first row contains the attributes names |
---|
96 | HSSFSheet sheet = wb.createSheet() |
---|
97 | HSSFRow row = sheet.createRow((short)0) |
---|
98 | for (i in 0..attributes_list.size()){ |
---|
99 | // HSSFCell cell = row.createCell((short)i) |
---|
100 | // cell.setCellValue(attributes_list[i]) |
---|
101 | // cell.setCellStyle(style) |
---|
102 | row.createCell((short)i).setCellValue(attributes_list[i]) |
---|
103 | } |
---|
104 | |
---|
105 | // Adding the next lines |
---|
106 | for (s in 1..studyInstance.samples.size()){ |
---|
107 | // creating new line for every sample |
---|
108 | HSSFRow sub = sheet.createRow((short)s) |
---|
109 | // adding subject name in row 1 |
---|
110 | sub.createCell((short)0).setCellValue(studyInstance.samples.getAt(s-1).parentSubject.name) |
---|
111 | // adding sample in row 4 |
---|
112 | sub.createCell((short)3).setCellValue(studyInstance.samples.getAt(s-1).name) |
---|
113 | // adding label (EventGroup) in row 6 |
---|
114 | for (ev in EventGroup.list()){ |
---|
115 | if (ev.subjects.name.contains(studyInstance.samples.getAt(s-1).parentSubject.name)) { |
---|
116 | sub.createCell((short)5).setCellValue(ev.name) |
---|
117 | break |
---|
118 | } |
---|
119 | else { |
---|
120 | sub.createCell((short)5).setCellValue(" ") |
---|
121 | } |
---|
122 | } |
---|
123 | // adding study title in row 7 |
---|
124 | sub.createCell((short)6).setCellValue(studyInstance.title) |
---|
125 | // Species row 9 |
---|
126 | sub.createCell((short)8).setCellValue(studyInstance.samples.getAt(s-1).parentSubject.species.name) |
---|
127 | |
---|
128 | for (u in 0..studyInstance.samples.getAt(s-1).parentSubject.giveFields().unique().size()-1){ |
---|
129 | row.createCell((short)9+u).setCellValue(studyInstance.samples.getAt(s-1).parentSubject.giveFields().getAt(u).toString()) |
---|
130 | // println "VALUE "+studyInstance.samples.getAt(s-1).parentSubject.getFieldValue(u.name) |
---|
131 | TemplateField tf = studyInstance.samples.getAt(s-1).parentSubject.giveFields().getAt(u) |
---|
132 | // println tf.name |
---|
133 | // println studyInstance.samples.getAt(s-1).parentSubject.giveFields().getAt(u) |
---|
134 | row.createCell((short)9+u).setCellValue(tf.name) |
---|
135 | studyInstance.samples.getAt(s-1).parentSubject.getFieldValue(tf.name) ? sub.createCell((short)9+u).setCellValue(studyInstance.samples.getAt(s-1).parentSubject.getFieldValue(tf.name).toString()) : "not define" |
---|
136 | } |
---|
137 | |
---|
138 | } |
---|
139 | |
---|
140 | //println "DOMAINS " +studyInstance.samples.getAt(s-1).parentSubject.giveFields() |
---|
141 | |
---|
142 | |
---|
143 | wb.write(fileOut) |
---|
144 | fileOut.close() |
---|
145 | } |
---|
146 | |
---|
147 | } |
---|