ช่วงนี้เข้า grials66 กับ agile66 บ่อยๆแล้วก็เริ่มสับสน URL (-__-” ) ลองทบทวนตัวเองดูว่าทำไมถึงสับสนนะ? แล้วก็ค้นพบว่า…
มันต้องไม่ใช่เรื่องบังเอิญแน่ๆ
Felipe Cypriano เขียนถึงวิธีการใช้ ZK plugin สำหรับ Grails เพื่อช่วยในการพัฒนา RIA คอมโพเนนท์ให้ง่ายขึ้นผ่านระบบ syntax แบบ builder ครับ
ผ่าน Groovy DZone

ปกติเมื่อเราสร้าง domain class แบบปกติก็คือ
class Customer {
static constraints = { }
String name
Date created_at
Date updated_at
}
ตัว grails จะ generate table ให้ และจะได้ ชื่อ table คือ
customer
———–
id
version
name
created_at
updated_at
ซึ่งในความจริง หาก table มีเยอะ ๆ ก็จะทำให้สับสนได้ ปัญหาโดยทั่วไปที่ใช้แก้ก็คือใช้ prefix name ให้กับ table นั้นเอง
ซึ่งในที่นี่ อยากให้ grails สร้าง
- table ให้เป็น cus_customer
- นำ column: version ใน table ออก
- เปลี่ยน primary key จาก id เป็น customer_id
สิ่งที่ต้องเขียน เพิ่มก็คือ
class Customer {
...
static mapping = {
table 'cus_customer' // ชื่อ table ที่ต้องการให้ grails สร้าง
version false // ไม่ต้องการ column version ใน table
columns {
id column:'customer_id' // กำหนด primary key เป็น customer_id
}
}
}
วิธีการนี้จะทำการ maintain table ได้ง่าย ยิ่งหากใช้ tools เช่น plsql developer, toad เป็นต้น
————————–
สิ่งที่ต้องติดตามต่อก็คือ แล้วเวลามัน join กับ table ด้วยวิธีการ hasMany, belongsTo, และอื่นละ จะเป็นยังไง
เตรียม table ที่จะมา join ด้วยนั้นก็คือ table Orderlist
class Orderlist {
static constraints = {
}
static belongsTo = [cus:Customer]
static mapping = {
table 'odr_orderlist' // ชื่อ table ที่ต้องการให้ grails สร้าง
version false // ไม่ต้องการ column version ใน table
}
String name_order
Date created_at
Date updated_at
}
จากนั้นก็เพิ่ม ใน Customer domain [ Customer.groovy ]
static hasMany = [orderlists:Orderlist]
ผล ปรากฏว่า
สามารถใช้งานได้ตามปกติเหมือนเดิม ^___^
เท่านี้ ก็สามารถปรับแต่งชื่อ table และ primary key ได้อย่างสบาย
อ้างอิงจาก http://www.grails.org/GORM+-+Mapping+DSL
กลับมาอีกแล้ว แบบ 2 ปีกลับมาที 555+
ช่วงนี้มีคนมาใช้ grails ให้เห็น และกำลังจะ train ให้น้องๆ รู้จัก grails แบบพื้นฐาน เลยกลับมาดู grails อีกรอบ ลองทำ authentication โดยใช้ Acegi plugin เลยจะเอามาจดไว้ที่นี่ค่ะ
เท่าที่ดู พวก plugin ของ grails สำหรับการทำ authentication ก็มีหลายตัวเหมือนกัน ตัวที่น่าสนใจ คือ ทำแบบปะ annotation ไว้ที่ method ได้ ก็ทำให้ add role เข้าไปได้แบบยืดหยุ่นมากๆ เลยเลือก acegi ตัวนี้ เพราะมันสามารถใช้งานแบบที่ต้องการได้ค่ะ
หลังจากที่ลองทำตาม tutorial ดูแล้ว ก็พบว่า ใน tutorial ก็มีส่วนที่ไม่ครบ และก็มีความแตกต่างกันในเวอร์ชั่นด้วย เลยเข้าไปแก้ tutorial ใน grails.org ไปนิดหน่อย แต่ก็อยากเอามาจดไว้ที่นี่เหมือนกัน อันนี้เก๋ทำตอนที่เป็น grails 1.2.1 นะคะ (ล่าสุด ณ ขณะนี้)
หลังจากที่ create-app เรียบร้อยแล้ว ก็ให้ install ตัว plugin ลงใน app ของเรานะคะ
# grails install-plugin acegi
แล้วก็สร้าง domain ของ User และ Role (ไม่ต้องสร้าง RequestMap เพราะเราจะใช้ annotation)
# grails create-auth-domains User Role Requestmap //ต้องใส่ parameter 3 ตัวให้ครบก่อน แต่ตอนหลังลบ Requestmap ออก หากจะใช้ annotation
สิ่งที่ได้ จะเป็น User.groovy และ Role.groovy อยู่ใน grails-app/domain
ถ้าอยาก create controller กับ view เพิ่มสำหรับ domain class ก็ใช้คำสั่งนี้ได้เลย
# grails generate-manager
generate controller มาตรฐานของ Grails
# grails generate-all User # grails generate-all Role # grails generate-all Requestmap
ใน UserController ที่ถูก generate ขึ้นมา ไม่ได้มีการ encode ตัว password เอาไว้ให้ ทำให้ตอนที่เราไปใช้งานจริง เวลาเราพิมพ์ password เข้าไป ระบบ reject ค่า ดังนั้น จึงต้องเพิ่มสิ่งต่อไปนี้เข้าไปด้วย
- import org.grails.plugins.springsecurity.service.AuthenticateService
- ประกาศ def authenticateService ใน class UserController
- เพิ่มบรรทัดนี้
userInstance.passwd = authenticateService.encodePassword(params.passwd)
ในส่วน update closure หลังบรรทัด userInstance.properties = params และในส่วน save หลังบรรทัด def userInstance = new User(params)
หากอยากได้ option เสริมให้สร้างพวกหน้า register, captcha หรือว่า email service ก็ใช้คำสั่งนี้
# grails generate-registration
ลบ requestMap ออกก็ได้หากไม่ใช้
คราวนี้ ก็จะให้มันสามารถใช้งาน annotation โดยการไปแก้ไฟล์ grails-app/conf/SecurityConfig.groovy ให้มัน disable ตัว requestMap และ enable การใช้งาน controller annotation แทน โดยถ้าดูตาม tutorial ก็แค่ไปกำหนดค่า true false ให้มัน แต่พอมาดู code ในเวอร์ชั่น 1.2.1 ที่ใช้งาน มันไม่เหมือนกับใน tutorial เลยเกือบงง มันเป็นบรรทัดที่เขียนว่า
requestMapClass = "Requestmap"
เก๋เลยแค่ comment บรรทัดนี้ไป แล้วใส่
useControllerAnnotations = true
เข้าไปแทน แต่เข้าใจว่า น่าจะมีวิธีอื่นอีก ใครทราบก็แนะนำด้วยค่ะ
ตอนนี้เราเตรียมทุกอย่างให้กับการใช้ annotation เสร็จเรียบร้อยแล้ว ทีนี้จะลองเอามาใช้งานกันดู
ตาม tutorial จะลองสร้าง controller ตัวนึงขึ้นมาแล้วลองแปะ annotation เข้าไปค่ะ
สร้าง controller ชื่อ Secure
grails create-controller Secure
แก้ grails-app/controllers/SecureController.groovy โดยลองไปใส่ค่าในหน้า index ให้แสดง text เพื่อที่จะเอาไว้ test เฉยๆ ใส่ว่าอะไรก็ได้ เก๋จะทำตาม tutorial ไปเลยนะคะ
class SecureController {
def index = {
render 'Secure access only'
}
}
แล้วก็ลองใช้คำสั่ง grails run-app ดูเลย หากตอนนี้เราเข้าไปที่หน้า SecureController ใน browser จะเห็น text ที่เราใส่ไว้เลย โดยไม่ต้องผ่านการ authenticate อะไรทั้งสิ้น อันนี้เป็นการ test เฉยๆ ว่า app เราใช้งานได้แล้ว
กลับไปที่ grails-app/controllers/SecureController.groovy อีกรอบ ทีนี้ปะ annotation ลงไปเลย สิ่งที่เรากำลังจะทำคือ จะทำให้หน้า secure เข้าได้โดย user ที่มี role เป็น admin เท่านั้น (จริงๆ เราจะปะเหนือ class เลยก็ได้ มันจะครอบคลุมทั้งหมดใน class นี้)
ต้อง import org.codehaus.groovy.grails.plugins.springsecurity.Secured ด้วย
import org.codehaus.groovy.grails.plugins.springsecurity.Secured
class SecureController {
@Secured(['ROLE_ADMIN'])
def index = {
render 'Secure access only'
}
}
จะเห็นว่าเรากำหนดชื่อ role เป็น ROLE_ADMIN หมายความว่า เราจะต้องสร้าง role นี้ใน database ด้วย
create user ที่หน้า UserController ใส่ข้อมูล user เข้าไป
create role ที่หน้า RoleController ใส่ค่า Authority: ROLE_ADMIN, description: xxxx แล้วกด edit อีกครั้งเพื่อ add user เข้าไปใน role นี้ (มีวิธีที่ดีกว่านี้มั๊ยเนี่ย = =)
คราวนี้ถ้าเราลอง run-app ใหม่อีกครั้ง แล้วลองไปเข้าหน้า SecureController มันจะถาม user password เราก่อนเข้า หากมันเช็คแล้วว่าเป็น user ที่เป็น ROLE_ADMIN ก็จะมองเห็นข้อความ Secure access only แต่หากลอง login ด้วย user อื่น มันก็จะเด้งไปหน้า denied เลย
เก๋ว่า มันง่ายมากกว่า Authentication ที่เคยทำมาเลยนะเนี่ย
ติชม จักขอบพระคุณยิ่งค่ะ
Reference: Grails – AcegiSecurity Plugin – Basic Tutorial with Annotations
Tags: authentication, login
ทาง InfoQ ได้ออก Free EBook ชื่อว่า Getting Started with Grails, Second Edition เป็น edittion 2 ซึ่ง update มาจากเล่มแรกที่ออกมานานแล้ว [ ในเล่นแรกนั้นอ่านง่ายมากครับ ]
หนังสือเล่มนี้มี 160 หน้า สามารถ download ได้ free เป็น file PDF นะครับ จากที่นี่ [ แต่ต้อง login เข้า InfoQ ก่อนนะครับ ]
เนื้อหาในหนังสือเล่นนี้ ประกอบไปด้วย
1. INTRODUCTION
Learning by Example
The RaceTrack Application2. INSTALLING GRAILS
Installing a JDK
Installing Grails
Installing a Database3. CREATING A GRAILS APPLICATION
Creating the RaceTrack Application
The Grails Directory Structure
Domain Classes
Scaffolding Controllers and Views4. VALIDATION
Customizing the Field Order
Adding Validation
Changing Error Messages
Creating Custom Validations
Testing Validations5. RELATIONSHIPS
Creating a One-to-Many Relationship
Creating a Many-to-Many Relationship
Bootstrapping Data6. DATABASES
GORM
DataSource.groovy
Switching to an External Database7. CONTROLLERS
create-controller vs. generate-controller
Understanding URLs and Controllers
From Request to Controller to View
A Quick Look at GSPs
Exploring the Rest of the Controller Actions
Rendering Views That Don’t Match Action Names8. GROOVY SERVER PAGES
Understanding GSPs
Understanding SiteMesh
Understanding Partial Templates
Understanding Custom TagLibs
Customizing the Default Templates9. AUTHENTICATION
Implementing User Authentication
Unit Testing Controllers
Creating a Password Codec
Creating an Authorization TagLib
Leveraging the beforeInterceptor
Leveraging Filters
Security Plugins10. PLUGINS AND SERVICES
Understanding Plugins
Installing the Searchable Plugin
Exploring the Searchable Plugin
Understanding Services
Adding a Search Box
Changing the Home Page with UrlMappings
Production Deployment Checklist
Conclusion
ปล. ถ้าใครอยากได้เป็นหนังสือ ก็ต้องลงทุนกันหน่อยนะครับ
มาลอง run Groovy บน Apache WebServer กัน
เมื่อวานนี้ผมไปเจอ project ใน GoogleCode คือ Groovy-LAMP
โดยคำอธิบายของ project นี้ก็ง่ายนิดเดียวคือ
คำถาม
ถ้าคุณรัก Groovy แต่เกลียด Java Web Servers ล่ะจะทำยังไงคำตอบ
เราจะทำให้การเขียน Groovy เหมือนกับการเขียน php เลย กล่าวคือ เมื่อทำการเขียน code และแก้ไข code groovy แล้วก็ทำงานได้เลย และที่สำคัญทำงานผ่าน Apache WebServer กันไปเลย
พออ่านมาได้นิดหน่อย ผมจึงตาลุกวาว เนื่องจากเป็นโลกที่ผมไม่เคยเข้าไปมาก่อน ดังนั้นเรามาเริ่มศึกษาไปพร้อมๆ กัน
โครงสร้างของ Groovy-LAMP project
- ทำการสร้าง java proxy ให้ Apache WebServer, Lighttpd ติดต่อมาโดยผ่าน Simple CGI ( SCGI ) โดย java proxy ที่สร้างขึ้นมานั้นจะรับ file ที่มีนามสกุล .groovy เท่านั้น
- เบื้องหลังการทำงานก็คือ GroovyScriptEngine เป็น unitilies class ของ groovy นั่นเอง
มาถึงตรงนี้แล้ว สิ่งที่ต้องการในการติดตั้ง Groovy-LAMP project มีดังนี้ [ Software เหล่านี้ผมใช้ในการทดสอบ ]
- Apache WebServer 2.x
- Java 1.5
- Simple CGI for Apache WebServer 2.x
- Groovy Library ( groovy-all.jar )
- Groovy-LAMP
ก่อนจะเริ่มผมเจอปัญหาตั้งแต่ต้น คือ Groovy-LAMP ที่ download มานั้นใช้งานบน Windows ไม่ได้เกิด error ตาม issue นี้
ดังนั้นผมจึงตัดสินในทำการ checkout sourcecode มาแก้ไขเองดีกว่า ด้วยคำสั่งนี้
svn checkout http://groovy-lamp.googlecode.com/svn/trunk/ groovy-lamp-read-only
หลังจากที่อ่าน sourcecode แล้วพบว่ามีปัญหาในการ parser request นั่นเอง ผมคิดว่าข้อมูลของ request ที่มาจาก Apache WebServer และ Lighttpd ไม่เหมือนกัน ดังนั้นผมจึงเพิ่มบรรทัดนี้ไปที่ท้าย method parse() ใน file groovyrun.scgi.SCGIParser.java ดังนี้
env.put("SCRIPT_FILENAME", "file:/" + env.get("DOCUMENT_ROOT").toString() + env.get("SCRIPT_NAME").toString() + env.get("PATH_INFO").toString());
แล้วก็ build เป็น jar file หรือทำการ run ได้เลยครับ ลอง run ด้วยคำสั่ง
java -jar GroovyLamp.jar -port=4444 -mode=script
หรือ
java -jar GroovyLamp.jar -port=4444หรือ
java Main -port=4444
ผลการทำงานจะค้างหน้าจอหรือ console ดังนี้
Fri Feb 05 13:45:53 PST 2010 : NOTICE : Starting server
Fri Feb 05 13:45:53 PST 2010 : NOTICE : SCGIApplicationServer running on port: 4444
กลับมาต่อที่การ config ให้ Apache Web Server ติดต่อไปยัง Groovy-LAMP โดยจะต้องไปหา SCGI ตาม link จากข้างต้นมาด้วย ดังนั้นทำการ download และ extract mod_scgi.so file ไปไว้ใน %APACHE_HOME%\modoles และทำการ config ใน file %APACHE_HOME%\conf\httpd.conf ดังนี้
1. ทำการ load scgi module ขึ้นมา
LoadModule scgi_module modules/mod_scgi.so</blockquote>
2. เป็นการบอกให้รู้ว่า file ที่อยู่ใน folder groovy [ ใน www root ] ให้ส่ง request ไปที่ Groovy-LAMP ที่เราสร้างขึ้นมา ผ่าน port 4444 ที่เปิดเอาไว้
SCGIMount /groovy 127.0.0.1:4444
ต่อไปทดลองโดยใช้ file ตัวอย่างจากการ Download Groovy-LAMP มา ซึ่งชื่อว่า test.groovy โดย copy ไปไว้ใน folder /groovy/ แล้วทำการ restart ตัว Apache WebServer และ run http://localhost/groovy/test.groovy ใน browser จะได้ผลลัพธ์ดังนี้
Sourcecode ตัวอย่าง
out.println "<h1>Server</h1>"
out.println "<table border=\"1\">"
request.env.each(){
k,v -> out.println "<tr><td>$k</td><td>$v</td></tr>"
}
out.println "</table>"
out.println "<h1>Get parameters</h1>"
out.println "<table border=\"1\">"
request.get.each(){
k,v -> out.println "<tr><td>$k</td><td>$v</td></tr>"
}
out.println "</table>"
ผลการทำงาน

