মঙ্গলবার, ১৯ জুলাই, ২০১১

Restfull in grails

Restfull for Grails :

grails create-app RestFullTest
cd RestFullTest

grails create-domain-class book

grails create-controller Book



You can add some code this /grails-app/conf/BootStrap.groovy file:
def init = { servletContext ->
new Book(author:"S.M. Mahmudul Hasan",title:"Grails").save()
new Book(author:"Shohan",title:"Restfull").save()
}

or U can insert value in db
or U can insert by programatically
Final project structure will be:




















RESTful server
We need to create controller that will be performing base CRUD operations and map its methods to REST style URLs.

Edit /grails-app/conf/UrlMappings.groovy:
class UrlMappings {

static mappings = {
"/$controller/$action?/$id?" {
constraints {
// apply constraints here
}
}

"/"(view: "/index")
"500"(view: '/error')
"/book"(controller: "book") {
action = [GET: "list", POST: "create"]
}
"/book/$id"(resource: "book")
}

Entry with /book maps GET and POST HTTP requests to this URL to list and create controller methods. Below we map URLs like /book/$id using default grails REST mapping:
  • GET -> show
  • PUT -> update
  • POST -> save (used when you know ID of item you create - not our case;)
  • DELETE -> delete
Implementation of our controller will use GORM for data storage and will communicate with client application using JSON format. Edit /grails-app/controllers/BookController.groovy:

import grails.converters.*

class BookController {
def list = {
response.setHeader("Cache-Control", "no-store")
render Book.list(params) as JSON
}

def show = {
Book b=Book.get(params.id)
render b as JSON
}

def create = {
def json = JSON.parse(request);
def b = new Book(json)
b.save()
response.status = 201
response.setHeader('Location', '/book/'+b.id)
render b as JSON
}

def delete = {
Book b=Book.get(params.id)
b.delete()
render(status: 200)
}
}

Run this code with grails run-app and point your browser to http://localhost:8080/GrailsRestTest/book. You should get following response:
[{"class":"Book","id":1,"author":"S.M. Mahmudul Hasan","title":"Grails"},{"class":"Book","id":2,"author":"Shohan","title":"Restfull"}]
To get particular item try http://localhost:8080/GrailsRestTest/book/1 - response should be:
{"class":"Book","id":1,"author":"S.M. Mahmudul Hasan","title":"Grails"}

সোমবার, ১৮ জুলাই, ২০১১

রবিবার, ১৭ জুলাই, ২০১১

About JSF

basic link

বৃহস্পতিবার, ১৪ জুলাই, ২০১১

About basic Java

What Is Java

Java is a computer programming language created by Sun Microsystems.

The Java programming language is a high-level language that can be characterized by all of the following buzzwords:

  • Simple

  • Architecture neutral

  • bject oriented

  • Portable

  • istributed

  • High performance

  • Multithreaded

  • Robust

  • Dynamic

