Tuesday, 30 October 2012

Getting started with Rails 3.2.8



PART A Getting Started with ruby
[STEP 1] install ruby with RVM("Ruby Version Manager")
prayag@prayag:~$ curl -L get.rvm.io | bash -s stable
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   185  100   185    0     0     11      0  0:00:16  0:00:16 --:--:--   298
100 10237  100 10237    0     0    428      0  0:00:23  0:00:23 --:--:--  4784
Downloading RVM from wayneeseguin branch stable
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   125  100   125    0     0     53      0  0:00:02  0:00:02 --:--:--    65
100 1123k  100 1123k    0     0  33460      0  0:00:34  0:00:34 --:--:-- 56459

Installing RVM to /home/prayag/.rvm/

    Adding rvm PATH line to /home/prayag/.bashrc /home/prayag/.zshenv.
    Adding rvm loading line to /home/prayag/.bash_profile /home/prayag/.zprofile.

# RVM:  Shell scripts enabling management of multiple ruby environments.

# RTFM: https://rvm.io/
# HELP: http://webchat.freenode.net/?channels=rvm (#rvm on irc.freenode.net)
# Cheatsheet: http://cheat.errtheblog.com/s/rvm/
# Screencast: http://screencasts.org/episodes/how-to-use-rvm

# In case of any issues read output of 'rvm requirements' and/or 'rvm notes'


Installation of RVM in /home/prayag/.rvm/ is almost complete:


  * To start using RVM you need to run `source /home/prayag/.rvm/scripts/rvm`

    in all your open shell windows, in rare cases you need to reopen all shell windows.

# iPrayag,

#
#   Thank you for using RVM!
#   I sincerely hope that RVM helps to make your life easier and more enjoyable!!!
#
# ~Wayne



[STEP 2] ensure that changes to the environment made in .profile affect the current shell

prayag@Prayag:~$ ls -l .rvm/scripts/
total 372
-rwxrwxr-x 1 prayag prayag  5671 Jan 25 11:20 alias
-rw-rw-r-- 1 prayag prayag    97 Jan 25 11:20 aliases
-rwxrwxr-x 1 prayag prayag  2602 Jan 25 11:20 base
-rwxrwxr-x 1 prayag prayag  3389 Jan 25 11:20 cd
-rwxrwxr-x 1 prayag prayag  2289 Jan 25 11:20 cleanup
-rwxrwxr-x 1 prayag prayag 33683 Jan 25 11:20 cli
-rwxrwxr-x 1 prayag prayag  1371 Jan 25 11:20 color
-rwxrwxr-x 1 prayag prayag  2785 Jan 25 11:20 completion
-rwxrwxr-x 1 prayag prayag  1407 Jan 25 11:20 cron
-rwxrwxr-x 1 prayag prayag  1628 Jan 25 11:20 db
-rwxrwxr-x 1 prayag prayag  1256 Jan 25 11:20 default
-rwxrwxr-x 1 prayag prayag  2140 Jan 25 11:20 disk-usage
-rwxrwxr-x 1 prayag prayag  3720 Jan 25 11:20 docs
-rwxrwxr-x 1 prayag prayag   508 Jan 25 11:20 env
-rwxrwxr-x 1 prayag prayag 14978 Jan 25 11:20 external
drwxrwxr-x 2 prayag prayag  4096 Jan 25 11:20 extras
-rwxrwxr-x 1 prayag prayag  3680 Jan 25 11:20 fetch
drwxrwxr-x 5 prayag prayag  4096 Jan 25 11:20 functions
-rwxrwxr-x 1 prayag prayag 28401 Jan 25 11:20 gemsets
-rwxrwxr-x 1 prayag prayag   473 Jan 25 11:20 get
-rwxrwxr-x 1 prayag prayag  1194 Jan 25 11:20 group
-rwxrwxr-x 1 prayag prayag  1120 Jan 25 11:20 hash
-rwxrwxr-x 1 prayag prayag  1468 Jan 25 11:20 help
-rwxrwxr-x 1 prayag prayag   978 Jan 25 11:20 hook
-rwxrwxr-x 1 prayag prayag  5688 Jan 25 11:20 info
-rwxrwxr-x 1 prayag prayag  6236 Jan 25 11:20 initialize
-rwxrwxr-x 1 prayag prayag  2675 Jan 25 11:20 install
-rw-rw-r-- 1 prayag prayag   406 Jan 25 11:20 irbrc
-rw-rw-r-- 1 prayag prayag  2540 Jan 25 11:20 irbrc.rb
-rwxrwxr-x 1 prayag prayag  8967 Jan 25 11:20 list
-rwxrwxr-x 1 prayag prayag  7356 Jan 25 11:20 maglev
-rwxrwxr-x 1 prayag prayag   660 Jan 25 11:20 manage
-rwxrwxr-x 1 prayag prayag  5467 Jan 25 11:20 migrate
-rwxrwxr-x 1 prayag prayag  2864 Jan 25 11:20 monitor
-rwxrwxr-x 1 prayag prayag  6422 Jan 25 11:20 notes
-rwxrwxr-x 1 prayag prayag   565 Jan 25 11:20 override_gem
-rwxrwxr-x 1 prayag prayag  1472 Jan 25 11:20 patches
-rwxrwxr-x 1 prayag prayag   851 Jan 25 11:20 patchsets
-rwxrwxr-x 1 prayag prayag  1504 Jan 25 11:20 pkg
-rwxrwxr-x 1 prayag prayag  4055 Jan 25 11:20 repair
-rwxrwxr-x 1 prayag prayag  1325 Jan 25 11:20 requirements
-rwxrwxr-x 1 prayag prayag   297 Jan 25 11:20 rtfm
-rwxrwxr-x 1 prayag prayag  6261 Jan 25 11:20 rubygems
-rwxrwxr-x 1 prayag prayag  4912 Jan 25 11:20 rvm
-rwxrwxr-x 1 prayag prayag 33836 Jan 25 11:20 selector
-rwxrwxr-x 1 prayag prayag  4482 Jan 25 11:20 selector_gemsets
-rwxrwxr-x 1 prayag prayag  5647 Jan 25 11:20 set
-rwxrwxr-x 1 prayag prayag  7570 Jan 25 11:20 snapshot
-rwxrwxr-x 1 prayag prayag  5736 Jan 25 11:20 tools
-rwxrwxr-x 1 prayag prayag  3772 Jan 25 11:20 upgrade
-rwxrwxr-x 1 prayag prayag   550 Jan 25 11:20 version
-rwxrwxr-x 1 prayag prayag  4602 Jan 25 11:20 wrapper
drwxrwxr-x 3 prayag prayag  4096 Jan 25 11:20 zsh


