Menu

Drop Down MenusCSS Drop Down Menu Pure CSS Dropdown Menu

Groovy and Grails



Groovy is an object oriented language which is based on Java platform. Groovy 1.0 was released in January 2, 2007 with Groovy 2.4 as the current major release. Groovy is distributed via the Apache License v 2.0.
Features of Groovy
Groovy has the following features −
Support for both static and dynamic typing.
  • Support for operator overloading.
  • Native syntax for lists and associative arrays.
  • Native support for regular expressions.
  • Native support for various markup languages such as XML and HTML.
  • Groovy is simple for Java developers since the syntax for Java and Groovy are very similar.
  • You can use existing Java libraries.
  • Groovy extends the java.lang.Object.

Grails is a web application framework that is great to use for real projects. It is also very friendly for beginners to learn. It is contrary to the steep learning curve required for other Java based frameworks. With Grails, you can start hacking through it and be productive in a matter of hours. Ideal as starting point even for developers with no web experience.

what is the difference between get() and read()?
 domain.get() retrieved data we can modify it but domain.read() retrieved data read Only mode we can not modify it .
what's is groovy?
is an dynamic, object oriented, scripting language that it's compiled in byte code and runs over the JVM. this language include some features like metha programing, closures, etc.

what is grails?
Grails is an open source web application framework that uses the Groovy and Java as programming language, this framework use another frameworks like Spring, Hibernate, SiteMesh and have and embebed H2 database, Tomcat server and ORM(GORM). this framework follow some design patters as Model View Controller(MVC), Convention Over Configuration(CoC), Don't repeat yourself(DRY) and runs over the Java Virtual Machine(JVM).
What is ORM?
means object relational mapping, A simple answer is that you wrap your tables or stored procedures in classes in your programming language, so that instead of writing SQL statements to interact with your database, you use methods and properties of objects.
It's a library or framework that help you to map/convert your models in to tables in your database, It's like a mediator or an intermediate that avoid that you have to write SQL code and allows you to interact with the DB using a programming language like groovy without have to worry about write SQL code or what kind of database are you using, you can switch from mySql to Oracle DB modifying only 4 lines of code, an example of what is an ORM It's the Hibernate Framework.
What is GORM?
GORM is Grails' object relational mapping (ORM) implementation. Under the hood it uses Hibernate (a very popular and flexible open source ORM solution) and thanks to the dynamic nature of Groovy with its static and dynamic typing, along with the convention of Grails, there is far less configuration involved in creating Grails domain classes.
what is the command to create a new application in grails?
grails create-app "the name of your app"
what is the command to run a grails application?
grails run-app
what is the command to create a domain class?
grails create-domain-class "packpage" +"name of your domain class"
what is the command to create a controller?
grails create-controller  "packpage" +"name of your domain class"
what are the default environments in grails?
Development, Test And Production
what is the command to generate a war file?
grails war
what is the command to run your application in a different environment that development?
grails  -Dgrails.env=test run-app
what is the command to run you application in a different port that 8080?
grails  -Dserver.port=9090 run-app
what is the command to do static scaffolding of a controller?
grails generate-controller "packpage"+"domain class"
what is the command to do static scaffolding of the view of a domain?
grails generate-views "packpage"+"domain class"
what is the command to generate the static scaffolding of the controllers and views of a domain class?
grails generate-all "packpage"+"domain class"
what is the command to generate the static scaffolding of the controllers and views of all your domain classes?
grails generate-all "*"
what is the command to stop your application?
grails stop-app
what is the command to test your application?
grails test-app
what is the command to test your application for unit test only?
grails test-app --unit
what is the command to test your application for integration test only?
grails test-app --integration
what is the command to rerun test in your application?
grails test-app --rerun
what is the command to see your grails version?
grails  -version
what is the command to create the unit test of a domain class?
grails create-unit-test "packpage"+"domain class"
what is the command to create the integration test of a domain class?
grails create-integration-test "packpage"+"domain class"
what is data binding?
It's the act of binding incoming request parameters on to the properties of an object.
where do you setup your DB , hibernate version and environments?
DataSource.groovy
where do you setup your dependency manager, your repositories, dependencies and pluguins?
BuildConfing.groovy
what is the difference between BuildConfig.groovy and Config.groovy?
that Config include configuration needed to run your application and BuildConfig include configuration to build and deploy your application.
what do you have to do if you don't want that a property of your domain be mapped to the DB?
in your domain class:
static transient['cant save this']
what is methaprogramming?
It's the groovy ability to add new methods or variables to classes dynamically at run time, and without torching the code.
could you give me an example of methaprogramming in grails?
The dynamic finders in the domains class.
what type of looking is by default in grails optimistic or pessimist looking?
optimistic looking
Optimistic locking is a feature of Hibernate which involves storing a version value in a special version column in the database that is incremented after each update.
What is pessimistic locking.
Locking a row until the current transaction is completed. This has the implication that other read operations will be blocking until the lock is released.
how do you use pessimist looking in a domain class?
Call  ".lock()" method on domain instance.
Example.
def airport = Airport.get(10)
airport.lock() // lock for update
airport.name = "Heathrow"
airport.save()

