#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
#
# alone : web application framework for small embedded systems.
#               Copyright (c) 2009-2010 Inas Co Ltd. All Rights Reserved.
#
# This file is destributed under BSD License. Please read the COPYRIGHT file.
#

require 'al_form'
require 'al_persist_postgres'
require 'al_mif'

CONN_STR = "host=localhost dbname=testdb1 user=testuser1 password=testpass1"


class AlController

  ##
  # constructor
  #
  def initialize()
    @form = AlForm.new(
      AlInteger.new( "id", :foreign=>true ),
      AlDate.new( "create_date", :label=>"登録日", :value=>Time.now.strftime( '%Y/%m/%d' ) ),
      AlTextArea.new( "memo", :label=>"ToDoメモ", :required=>true ),
      AlDate.new( "limit_date", :label=>"期限" ),
      AlSubmit.new( "submit1", :value=>"決定",
        :tag_attr=> {:style=>"float: right;"} )
    )

    # use postgresql
    @db = AlPersistPostgres.connect( CONN_STR )
    @persist = @db.table( "todo", :id )

  end


  ##
  # デフォルトアクション
  #
  def action_index
    begin
      action_list
      
    rescue =>ex
      if ex.message =~ /relation "todo" does not exist/
        @db.execute( "create table todo ( id serial primary key, create_date date, memo text, limit_date date );" )
        retry
      else
        raise ex
      end
    end
  end

end