prayag@prayag:~$ source /home/prayag/.rvm/scripts/rvm

Check rvm version
prayag@prayag:~$ rvm --version

rvm 1.16.17 (stable) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]




[STEP 3] install ruby 1.9.3 from available list 
prayag@prayag:~$ rvm list known
# MRI Rubies                                                     
[ruby-]1.8.6[-p420]                                              
[ruby-]1.8.7-p370                                                
[ruby-]1.8.7[-p371]                                              
[ruby-]1.9.1[-p431]                                              
[ruby-]1.9.2-p180
[ruby-]1.9.2-p290
[ruby-]1.9.2-p318
[ruby-]1.9.2[-p320]
[ruby-]1.9.2-head
[ruby-]1.9.3-preview1
[ruby-]1.9.3-rc1
[ruby-]1.9.3-p0
[ruby-]1.9.3-p125
[ruby-]1.9.3-p194
[ruby-]1.9.3-[p286]
[ruby-]1.9.3-head
ruby-head

prayag@prayag:~$ source ~/.bash_profile

prayag@prayag:~$ rvm install 1.9.3
ruby-1.9.3-p286 - #configure
ruby-1.9.3-p286 - #download
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 5061k  100 5061k    0     0  30425      0  0:02:50  0:02:50 --:--:-- 18902
ruby-1.9.3-p286 - #extract
ruby-1.9.3-p286 - #validate
ruby-1.9.3-p286 - #setup
'ruby' was not found, cannot install rubygems unless ruby is present (Do you have an RVM ruby installed & selected?)
Saving wrappers to '/home/prayag/.rvm/bin'.
ruby-1.9.3-p286 - #importing default gemsets (/home/prayag/.rvm/gemsets/)


[STEP 4] Use the newly installed ruby
prayag@prayag:~$ rvm use 1.9.3
Using /home/prayag/.rvm/gems/ruby-1.9.3-p286

check this worked correctly
prayag@prayag:~$ ruby -v
ruby 1.9.3p286 (2012-10-12 revision 37165) [i686-linux]

prayag@prayag:~$ which ruby;
/home/prayag/.rvm/rubies/ruby-1.9.3-p286/bin/ruby

prayag@prayag:~$ rvm use 1.9.3 --default

Using /home/prayag/.rvm/gems/ruby-1.9.3-p286


[STEP 5]
$ sudo apt-get install libsqlite3-dev

To update ruby version later,
$ rvm get stable
$ rvm upgrade 2.0.0
$ rvm use 2.0.0

REFERENCES
http://junaidpven.wordpress.com/2011/10/25/how-to-install-ruby-and-rails-with-rvm-in-ubuntu-11-10/


PART B Getting Started with Rails

