วันพุธที่ 6 มกราคม พ.ศ. 2559

Bootstrap Validator

Bootstrap Validator


จากเดิมที่ค้างทุกคนไว้ หลังเข้าชาน(ชรา) ก็นึกออก เอาความสามรถของ Class CSS ปรับเอามาใช้งานซ่ะ

หลักการคือ
1.    Plugin Validate ส่วนใหญ่จะตอบสนองไว้กับการปุ่ม submit (input หรือ button type ที่เป็น submit) ดังนั้น
2.    ปุ่มของเราก็ให้มันเป็น button นี่แร๊ะ จะได้ไม่ต้องไปไหน
3.    ภายในคำสั่งใน event click สั่งให้ plugin ทำงาน โดยการตรวจสอบ Form ค้นหา input ทั้งหมด ที่เราได้ตั้งค่า ให้ตรวจสอบ (แอตทริบิ้วที่มีการระบุเป็น required)
4.    หากตรวจสอบแล้วเงื่อนไขหากมี validate เกิดขึ้น div ที่คลุม text ฟิล์ด input ในฟอร์มนั้นจะ troggle class “has-error” แสดงเป็นสีแดงเกิดขึ้น
5.    ทำการตรวจสอบ div ภายใน Form ทั้งหมดหากไม่มี CSS class  “has-error” ก็สั่ง Form submit()


สวย ...หรู มีระดับ

Controller
def save(){
    if(yourInstance.save(flush: true)){
       redirect(action : ‘nextmethod’)
    }
}
def nextmethod(){
   render ...
}
View
<g:formRemote url="[controller: 'yourController', action: 'save']" data-toggle="validator"
              role="form" name="yourForm" id="idYourForm"
              method="post" update="stateDivUpdate">
<div class="row">
   <div class="form-group">
     <div class="col-lg-3">
       <label for="idTxt" class="control-label">Label</label>
     </div>
     <div class="col-lg-9">
       <input type="text" class="form-control" id="idTxt" name="txtName" maxlength="50"
value="" required/>
     </div>
   </div>
</div>
<button type="button" id="idButton" class="btn btn-primary">Click</button>
</g:formRemote>





JavaScript
<script>
$(function () {
  $("#idButton").on("click",function(){
    $("#idYourForm").validator('validate');
      if(!$("#idYourForm div").hasClass("has-error")){
          $("#idYourForm").submit();
     }
  });
});
</script>

การแปลงค่าเงินบาทจากตัวเลข เป็นตัวหนังสือไทยใน Java

การแปลงค่าเงินบาทจากตัวเลข เป็นตัวหนังสือไทยใน Java

Quartz plugin allows your Grails application to schedule jobs

Quartz plugin allows your Grails application to schedule jobs to be executed using a specified interval or cron expression. The underlying system uses the Quartz Enterprise Job Scheduler configured via Spring, but is made simpler by the coding by convention paradigm.

Installation

Just execute following from your application directory:
grails install-plugin quartz
If above method is not work for some reason you can download plugin distribution from http://svn.codehaus.org/grails-plugins/grails-quartz/tags/RELEASE_0_3_3/grails-quartz-0.3.3.zip and install it with:
grails install-plugin /path/to/downloaded/plugin/distrib


Using

Scheduling Jobs

To create a new job run the "grails create-job" command and enter the name of the job. Grails will create a new job and place it in the "grails-app/jobs" directory:
class MyJob {
  def startDelay = 60000
  def timeout = 1000
def group = "MyGroup"
def execute(){ print "Job run!" } }
The above example will wait for 1 minute and after that will call the 'execute' method every second. The 'timeout' and 'startDelay' properties are specified in milliseconds and must have Integer or Long type. If these properties are not specified default values are applied (1 minute for 'timeout' property and 30 seconds for 'startDelay' property). It isn't recommended to set 'startDelay' property value lesser than 30 seconds since your application needs to fully startup before executing jobs. Jobs can optionally be placed in different groups.