or defining in your domain class "version= false"
Example:
class Person {
    …
    static mapping = {
        version false // here you disability optimist looking
        autoTimestamp false
    }
}

What method do you use to check if any field of an object has been modified.
isDirty
getDirtyPropertyNames : to get names of modified fields.
What is dynamic finders.
Are the methods auto generated by grails based on fields of the domain class.

               Example. class Book {
                   String title
                   Date releaseDate
                   Author author
                       }
              
               def book = Book.findByTitle("The Stand")
               book = Book.findByTitleLike("Harry Pot%")

Associations in GORM are by default lazy or eager?
lazy
how do you Configuring Eager Fetching in a domain class?
using:
lazy: false
or
fetch: 'join'
Example:
class Airport {
    String name
    static hasMany = [flights: Flight]
    static mapping = {
        flights lazy: false //here you configure eager fetching
    }
}
class Person {
    String firstName
    Pet pet
    static hasMany = [addresses: Address]
    static mapping = {
        addresses lazy: false
        pet fetch: 'join' //this is another way to configure eager fetching
    }
}
What is the configuration file to define URL pattern and its controller and action name?
grails-app/conf/UrlMappings.groovy.
How do you secure your grails application?
Use Grails Filter
How can I turn on logging for hibernate in order to see SQL statements, input parameters and output results?
Edit your Config.groovy file. Find the line with:
      hibernate = "off"


and replace it with:

hibernate.SQL="trace,stdout"
hibernate.type="trace,stdout"
 How do you render a domain object as XML or JSON.
render Book.list(object) as JSON
render Book.get(object) as XML
How do you access servlet attributes from controller.
servletContext
  servletContext is available in controller.
What is javascript tag in GSP.
Includes JavaScript libraries and scripts as well as providing a shorthand for inline JavaScript.
What is the name of config file to define database connection properties.
grails-app/conf/DataSource.groovy.
What is command to refresh dependencies?
grails refresh-dependencies
What option do you use to create war file for specific environment?
-Dgrails.env
Example : grails -Dgrails.env=dev war
By default services in grail is singleton or prototype
Singleton.
what is the command to create a service in grails?
grails create-service "packpage"+"service name"
what is the command to create a tag library in grails?
grails create-tag-library+ "packpage"+"name"
what is are the closures in groovy?
A closure in Groovy is an open, anonymous, block of code that can take arguments, return a value and be assigned to a variable
how do you inject a service in your controller?
def taskService
how render a template in a view in grails?
use the tag:
<g:render  template="form"/>
how you can load preload data when startup your aplication?
using Boostrap.groovy
how use dynamic scaffolding in your app?
in your controller:
def scaffold=true  or def scaffold="the name of your domain class"
what is a template in grails?
it's a reusable part of a view that can be used to render a small part of a view.
what is a grails pluguin?
it's a bundle/set of functionality to a desired propose that can be installed in your grails application.
how you can use a tag library in your view?
<g:"your tag library name" />
what is the command to compile your grails application?
grails compile
what is the difference between User.findByName() and User.findAllByName() in the dynamic finders?
That Findbyname Return The First Result And Findallbyname Return A List With All The Results
how you can map directly a domain class to an specific table in your DB?
in your domain class:
class Person {
    …
    static mapping = {
        table 'people'///this is the mapping of the domain in the table
    }
}
how you can map directly a property of a domain class to an specific column in table of your DB?
class Person {
    String firstName
    static mapping = {
        table 'people'
        firstName column: 'First_Name' ///this is the mapping of the columm in the table
    }
}
what is SiteMesh?
It's a HTML templating framework
how you can validate your domain classes against invalid data?
using :  static constraints{ }
how you can have control over the way grails mapping the URLs in your application?
in "conf/UrlMappings.groovy" you can customize how grails maps the URLs in your application   
what is the command to run your app in a custom environment?
grails -Dgrails.env="your custom enviroment" run-app
how you can use a namespace different than "g: " to render your tag library in your views?
using the  line in your tag library:
static namespace = "your custom namespace"
what are the differents options to "dbCreate" inside a data source of an environment?
'create', 'create-drop', 'update', 'validate' and ' null '
what happens with the DB if you have 'create' in the option "dbCreate" of your data source?
Drops the existing schemaCreates the schema on startup, dropping existing tables, indexes, etc. first.
  what happens with the DB if you have 'create-drop' in the option "dbCreate" of your data source?