[STEP 1] install rails using the gem install command provided by RubyGems (may take some time)
Rubygems is a package manager for ruby.

prayag@prayag:~$ gem install rails
Fetching: i18n-0.6.1.gem (100%)
Fetching: multi_json-1.3.6.gem (100%)
Fetching: activesupport-3.2.8.gem (100%)
Fetching: builder-3.0.4.gem (100%)
Fetching: activemodel-3.2.8.gem (100%)
...........
....................
...........................



[STEP 2] verify, everything is installed correctly
prayag@prayag:~$ rails --version
Rails 3.2.8

Check for issue Installed Rails but the rails command says it's not installed

[STEP 3] create an app
prayag@prayag:~$ sudo mkdir workspace_rails
prayag@prayag:~$ sudo chmod 777 /home/prayag/workspace_rails/
prayag@prayag:~/workspace_rails$ rails new Gwitter

[STEP 4] structure of Gwitter rails app
prayag@prayag:~/workspace_rails$ cd Gwitter/
prayag@prayag:~/workspace_rails/Gwitter$ ls -l
total 68
drwxrwxr-x 8 prayag prayag 4096 Oct 30 12:21 app
drwxrwxr-x 5 prayag prayag 4096 Oct 30 12:21 config
-rw-rw-r-- 1 prayag prayag  157 Oct 30 12:21 config.ru
drwxrwxr-x 2 prayag prayag 4096 Oct 30 12:21 db
drwxrwxr-x 2 prayag prayag 4096 Oct 30 12:21 doc
-rw-rw-r-- 1 prayag prayag  765 Oct 30 12:21 Gemfile
drwxrwxr-x 4 prayag prayag 4096 Oct 30 12:21 lib
drwxrwxr-x 2 prayag prayag 4096 Oct 30 12:21 log
drwxrwxr-x 2 prayag prayag 4096 Oct 30 12:21 public
-rw-rw-r-- 1 prayag prayag  272 Oct 30 12:21 Rakefile
-rw-rw-r-- 1 prayag prayag 9208 Oct 30 12:21 README.rdoc
drwxr-xr-x 2 prayag prayag 4096 Oct 30 12:21 script
drwxrwxr-x 7 prayag prayag 4096 Oct 30 12:21 test
drwxrwxr-x 3 prayag prayag 4096 Oct 30 12:21 tmp
drwxrwxr-x 4 prayag prayag 4096 Oct 30 12:21 vendor


The structure of app
prayag@prayag:~/workspace_rails/Gwitter/app$ ls -l
total 24
drwxrwxr-x 5 prayag prayag 4096 Oct 30 12:21 assets
drwxrwxr-x 2 prayag prayag 4096 Oct 30 12:21 controllers
drwxrwxr-x 2 prayag prayag 4096 Oct 30 12:21 helpers
drwxrwxr-x 2 prayag prayag 4096 Oct 30 12:21 mailers
drwxrwxr-x 2 prayag prayag 4096 Oct 30 12:21 models
drwxrwxr-x 3 prayag prayag 4096 Oct 30 12:21 views

[STEP 5] make some changes in Gwitter/public/index.html
I made some changes in header div.
<div id="header">
          <h1>Gwitter</h1>
          <h2>Watch your favourite games live!</h2>
  </div>


[STEP 6] Configure database
prayag@prayag:~/workspace_rails/Gwitter$ ls -l config
total 32
-rw-rw-r-- 1 prayag prayag 2812 Oct 30 12:21 application.rb
-rw-rw-r-- 1 prayag prayag  191 Oct 30 12:21 boot.rb
-rw-rw-r-- 1 prayag prayag  576 Oct 30 12:21 database.yml
-rw-rw-r-- 1 prayag prayag  151 Oct 30 12:21 environment.rb
drwxrwxr-x 2 prayag prayag 4096 Oct 30 12:21 environments
drwxrwxr-x 2 prayag prayag 4096 Oct 30 12:21 initializers
drwxrwxr-x 2 prayag prayag 4096 Oct 30 12:21 locales
-rw-rw-r-- 1 prayag prayag 1790 Oct 30 12:21 routes.rb

Change database.yml
prayag@prayag:~/workspace_rails/Gwitter$ sudo vi config/database.yml
# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

# Warning: The database defined as "test" will be erased and

# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:

  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

[STEP 7] start a WEBrick server 
prayag@prayag:~/workspace_rails/Gwitter$ rails server

/home/prayag/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.2.1/lib/bundler/runtime.rb:197: warning: Insecure world writable dir /usr/local/sbin in PATH, mode 040777
/home/prayag/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.2.1/lib/bundler/runtime.rb:197: warning: Insecure world writable dir /usr/local/sbin in PATH, mode 040777
=> Booting WEBrick
=> Rails 3.2.8 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server

