#This class represents a facility and includes related database methods

require 'mysql'

class Facility

   attr_accessor :name
   attr_accessor :type
   attr_accessor :facility_code
   attr_accessor :state
   attr_accessor :city
   attr_accessor :address
   attr_accessor :parent_site_code


   HOST = 'localhost'
   USER = 'root'
   DBPASSWORD = 'Agilexadmin99$'
   SCHEMA = 'VARDB'
   
  def populateFacility()

    self.name ='TEST VAMC 3'
    self.type = 'Facility'
    self.facility_code = '777'
    self.state = 'VA'
    self.city = 'Chantilly'
    self.address = '5155 Parkstone Dr.'
    self.parent_site_code = '777'

  end

  def populateAndSaveFacility()
    self.populateFacility
    self.insertRecord(self)
  end

   def insertRecord(target_record)
     #create the insert statement and post to the db
     begin
       con = Mysql.new(HOST, USER, DBPASSWORD, SCHEMA)
       insert_statement = "INSERT INTO `FACILITY`
(`NAME`,
`TYPE`,
`FACILITY_CODE`,
`STATE`,
`CITY`,
`ADDRESS`,
`PARENT_SITE_CODE`)
VALUES ('#{target_record.name}',
'#{target_record.type}',
'#{target_record.facility_code}',
'#{target_record.state}',
'#{target_record.city}',
'#{target_record.address}',
'#{target_record.parent_site_code}'
);"
       rs = con.query(insert_statement)
       con.close

     rescue Exception=>e
       puts "MySQL Exception: " + e.to_s

     ensure
       puts "insert facility statement executed"
     end

   end
end