CERN EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
Grails Framework
Modern Web Applications written in Groovy
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN You… EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
Who knows what Groovy is?
Who knows what Grails is?
Who knows JAVA?
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN Summary EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
• What is Grails • What is Groovy • Grails Architecture • Grails MVC and other goodies • Build your new grails applica on
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN What is Grails? EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
• Open Source Framework h p://grails.org • A bit of history • Bring Ruby on Rails (h p://rubyonrails.org) hight produc vity to the Java community • Was born with the name Groovy on Rails in July 2005 • David Heinemeier - founder of Ruby on Rails requested the change of name Grails • First released version (0.1) on March 2006 • In February 2008 Grails 1.0 is released
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN What is Grails? EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
• Current version 2.3.7 • A Full-stack Web Framework • Runs on the Java Virtual Machine (JVM) • Rapid development • Inspired of Ruby on Rails • Re-use exis ng Java technologies: Spring, Hibernate, … • Use a Java-like dynamic language (Groovy) • Plugin-in based framework • Conven on over configura on • Many built-in conven ons • Config files are in Groovy • Don´t Repeat Yourself (DRY) principle • Formulated by Andy Hunt and Dave Thomas in “The Pragma c Programmer”, underlies many other well- known so ware development best prac ces and design pa erns. Duplica on is waste, Repe on in process class for automa on, repe on in logic calls for abstrac on…
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN What is Groovy? EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
• Agile and Dynamic language for the JVM • Supports also sta c typing • Compiles directly to byte code • Open source • Java on steroids • Builds upon the strengths of Java but… • with power features borrowed from Smaltalk/Python/Ruby • Makes modern programming features available to Java developers with a flat learning curve • Seamlessly integrates with all exis ng Java objects and libraries • Feels natural to Java developers
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN Normal Java Program EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN
Normal Groovy Program EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN But…. EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN
A Groovier programmer EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
• Dynamic types using def keyword • Everything is public unless defined otherwise • Automa c ge ers and se ers • Semicolons at end-of-line are op onal • Variable interpola on through GStrings • Return keyword is op onal
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN Grails Architecture EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN Grails Architecture EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN Stack of Technologies EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
• Java technologies: • Spring: • Plumbing • Dependency Injec on • Transac on management • Hibernate: • Object-Rela onal Mapping • Sitemesh • Layouts • Templates • Groovy Server Pages • jQuery • Built into Grails 2.0+ • Other js libraries available as plugins
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN Conventions in Grails EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN Conventions in Grails EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
• Every Project Layout in Grails is the same • Default URL mappings:
h p://
map called “params”
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN Grails MVC EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
• MVC pa ern is a so ware architecture pa ern that encourages separa on of concerns, such as: • Model – handles data representa on and opera ons. For example, Person • View – handles how data is viewed by the user. For example, the HTML code for the list of customers. • Controller – handle the code that links a user to the system. It receives request or events from the user, prepare data and invoke proper business logic, and then return the view that represents the result of user ac ons.
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN Grails MVC EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN Controllers EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
• Responsible for handing cliente request (web requests) h p://grails.org/doc/latest/ref/Controllers/Usage.html • Controller Ac ons • Input request parameters: “params” variable • 3 ways to exit a controller ac on (The Three R´s): • render • write to output stream: REST Services… render(text: "
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN Views EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
• Groovy Server Pages (*.gsp) • Similar to Java Server Pages but using Groovy • custom tag library • They are other possibili es using Plugins
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN
Modeling with GORM EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH • Grails Object Rela onal Mapping • DSL for Managing ORM - Hibernate • Update, save, delete… • Dynamic finders generated • using Groovy metaprogramming • Define constraints
!
Person.findByNameAndSurname(“Eloy”, “Reguero Fuentes”)
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN Scope EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
• Objects can live at scopes: • page • declared in a .gsp and we can only use it there • request • objects life during the whole round-trip of the request • flash • coming from Ruvy and Rails • variable lives in the current request plus one more • great for informa on messages and errors • session • objects can be in session: for example shopping carts • applica on • not so common, en rely life of the applica on
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN Testing EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
• Unit tests • No grails infrastructure (no dependency injec on, no database,…) • Lots of mock objects are available to simulate Grails • HashMap to simulate Database • Very fast! • Integra on tests • Grails infrastructure available • Database access, Web Server, Dependency Injec on…
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN Plugins EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
h p://grails.org/plugins/ • 1079 Plug-ins available (different quali es… ) • Some good ones • Quartz schedulers • Neo4J GORM • Resources • Spring security • Searchable • Google visualisa on • …
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN Demo EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH Part 1:
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN Demo EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH Part 2:
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN Demo EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH Part 3:
New player will have the max number of dorsal +1
Eloy Reguero Fuentes April 2014 http://cern.ch/ais
CERN Your turn … EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH
Eloy Reguero Fuentes April 2014 http://cern.ch/ais