[STEP 8]
Goto browser, hit http://localhost:3000/

Monday, 29 October 2012

Getting Started With Symfony 212

Getting Started With Symfony 212

PART A : Creating an app
METHOD 1
STEP 1 : Download Symfony 2.1.2 and Move it to proper location
sudo mv /home/prayag/Downloads/Symfony_Standard_Vendors_2.1.2.tgz /var/www/

STEP 2 : Unzip Symfony_Standard_Vendors_2.1.2.tgz
tar zxvf Symfony_Standard_Vendors_2.1.2.tgz

STEP 3 : app structure
prayag@prayag:/var/www/Symfony$ ls -l
total 80
drwxr-xr-x  6 prayag prayag  4096 Oct 28 15:39 app
-rw-r--r--  1 prayag prayag  1707 Sep 20 16:14 composer.json
-rw-r--r--  1 prayag prayag 39727 Sep 20 16:14 composer.lock
-rw-r--r--  1 prayag prayag  1065 Sep 20 16:13 LICENSE
-rw-r--r--  1 prayag prayag  5929 Sep 20 16:13 README.md
drwxr-xr-x  3 prayag prayag  4096 Oct 28 15:39 src
-rw-r--r--  1 prayag prayag  7859 Sep 20 16:13 UPGRADE.md
drwxr-xr-x 12 prayag prayag  4096 Oct 28 15:39 vendor
drwxr-xr-x  3 prayag prayag  4096 Sep 20 16:14 web


STEP 4 : make some changes in web/config.php
prayag@prayag:/var/www/Symfony$ sudo vi web/config.php
//I am putting my project name to default values.
<div class="symfony-block-content">
                        <h1>Gwitter!</h1>
                        <p>Gwitter</p>
                        <p>
                            This script will guide you through the basic configuration of your project.
                            You can also do the same by editing the ‘<strong>app/config/parameters.yml</strong>’ file directly.
                        </p>

</div>

STEP 5 : Configure database in parameters.yml
prayag@prayag:/var/www/Symfony$ sudo vi app/config/parameters.yml
parameters:
    database_driver:   pdo_mysql
    database_host:     localhost
    database_port:     3306
    database_name:     Gwitter
    database_user:     root
    database_password: mysql55

    mailer_transport:  smtp
    mailer_host:       localhost
    mailer_user:       ~
    mailer_password:   ~

    locale:            en
    secret:            ThisTokenIsNotSoSecretChangeIt

STEP 6 : Run a Symfony212 app
Goto http://localhost/Symfony/web/config.php and happy coding !!

REFERENCES
http://www.slideshare.net/weaverryan/symfony2-get-your-project-started



PART B : Using Git
STEP 1
prayag@prayag:/var/www/Symfony$ git init
Initialized empty Git repository in /var/www/Symfony/.git/

STEP 2
prayag@prayag:/var/www/Symfony$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .travis.yml
#       LICENSE
#       README.md
#       UPGRADE.md
#       app/
#       composer.json
#       composer.lock
#       src/
#       vendor/
#       web/
nothing added to commit but untracked files present (use "git add" to track)


STEP 3 : Ignore Some Files
create a .gitignore file at the root of the project Gwitter.
prayag@prayag:/var/www/Symfony$ touch .gitignore
prayag@prayag:/var/www/Symfony$ sudo vi .gitignore
/web/bundles/
/app/bootstrap*
/app/cache/*
/app/logs/*
/vendor/
/app/config/parameters.ini

STEP 4 : create an initial commit
prayag@prayag:/var/www/Symfony$ git add .

prayag@prayag:/var/www/Symfony$ git commit -m "[initial framework]"
[master (root-commit) a988523] [initial framework]
 59 files changed, 3732 insertions(+)

Sunday, 28 October 2012

Connecting Liftweb to MySQL

After successfully creating a liftweb app, here is how to connect liftweb to mysql and get things done.

The structure of the liftweb app is as follows:

prayag@prayag:~/workspace_lift/gwitter/gwitter$ ls -l
total 92
-rw-rw-r-- 1 prayag prayag 18432 Oct 28 10:45 lift_proto.db.h2.db
-rw-rw-r-- 1 prayag prayag 42000 Oct 28 10:45 lift_proto.db.trace.db
-rw-rw-r-- 1 prayag prayag  7459 Oct 28 11:10 pom.xml
drwxrwxr-x 3 prayag prayag  4096 Oct 16 12:22 project
drwxrwxr-x 4 prayag prayag  4096 Oct 16 12:22 src
drwxrwxr-x 9 prayag prayag  4096 Oct 28 11:14 target

STEP 1 configure db properties
prayag@prayag:~/workspace_lift/gwitter/gwitter$ sudo kate src/main/resources/props/default.props 

# Properties in this file will be read when running in dev mode
db.dialect=org.hibernate.dialect.MySQL5Dialect
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost/Gwitter
db.user=root
db.password=root

STEP 2 configure Database Vendor in Boot.scala
prayag@prayag:~/workspace_lift/gwitter/gwitter$ sudo kate src/main/scala/bootstrap/liftweb/Boot.scala

/**
 * A class that's instantiated early and run.  It allows the application
 * to modify lift's environment
 */