เพียงเท่านี้ก็สามารถเริ่มใช้งาน Groovy-LAMP ได้แล้วครับ ซึ่งเป็นแนวคิดที่น่าสนใจเหมือนกันครับ ใครสนใจก็สามารถนำมาพัฒนาต่อยอดได้ครับ
Tags: groovy, groovy-lamp
เกรลส์รุ่น 1.2.1 ออกแล้วเมื่อวานนี้ครับ เป็นรุ่นแก้บั๊กและปรับปรุง
อ่านรีลีสโน้ตได้จากที่นี่ และดาวน์โหลดได้จาก grails.org/Download เช่นเคยครับ
ที่มา Blog เกรม รอชเชอร์
Tags: grails
เราคูณ String ได้ อาจจะเป็นเรื่องที่หลายท่านยังไม่ได้ลองทำดูในภาษา Groovy นะครับ แต่ก็อาจจะมีประโยชน์โดยเฉพาะเวลาที่ต้องการประมวลผลหรือแสดงผลข้อความ
พิมพ์
println ‘-’.multiply 80
จะทำให้เกิดสายอัขระเท่ากับจำนวน เป็นต้นครับ
หรือสามารถใช้ operator * ได้ในทำนองเดียวกับ (เพราะ operator * จะแปลงเป็น multiply method ในระดับ bytecode ครับ)
println (‘-’ * 80)
จาก Groovy Goodness
Tags: groovy, groovy goodness
สวัสดีปีใหม่ทุกๆ ท่านนะครับ
ก่อนที่ผมจะไม่อยู่หลายวันเนื่องจากมีภารกิจสำคัญ ทำให้ห่างหายจากโลก internet ไปสัก 1-2 สัปดาห์ เลยขอฝาก Grails plugin ที่น่าสนใจสักตัวให้ลองใช้งานนะครับ
นั่นก็คือเครื่องมือสำหรับ Monitoring การใช้งาน resources ต่างๆ ของ grails application ที่ทำงานอยู่ โดยตัวที่แนะนำคือ Melody Grails Plugin
การใช้งานก็ง่ายๆ ครับ คือ ใช้คำสั่ง
>grails install-plugin grails-melody
สามารถใช้งานผ่าน url ดังนี้
http://localhost:8080/
/monitoring
ผลการทำงานจะแบ่งเป็นส่วนต่างๆ ดังนี้
1. ผลการทำงานโดยรวม ซึ่งแสดงเป็นรูปกราฟ

