groovy & grails - lecture 10

114
Groovy: Efficiency Oriented Programming Lecture 10 Master Proteomics & Bioinformatics - University of Geneva Alexandre Masselot - summer 2011

Upload: alexandre-masselot

Post on 29-Jan-2018

810 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: groovy & grails - lecture 10

Groovy: Efficiency Oriented ProgrammingLecture 10

Master Proteomics & Bioinformatics - University of GenevaAlexandre Masselot - summer 2011

Page 2: groovy & grails - lecture 10

Agenda

‣SVN & google code hosting

‣More on GSP

‣ Including third parties libraries

‣Ajax

‣Runtime environment

‣Domains

Page 3: groovy & grails - lecture 10

SCM

‣Code evolve in time

Page 4: groovy & grails - lecture 10

SCM

‣Code evolve in time

‣Source code is shared

Page 5: groovy & grails - lecture 10

SCM

‣Code evolve in time

‣Source code is shared

‣Source Code Management system host code + versioning

Page 6: groovy & grails - lecture 10

SCM

‣Code evolve in time

‣Source code is shared

‣Source Code Management system host code + versioning

‣Many solutions exist: CVS, SVN, GIT, SourceSafe...

Page 7: groovy & grails - lecture 10

SCM

‣Code evolve in time

‣Source code is shared

‣Source Code Management system host code + versioning

‣Many solutions exist: CVS, SVN, GIT, SourceSafe...

‣SVN is a client/server solution

Page 8: groovy & grails - lecture 10

SCM

‣Code evolve in time

‣Source code is shared

‣Source Code Management system host code + versioning

‣Many solutions exist: CVS, SVN, GIT, SourceSafe...

‣SVN is a client/server solution

‣Server can be in-house

Page 9: groovy & grails - lecture 10

SCM

‣Code evolve in time

‣Source code is shared

‣Source Code Management system host code + versioning

‣Many solutions exist: CVS, SVN, GIT, SourceSafe...

‣SVN is a client/server solution

‣Server can be in-house

‣Free solution for open source

http://code.google.com/hosting/

Page 10: groovy & grails - lecture 10

make an google account

Page 11: groovy & grails - lecture 10
Page 12: groovy & grails - lecture 10

Create ProjectProject name: geop-demo

Project summary: A demo project for GEOP course

Project description: geop demo code Version control system: Subversion Source code license:  New BSD License Use a separate content license:license...

Project labels:

Create project...

Project name must start with a lowercase letter, followed by lowercase letters, digits, and dashes, with no spaces. This will be part of your project's URL and cannot be changed later.Project summary will be shown whenever the project's name is displayed.Project description is the main content of your project's home page. You may use wiki markup.Version control system selects the type of your project's repository. Learn more.Licenses determine how others may build upon your work. Code and documentation may be distributed under separate licenses.Project labels help classify your project so others can easily find it or browse projects by label.

Page 13: groovy & grails - lecture 10

Google proposes command line commandsWe’ll see how to integrate svn into eclipse

Page 14: groovy & grails - lecture 10
Page 15: groovy & grails - lecture 10

Command-line access

If you plan to make changes, use this command to check out the code as yourself using HTTPS:

# Project members authenticate over HTTPS to allow committing changes.svn checkout https://geop-demo.googlecode.com/svn/trunk/ geop-demo --username alexandre.masselot

When prompted, enter your generated googlecode.com password.

Use this command to anonymously check out the latest project source code:

# Non-members may check out a read-only working copy anonymously over HTTP.svn checkout http://geop-demo.googlecode.com/svn/trunk/ geop-demo-read-only

GUI and IDE access

This project's Subversion repository may be accessed using many different client programs and plug-ins. See your client's documentation for more information.

Page 16: groovy & grails - lecture 10

Google code offers for “free”

‣SVN repository

Page 17: groovy & grails - lecture 10

Google code offers for “free”

‣SVN repository

‣Managing contributors to a project

Page 18: groovy & grails - lecture 10

Google code offers for “free”

‣SVN repository

‣Managing contributors to a project

‣Wiki

Page 19: groovy & grails - lecture 10

Google code offers for “free”

‣SVN repository

‣Managing contributors to a project

‣Wiki

‣Ticketing system

Page 20: groovy & grails - lecture 10

Google code offers for “free”

‣SVN repository

‣Managing contributors to a project

‣Wiki

‣Ticketing system

‣Bug reporting

Page 21: groovy & grails - lecture 10

Google code offers for “free”

‣SVN repository