class Boot {
  def boot {
    //add the following code in method boot
    if (!DB.jndiJdbcConnAvailable_?) {
      println("jndiJdbcConnAvailable_ : " + jndiJdbcConnAvailable_);
      val vendor = 
 new StandardDBVendor(Props.get("db.driver") openOr "org.h2.Driver",
        Props.get("db.url") openOr "jdbc:h2:lift_proto.db;AUTO_SERVER=TRUE",
        Props.get("db.user"), 
        Props.get("db.password"))

      LiftRules.unloadHooks.append(vendor.closeAllConnections_! _)

      DB.defineConnectionManager(DefaultConnectionIdentifier, vendor)
    }//end of jndiJdbcConnAvailable_
 }//end of boot
}

STEP 3 Add maven dependency for mysql in pom.xml
<dependencies>
   ....
   .....
   .......
    <!-- for LiftConsole -->
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-compiler</artifactId>
      <version>${scala.version}</version>
      <scope>test</scope>
    </dependency>

    <!-- mysql dependency-->
    <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>5.1.17</version>
     <type>jar</type>
    </dependency>
  </dependencies>

STEP 4 Update maven dependency
prayag@prayag:~/workspace_lift/gwitter/gwitter$ mvn install

STEP 5 - Have a look at model
http://exploring.liftweb.net/master/index-2.html#lst:PocketChange-User-entity

prayag@prayag:~/workspace_lift/gwitter/gwitter$ sudo kate src/main/scala/com/zam/gwitter/model/User.scala

5.A)
Create a User class extending the Mapper base class MegaProtoUser, which provides default fields and methods
for a User.
class User extends MegaProtoUser[User] {
  def getSingleton = User // what's the "meta" server
                          // reference to the companion object below

  // define an additional field for a personal essay
  object textArea extends MappedTextarea(this, 2048) {
    override def textareaRows  = 10
    override def textareaCols = 50
    override def displayName = "Personal Essay"
  }
  
  //def findUser : User = 
    /User.find(By(User.id, this.id))
}

5.B)
Create a "companion object" to the User class (above).
The companion object is a "singleton" object that shares the same name as its companion class.
It provides global (i.e. non-instance) methods and fields, such as find, dbTableName, dbIndexes, etc.
object User extends User with MetaMegaProtoUser[User] {
  override def dbTableName = "User" // define the DB table name, I love to use singular names for tables
  override def screenWrap = Full( <lift:surround with="default" at="content">
           <lift:bind />
       </lift:surround>
      )
  // define the order fields will appear in forms and output
  override def fieldOrder = List(id, 
                                 firstName, 
                                 lastName, 
                                 email,
                                 locale, 
                                 timezone, 
                                 password, 
                                 textArea)

  // comment this line out to require email validations
  override def skipEmailValidation = true
}

STEP 6 Create database Gwitter at mysql server
mysql>create database Gwitter;

STEP 7 Run the app
prayag@prayag:~/workspace_lift/gwitter/gwitter$ mvn jetty:run

I get the message like the following in between :
[INFO] Scanning for projects...
...
...
...
11:18:52.922 [main] DEBUG net.liftweb.util.Props - Loaded key/value properties from resource /props/default.props
11:18:53.017 [main] DEBUG net.liftweb.mapper.MetaMapper - Initializing MetaMapper for User
11:18:53.018 [main] DEBUG net.liftweb.mapper.MetaMapper - Created FieldFinder for interface net.liftweb.mapper.MappedField
11:18:53.787 [main] DEBUG net.liftweb.db.ProtoDBVendor - Created new pool entry. name=ConnectionIdentifier(lift), poolSize=1
11:18:53.830 [main] DEBUG net.liftweb.mapper.Schemifier - Starting schemify. write=true, structureOnly=false, dbId=ConnectionIdentifier(lift), schema=esewa@10.20.222.254, tables=List(users)
11:18:53.832 [main] DEBUG net.liftweb.mapper.Schemifier - Running beforeSchemifier on table users
11:18:53.992 [main] DEBUG net.liftweb.mapper.Schemifier - Executing DDL statements
11:18:53.993 [main] DEBUG net.liftweb.mapper.Schemifier - Running afterSchemifier on table users
11:18:53.995 [main] DEBUG net.liftweb.db.ProtoDBVendor - Released connection. poolSize=1
2012-10-28 11:18:54.232:INFO::Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server
[INFO] Starting scanner at interval of 5 seconds.