และยังมีส่วนอื่นๆ อีกเช่น System Error Log, System Information, จำนวน Thread การทำงาน, จำนวนข้อมูลที่ Cache ไว้ สามารถ export report ออกเป็น pdf file ได้ รวมทั้งยังสามารถดู report ตาม วัน เดือน และ ปี ได้อีกด้วย
ดังนั้น Melody Plugin ตัวนี้จึงเป็นที่น่าสนใจมากครับ แต่ถ้าเป็น JavaEE Application ก็สามารถใช้ได้เช่นกันนะครับ ซึ่งดูเพิ่มเติ่มได้ที่นี่
Tags: melody, monitoring
Groovy Community, นอกจากจะมี Grails ซึ่งเป็น web application platform ที่ทำมาให้เราใช้ในการพัฒนาเว็ปอย่าง รวดเร็วส์ แล้ว ได้มีกลุ่มคนอีกกลุ่มหนึ่ง นำโดย
คุณ Andres Almiray ที่ได้เห็นความซับซ้อนในอดึต สำหรับการพัฒนา Java GUI ด้วย Swing จึงได้คิดปรับปรุง วิธีคิด ในการพัฒนา Java Desktop Application ในรูปแบบใหม่ โดยใช้ความสามารถของตัวภาษา Groovy อย่างเต็มที่ และมีรูปแบบการพัฒนาคล้ายๆ กับ การพัฒนา web application บน Grails แต่ก็ยังคงยึดแบบแผนที่ดีที่มีการพัฒนามายาวนาน และต่อเนื่องของ Swing Application Framework(JSR 296) ซึ่งก็คือ “Griffon Framework” นั่นเอง
เนื่องจาก Griffon ใช้ตัวภาษา Groovy เป็นพื้นฐานในการพัฒนาดังนั้น แนวคิดหลักๆที่เราคุ้นเคยกันใน grails ก็จะมีมากับ Griffon เหมือนกัน เช่น
Convention over Configuration, MVC architecture, Command line interface และ มี plug-in system ให้ได้ใช้กันอย่างสนุกสนาน เหมือน grails อีกด้วย
เริ่มติดตั้ง Griffon บน Window
1. ติดตั้ง jdk 1.5 หรือ jdk 1.6
2. เนื่อง จาก Griffon 0.2 ยังมีปัญหาอยู่บ้าง จึงขอแนะนำให้ไปใช้ version: Griffon 0.3 SNAPSHOT เลย สามารถ download ได้ที่ griffon-bin-0.3-SNAPSHOT.zip
3. เพิ่มเติมค่าใน System variable setting บน Window
JAVA_HOME ชี้ไปที่ เราติดตั้ง jdk เอาไว้ ( D:\Java\jdk1.6.0_17)
GRIFFON_HOME ชี้ไปที่เราติดตั้ง griffon ไว้ ( D:\Java\griffon-0.3-SNAPSHOT )
และ เพิ่ม PATH variable : %JAVA_HOME%\bin;%GRIFFON_HOME%\bin
ทำการทดสอบ Griffon
1. เปิด command line ขึ้นมา แล้วพิมพ์ดังนี้
C:\Users\Prakart>griffon help
ก็จะเห็นคำสั่งต่าง ๆ ที่เราจะใช้ในการพัฒนา application ด้วย Griffon