Scheduling a Cron Job

Jobs can be scheduled using a cron expression. For those unfamiliar with "cron", this means being able to create a firing schedule such as: "At 8:00am every Monday through Friday" or "At 1:30am every last Friday of the month". (See the API docs for the CronTrigger class in Quartz for more info on cron expressions):
class MyJob  {
 def cronExpression = "0 0 6 * * ?"
def group = "MyGroup"
def execute(){ print "Job run!" } }

Multiple Triggers for a Job

Since 0.3 version you can also configure triggers in other way. With this method you can configure multiple triggers per job as well as register your custom trigger. To do this simply add static triggers closure to your job:
class MyJob {
    static triggers = {
        simpleTrigger startDelay:10000, timeout: 30000, repeatCount: 10
        cronTrigger startDelay:10000, cronExpression: '0/6 * 15 * * ?'
        customTrigger triggerClass:MyTriggerClass, myParam:myValue, myAnotherParam:myAnotherValue
    }
def execute() { println "Job run!" } }
With this configuration job will be executed 11 times with 30 seconds interval with first run in 10 seconds after scheduler startup (simple trigger), also it'll be executed each 6 second during 15th hour (15:00:00, 15:00:06, 15:00:12, … — this configured by cron trigger) and also it'll be executed each time your custom trigger will fire.
Three kinds of triggers are supported with the following parameters:
  • simpleTrigger :
    • startDelay — delay (in milliseconds) between scheduler startup and first job's execution
    • timeout — timeout (in milliseconds) between consecutive job's executions
    • repeatCount — trigger will fire job execution (1 + repeatCount) times and stop after that (specify 0 here to have one-shot job)
  • cronTrigger :
    • startDelay — delay (in milliseconds) between scheduler startup and first job's execution
    • cronExpression — cron expression
  • customTrigger :
    • triggerClass — your class which implements Trigger interface
    • any params needed by your trigger

Dependency Injection and Jobs

Jobs are configured for auto-wiring by name thus properties can be injected into them. To get hold of the data source you can do:
def dataSource
Or a service class:
def myService
The Grails application context is configured using convention so most types can be injected simply by using their property name representation (ie the first letter being lower case in the majority of cases).

Using JobExecutionContext

Since 0.3.2 you could define execute method in your job as def execute(context){} in this case it will be provided with Quartz's JobExecutionContext. You could obtain some information from this context (name of the trigger which fired the execution, previous execution time, next execution time and so on) as well as provide some parameters to job through context.getMergedJobDataMap() . If your job is stateful (see section about concurrent execution below), job data in JobExecutionContext will also persist per job executions.

Configuring the plugin

Since 0.3 version plugin supports configuration file which is stored in grails-app/conf/QuartzConfig.groovy . The syntax is the same as default Grails configuration file Config.groovy . You could also use per-environment configuration feature (more info).
To install initial config type in the command line: 'grails install-quartz-config' .
Currently supported options:
  • autoStart — controls automatic startup of the Quartz scheduler during application bootstrap (default: true )
  • jdbcStore — set to true if you want Quartz to persist jobs in your DB (default: false ), you'll also need to provide quartz.properties file and make sure that required tables exist in your db (see Clustering section below for the sample config and automatic tables creation using Hibernate)

Hibernate Sessions and Jobs

Jobs are configured by default to have Hibernate Session bounded to thread each time job is executed. This is required if you are using Hibernate code which requires open session (such as lazy loading of collections) or working with domain objects with unique persistent constraint (it uses Hibernate Session behind the scene). If you want to override this behavior (rarely useful) you can use 'sessionRequired' property:
def sessionRequired = false

Configuring concurrent execution

By default Jobs are executed in concurrent fashion, so new Job execution can start even if previous execution of the same Job is still running. If you want to override this behavior you can use 'concurrent' property, in this case Quartz's StatefulJob will be used (you can find more info about it here):
def concurrent = false

Clustering