STEP 8 Check database
prayag@prayag:~$ mysql -u root -p
Enter password:

mysql> use Gwitter;

mysql> show tables;
+------------------------+
| Tables_in_Gwitter |
+-----------------------+
| users                      |
+-----------------------+
1 row in set (0.00 sec)


mysql> desc users;
+------------------+-------------------------+------+-----+-------------+--------------------+
| Field            | Type       | Null | Key | Default    | Extra                 |
+-------------------+-------------------------+------+-----+------------+--------------------+
| id                     | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| firstname         | varchar(32)              | YES  |     | NULL      |                           |
| lastname          | varchar(32)              | YES  |     | NULL      |                           |
| email               | varchar(48)               | YES  | MUL | NULL|                           |
| locale              | varchar(16)               | YES  |     | NULL      |                           |
| timezone         | varchar(32)              | YES  |     | NULL      |                           |
| password_pw  | varchar(48)              | YES  |     | NULL      |                           |
| password_slt   | varchar(20)             | YES  |     | NULL      |                           |
| textarea           | varchar(2048)        | YES  |     | NULL      |                           |
| uniqueid         | varchar(32)              | YES  | MUL | NULL|                           |
| validated         | tinyint(1)                  | YES  |     | NULL      |                           |
| superuser        | tinyint(1)                 | YES  |     | NULL      |                           |
+-----------------+-------------------------+-------+-----+------------+---------------------+
12 rows in set (0.01 sec)




REFERENCES
1 - How to create database connection with MySQL in Liftweb Scala, available at http://www.jooiner.com/how-to-create-database-connection-with-mysql-in-liftweb-scala/

2 - 2.1 Defining the Model, http://exploring.liftweb.net/master/index-2.html#lst:PocketChange-User-entity

3 - Scala/Lift Database Connections, http://stackoverflow.com/a/5026630/432903

4 - Mapper, https://www.assembla.com/spaces/liftweb/wiki/Mapper

5 - pocketchange app, https://github.com/tjweir/pocketchangeapp/tree/master/PocketChange/src/main

6 - How to create Simple Mini Blog With LiftWeb, https://github.com/slashmili/LiftWeblogIn30Mins/wiki/SimpleMiniBlog

Wednesday, 17 October 2012

Command Line Tools - GWT

STEP 1  Download GWT SDK 

STEP 2 Configure PATH
update profile as follows:
PATH=$PATH:/opt/gwt250RC2/gwt-2.5.0.rc2/
export PATH

$ source /etc/profile

STEP 3 webAppCreator


prayag@prayag:~/workspace_gwt$ webAppCreator -junit /home/prayag/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar -out gwitter com.zam.gwitter.GwitterModule

Generating from templates: [sample, eclipse, readme, _eclipse-test, _sample-test, ant]
Created directory gwitter
Created directory gwitter/src
Created directory gwitter/src/com/zam/gwitter
Created directory gwitter/src/com/zam/gwitter/client
Created directory gwitter/src/com/zam/gwitter/server
Created directory gwitter/src/com/zam/gwitter/shared
Created directory gwitter/test
Created directory gwitter/test/com/zam/gwitter
Created directory gwitter/war
Created directory gwitter/war/WEB-INF
Created directory gwitter/test/com/zam/gwitter/client
Created file gwitter/src/com/zam/gwitter/GwitterModule.gwt.xml
Created file gwitter/src/com/zam/gwitter/client/GreetingService.java
Created file gwitter/src/com/zam/gwitter/client/GreetingServiceAsync.java
Created file gwitter/src/com/zam/gwitter/client/GwitterModule.java
Created file gwitter/src/com/zam/gwitter/server/GreetingServiceImpl.java
Created file gwitter/src/com/zam/gwitter/shared/FieldVerifier.java
Created file gwitter/war/WEB-INF/web.xml
Created file gwitter/war/GwitterModule.css
Created file gwitter/war/GwitterModule.html
Created file gwitter/war/favicon.ico
Created file gwitter/.classpath
Created file gwitter/.project
Created file gwitter/GwitterModule.launch
Created file gwitter/README.txt
Created file gwitter/GwitterModuleTest-dev.launch
Created file gwitter/GwitterModuleTest-prod.launch
Created file gwitter/test/com/zam/gwitter/GwitterModuleJUnit.gwt.xml
Created file gwitter/test/com/zam/gwitter/client/GwitterModuleTest.java
Created file gwitter/build.xml

[ NOTE :  GwitterModule*.launch are launch configurations for Eclipse depending on application modes.]