‣Managing contributors to a project

‣Wiki

‣Ticketing system

‣Bug reporting

‣Visibility

Page 22: groovy & grails - lecture 10

Google code hosting⇒

your code is open source

Page 23: groovy & grails - lecture 10

SVN: eclipse or command line

Page 24: groovy & grails - lecture 10

Installing svn in Eclipse

‣Help > dashboard > extension- Subversion (subclipse)

Page 25: groovy & grails - lecture 10

Installing svn in Eclipse

‣Help > dashboard > extension- Subversion (subclipse)

‣Or command line: installed by default or- sudo apt-get install subversion

Page 26: groovy & grails - lecture 10

Eclipse: connecting to the SVN repository

‣menu Window - > open perspective

- > SVN repository exploring

Page 27: groovy & grails - lecture 10

Eclipse: connecting to the SVN repository

‣menu Window - > open perspective

- > SVN repository exploring

‣view SVN repositories- > right click

- > new repository location

- https://geop-demo.googlecode.com/svn

Page 28: groovy & grails - lecture 10
Page 29: groovy & grails - lecture 10
Page 30: groovy & grails - lecture 10
Page 31: groovy & grails - lecture 10

Sharing an existing project to your google code account

Page 32: groovy & grails - lecture 10

Sharing an existing project to your google code account

‣Perspective java

Page 33: groovy & grails - lecture 10

Sharing an existing project to your google code account

‣Perspective java

‣project right-click- > team

- > share project- use specifier folder name

trunk/your.project.name

Page 34: groovy & grails - lecture 10

‣username & password

from google page

‣save password

check box

Page 35: groovy & grails - lecture 10

Commit the project

‣At this stage, the project is only created on the remote server- check with SVN repository exploring perspective- or browse source code from google page

Page 36: groovy & grails - lecture 10

Commit the project

‣At this stage, the project is only created on the remote server- check with SVN repository exploring perspective- or browse source code from google page

‣Mark generated file & folder to be ignored from svn- e.g. target/

Page 37: groovy & grails - lecture 10

Commit the project

‣At this stage, the project is only created on the remote server- check with SVN repository exploring perspective- or browse source code from google page

‣Mark generated file & folder to be ignored from svn- e.g. target/

‣Right-click > team > add to svn:ignore

Page 38: groovy & grails - lecture 10
Page 39: groovy & grails - lecture 10
Page 40: groovy & grails - lecture 10

All eclipse functionalitiescan be replaced by shell commands

Page 41: groovy & grails - lecture 10

SVN command line: share project

‣ Import original project demo.svn to the repository

‣cd workspace directorysvn import -m “initial import” --non-recursive demo.svn https://geop-demo.googlecode.com/svn/trunk/demo.svn

‣Directory itself is not imported, so checkout (with force)svn checkout --force https://geop-demo.googlecode.com/svn/trunk/demo.svn

Page 42: groovy & grails - lecture 10

SVN command line: share project (cont’d)

cd demo.svn

‣Add target/ directory to svn:ignoresvn propset svn:ignore target .

‣Add other directories to svnsvn add $(ls | grep -v target)

‣Commit filessvn commit -m "adding all initial files"

‣Back to eclipse, F5 (refresh in package explorer view)

Page 43: groovy & grails - lecture 10

Browse your project

Page 44: groovy & grails - lecture 10

From google code source page

25

Page 45: groovy & grails - lecture 10

SVN for a daily use

Page 46: groovy & grails - lecture 10

SVN for a daily useSVN for a daily useminutely

Page 47: groovy & grails - lecture 10

SVN: flow use

‣Checkout out or share an existing project to contribute

Page 48: groovy & grails - lecture 10

SVN: flow use

‣Checkout out or share an existing project to contribute

‣Commit changes to the repository

Page 49: groovy & grails - lecture 10

SVN: flow use

‣Checkout out or share an existing project to contribute

‣Commit changes to the repository

‣Update changes from repository

Page 50: groovy & grails - lecture 10

SVN: flow use

‣Checkout out or share an existing project to contribute

‣Commit changes to the repository

‣Update changes from repository

‣Eclipse: - select project- > right-click - > team- > Synchronize with repository- commit/update/manage conflict

Page 51: groovy & grails - lecture 10

SVN: flow use (cont’d)

‣Command linesvn statussvn updatesvn commit -m “my message to describe changes”

Page 52: groovy & grails - lecture 10

Commit/update often

Page 53: groovy & grails - lecture 10

SVN is more

‣History

Page 54: groovy & grails - lecture 10