Quartz plugin doesn't support clustering out-of-the-box now. However, you could use standard Quartz clustering configuration. Take a look at the example provided by Burt Beckwith: here. You'll also need to set jdbcStore configuration option to true .
There are also two parameters for configuring store/clustering on jobs ( volatility and durability , both are true by default) and one for triggers ( volatility , also true by default). Volatile job and trigger will not persist between Quartz runs, and durable job will live even when there is no triggers referring to it.
Read Quartz documentation for more information on clustering and job stores as well as volatility and durability.

Compatibility with Grails versions

Quartz Plugin 0.3.x — tested to work with Grails 1.0.3 Quartz Plugin 0.2 — works with Grails 0.6+
In earlier version of Grails (before 0.5.5) job sheduling with Quartz was a part of Grails core, but as of 0.5.5 it was moved out of core to this plugin. Use this plugin with Grails 0.6 or higher.

Roadmap

0.4 version (September 2008)

  • dynamic (programmatic) jobs scheduling
  • support configurable trigger listeners and job listeners
  • support Quartz clustering and store configuration out-of-the-box
  • more configuration options for QuartzConfig

Known issues

You can find a list of known issues in the JIRA

วันอาทิตย์ที่ 3 มกราคม พ.ศ. 2559

2559 New year

2559 New year

เปิดพุทธศักราชใหม่กับโต๊ะทำงานใหม่ชีวิตกรรมกรห้องแอร์นักพัฒนานวัตกรรมสิ่งใหม่ความรู้ไม่ควรหยุดนิ่งเพราะจะตามอะไรไม่ทันเลย บนโลกใบนี้มีอะไรให้เราต้องเรียนรู้อีกเยอะ ถ้าพยายามทำให้ได้และประคับประคองการดำรงชีวิตครอบครัวให้มีความสุขก็ถือว่าเราประสบความสำเร็จแล้ว

วันพุธที่ 16 กรกฎาคม พ.ศ. 2557

Grails  คือ Rapid Web Framework ที่ใช้ภาษา Groovy ในการพัฒนา  ได้แรงบันดาลใจจาก Ruby on Rails  นั่นคือใช้แนวคิด MVC โดยแต่ละส่วนมีรายละเอียดดังนี้

M = Model สำหรับ Grails จะเรียกว่า Domain Class ซึ่ง Grails ได้เตรียม Persistence Framework ชื่อ GORM ซึ่งใช้ ActiveRecord Pattern โดยมีลักษณะเป็น Rich Model คือส่วน data และส่วน operation อยู่ใน class เดียวกัน  Business Logic และ Business Rule ต่างๆ จะอยู่ใน Domain Class เลย    โดยมีความสามารถต่างๆ มากมาย  เช่น การค้นหาข้อมูลซึ่งมีการใช้ dynamic method  ทำให้ค้นข้อมูลได้ง่ายและยืดหยุ่น   ซึ่งเราระบุชื่อฟิลด์ที่เป็นเงื่อนไขการค้นลงใจชื่อ method ได้เลย  เช่น
def user = User.findByUsernameAndPassword('user1', 'password')


V = View คือส่วนแสดงผลลัพธ์และส่วนรับข้อมูลเพื่อนำไปใช้หรือประมวลผล  code จะเป็น HTML ผสมกับ tag library ของ Grails เอง  จะมีเฉพาะ code กำหนดรูปแบบการแสดงผล  หรือ logic เฉพาะส่วนแสดงผลเท่านั้น  ตัวอย่างดังนี้
§  ${topic.title}