Same as create, but also drops the tables when the application shuts down cleanly.
what happens with the DB if you have 'update' in the option "dbCreate" of your data source?
Creates missing tables and indexes, and updates the current schema without dropping any tables or data. Note that this can't properly handle many schema changes like column renames (you're left with the old column containing the existing data).
what happens with the DB if you have 'validate' in the option "dbCreate" of your data source?
Makes no changes to your database. Compares the configuration with the existing database schema and reports warnings.
what happens with the DB if you have 'nothing/null' in the option "dbCreate" of your data source?
 Does nothing with your data base
What is new scope available in the grails services which makes sure that a new service will be created for the current and next request only.
Flash
What annotation is used for unit testing of grails controller
grails.test.mixin.TestFor
how you can create a One-to-many relationship in GORM?

you can use static hasMany property at the "one" side:

class User {
    static hasMany = [articles: Article]
}

class Article {
}

how you can create a bidirectional relationships in GORM?
using:
static belongsTo = User

example:
class User {
    static hasMany = [articles: Article]
}
class Article {
    static belongsTo = User
}

how you can create One-to-One relationship in GORM?

There are two ways of specifying a one-to-one relationship:
                                    First Add an article property and the unique constraint to the User domain:
class User {
    Article article
    static constraints = {
        article unique: true
    }
}
class Article {
    static belongsTo = [user: User]
}

                        Second use the hasOne property on the owning (User) side:
class User {
    static hasOne = [article: Article]
}
class Article {
    static belongsTo = [user: User]
                        }



how you can create Many-to-Many relationship in Gorm?
Many-to-many relationships in Grails can be specified by defining a hasMany property on both sides and having a belongsTo on the owned side:
class Tag {
    static belongsTo = Post
    static hasMany = [posts: Post]
}
class Post {
    static hasMany = [tags: Tag]
}
What is the use of flush option in save operation of a domain.
Saves the row immediately without any delay.
Example 



def book = new Book(title:"New Grails Book")
book.save(flush:true)
how can you install a pluguin in your grails application?
you have to write your pluguin inside the pluguins Block/closure {} in the BuildConfig.groovy file and you have to specify if your pluguin works at run time or compilation time.
Example:
plugins {
                        build ":tomcat:7.0.55"
                        compile ":scaffolding:2.1.2"
                        runtime ":jquery:1.11.1"
}
How can you made a all the method of a service in grails be transactional?
with the anotation @Transactional above the class's name

Example:
import grails.transaction.*

@Transactional
class CountryService {
}

how you can made a method of a service be transactional?
with the anotation @Transactional above the method's name of the service

Example:
import grails.transaction.Transactional

class BookService {
   
@Transactional
    def updateBook() {
        // …
    }
}

how you can make a method not transactional if the whole class was marked as transactional?
using the annotation @NotTransactional above the method's name in the service
Example:
import grails.transaction.Transactional

@Transactional
class BookService {
    @NotTransactional
    def listBooks() {
                 Book.list()
        }
    }
What is another way to make a transaction method that not use the @Transactional annotation in grails?
using the ".withTransaction" method

Example:
Account.withTransaction { status ->
    def source = Account.get(params.from)
    def dest = Account.get(params.to)
    int amount = params.amount.toInteger()
    if (source.active) {
        source.balance -= amount
        if (dest.active) {
            dest.amount += amount
        }
        else {
            status.setRollbackOnly()
        }
    }
}

No comments:

Post a Comment

Allah Is Almighty