SVN is more

‣History

‣See contribution from others

Page 55: groovy & grails - lecture 10

SVN is more

‣History

‣See contribution from others

‣Tag / branch versions

Page 56: groovy & grails - lecture 10

SVN is more

‣History

‣See contribution from others

‣Tag / branch versions

‣Conflict resolution

Page 57: groovy & grails - lecture 10

SVN is more

‣History

‣See contribution from others

‣Tag / branch versions

‣Conflict resolution

‣Continuous integration / testing on dedicated server

Page 58: groovy & grails - lecture 10

SVN is more

‣History

‣See contribution from others

‣Tag / branch versions

‣Conflict resolution

‣Continuous integration / testing on dedicated server

‣File locking :(

Page 59: groovy & grails - lecture 10

SVN is more

‣History

‣See contribution from others

‣Tag / branch versions

‣Conflict resolution

‣Continuous integration / testing on dedicated server

‣File locking :(

‣ Integration with tickets

Page 60: groovy & grails - lecture 10

SVN is more

‣History

‣See contribution from others

‣Tag / branch versions

‣Conflict resolution

‣Continuous integration / testing on dedicated server

‣File locking :(

‣ Integration with tickets

‣Windows integration: http://tortoisesvn.tigris.org/

Page 61: groovy & grails - lecture 10

Back to grails... and proteins isoforms

Page 62: groovy & grails - lecture 10

Problem: uniprot AC ➙ isoform listin a web application

Page 63: groovy & grails - lecture 10

Grails: including a third parties libraries

‣Library is available as a jar (export ➙ .jar)

Page 64: groovy & grails - lecture 10

Grails: including a third parties libraries

‣Library is available as a jar (export ➙ .jar)

‣Copy jar in lib/

Page 65: groovy & grails - lecture 10

Grails: including a third parties libraries

‣Library is available as a jar (export ➙ .jar)

‣Copy jar in lib/

‣For eclipse completion- right-click- build path- add to build path

Page 66: groovy & grails - lecture 10

Get is forms for ac (00:44:01.975)Q70Z44List

>Q70Z44-1MQKHSPGPPALALLSQSLLTTGNGDTLIINCPGFGQHRVDPAAFQAVFDRKAIGPVTNYSVATHVNISFTLSAIWNCYSRIHTFNCHHARPWHNQFVQWNPDECGGIKKSGMATENLWLSDVFIEESVDQTPAGLMASMSIVKATSNTISQCGWSASANWTPSISPSMDRARAWRRMSRSFQIHHRTSFRTRREWVLLGIQKRTIKVTVATNQYEQAIFHVAIRRRCRPSPYVVNFLVPSGILIAIDALSFYLPLESGNCAPFKMTVLLGYSVFLLMMNDLLPATSTSSHASLVAPLALMQTPLPAGVYFALCLSLMVGSLLETIFITHLLHVATTQPLPLPRWLHSLLLHCTGQGRCCPTAPQKGNKGPGLTPTHLPGVKEPEVSAGQMPGPGEAELTGGSEWTRAQREHEAQKQHSVELWVQFSHAMDALLFRLYLLFMASSIITVICLWNT

>Q70Z44-2MASMSIVKATSNTISQCGWSASANWTPSISPSMDRAERSPSALSPTQVAIRRRCRPSPYVVNFLVPSGILIAIDALSFYLPLESGNCAPFKMTVLLGYSVFLLMMNDLLPATSTSSHASLVRPHPSRDQKRGVYFALCLSLMVGSLLETIFITHLLHVATTQPLPLPRWLHSLLLHCTGQGRCCPTAPQKGNKGPGLTPTHLPGVKEPEVSAGQMPGPGEAELTGGSEWTRAQREHEAQKQHSVELWVQFSHAMDALLFRLYLLFMASSIITVICLWNT

Page 67: groovy & grails - lecture 10

Isoforms_v1 -> v5 same controllerdifferent views (gsp)

for different rendering interaction

Page 68: groovy & grails - lecture 10

Isoforms_v1 -> v5 same controllerdifferent views (gsp)

for different rendering interaction

Page 69: groovy & grails - lecture 10

Isoforms_vXController⇒

views/isoforms_vX/ direcgtory with gsp’s

Page 70: groovy & grails - lecture 10

IsoformController_v?.groovy

def list = { def ac=params.ac def xml=new XmlSlurper() .parseText("http://pir.uniprot.org/uniprot/${ac}.xml".toURL().text).entry UniprotEntrySplicer splicer=[entryXml:xml] def proteins=splicer.buildAllIsoforms().values() as List

[proteins: proteins] }

Page 71: groovy & grails - lecture 10

v1

Page 72: groovy & grails - lecture 10

isoforms_v1/list.gsp

<html> <body> <h2>Get is forms for ac (${String.format('%tH:%<tM:%<tS.%<tL', new Date())})</h2> <g:form action="list"> <g:textField name="ac" value="${params.ac}" /> <g:submitButton name="submit" /> </g:form> <h3>List</h3> <g:each in="${proteins}" var="prot"> <pre> ${prot} </pre> </g:each> </body></html>

Page 73: groovy & grails - lecture 10

Problem: direct access to /list only

Page 74: groovy & grails - lecture 10

v2

Page 75: groovy & grails - lecture 10

v2

Page 76: groovy & grails - lecture 10

isoforms_v2: add index.gsp

<html> <body> <h1>Isoform list application</h1> <h2>Get is forms for ac (${String.format('%tH:%<tM:%<tS.%<tL', new Date())})</h2> <g:form action="list"> <g:textField name="ac" value="${params.ac}" /> <g:submitButton name="submit" /> </g:form> </body></html>

Page 77: groovy & grails - lecture 10

Problem: <g:form> is duplicated (DRY!)

Page 78: groovy & grails - lecture 10

isoform_v3: use template

‣_form.gsp<h2>Get is forms for ac (${String.format('%tH:%<tM:%<tS.%<tL', new Date())})</h2><g:form action="list"> <g:textField name="ac" value="${params.ac}" /> <g:submitButton name="submit" /></g:form>

Page 79: groovy & grails - lecture 10

isoform_v3: use template

‣_form.gsp<h2>Get is forms for ac (${String.format('%tH:%<tM:%<tS.%<tL', new Date())})</h2><g:form action="list"> <g:textField name="ac" value="${params.ac}" /> <g:submitButton name="submit" /></g:form>

‣ In index.gsp and list.gsp <g:render template="form" />

Page 80: groovy & grails - lecture 10

isoform_v3: use template

‣_form.gsp<h2>Get is forms for ac (${String.format('%tH:%<tM:%<tS.%<tL', new Date())})</h2><g:form action="list"> <g:textField name="ac" value="${params.ac}" /> <g:submitButton name="submit" /></g:form>

‣ In index.gsp and list.gsp <g:render template="form" />

Page 81: groovy & grails - lecture 10

Problem: all the page is reloaded totally

Page 82: groovy & grails - lecture 10

v4

Page 83: groovy & grails - lecture 10

Ajax: asynchronous load

47

Q70Z44

web browser

submit

isoform/list

server

Page 84: groovy & grails - lecture 10

Ajax: asynchronous load

47

Q70Z44

web browser

submit

isoform/list

server

Page 85: groovy & grails - lecture 10

Ajax: asynchronous load

47

Q70Z44

List

>Q70Z44-1MQKHSPGPPALALLSQSLLTTGNGDTLIINCPGFGQHRVDPAAFQAVFDRKAIGPVTNYSVATHVNISFTLSAIWNCYSRIHTFNCHHARPWHNQFVQWNPDECGGIKKSGMATENLWLSDVFIEESVDQTPAGLMASMSIVKATSNTISQCGWSASANWTPSISPSMDRARAWRRMSRSFQIHHRTSFRTRREWVLLGIQKRTIKVTVATNQYEQAIFHVAIRRRCRPSPYVVNFLVPSGILIAIDALSFYLPLESGNCAPFKMTVLLGYSVFLLMMNDLLPATSTSSHASLVAPLALMQTPLPAGVYFALCLSLMVGSLLETIFITHLLHVATTQPLPLPRWLHSLLLHCTGQGRCCPTAPQKGNKGPGLTPTHLPGVKEPEVSAGQMPGPGEAELTGGSEWTRAQREHEAQKQHSVELWVQFSHAMDALLFRLYLLFMASSIITVICLWNT >Q70Z44-2MASMSIVKATSNTISQCGWSASANWTPSISPSMDRAERSPSALSPTQVAIRRRCRPSPYVVNFLVPSGILIAIDALSFYLPLESGNCAPFKMTVLLGYSVFLLMMNDLLPATSTSSHASLVRPHPSRDQKRGVYFALCLSLMVGSLLETIFITHLLHVATTQPLPLPRWLHSLLLHCTGQGRCCPTAPQKGNKGPGLTPTHLPGVKEPEVSAGQMPGPGEAELTGGSEWTRAQREHEAQKQHSVELWVQFSHAMDALLFRLYLLFMASSIITVICLWNT

web browser

submit

isoform/list

server

Page 86: groovy & grails - lecture 10

isoform_v4/index.gsp

<html> <head> <g:javascript library="prototype" /> </head> <body> <h2>Get is forms for ac (${String.format('%tH:%<tM:%<tS.%<tL', new Date())})</h2> <!-- g:formRemote stands for an ajax form --> <g:formRemote name="listForm" url="[action:'list']" update="isoforms-list"> <g:textField name="ac" value="${params.ac}" /> <g:submitButton name="submit" /> </g:formRemote> <!-- the target element of the form --> <div id="isoforms-list"/> </body></html>

Page 87: groovy & grails - lecture 10

isoform_v4/list.gsp

<h3>List</h3><g:each in="${proteins}" var="prot"> <pre>${prot} </pre></g:each>

Page 88: groovy & grails - lecture 10

Twitter 1

Page 89: groovy & grails - lecture 10

People post messages and follow others

Page 90: groovy & grails - lecture 10

database (domain)+ controller (actions)

+ views (web)

Page 91: groovy & grails - lecture 10

Domain: Person & Message

Page 92: groovy & grails - lecture 10

Domain: bean definition + database storage

Page 93: groovy & grails - lecture 10

Person domain

‣Create the database entry + bean class grails create-domain-class Personproject > right-click > grails > create-domain-class Person

Page 94: groovy & grails - lecture 10

Person domain

‣Create the database entry + bean class grails create-domain-class Personproject > right-click > grails > create-domain-class Person

‣Files created:./grails-app/domain/eop/lec10/twitter/Person.groovy./test/unit/eop/lec10/twitter/PersonTests.groovy

Page 95: groovy & grails - lecture 10

Person.groovy

‣Created classpackage eop.lec10.twitter

class Person {

static constraints = { }}

Page 96: groovy & grails - lecture 10

Person.groovy

Page 97: groovy & grails - lecture 10

Person.groovy

‣Enriched with fieldspackage eop.lec10.twitter

class Person { String username String firstName String lastName String email Date dateCreated

static constraints = { }}

Page 98: groovy & grails - lecture 10

run-app ⇒ database + table created

Page 99: groovy & grails - lecture 10

Web access: need controller + views

Page 100: groovy & grails - lecture 10

Scaffolding is pure magic

Page 101: groovy & grails - lecture 10

PersonController.groovy

create-controller Person

Page 102: groovy & grails - lecture 10

PersonController.groovy

create-controller Person

‣grails-app/controller/PersonController.groovy:package eop.lec10.twitter

class PersonController { def scaffold = true}

Page 103: groovy & grails - lecture 10

PersonController.groovy

create-controller Person

‣grails-app/controller/PersonController.groovy:package eop.lec10.twitter

class PersonController { def scaffold = true}

‣scaffold generate all default action & view implicitely

Page 104: groovy & grails - lecture 10

PersonController.groovy

create-controller Person

‣grails-app/controller/PersonController.groovy:package eop.lec10.twitter

class PersonController { def scaffold = true}

‣scaffold generate all default action & view implicitely

Page 105: groovy & grails - lecture 10

http://localhost:8080/eop.lec10.twitter/person

‣Default url is redirected on the list page

Page 106: groovy & grails - lecture 10

http://localhost:8080/eop.lec10.twitter/person

‣Default url is redirected on the list page

Page 107: groovy & grails - lecture 10

http://localhost:8080/eop.lec10.twitter/person

‣Entering a new Person lucky luke

Page 108: groovy & grails - lecture 10

http://localhost:8080/eop.lec10.twitter/person

‣Entering a new Person lucky luke

Page 109: groovy & grails - lecture 10

http://localhost:8080/eop.lec10.twitter/person

‣Entering a new Person lucky luke

Page 111: groovy & grails - lecture 10

http://localhost:8080/eop.lec10.twitter/person

‣Back to list- view all- edit- delete- sort

Page 112: groovy & grails - lecture 10

Domain constraints

Page 113: groovy & grails - lecture 10

Person.groovy

class Person { String username String firstName String lastName String email Date dateCreated

//firstName is compulsory //email field has an email format //username cannot be null, is unique and is between 6 and 20 characters static constraints = { firstName(blank:false) email(email:true, blank:false) username(blank:false, unique:true, matches:/\w{6,20}/) }}

Page 114: groovy & grails - lecture 10