C = Controller เป็นที่ซึ่งมี logic สำหรับควบคุม flow ของเพจ  ในแต่ละ Controller จะเก็บ Action ต่างๆ  ซึ่งเรา define ไว้ด้วย Closure  ในการเรียกแต่ละเพจจะเป็นการระบุ Action ที่ต้องการกระทำ  ไม่ว่าจะเป็นเพื่อแสดงผล  หรือประมวลผลใดๆ ก็ตาม  มีรูปแบบดังนี้
โดยที่ controller คือชื่อ class Controller และ action คือชื่อ Closure
ในระบบเล็กๆ  เราสามารถใส่ code ส่วนเรียกใช้ Domain Class ใน Action ได้เลย  แต่หากเป็นระบบใหญ่ควรจะใส่ไว้ที่ Service จะดีกว่า
Data Binding มีการ bind ค่าจาก html form มายัง Model ด้วยการส่งตัวแปร params เข้าไปใน constructor ของ Domain Class ดังนี้
def user = new User(params)

Domain Relationship  Domain Class มีความสัมพันธ์ได้ดังนี้
§  one to one
§  one to many
§  many to many
โดยที่ความสัมพันธ์ต่างๆ จะถูกกำหนดไว้ที่ Domain Class เลย
สำหรับส่วนสุดท้ายผมไม่รู้จะจัดหัวข้ออย่างไรดีจึงจะเหมาะ  เพราะมันเป็นแค่ประโยคสั้นๆ เป็นเหมือน tip&trick หรือข้อควรจำเท่านั้น   ผมจึงของ list ไว้ตรงนี้เลยแล้วกันครับ  (อาจะมีเพิ่มได้ภายหลัง)
1.    Grails เริ่มต้นด้วย grails command
2.    เริ่มต้นใช้ grails ต้องต่อ Internet
3.    เมื่อสั่ง grails run-app สามารถเรียก app ผ่าน IE ได้เลยเพราะ grails จะทำการ compile และ start jetty ให้แล้ว
4.    ขณะ run-app อยู่นั้น  หากเราแก้ไข code ส่วนใดก็ตามและทำการบันทึกเท่านั้น grails จะ re-compile code นั้นให้เราเลย
5.    topic?.id หมายความว่าหาก object topic ไม่เป็น null ให้อ้างค่า id
6.    flash.message ได้แนวคิดจาก Rails เป็น session variable ที่มี life time = current request to next request
7.    ฟิลด์ที่สร้างใน Domain Class จะถูกนำไปสร้างเป็น table field ในฐานข้อมูล   นอกจากฟิลด์ที่มีชื่ออยู่ใน list ของ static transients
8.    ตารางที่ได้จาก Domain Class จะมีฟิลด์ ID ซึ่งเป็น identity และ version ไว้สำหรับทำ Optimistic Locking
9.    ในหน้าจอ add หรือ edit ของ scaffold นั้น ฟิลด์ต่างๆ จะแสดงและเรียงตามที่เราระบุในส่วน constrain ของ Domain Class
10. เราสามารถเก็บฟิลด์ CreatedDate ใน Domain Class ได้  และ assign ค่าให้เลยก็ได้หากไม่ต้องการให้ผู้ใช้กรอก
11. default ของ GORM ใช้ Optimistic Locking



ทดสอบ Hello World with Grails Application

ต่อมาลองสร้าง Grails Application อย่างง่ายกันครับ ดังนี้
1. สร้าง application ด้วยคำสั่ง   
Quote
grails create-app HelloWorld

2. Grails จะสร้าง folder HelloWorld ซึ่งมีโครงสร้างของ folder ดังนี้
Attached Image 

จากรูปอธิบายคร่าวๆ ได้ดังนี้
conf => จะเก็บ configuration ต่างๆ เช่น DataSource, BootStrap, UrlMapping 

----- ส่วนของ MVC framework [ Model-View-Controller ] ------
controllers => เก็บ Controller
domain => Domain หรือ Model ซึ่งเทียบได้กับ Java Bean, Value Object ประมาณนั้น
view => ใน Java คือ JSP  แต่ใน Grails จะชื่อว่า GSP 
โดยใน view จะมี 
ชื่อ folder แยกตาม domain 
ชื่อ file จะแยกตาม method ใน controller

lib => สำหรับ library ต่างๆ ที่ต้องการใช้เช่น JDBC  Driver เป็นต้น