STEP 4 Run an app in dev mode
prayag@prayag:~/workspace_gwt$ cd gwitter/

prayag@prayag:~/workspace_gwt/gwitter$ ant devmode 

Buildfile: /home/prayag/workspace_gwt/gwitter/build.xml

libs:
    [mkdir] Created dir: /home/prayag/workspace_gwt/gwitter/war/WEB-INF/lib
     [copy] Copying 1 file to /home/prayag/workspace_gwt/gwitter/war/WEB-INF/lib
     [copy] Copying 1 file to /home/prayag/workspace_gwt/gwitter/war/WEB-INF/lib

javac:
    [mkdir] Created dir: /home/prayag/workspace_gwt/gwitter/war/WEB-INF/classes
    [javac] /home/prayag/workspace_gwt/gwitter/build.xml:29: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 5 source files to /home/prayag/workspace_gwt/gwitter/war/WEB-INF/classes
     [copy] Copying 1 file to /home/prayag/workspace_gwt/gwitter/war/WEB-INF/classes

devmode:

STEP 5 
The above command starts GWT's development mode server, a local server used for development and debugging, as follows:

Launch the local server in a browser by either 
1) clicking "Launch Default Browser" or 
2) clicking "Copy to Clipboard" (to copy its URL), then pasting into Firefox, Internet Explorer, Chrome, or Safari. 
Since this is the first time hitting the development mode server, it will prompt to install the Google Web Toolkit Developer Plugin.

The application will load in development mode, as follows:


References 
Get Started with the GWT SDK, available at https://developers.google.com/web-toolkit/gettingstarted


Tuesday, 16 October 2012

Getting started with liftweb framework


Required 
Java
maven2 or sbt
jetty - a Java servlet engine
scala

STEP 1 Install Java and configure PATH
http://www.oracle.com/technetwork/java/javase/downloads/jdk7u9-downloads-1859576.html

STEP 2 Install scala 2.9.2
Download scala from http://www.scala-lang.org/downloads

$ wget  http://www.scala-lang.org/files/archive/scala-2.9.2.tgz

$ sudo mkdir /opt/scala292
Open the dowloaded file (Archive manager - file-roller) and extract to /opt/scala/

Create a sybolic link (future proof the install):
$ ln -s /opt/scala/scala-2.9.2 /opt/scala292/current

Define environment variable SCALA_HOME by editing /etc/profile and adding the line:
export SCALA_HOME=/opt/scala292/current

Add the Scala executables to your path by editing /etc/profile and adding the line:
export PATH=$PATH:$SCALA_HOME/bin

restart computer, and enter command
scala -version
Scala code runner version 2.9.2  -- Copyright 2002-2011, LAMP/EPFL


STEP 3 Install maven
$ sudo apt-get install maven2

OR
Download maven from http://maven.apache.org/download.html and configure PATH.
(mine /etc/profile is)

JAVA_HOME=/usr/java/jdk1.7.0; export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH; export PATH
export PATH=$PATH:/home/prayag/apache-maven-3.0.4/bin


STEP 4 Install jetty, an HTTP server, HTTP client, and javax.servlet container
$ sudo apt-get install jetty


STEP 5 Create liftweb app with mvn
$ mkdir /home/prayag/workspace_lift
$ cd /home/prayag/workspace_lift

Issue the following maven2 command for a lift project template downloaded from http://scala-tools.org/repo-snapshots :

mvn archetype:generate \
 -DarchetypeGroupId=net.liftweb \
 -DarchetypeArtifactId=lift-archetype-basic_2.9.1 \
 -DarchetypeVersion=2.4-M5 \
 -DarchetypeRepository=http://scala-tools.org/repo-releases \
 -DremoteRepositories=http://scala-tools.org/repo-releases \
 -DgroupId=com.zam.gwitter \
 -DartifactId=gwitter \
 -Dversion=1.0

The architecture of app will be as follows :

5 - A) The dependencies in pom.xml will be as follows :
<repositories>
    <repository>
      <id>scala-tools.releases</id>
      <name>Scala-Tools Dependencies Repository for Releases</name>
      <url>http://scala-tools.org/repo-releases</url>
    </repository>
    <repository>
      <id>java.net.maven2</id>
      <name>java.net Maven2 Repository</name>
      <url>http://download.java.net/maven/2/</url>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>scala-tools.releases</id>
      <name>Scala-Tools Plugins Repository for Releases</name>
      <url>http://scala-tools.org/repo-releases</url>
    </pluginRepository>
  </pluginRepositories>

  <dependencies>
    <dependency>
      <groupId>net.liftweb</groupId>
      <artifactId>lift-mapper_2.9.1</artifactId>
      <version>2.4-M5</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>0.9.26</version>
    </dependency>
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <version>1.2.138</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.7</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.scala-tools.testing</groupId>
      <artifactId>specs_2.9.1</artifactId>
      <version>1.6.9</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mortbay.jetty</groupId>
      <artifactId>jetty</artifactId>
      <version>6.1.25</version>
      <scope>test</scope>
    </dependency>
    <!-- for LiftConsole -->
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-compiler</artifactId>
      <version>${scala.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

5 - B) The gwitter/src/main/webapp/WEB-INF/web.xml file will be as follows :
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<filter>
  <filter-name>LiftFilter</filter-name>
  <display-name>Lift Filter</display-name>
  <description>The Filter that intercepts lift calls</description>
  <filter-class>net.liftweb.http.LiftFilter</filter-class>
