1 | import dbnp.studycapturing.* |
---|
2 | |
---|
3 | import dbnp.data.Ontology |
---|
4 | import dbnp.data.Term |
---|
5 | import dbnp.rest.common.CommunicationManager |
---|
6 | import org.codehaus.groovy.grails.commons.GrailsApplication |
---|
7 | import grails.util.GrailsUtil |
---|
8 | |
---|
9 | // Imports for Nimble |
---|
10 | import grails.plugins.nimble.InstanceGenerator |
---|
11 | import grails.plugins.nimble.core.LevelPermission |
---|
12 | import grails.plugins.nimble.core.Role |
---|
13 | import grails.plugins.nimble.core.Group |
---|
14 | import grails.plugins.nimble.core.AdminsService |
---|
15 | import grails.plugins.nimble.core.UserService |
---|
16 | |
---|
17 | /** |
---|
18 | * Application Bootstrapper |
---|
19 | * @Author Jeroen Wesbeek |
---|
20 | * @Since 20091021 |
---|
21 | * |
---|
22 | * Revision information: |
---|
23 | * $Rev: 860 $ |
---|
24 | * $Author: duh $ |
---|
25 | * $Date: 2010-08-30 13:13:30 +0000 (ma, 30 aug 2010) $ |
---|
26 | */ |
---|
27 | class BootStrap { |
---|
28 | |
---|
29 | // Injections for Nimble |
---|
30 | def grailsApplication |
---|
31 | def nimbleService |
---|
32 | def userService |
---|
33 | def adminsService |
---|
34 | |
---|
35 | def init = {servletContext -> |
---|
36 | // define timezone |
---|
37 | System.setProperty('user.timezone', 'CET') |
---|
38 | |
---|
39 | // If there are no users yet in the database |
---|
40 | println "Executing Nimble bootstrap..." |
---|
41 | |
---|
42 | // The following must be executed |
---|
43 | nimbleService.init() |
---|
44 | |
---|
45 | // Add users |
---|
46 | def user |
---|
47 | |
---|
48 | if (dbnp.user.User.count() == 0) { |
---|
49 | println "Adding example user..." |
---|
50 | |
---|
51 | // Create example User account |
---|
52 | user = InstanceGenerator.user() |
---|
53 | user.username = "user" |
---|
54 | user.pass = 'useR123!' |
---|
55 | user.passConfirm = 'useR123!' |
---|
56 | user.enabled = true |
---|
57 | |
---|
58 | def userProfile = InstanceGenerator.profile() |
---|
59 | userProfile.fullName = "Test User" |
---|
60 | userProfile.owner = user |
---|
61 | user.profile = userProfile |
---|
62 | |
---|
63 | def savedUser = userService.createUser(user) |
---|
64 | if (savedUser.hasErrors()) { |
---|
65 | savedUser.errors.each { |
---|
66 | log.error(it) |
---|
67 | } |
---|
68 | throw new RuntimeException("Error creating example user") |
---|
69 | } |
---|
70 | |
---|
71 | println "Adding example admin user..." |
---|
72 | |
---|
73 | // Create example Administrative account |
---|
74 | def admins = Role.findByName(AdminsService.ADMIN_ROLE) |
---|
75 | def admin = InstanceGenerator.user() |
---|
76 | admin.username = "admin" |
---|
77 | admin.pass = "admiN123!" |
---|
78 | admin.passConfirm = "admiN123!" |
---|
79 | admin.enabled = true |
---|
80 | |
---|
81 | def adminProfile = InstanceGenerator.profile() |
---|
82 | adminProfile.fullName = "Administrator" |
---|
83 | adminProfile.owner = admin |
---|
84 | admin.profile = adminProfile |
---|
85 | |
---|
86 | def savedAdmin = userService.createUser(admin) |
---|
87 | if (savedAdmin.hasErrors()) { |
---|
88 | savedAdmin.errors.each { |
---|
89 | log.error(it) |
---|
90 | } |
---|
91 | throw new RuntimeException("Error creating administrator") |
---|
92 | } |
---|
93 | |
---|
94 | adminsService.add(admin) |
---|
95 | } |
---|
96 | else { |
---|
97 | user = dbnp.user.User.findByUsername("user") |
---|
98 | } |
---|
99 | |
---|
100 | println "Done with Nimble bootstrap" |
---|
101 | |
---|
102 | // If there are no templates yet in the database |
---|
103 | if (Template.count() == 0) { |
---|
104 | println "No templates in the current database."; |
---|
105 | |
---|
106 | // If in development or test mode, add the ontologies manually to the database |
---|
107 | // without contacting the BioPortal website, to avoid annoying hiccups when the server is busy |
---|
108 | if (grails.util.GrailsUtil.environment != GrailsApplication.ENV_PRODUCTION) { |
---|
109 | println "Adding ontology descriptors" |
---|
110 | BootStrapTemplates.initTemplateOntologies() |
---|
111 | } |
---|
112 | |
---|
113 | // Add example study, subject, event etc. templates |
---|
114 | BootStrapTemplates.initTemplates() |
---|
115 | |
---|
116 | // If in development mode and no studies are present, add example studies |
---|
117 | if (Study.count() == 0 && grails.util.GrailsUtil.environment != GrailsApplication.ENV_TEST) { |
---|
118 | // check if special file is present in project directory |
---|
119 | if ((new File(System.properties['user.dir']+"/.skip-studies").exists())) { |
---|
120 | // yes it is, skip study bootstrapping |
---|
121 | println ".skipping study bootstrapping" |
---|
122 | |
---|
123 | // get species ontology |
---|
124 | def speciesOntology = Ontology.getOrCreateOntologyByNcboId(1132) |
---|
125 | |
---|
126 | // add terms |
---|
127 | def mouseTerm = new Term( |
---|
128 | name: 'Mus musculus', |
---|
129 | ontology: speciesOntology, |
---|
130 | accession: '10090' |
---|
131 | ).with { if (!validate()) { errors.each { println it} } else save(flush:true)} |
---|
132 | |
---|
133 | def humanTerm = new Term( |
---|
134 | name: 'Homo sapiens', |
---|
135 | ontology: speciesOntology, |
---|
136 | accession: '9606' |
---|
137 | ).with { if (!validate()) { errors.each { println it} } else save(flush:true)} |
---|
138 | } else { |
---|
139 | // general study boostrapping |
---|
140 | BootStrapStudies.addExampleStudies(user) |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | println "Finished adding templates and studies" |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * attach ontologies in runtime. Possible problem is that you need |
---|
149 | * an internet connection when bootstrapping though. |
---|
150 | * @see dbnp.studycapturing.Subject |
---|
151 | * @see dbnp.studycapturing.Sample |
---|
152 | */ |
---|
153 | TemplateEntity.getField(Subject.domainFields, 'species').ontologies = [Ontology.getOrCreateOntologyByNcboId(1132)] |
---|
154 | TemplateEntity.getField(Sample.domainFields, 'material').ontologies = [Ontology.getOrCreateOntologyByNcboId(1005)] |
---|
155 | |
---|
156 | println "Registering SAM REST methods" |
---|
157 | // register methods for accessing SAM's Rest services |
---|
158 | if (grails.util.GrailsUtil.environment == GrailsApplication.ENV_PRODUCTION) { |
---|
159 | CommunicationManager.SAMServerURL = 'http://sam.nmcdsp.org' |
---|
160 | } |
---|
161 | else { |
---|
162 | CommunicationManager.SAMServerURL = 'http://localhost:8182/sam' |
---|
163 | } |
---|
164 | CommunicationManager.registerRestWrapperMethodsSAMtoGSCF() |
---|
165 | |
---|
166 | println "Done with BootStrap" |
---|
167 | } |
---|
168 | |
---|
169 | def destroy = { |
---|
170 | } |
---|
171 | } |
---|