ลอง สร้าง application และ เทส
1. C:\Users\Prakart> griffon create-app TestGriffon
2. cd TestGriffon
ภายใน จะมี folder ที่ถูกเตรียมไว้ให้แบบนี้

3. ทำการ run โดยใช้คำสั่ง griffon run-app
C:\Users\Prakart\TestGriffon\griffon run-app
จะปรากฏ หน้าต่าง window ของ griffon เล็กๆ ขึ้นมา

นอกจากนี้ griffon ยังได้เตรียม deployment options ต่างๆ ไว้ให้ด้วย(ดูได้จาก griffon help)
- griffon run-applet และ
- griffon run-webstart
ซึ่งเราสามารถลองเรียกดูได้เลย
C:\Users\Prakart\TestGriffon\griffon run-applet และ
C:\Users\Prakart\TestGriffon\griffon run-webstart
Griffon เป็นอีก community หนึ่งของ Groovy ที่น่าสนใจนะครับ
ขอให้สนุกสนาน กับภาษา Groovy และ ขอให้ทุกคนมีความสุขในปี 2010 และตลอดไป นะครับ
References:
1. Griffon’s website > http://griffon.codehaus.org/
2. Griffon in Action > http://www.manning.com/almiray/Almiray_MEAPch01_free.pdf
Tags: griffon