</filter>
   

<filter-mapping>
  <filter-name>LiftFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>


5 - C) The gwitter/src/main/webapp/index.html will be as follows :
<div class="lift:surround?with=default;at=content">
  <h2>Welcome to your project!</h2>
  <p>
    <div class="lift:helloWorld.howdy">
      Welcome to gwitter at <span id="time">The current time</span>
    </div>
  </p>
</div>



5 - D) The com/zam/gwitter/model/User.scala will be as follows :
package com.zam.gwitter {
package model {

import _root_.net.liftweb.mapper._
import _root_.net.liftweb.util._
import _root_.net.liftweb.common._

/**
 * The singleton that has methods for accessing the database
 */
object User extends User with MetaMegaProtoUser[User] {
  override def dbTableName = "User" // define the DB table name
  override def screenWrap = Full(<lift:surround with="default" at="content">
          <lift:bind /></lift:surround>)
  // define the order fields will appear in forms and output
  override def fieldOrder = List(id, firstName, lastName, email,
  locale, timezone, password, textArea)

  // comment this line out to require email validations
  override def skipEmailValidation = true
}

/**
 * An O-R mapped "User" class that includes first name, last name, password and we add a "Personal Essay" to it
 */
class User extends MegaProtoUser[User] {
  def getSingleton = User // what's the "meta" server

  // define an additional field for a personal essay
  object textArea extends MappedTextarea(this, 2048) {
    override def textareaRows  = 10
    override def textareaCols = 50
    override def displayName = "Personal Essay"
  }
}
}
}

5 - E)  The com/zam/gwitter/snippet/HelloWorld.scala snippet will be as follows:
package com.zam.gwitter {
package snippet {

import _root_.scala.xml.{NodeSeq, Text}
import _root_.net.liftweb.util._
import _root_.net.liftweb.common._
import _root_.java.util.Date
import com.zam.gwitter.lib._
import Helpers._

class HelloWorld {
  lazy val date: Box[Date] = DependencyFactory.inject[Date] // inject the date

  // bind the date into the element with id "time"
  def howdy = "#time *" #> date.map(_.toString)

  /*
   lazy val date: Date = DependencyFactory.time.vend // create the date via factory

   def howdy = "#time *" #> date.toString
   */
}//end of class
}
}

5 - F)  The com/zam/gwitter/lib/DependencyFactory.scala will be as follows :
package com.zam.gwitter {
package lib {

import net.liftweb._
import http._
import util._
import common._
import _root_.java.util.Date

/**
 * A factory for generating new instances of Date.  You can create
 * factories for each kind of thing you want to vend in your application.
 * An example is a payment gateway.  You can change the default implementation,
 * or override the default implementation on a session, request or current call
 * stack basis.
 */
object DependencyFactory extends Factory {
  implicit object time extends FactoryMaker(Helpers.now _)

  /**
   * objects in Scala are lazily created.  The init()
   * method creates a List of all the objects.  This
   * results in all the objects getting initialized and
   * registering their types with the dependency injector
   */
  private def init() {
    List(time)
  }
  init()
}

/*
/**
 * Examples of changing the implementation
 */
sealed abstract class Changer {
  def changeDefaultImplementation() {
    DependencyFactory.time.default.set(() => new Date())
  }

  def changeSessionImplementation() {
    DependencyFactory.time.session.set(() => new Date())
  }

  def changeRequestImplementation() {
    DependencyFactory.time.request.set(() => new Date())
  }

  def changeJustForCall(d: Date) {
    DependencyFactory.time.doWith(d) {
      // perform some calculations here
    }
  }
}
*/
}
}


STEP 6 Run a liftweb app
Issue mvn jetty:run and access the web application in your browser via http://localhost:8080

STEP 7 RESULT





REFERENCES
1 - Lift + Scala, Installation + First contact, 11. AUGUST 2010 available at http://tuxpool.blogspot.com/2010/08/lifting-scala-installation-first.html

2 - Using Maven, available at https://www.assembla.com/wiki/show/liftweb/Using_Maven