  • Secure

Why Java is so popular

1. It is free unlike the other rivals.

2. Platform independence (Write Once Run Anywhere).

3. Automatic Garbage collection (Memory management)


Java Object: Object is a real time entity and it has state and behavior.

Java Class: Java class is a blue print of java object.

Abstract class: Abstract classes are super classes which contain abstract methods and are defined such that concrete subclasses are to extend them by implementing the methods.

Concrete class: A concrete class, however, is a class for which entities (instances) may be created. This contrasts with abstract classes which can not be instantiated because it defeats its purpose of being an 'abstract'.


Identifiers:

1. Identifiers must start with a letter, a currency character ($), or a connecting
character such as the underscore ( _ ). Identifiers cannot start with a number!
2. After the first character, identifiers can contain any combination of letters,
currency characters, connecting characters, or numbers.
3. In practice, there is no limit to the number of characters an identifier can
contain.
4. You can't use a Java keyword as an identifier. Table 1-1 lists all of the Java
keywords including one new one for 5.0, enum.
5. Identifiers in Java are case-sensitive; name and NAME are two different identifiers.


JavaBeans: JavaBeans are Java classes that have properties with setter and getter.


JavaBean Property Naming Rules:

1. If the property is not a boolean, the getter method's prefix must be get. getSize()

2. If the property is a boolean, the getter method's prefix is either get or is .getStopped() or isStopped()

3. The setter method's prefix must be set. setSize()

4. Setter method signatures must be marked public, with a void return type
and an argument that represents the property type.

5. Getter method signatures must be marked public, take no arguments, and
have a return type.

Source File Declaration Rules


1. There can be only one public class per source code file.
2. Comments can appear at the beginning or end of any line in the source code
file.
3. If there is a public class in a file, the name of the file must match the name
of the public class. For example, a class declared as public class Dog { }
must be in a source code file named Dog.java.
4. If the class is part of a package, the package statement must be the first line
in the source code file, before any import statements that may be present.
If there are import statements, they must go between the package statement
(if there is one) and the class declaration. If there isn't a package statement,
then the import statement(s) must be the first line(s) in the source code file.
If there are no package or import statements, the class declaration must be
the first line in the source code file.
5. import and package statements apply to all classes within a source code file.
In other words, there's no way to declare multiple classes in a file and have
them in different packages, or use different imports.
6. A file can have more than one nonpublic class.
7. Files with no public classes can have a name that does not match any of the
classes in the file.


Modifiers fall into two categories:
1. Access modifiers: public, protected, private.
2. Non-access modifiers (including strictfp, final, and abstract).

strictfp :

strictfp is a keyword and can be used to modify a class or a method, but never a
variable. Marking a class as strictfp means that any method code in the class will
conform to the IEEE 754 standard rules for floating points. Without that modifier,
floating points used in the methods might behave in a platform-dependent way.



Online Groovy Console

This is very nice console for groovy code compiling. link

শনিবার, ১৮ জুন, ২০১১

git

for install: http://veerasundar.com/blog/2011/06/git-tutorial-getting-started/



To see the list of git commands

git help

To see help on a git command


git help COMMAND_NAME

Configure your GIT first time


git config --global user.name "Your Full Name"
git config --global user.email "name@therapservices.net"

Configure your GIT to get colorize output


git config --global color.status auto
git config --global color.diff auto
git config --global color.branch auto

Clone your project


git clone git://masum/therap therap

Clone a bare repository for publishing (for team leads):

git clone --bare git://masum/therap therap

Add release branch

git branch --track release origin/release

Delete branch

git branch -D BRANCH_NAME

Add other’s repo as remote branch

git remote add -t master -t release otherperson git://otherperson/therap

Fetch/merge master from otherperson

git checkout master
git fetch git://otherperson master:master
git merge otherperson/master

See all the tags (usually used to mark releases)

git tag -l

See list of all commits from a particular release tag to now

git log --no-merges ..HEAD
git log --no-merges v7.2.4..HEAD

See list of all commits between two release tags

git log --no-merges ..
git log --no-merges v7.2.3..v7.2.4

See which files you are changing

git log --stat

See change details too

git log -p --stat

Viewing the changes you have made since last commit

1) git status
2) git diff
3) git diff --cached
4) git diff HEAD
  1. See status of all files changed or added
  2. See the changes that you have made but not added, changes in a file after the last git add is also
  3. See the changes that you have added to commit, changes done in working directory but not added are not shown
  4. See all changes you have made since last commit (HEAD)
Adding files for commit

1) git add filePath
2) git add filePattern
3) git add .
4) git add web/WEB-INF/
5) git add *.java
  1. Adding a single file
  2. Adding multiple files
  3. Adding all files you have changed, give the command from your project root
  4. Adding all files under web/WEB-INF directory
  5. Adding all changes to java files

Reset/Undo Changes of a file

git checkout HEAD filepath
or git checkout -- filepath

Undo Commit and Keep Changes:

git reset --soft HEAD^

To see who, when added/modified each line of a file

git blame 

Make changes in commit message before pulled by anyone else

git commit --amend

More information on git: http://git-scm.com/

Date format match with regular expression

import java.util.regex.Pattern
import java.util.regex.Matcher
......
.........

boolean isValidDate(String dates){

//String DATE_PATTERN = "(0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])/((19|20)\\d\\d)"; // for simple

String DATE_PATTERN = "(((0[13578]|1[02])\\/(0[1-9]|[12]\\d|3[01])\\/((19|[2-9]\\d)\\d{2}))|((0[13456789]|1[012])\\/(0[1-9]|[12]\\d|30)\\/((19|[2-9]\\d)\\d{2}))|(02\\/(0[1-9]|1\\d|2[0-8])\\/((19|[2-9]\\d)\\d{2}))|(02\\/29\\/((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))"

// detail solve for 31 problems/ leap year problem

Pattern pattern = Pattern.compile(DATE_PATTERN);

Matcher matcher = pattern.matcher(dates);

if(!matcher.matches()){

return true;

}

else {

return false;

}

}

System.out.println(isValidDate(06/20/2012)); // true



help from .....link