| 1 | import grails.util.GrailsUtil | 
|---|
| 2 |  | 
|---|
| 3 | class BootStrap  | 
|---|
| 4 | { | 
|---|
| 5 |     //Required to be right here for Acegi plugin. | 
|---|
| 6 |     def authenticateService | 
|---|
| 7 |     Boolean BootStrapDemoDataSuccessful = true | 
|---|
| 8 |  | 
|---|
| 9 |     def init = { servletContext -> | 
|---|
| 10 |  | 
|---|
| 11 |     println "**** BootStrap GrailsUtil.environment = ${GrailsUtil.environment}" | 
|---|
| 12 |      | 
|---|
| 13 |         switch (GrailsUtil.environment) | 
|---|
| 14 |         { | 
|---|
| 15 |             case "development": | 
|---|
| 16 |                         bootStrapDemoData() | 
|---|
| 17 |                         break | 
|---|
| 18 |             case "test": | 
|---|
| 19 |                         break | 
|---|
| 20 |             case "production": | 
|---|
| 21 |                         bootStrapDemoData() | 
|---|
| 22 |                         break  | 
|---|
| 23 |         } | 
|---|
| 24 |      | 
|---|
| 25 |     } | 
|---|
| 26 |  | 
|---|
| 27 |     def destroy = { | 
|---|
| 28 |     } | 
|---|
| 29 |  | 
|---|
| 30 |     //Insert some demo/startup data. | 
|---|
| 31 |     void bootStrapDemoData() | 
|---|
| 32 |     { | 
|---|
| 33 |         println "BootStrapping demo data..." | 
|---|
| 34 |      | 
|---|
| 35 |         //TypeOfPersonGroup | 
|---|
| 36 |         def personGroupTypeInstance | 
|---|
| 37 |         personGroupTypeInstance = new PersonGroupType(name:"Department") | 
|---|
| 38 |         BootStrapSaveAndTest(personGroupTypeInstance) | 
|---|
| 39 |         personGroupTypeInstance = new PersonGroupType(name:"Contractor") | 
|---|
| 40 |         BootStrapSaveAndTest(personGroupTypeInstance) | 
|---|
| 41 |         personGroupTypeInstance = new PersonGroupType(name:"ProjectTeam") | 
|---|
| 42 |         BootStrapSaveAndTest(personGroupTypeInstance) | 
|---|
| 43 |      | 
|---|
| 44 |         //PersonGroup | 
|---|
| 45 |         def personGroupInstance | 
|---|
| 46 |         personGroupInstance = new PersonGroup(personGroupType:PersonGroupType.findByName("Department"), | 
|---|
| 47 |                         name:"Electrical") | 
|---|
| 48 |         BootStrapSaveAndTest(personGroupInstance) | 
|---|
| 49 |         personGroupInstance = new PersonGroup(personGroupType:PersonGroupType.findByName("Department"), | 
|---|
| 50 |                         name:"Mechanical") | 
|---|
| 51 |         BootStrapSaveAndTest(personGroupInstance) | 
|---|
| 52 |         personGroupInstance = new PersonGroup(personGroupType:PersonGroupType.findByName("Department"), | 
|---|
| 53 |                         name:"Production") | 
|---|
| 54 |         BootStrapSaveAndTest(personGroupInstance) | 
|---|
| 55 |         personGroupInstance = new PersonGroup(personGroupType:PersonGroupType.get(2), | 
|---|
| 56 |                         name:"Kewl AirCon Guys") | 
|---|
| 57 |         BootStrapSaveAndTest(personGroupInstance) | 
|---|
| 58 |         personGroupInstance = new PersonGroup(personGroupType:PersonGroupType.get(3), | 
|---|
| 59 |                         name:"gnuMims") | 
|---|
| 60 |         BootStrapSaveAndTest(personGroupInstance) | 
|---|
| 61 |  | 
|---|
| 62 |         //Authority | 
|---|
| 63 |         def authInstance | 
|---|
| 64 |  | 
|---|
| 65 |         authInstance = new Authority(description:"Application Admin, not required for daily use! Grants full admin access to the application.", | 
|---|
| 66 |                                         authority:"ROLE_AppAdmin") | 
|---|
| 67 |         BootStrapSaveAndTest(authInstance) | 
|---|
| 68 |  | 
|---|
| 69 |         authInstance = new Authority(description:"Business manager, grants full management access.", | 
|---|
| 70 |                                         authority:"ROLE_Manager") | 
|---|
| 71 |         BootStrapSaveAndTest(authInstance) | 
|---|
| 72 |  | 
|---|
| 73 |         authInstance = new Authority(description:"Application User, all application users need this base role to allow login.", | 
|---|
| 74 |                                         authority:"ROLE_AppUser") | 
|---|
| 75 |         BootStrapSaveAndTest(authInstance) | 
|---|
| 76 |              | 
|---|
| 77 |         //Person | 
|---|
| 78 |         def passClearText = "pass" | 
|---|
| 79 |         def passwordEncoded = authenticateService.encodePassword(passClearText) | 
|---|
| 80 |         def personInstance | 
|---|
| 81 |  | 
|---|
| 82 |         //Person #1 | 
|---|
| 83 |         personInstance = new Person(loginName:"admin", | 
|---|
| 84 |                                     firstName:"Admin", | 
|---|
| 85 |                                     lastName:"Powers", | 
|---|
| 86 |                                     pass:passClearText, | 
|---|
| 87 |                                     password:passwordEncoded, | 
|---|
| 88 |                                     email:"admin@example.com") | 
|---|
| 89 |         BootStrapSaveAndTest(personInstance) | 
|---|
| 90 |         personInstance.addToAuthorities(Authority.get(1)) | 
|---|
| 91 |         personInstance.addToAuthorities(Authority.get(2)) | 
|---|
| 92 |         personInstance.addToAuthorities(Authority.get(3)) | 
|---|
| 93 |         personInstance.addToPersonGroups(PersonGroup.findByName("gnuMims")) | 
|---|
| 94 |  | 
|---|
| 95 |         //Person #2 | 
|---|
| 96 |         personInstance = new Person(loginName:"manager", | 
|---|
| 97 |                                     firstName:"Meca", | 
|---|
| 98 |                                     lastName:"Manager", | 
|---|
| 99 |                                     pass:passClearText, | 
|---|
| 100 |                                     password:passwordEncoded, | 
|---|
| 101 |                                     email:"manager@example.com") | 
|---|
| 102 |         BootStrapSaveAndTest(personInstance) | 
|---|
| 103 |         personInstance.addToAuthorities(Authority.get(2)) | 
|---|
| 104 |         personInstance.addToAuthorities(Authority.get(3)) | 
|---|
| 105 |         personInstance.addToPersonGroups(PersonGroup.findByName("gnuMims")) | 
|---|
| 106 |  | 
|---|
| 107 |         //Person #3 | 
|---|
| 108 |         personInstance = new Person(loginName:"user", | 
|---|
| 109 |                                     firstName:"Demo", | 
|---|
| 110 |                                     lastName:"User", | 
|---|
| 111 |                                     pass:passClearText, | 
|---|
| 112 |                                     password:passwordEncoded, | 
|---|
| 113 |                                     email:"user@example.com") | 
|---|
| 114 |         BootStrapSaveAndTest(personInstance) | 
|---|
| 115 |         personInstance.addToAuthorities(Authority.get(3)) | 
|---|
| 116 |         personInstance.addToPersonGroups(PersonGroup.findByName("Electrical")) | 
|---|
| 117 |  | 
|---|
| 118 |         //Person #4 | 
|---|
| 119 |         personInstance = new Person(loginName:"craig", | 
|---|
| 120 |                                     firstName:"Craig", | 
|---|
| 121 |                                     lastName:"SuperSparky", | 
|---|
| 122 |                                     pass:passClearText, | 
|---|
| 123 |                                     password:passwordEncoded, | 
|---|
| 124 |                                     email:"user@example.com") | 
|---|
| 125 |         BootStrapSaveAndTest(personInstance) | 
|---|
| 126 |         personInstance.addToAuthorities(Authority.get(3)) | 
|---|
| 127 |         personInstance.addToPersonGroups(PersonGroup.findByName("Electrical")) | 
|---|
| 128 |  | 
|---|
| 129 |         //Person #5 | 
|---|
| 130 |         personInstance = new Person(loginName:"john", | 
|---|
| 131 |                                     firstName:"John", | 
|---|
| 132 |                                     lastName:"SuperFitter", | 
|---|
| 133 |                                     pass:passClearText, | 
|---|
| 134 |                                     password:passwordEncoded, | 
|---|
| 135 |                                     email:"user@example.com") | 
|---|
| 136 |         BootStrapSaveAndTest(personInstance) | 
|---|
| 137 |         personInstance.addToAuthorities(Authority.get(3)) | 
|---|
| 138 |         personInstance.addToPersonGroups(PersonGroup.findByName("Mechanical")) | 
|---|
| 139 |  | 
|---|
| 140 |         //Person #6 | 
|---|
| 141 |         personInstance = new Person(loginName:"mann", | 
|---|
| 142 |                                     firstName:"Production", | 
|---|
| 143 |                                     lastName:"Mann", | 
|---|
| 144 |                                     pass:passClearText, | 
|---|
| 145 |                                     password:passwordEncoded, | 
|---|
| 146 |                                     email:"user@example.com") | 
|---|
| 147 |         BootStrapSaveAndTest(personInstance) | 
|---|
| 148 |         personInstance.addToAuthorities(Authority.get(3)) | 
|---|
| 149 |         personInstance.addToPersonGroups(PersonGroup.findByName("Production")) | 
|---|
| 150 |  | 
|---|
| 151 |         //TaskGroup | 
|---|
| 152 |         def taskGroupInstance | 
|---|
| 153 |  | 
|---|
| 154 |         taskGroupInstance = new TaskGroup(name:"Engineering Activites", | 
|---|
| 155 |                       description:"Engineering daily activities") | 
|---|
| 156 |         BootStrapSaveAndTest(taskGroupInstance) | 
|---|
| 157 |  | 
|---|
| 158 |         taskGroupInstance = new TaskGroup(name:"Production Activites", | 
|---|
| 159 |                       description:"Production daily activities") | 
|---|
| 160 |         BootStrapSaveAndTest(taskGroupInstance) | 
|---|
| 161 |  | 
|---|
| 162 |         taskGroupInstance = new TaskGroup(name:"New Projects", | 
|---|
| 163 |                       description:" ") | 
|---|
| 164 |         BootStrapSaveAndTest(taskGroupInstance) | 
|---|
| 165 |  | 
|---|
| 166 |         //TaskStatus | 
|---|
| 167 |         def taskStatusInstance | 
|---|
| 168 |      | 
|---|
| 169 |         taskStatusInstance = new TaskStatus(name:"Not Started") | 
|---|
| 170 |         BootStrapSaveAndTest(taskStatusInstance) | 
|---|
| 171 |  | 
|---|
| 172 |         taskStatusInstance = new TaskStatus(name:"In Progress") | 
|---|
| 173 |         BootStrapSaveAndTest(taskStatusInstance) | 
|---|
| 174 |  | 
|---|
| 175 |         taskStatusInstance = new TaskStatus(name:"Completed") | 
|---|
| 176 |         BootStrapSaveAndTest(taskStatusInstance) | 
|---|
| 177 |  | 
|---|
| 178 |         //TaskPriority | 
|---|
| 179 |         def taskPriorityInstance | 
|---|
| 180 |  | 
|---|
| 181 |         taskPriorityInstance = new TaskPriority(name:"Low") | 
|---|
| 182 |         BootStrapSaveAndTest(taskPriorityInstance) | 
|---|
| 183 |  | 
|---|
| 184 |         taskPriorityInstance = new TaskPriority(name:"Normal") | 
|---|
| 185 |         BootStrapSaveAndTest(taskPriorityInstance) | 
|---|
| 186 |  | 
|---|
| 187 |         taskPriorityInstance = new TaskPriority(name:"High") | 
|---|
| 188 |         BootStrapSaveAndTest(taskPriorityInstance) | 
|---|
| 189 |  | 
|---|
| 190 |         taskPriorityInstance = new TaskPriority(name:"Immediate") | 
|---|
| 191 |         BootStrapSaveAndTest(taskPriorityInstance) | 
|---|
| 192 |  | 
|---|
| 193 |         //TaskType | 
|---|
| 194 |         def taskTypeInstance | 
|---|
| 195 |  | 
|---|
| 196 |         taskTypeInstance = new TaskType(name:"Unscheduled Breakin") | 
|---|
| 197 |         BootStrapSaveAndTest(taskTypeInstance) | 
|---|
| 198 |  | 
|---|
| 199 |         taskTypeInstance = new TaskType(name:"Planned Maintenance") | 
|---|
| 200 |         BootStrapSaveAndTest(taskTypeInstance) | 
|---|
| 201 |  | 
|---|
| 202 |         taskTypeInstance = new TaskType(name:"Project") | 
|---|
| 203 |         BootStrapSaveAndTest(taskTypeInstance) | 
|---|
| 204 |  | 
|---|
| 205 |         taskTypeInstance = new TaskType(name:"Turnaround") | 
|---|
| 206 |         BootStrapSaveAndTest(taskTypeInstance) | 
|---|
| 207 |  | 
|---|
| 208 |         taskTypeInstance = new TaskType(name:"Production Run") | 
|---|
| 209 |         BootStrapSaveAndTest(taskTypeInstance) | 
|---|
| 210 |  | 
|---|
| 211 |         //Task | 
|---|
| 212 |         def taskInstance | 
|---|
| 213 |         def subTaskInstance | 
|---|
| 214 |  | 
|---|
| 215 |         taskInstance = new Task(taskGroup:TaskGroup.findByName("Engineering Activites"), | 
|---|
| 216 |                  taskStatus:TaskStatus.findByName("Not Started"), | 
|---|
| 217 |                  taskPriority:TaskPriority.get(2), | 
|---|
| 218 |                  taskType:TaskType.get(1), | 
|---|
| 219 |                  leadPerson:Person.get(3), | 
|---|
| 220 |                  description:"Check specific level sensor", | 
|---|
| 221 |                  comment:"Has been noted as problematic, try recallibrating") | 
|---|
| 222 |         BootStrapSaveAndTest(taskInstance) | 
|---|
| 223 |  | 
|---|
| 224 |         subTaskInstance = new Task(taskGroup:TaskGroup.findByName("Engineering Activites"), | 
|---|
| 225 |                  taskStatus:TaskStatus.findByName("Not Started"), | 
|---|
| 226 |                  taskPriority:TaskPriority.get(2), | 
|---|
| 227 |                  taskType:TaskType.get(1), | 
|---|
| 228 |                  leadPerson:Person.get(5), | 
|---|
| 229 |                  description:"Some follow-up work", | 
|---|
| 230 |                  comment:"Some help required") | 
|---|
| 231 |         BootStrapSaveAndTest(subTaskInstance) | 
|---|
| 232 |          | 
|---|
| 233 |         //Add task 2 as a subTask of task 1. | 
|---|
| 234 |         taskInstance.addToSubTasks(Task.get(2)) | 
|---|
| 235 |         subTaskInstance.parentTask = Task.get(1) | 
|---|
| 236 |  | 
|---|
| 237 |         subTaskInstance = new Task(taskGroup:TaskGroup.findByName("Engineering Activites"), | 
|---|
| 238 |                  taskStatus:TaskStatus.findByName("Not Started"), | 
|---|
| 239 |                  taskPriority:TaskPriority.get(2), | 
|---|
| 240 |                  taskType:TaskType.get(1), | 
|---|
| 241 |                  leadPerson:Person.get(4), | 
|---|
| 242 |                  description:"Replace sensor at next opportunity.", | 
|---|
| 243 |                  comment:"Nothing else has worked.") | 
|---|
| 244 |         BootStrapSaveAndTest(subTaskInstance) | 
|---|
| 245 |  | 
|---|
| 246 |         //Add task 3 as a subTask of task 1. | 
|---|
| 247 |         taskInstance.addToSubTasks(Task.get(3)) | 
|---|
| 248 |         subTaskInstance.parentTask = Task.get(1) | 
|---|
| 249 |  | 
|---|
| 250 |         taskInstance = new Task(taskGroup:TaskGroup.findByName("Production Activites"), | 
|---|
| 251 |                  taskStatus:TaskStatus.findByName("Not Started"), | 
|---|
| 252 |                  taskPriority:TaskPriority.get(2), | 
|---|
| 253 |                  taskType:TaskType.get(5), | 
|---|
| 254 |                  leadPerson:Person.get(6), | 
|---|
| 255 |                  description:"Production Report", | 
|---|
| 256 |                  comment:"Production report for specific production run or shift") | 
|---|
| 257 |         BootStrapSaveAndTest(taskInstance) | 
|---|
| 258 |  | 
|---|
| 259 |         taskInstance = new Task(taskGroup:TaskGroup.findByName("New Projects"), | 
|---|
| 260 |                  taskStatus:TaskStatus.findByName("Not Started"), | 
|---|
| 261 |                  taskPriority:TaskPriority.get(2), | 
|---|
| 262 |                  taskType:TaskType.get(3), | 
|---|
| 263 |                  leadPerson:Person.get(1), | 
|---|
| 264 |                  description:"Make killer CMMS app", | 
|---|
| 265 |                  comment:"Use Grails and get a move on!") | 
|---|
| 266 |         BootStrapSaveAndTest(taskInstance) | 
|---|
| 267 |  | 
|---|
| 268 |         //EntryType | 
|---|
| 269 |         def entryTypeInstance | 
|---|
| 270 |  | 
|---|
| 271 |         entryTypeInstance = new EntryType(name:"Fault") | 
|---|
| 272 |         BootStrapSaveAndTest(entryTypeInstance) | 
|---|
| 273 |  | 
|---|
| 274 |         entryTypeInstance = new EntryType(name:"WorkDone") | 
|---|
| 275 |         BootStrapSaveAndTest(entryTypeInstance) | 
|---|
| 276 |  | 
|---|
| 277 |         entryTypeInstance = new EntryType(name:"Production Note") | 
|---|
| 278 |         BootStrapSaveAndTest(entryTypeInstance) | 
|---|
| 279 |  | 
|---|
| 280 |         entryTypeInstance = new EntryType(name:"Work Request") | 
|---|
| 281 |         BootStrapSaveAndTest(entryTypeInstance) | 
|---|
| 282 |  | 
|---|
| 283 |         //Entry | 
|---|
| 284 |         def entryInstance | 
|---|
| 285 |  | 
|---|
| 286 |         entryInstance = new Entry(enteredBy: Person.get(6), | 
|---|
| 287 |                                                     task: Task.get(1), | 
|---|
| 288 |                                                     entryType: EntryType.findByName("Fault"), | 
|---|
| 289 |                                                     comment: "This level sensor is causing us trouble.", | 
|---|
| 290 |                                                     durationMinute: 20) | 
|---|
| 291 |         BootStrapSaveAndTest(entryInstance) | 
|---|
| 292 |  | 
|---|
| 293 |         entryInstance = new Entry(enteredBy: Person.get(4), | 
|---|
| 294 |                                                     task: Task.get(1), | 
|---|
| 295 |                                                     entryType: EntryType.findByName("WorkDone"), | 
|---|
| 296 |                                                     comment: "Cleaned sensor, see how it goes.", | 
|---|
| 297 |                                                     durationMinute: 30) | 
|---|
| 298 |         BootStrapSaveAndTest(entryInstance) | 
|---|
| 299 |  | 
|---|
| 300 |         entryInstance = new Entry(enteredBy: Person.get(4), | 
|---|
| 301 |                                                     task: Task.get(1), | 
|---|
| 302 |                                                     entryType: EntryType.findByName("WorkDone"), | 
|---|
| 303 |                                                     comment: "Checked up on it later and sensor is dropping out intermittently, created subTask to replace sensor.", | 
|---|
| 304 |                                                     durationMinute: 20) | 
|---|
| 305 |         BootStrapSaveAndTest(entryInstance) | 
|---|
| 306 |  | 
|---|
| 307 |         //ModificationType | 
|---|
| 308 |         def taskModificationTypeInstance | 
|---|
| 309 |         taskModificationTypeInstance = new TaskModificationType(name:"Created").save() | 
|---|
| 310 |         taskModificationTypeInstance = new TaskModificationType(name:"Completed").save() | 
|---|
| 311 |         taskModificationTypeInstance = new TaskModificationType(name:"Closed").save() | 
|---|
| 312 |         taskModificationTypeInstance = new TaskModificationType(name:"Altered").save() | 
|---|
| 313 |         taskModificationTypeInstance = new TaskModificationType(name:"TargetDateModified").save() | 
|---|
| 314 |         taskModificationTypeInstance = new TaskModificationType(name:"ScheduledDateModified").save() | 
|---|
| 315 |         taskModificationTypeInstance = new TaskModificationType(name:"DescriptionModified").save() | 
|---|
| 316 |         taskModificationTypeInstance = new TaskModificationType(name:"AssignedToModified").save() | 
|---|
| 317 |         taskModificationTypeInstance = new TaskModificationType(name:"NameModified").save() | 
|---|
| 318 |      | 
|---|
| 319 |         //AssignedPerson | 
|---|
| 320 |         def assignedPersonInstance | 
|---|
| 321 |  | 
|---|
| 322 |         assignedPersonInstance = new AssignedPerson(person: Person.get(4), | 
|---|
| 323 |                                                                                         task: Task.get(1), | 
|---|
| 324 |                                                                                         estimatedHour: 1, | 
|---|
| 325 |                                                                                         estimatedMinute: 20) | 
|---|
| 326 |         BootStrapSaveAndTest(assignedPersonInstance) | 
|---|
| 327 |  | 
|---|
| 328 |         assignedPersonInstance = new AssignedPerson(person: Person.get(5), | 
|---|
| 329 |                                                                                         task: Task.get(1), | 
|---|
| 330 |                                                                                         estimatedHour: 3, | 
|---|
| 331 |                                                                                         estimatedMinute: 30) | 
|---|
| 332 |         BootStrapSaveAndTest(assignedPersonInstance) | 
|---|
| 333 |  | 
|---|
| 334 |  | 
|---|
| 335 |         //Finally did it all work.         | 
|---|
| 336 |         if(BootStrapDemoDataSuccessful) { | 
|---|
| 337 |             println "BootStrapping demo data...successful." | 
|---|
| 338 |         } | 
|---|
| 339 |         else println "BootStrapping demo data...failed." | 
|---|
| 340 |     } | 
|---|
| 341 |      | 
|---|
| 342 |     void BootStrapSaveAndTest(object) { | 
|---|
| 343 |         if(!object.save()) { | 
|---|
| 344 |             BootStrapDemoDataSuccessful = false | 
|---|
| 345 |             println "'${object}' failed to save!" | 
|---|
| 346 |             println object.errors | 
|---|
| 347 |  | 
|---|
| 348 |         } | 
|---|
| 349 |     }  | 
|---|
| 350 | } | 
|---|