groovy & grails - lecture 13

  • 909 views
Uploaded on

Grails project organization …

Grails project organization
Controller & view generation
Loggers
Grails plugins
Groovy and beyond
Coding re-remarks

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
909
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
17
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • info -> all controllers under the eop package\ndebug only MessageService\ncontroller, service and a couple of other class have log defined by default\nin a domain, we must define it explicitely\n
  • just a framework would be nothing without community contributions\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • see url\n
  • \n
  • \n
  • often, super light\nconfiguration can offer a lot of tuning\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Freedom has a price\nsome people prefer when there is only one solution\n
  • Freedom has a price\nsome people prefer when there is only one solution\n
  • Freedom has a price\nsome people prefer when there is only one solution\n
  • Freedom has a price\nsome people prefer when there is only one solution\n
  • Freedom has a price\nsome people prefer when there is only one solution\n
  • Freedom has a price\nsome people prefer when there is only one solution\n
  • Freedom has a price\nsome people prefer when there is only one solution\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • we saw parallelization in selected chapters\n\n
  • we saw parallelization in selected chapters\n\n
  • we saw parallelization in selected chapters\n\n
  • we saw parallelization in selected chapters\n\n
  • we saw parallelization in selected chapters\n\n

Transcript

  • 1. Groovy: Efficiency Oriented ProgrammingLecture 13Master Proteomics & Bioinformatics - University of GenevaAlexandre Masselot - summer 2011
  • 2. Agenda‣ Eclipse tip‣ Grails project organization‣ Controller & view generation‣ Loggers‣ Grails plugins‣ Groovy and beyond‣ Coding re-remarks
  • 3. Eclipse and packages‣ Packages: organize classes into folder-like structure
  • 4. Eclipse and packages‣ Packages: organize classes into folder-like structure‣ Classes into the same package: no need to import
  • 5. Eclipse and packages‣ Packages: organize classes into folder-like structure‣ Classes into the same package: no need to import‣ Classes into different packages: import path.to.package.ClassName import path.to.package.*
  • 6. Eclipse and packages‣ Packages: organize classes into folder-like structure‣ Classes into the same package: no need to import‣ Classes into different packages: import path.to.package.ClassName import path.to.package.*‣ Eclipse: refactor will rename dependencies
  • 7. Eclipse and packages‣ Packages: organize classes into folder-like structure‣ Classes into the same package: no need to import‣ Classes into different packages: import path.to.package.ClassName import path.to.package.*‣ Eclipse: refactor will rename dependencies‣ <ctrl> <shift> O : import all needed packages and removes all unnecessary packages - can be applied into the current file or on a whole tree
  • 8. Book of the day‣ “Getting Started with Grails” by Scott Davis & Jason Rudolph http://www.infoq.com/minibooks/grails-getting-started
  • 9. Grails code organization‣ config grails-app/conf
  • 10. Grails code organization‣ config grails-app/conf‣ web controller (actions) grails-app/controllers
  • 11. Grails code organization‣ config grails-app/conf‣ web controller (actions) grails-app/controllers‣ services (database related action) grails-app/services
  • 12. Grails code organization‣ config grails-app/conf‣ web controller (actions) grails-app/controllers‣ services (database related action) grails-app/services‣ database saved objects grails-app/domain
  • 13. Grails code organization‣ config grails-app/conf‣ web controller (actions) grails-app/controllers‣ services (database related action) grails-app/services‣ database saved objects grails-app/domain‣ all other groovy code src/groovy
  • 14. Grails code organization (cont’d)
  • 15. Grails code organization (cont’d)‣ Unit tests (+mocked domains)test/unit
  • 16. Grails code organization (cont’d)‣ Unit tests (+mocked domains) test/unit‣ Integration tests test/integration
  • 17. Controller & views generation‣ Controller can be implicitly generated def scaffold=true
  • 18. Controller & views generation‣ Controller can be implicitly generated def scaffold=true‣ Or explicitly written
  • 19. Controller & views generation‣ Controller can be implicitly generated def scaffold=true‣ Or explicitly written‣ Generate the scaffolded controller generate-controller eop.lec13.twitter.Person
  • 20. Controller & views generation (cont’d)‣ Use generated for inspiration
  • 21. Controller & views generation (cont’d)‣ Use generated for inspiration‣ Keep def scaffold = true and explicitly use only some generated methods
  • 22. Controller & views generation (cont’d)‣ Use generated for inspiration‣ Keep def scaffold = true and explicitly use only some generated methods‣ Do not generate controllers too early in a project
  • 23. Controller & views generation (cont’d)‣ Use generated for inspiration‣ Keep def scaffold = true and explicitly use only some generated methods‣ Do not generate controllers too early in a project‣ Same applies for generate-views
  • 24. Logger: how to println message on the console
  • 25. For debugging, reporting info, warnings or errors
  • 26. println “loading list with $params”is not a solution: - commenting in/out the lines - where does this console output is written?
  • 27. logging: println (bad) solution‣ in MessageController.groovy def loadList = { [SNIP] println "loading list with $params" [SNIP] }
  • 28. logging: println (bad) solution‣ in MessageController.groovy def loadList = { [SNIP] println "loading list with $params" [SNIP] }‣ in MessageService.groovy def loadMessages(loader, input) { [SNIP] list.each{ [SNIP] println "inserting message to $user" [SNIP] } }
  • 29. logging: println (bad) solutionloading list with [type:sciam,messagefile:org.springframework.web.multipart.commons.CommonsMultipartFile@10310199, action:loadList, controller:message]inserting message to Scientific Americaninserting message to Scientific Americaninserting message to Scientific Americaninserting message to Scientific Americaninserting message to Scientific Americaninserting message to Scientific Americaninserting message to Scientific American
  • 30. logging: println (bad) solution loading list with [type:sciam, messagefile:org.springframework.web.multipart.commons.Comm onsMultipartFile@10310199, action:loadList, control ler:message] inserting message to Scientific American inserting message to Scientific American inserting message to Scientific American inserting message to Scientific American inserting message to Scientific American inserting message to Scientific American inserting message to Scientific American‣ no filename source
  • 31. logging: println (bad) solution loading list with [type:sciam, messagefile:org.springframework.web.multipart.commons.Comm onsMultipartFile@10310199, action:loadList, control ler:message] inserting message to Scientific American inserting message to Scientific American inserting message to Scientific American inserting message to Scientific American inserting message to Scientific American inserting message to Scientific American inserting message to Scientific American‣ no filename source‣ no line number
  • 32. logging: println (bad) solution loading list with [type:sciam, messagefile:org.springframework.web.multipart.commons.Comm onsMultipartFile@10310199, action:loadList, control ler:message] inserting message to Scientific American inserting message to Scientific American inserting message to Scientific American inserting message to Scientific American inserting message to Scientific American inserting message to Scientific American inserting message to Scientific American‣ no filename source‣ no line number‣ no level of importance of the message
  • 33. log4j offers a convenient solution
  • 34. logging: log4j solution‣ in MessageController.groovy def loadList = { [SNIP] log.info "loading list with $params" [SNIP] }
  • 35. logging: log4j solution‣ in MessageController.groovy def loadList = { [SNIP] log.info "loading list with $params" [SNIP] }‣ in MessageService.groovy def loadMessages(loader, input) { [SNIP] list.each{ [SNIP] log.debug "inserting message to $user" [SNIP] } }
  • 36. logging: log4j solution (cont’d)2011-05-29 22:17:31,309 [http-8080-1] INFO twitter.MessageController - loadinglist with [type:sciam, messagefile:org.springframework.web.multipart.commons.CommonsMultipartFile@5f2d0b54, action:loadList,controller:message]2011-05-29 22:17:31,372 [http-8080-1] DEBUG twitter.MessageService - insertingmessage to Scientific American2011-05-29 22:17:31,464 [http-8080-1] DEBUG twitter.MessageService - insertingmessage to Scientific American2011-05-29 22:17:31,496 [http-8080-1] DEBUG twitter.MessageService - insertingmessage to Scientific American
  • 37. logging: log4j solution (cont’d)2011-05-29 22:17:31,309 [http-8080-1] INFO twitter.MessageController - loadinglist with [type:sciam, messagefile:org.springframework.web.multipart.commons.CommonsMultipartFile@5f2d0b54, action:loadList,controller:message]2011-05-29 22:17:31,372 [http-8080-1] DEBUG twitter.MessageService - insertingmessage to Scientific American2011-05-29 22:17:31,464 [http-8080-1] DEBUG twitter.MessageService - insertingmessage to Scientific American2011-05-29 22:17:31,496 [http-8080-1] DEBUG twitter.MessageService - insertingmessage to Scientific American log level
  • 38. logging: log4j solution (cont’d)2011-05-29 22:17:31,309 [http-8080-1] INFO twitter.MessageController - loadinglist with [type:sciam, messagefile:org.springframework.web.multipart.commons.CommonsMultipartFile@5f2d0b54, action:loadList,controller:message]2011-05-29 22:17:31,372 [http-8080-1] DEBUG twitter.MessageService - insertingmessage to Scientific American2011-05-29 22:17:31,464 [http-8080-1] DEBUG twitter.MessageService - insertingmessage to Scientific American2011-05-29 22:17:31,496 [http-8080-1] DEBUG twitter.MessageService - insertingmessage to Scientific American log level source file
  • 39. logging: log4j solution (cont’d)2011-05-29 22:17:31,309 [http-8080-1] INFO twitter.MessageController - loadinglist with [type:sciam, messagefile:org.springframework.web.multipart.commons.CommonsMultipartFile@5f2d0b54, action:loadList,controller:message]2011-05-29 22:17:31,372 [http-8080-1] DEBUG twitter.MessageService - insertingmessage to Scientific American2011-05-29 22:17:31,464 [http-8080-1] DEBUG twitter.MessageService - insertingmessage to Scientific American2011-05-29 22:17:31,496 [http-8080-1] DEBUG twitter.MessageService - insertingmessage to Scientific American log level source file message
  • 40. logging: log4j solution (cont’d) 2011-05-29 22:17:31,309 [http-8080-1] INFO twitter.MessageController - loading list with [type:sciam, messagefile:org.springframework.web. multipart.commons.CommonsMultipartFile@5f2d0b54, action:loadList, controller:message] 2011-05-29 22:17:31,372 [http-8080-1] DEBUG twitter.MessageService - inserting message to Scientific American 2011-05-29 22:17:31,464 [http-8080-1] DEBUG twitter.MessageService - inserting message to Scientific American 2011-05-29 22:17:31,496 [http-8080-1] DEBUG twitter.MessageService - inserting message to Scientific Americandate/time log level source file message
  • 41. What is logged?How to configure?
  • 42. log4j configuration
  • 43. log4j configuration‣ In Config.groovylog4j = { info grails.app.controller.eop debug grails.app.service.eop.lec13.twitter.MessageService error org.codehaus.groovy.grails.web.servlet, // controllers org.codehaus.groovy.grails.web.pages, // GSP org.codehaus.groovy.grails.commons, // core org.codehaus.groovy.grails.plugins, // pluginsetc.
  • 44. Grails plugin
  • 45. http://www.grails.org/plugins/ >600 plugins
  • 46. Instead of coding a functionality, check for community!
  • 47. Grails plugins: some examples‣ Security/authentication (spring-security, reCaptcha)
  • 48. Grails plugins: some examples‣ Security/authentication (spring-security, reCaptcha)‣ Javascript (jquery)
  • 49. Grails plugins: some examples‣ Security/authentication (spring-security, reCaptcha)‣ Javascript (jquery)‣ Sexy web widgets (Grails UI , RichUI, GWT, DOJO)
  • 50. Grails plugins: some examples‣ Security/authentication (spring-security, reCaptcha)‣ Javascript (jquery)‣ Sexy web widgets (Grails UI , RichUI, GWT, DOJO)‣ Search domain beans (searchable)
  • 51. Grails plugins: some examples‣ Security/authentication (spring-security, reCaptcha)‣ Javascript (jquery)‣ Sexy web widgets (Grails UI , RichUI, GWT, DOJO)‣ Search domain beans (searchable)‣ Mail (mail)
  • 52. Grails plugins: some examples‣ Security/authentication (spring-security, reCaptcha)‣ Javascript (jquery)‣ Sexy web widgets (Grails UI , RichUI, GWT, DOJO)‣ Search domain beans (searchable)‣ Mail (mail)‣ Excel, pdf, tsv exports (export)
  • 53. Grails plugins: some examples‣ Security/authentication (spring-security, reCaptcha)‣ Javascript (jquery)‣ Sexy web widgets (Grails UI , RichUI, GWT, DOJO)‣ Search domain beans (searchable)‣ Mail (mail)‣ Excel, pdf, tsv exports (export)‣ Chart visualization (Google visualization)
  • 54. Grails plugins: some examples‣ Security/authentication (spring-security, reCaptcha)‣ Javascript (jquery)‣ Sexy web widgets (Grails UI , RichUI, GWT, DOJO)‣ Search domain beans (searchable)‣ Mail (mail)‣ Excel, pdf, tsv exports (export)‣ Chart visualization (Google visualization)‣ etc. etc.
  • 55. A couple of examples
  • 56. How to install?grails tools > plugin manager
  • 57. How to install?grails tools > plugin manager
  • 58. => install new controllers, domain gsp etc.
  • 59. Might need configuration
  • 60. Might need configuration
  • 61. Enrich your web application GrailsUI
  • 62. in gsp form:<gui:datePicker id=simpleDatePicker /><gui:datePicker id=withCalendarAndTime includeTime="false"/>
  • 63. Show data chartsgoogle visualization
  • 64. <gvisualization:pieCoreChart elementId="piechart" title="MyDaily Activities" width="${450}" height="${300}" columns="${myDailyActivitiesColumns}" data="${myDailyActivitiesData}" /><div id="piechart"></div>
  • 65. How to choose a plugin?See if the plugin does what you need
  • 66. How to choose a plugin?See if the plugin does what you needLook for plugin latest release date
  • 67. How to choose a plugin?See if the plugin does what you needLook for plugin latest release dateLook for comments / user grades
  • 68. How to choose a plugin?See if the plugin does what you needLook for plugin latest release dateLook for comments / user gradesGoogle the plugin name, to see user’s comment
  • 69. How to choose a plugin?See if the plugin does what you needLook for plugin latest release dateLook for comments / user gradesGoogle the plugin name, to see user’s commentLook at authors (Burt Beckwith => you can go)
  • 70. How to choose a plugin?See if the plugin does what you needLook for plugin latest release dateLook for comments / user gradesGoogle the plugin name, to see user’s commentLook at authors (Burt Beckwith => you can go)Look at configuration
  • 71. How to choose a plugin?See if the plugin does what you needLook for plugin latest release dateLook for comments / user gradesGoogle the plugin name, to see user’s commentLook at authors (Burt Beckwith => you can go)Look at configurationTest the plugin in a dummy application
  • 72. Coding style
  • 73. Coding style‣ It’s not only for elegance
  • 74. Coding style‣ It’s not only for elegance‣ Allow clarity, readability, refactoring
  • 75. Coding style‣ It’s not only for elegance‣ Allow clarity, readability, refactoring‣ Limit complexity - max 10 lines per method - max 10 methods per class - max 10 classes par package
  • 76. Coding style (cont’d)‣ One class = one type of data
  • 77. Coding style (cont’d)‣ One class = one type of data‣ One method = one purpose (Single Level of Abstraction)
  • 78. Coding style (cont’d)‣ One class = one type of data‣ One method = one purpose (Single Level of Abstraction)‣ Inside a method: do not explicitly modify object members of another class
  • 79. Pushing coding style to the limit“Object Calisthenics” by Jeff Bay‣ Use only one level of indentation per method
  • 80. Pushing coding style to the limit“Object Calisthenics” by Jeff Bay‣ Use only one level of indentation per method‣ Dont use the else keyword
  • 81. Pushing coding style to the limit“Object Calisthenics” by Jeff Bay‣ Use only one level of indentation per method‣ Dont use the else keyword‣ Wrap all primitives and strings
  • 82. Pushing coding style to the limit“Object Calisthenics” by Jeff Bay‣ Use only one level of indentation per method‣ Dont use the else keyword‣ Wrap all primitives and strings‣ Use only one dot per line
  • 83. Pushing coding style to the limit“Object Calisthenics” by Jeff Bay‣ Use only one level of indentation per method‣ Dont use the else keyword‣ Wrap all primitives and strings‣ Use only one dot per line‣ Dont abbreviate
  • 84. Pushing coding style to the limit“Object Calisthenics” by Jeff Bay‣ Use only one level of indentation per method‣ Dont use the else keyword‣ Wrap all primitives and strings‣ Use only one dot per line‣ Dont abbreviate‣ Keep all entities small
  • 85. Pushing coding style to the limit“Object Calisthenics” by Jeff Bay‣ Use only one level of indentation per method‣ Dont use the else keyword‣ Wrap all primitives and strings‣ Use only one dot per line‣ Dont abbreviate‣ Keep all entities small‣ Dont use any classes with more than two instance variables
  • 86. Pushing coding style to the limit“Object Calisthenics” by Jeff Bay‣ Use only one level of indentation per method‣ Dont use the else keyword‣ Wrap all primitives and strings‣ Use only one dot per line‣ Dont abbreviate‣ Keep all entities small‣ Dont use any classes with more than two instance variables‣ Use first-class collections
  • 87. Pushing coding style to the limit“Object Calisthenics” by Jeff Bay‣ Use only one level of indentation per method‣ Dont use the else keyword‣ Wrap all primitives and strings‣ Use only one dot per line‣ Dont abbreviate‣ Keep all entities small‣ Dont use any classes with more than two instance variables‣ Use first-class collections‣ Dont use any getters/setters/properties
  • 88. Why Groovy + Grails?‣ Groovy: high productivity language
  • 89. Why Groovy + Grails?‣ Groovy: high productivity language‣ Grails: - spring (configuration) - hibernate (database) - web - plugins
  • 90. Why Groovy + Grails?‣ Groovy: high productivity language‣ Grails: - spring (configuration) - hibernate (database) - web - plugins‣ Java integration
  • 91. Why Groovy + Grails?‣ Groovy: high productivity language‣ Grails: - spring (configuration) - hibernate (database) - web - plugins‣ Java integration‣ Run on Java Virtual Machine
  • 92. Why Groovy + Grails?‣ Groovy: high productivity language‣ Grails: - spring (configuration) - hibernate (database) - web - plugins‣ Java integration‣ Run on Java Virtual Machine‣ JVM: a whole biotope of languages