test จะประกอบไปด้วย unit testing และ integration testing ซึ่งจะอธิบายการใช้งานในหัว Part ถัดๆ ไป

3. Start Grails Application โดยจะต้องเข้าไปที่ folder HelloWorld ก่อน แล้วพิมพ์คำสั่งดังนี้
Quote
grails run-app


ให้ทำการ run  URL นี้ใน browser :::  
http://localhost:8080/HelloWorld จะได้ผลดังนี้
Attached Image 



วันนี้มาลองสร้างระบบ User Profile แบบง่ายๆ กันครับ

ผมวางระบบคร่าวๆ ไว้ดังนี้
- Domain class ชื่อ User จะประกอบไปด้วย  username, password, firstname, lastname
- Controller ชื่อ User

มาเริ่มกันเลย
1. สร้าง domain class ด้วยคำสั่ง
Quote
grails create-domain-class User

จากคำสั่งนี้จะสร้าง file User.groovy ใน folder domain ตาม structure ของ Grails Application
ต่อมาให้เปิด file ขึ้นมาแล้ว edit ตามนี้
class User {
        String username;
        String password;
        String firstname;
        String lastname
}

2. สร้าง controller ด้วยคำสั่ง
Quote
grails create-controller User

จากคำสั่งนี้จะสร้าง file UserController.groovy ใน folder controllers ตาม structure ของ Grails Application
ต่อมาให้เปิด file ขึ้นมาแล้ว edit ตามนี้
class UserController {
        def scaffold = User;
        //def index = { }
}



โดย 
Scaffold นั้นเป็นการกำหนดให้ grails ทำการสร้าง views และ Action/Method ต่างๆ ใน controller ตาม create/read/update/delete (CRUD) operation แบบอัตโนมัติ  

3. start grails application ด้วยคำสั่ง grails run-app เหมือนเดิม จะได้ผลดังนี้
Attached Image 

Attached Image 

Attached Image 

จะสังเกตุได้ว่า ผมไม่ต้องไปยุ่งเกี่ยวกับการกำหนด Database เลย เนื่องจาก default database ของ Grails นั้นคือ HSQLDB แต่ถ้าต้องใช้ database ตัวอื่น จะต้องกำหนดใน /conf/DataSource.groovy และนำ JDBC driverมาใส่ไว้ใน /lib/

วันอังคารที่ 11 ธันวาคม พ.ศ. 2555

Web emulator for iPad, iPhone

ตามลิ้งค์เลยครับ
      http://ipadpeek.com

ขั้นตอน install tomcat7 on ubuntu

 Download file ที่ Download 

     1. แตกไฟล์


    1. tar xvzf apache-tomcat-7.0.29.tar.gz   
  1. ย้ายไฟล์ที่อยู่ข้างในโฟล์เดอ ไปไว้ใน /usr/share/tomcat7
    1. sudo mv apache-tomcat-7.0.29/ /usr/share/tomcat7  
  2. เปิด /usr/share/tomcat7/bin/catalina.sh โดยใช้ command ดังนี้
    1. gedit /usr/share/tomcat7/bin/catalina.sh   
  3. เพิ่มโค้ดเข้าไปหลังบรรทัดแรกดังนี้
    1. JAVA_HOME="/usr/lib/jvm/jdk-6u32"  
    2. JRE_HOME="/usr/lib/jvm/jdk-6u32/jre"  
  4. เปิดไฟล์ /usr/share/tomcat7/conf/tomcat-users.xml แล้วลบคอมเมนต์ยูเซอร์ออก และเพิ่มยูเซอร์ใหม่และสิทธิ์ manager-gui ดังนี้.
    1. <role rolename="manager-gui"/>  
      1. <user username="admin" password="tomcat" roles="manager-gui"/>  
  5. เริ่มใช้งาน Tomcat server พิมพ์คำสั่งใน command ดังนี้
    1. sudo /usr/share/tomcat7/bin/catalina.sh run  
  6. เข้าไปใช้ URL.
    1. http://127.0.0.1:8080/