Add language to book data.
[quanlib.git] / store.rb
index 660fc80bb64464fcb9aa20e18aa4c9a797666294..5faf7ce884c21371a58ea3dbafec2cece7899a0a 100644 (file)
--- a/store.rb
+++ b/store.rb
@@ -1,26 +1,39 @@
 
 require 'csv'
 require 'fileutils'
+require 'inifile'
 require 'pg'
 
-require 'series'
+require_relative 'series'
+require_relative 'tconn'
 
 class Store
-  def initialize
-    @basepath = '/arc/quanlib' # TODO: FIXME: configure this in a sane way
+  def unclassified_csv
+    @basePath + '/csv/unclassified.csv'
+  end
+
+  def initialize(config_file)
     @conn = nil
 
-    #@dburl = 'dbi:Pg:quanlib:localhost'
-    @dbhost = "localhost"
+    config = IniFile.load(config_file)
+    if nil == config
+      puts 'FATAL:  Failed to load config file "' + config_file + '".  Aborting initialization.'
+      return
+    end
+
+    section = config['database']
+    @dbhost = section['host']
     @dbport = 5432
-    @dbname = 'quanlib'
-    @dbuser = 'quanlib'
-    @dbpass = 'quanlib'
+    @dbname = section['name']
+    @dbuser = section['user']
+    @dbpass = section['pass']
+
+    section = config['filesystem']
+    @basePath = section['basePath']
   end
 
   def connect
-    # @conn = PGconn.connect('localhost', 5432, '', '', 'quanlib', 'quanlib', 'quanlib')
-    @conn = PG.connect('localhost', 5432, '', '', 'quanlib', 'quanlib', 'quanlib')
+    @conn = TimedConn.new(PG.connect(@dbhost, @dbport, '', '', @dbname, @dbuser, @dbpass))
     return @conn
   end
 
@@ -35,14 +48,14 @@ class Store
     return path, name
   end
 
-  def create_schema
+  def create_schema(skip_class)
     create_authors = 
 <<EOS
       CREATE TABLE Authors (
         id          INTEGER PRIMARY KEY,
         grouping    VARCHAR(64),
-        reading     VARCHAR(128),
-        sort        VARCHAR(128)
+        reading     VARCHAR(256),
+        sort        VARCHAR(256)
       );
 EOS
 
@@ -53,6 +66,7 @@ EOS
         author         INTEGER REFERENCES Authors(id),
         classification INTEGER REFERENCES Classifications(id),
         cover          INTEGER,
+        language       VARCHAR(64),
         description    TEXT,
         path           VARCHAR(256),
         series         INTEGER REFERENCES Series(id),
@@ -77,7 +91,7 @@ EOS
     create_efs = 
 <<EOS
       CREATE TABLE EFS (
-        id          INTEGER,
+        id          INTEGER PRIMARY KEY,
         mimetype    VARCHAR(64)
       );
 EOS
@@ -131,9 +145,13 @@ EOS
       @conn.exec(stmt)
     end
 
-    populate_fast_table()
-    populate_classifications_table()
+    if skip_class == false
+      populate_fast_table()
+      populate_classifications_table()
+    end
+
     populate_series_table()
+
   end
 
   def dropSchema
@@ -172,7 +190,7 @@ EOS
     return nil
   end
 
-  def init_db
+  def init_db(skip_class)
     sql = "SELECT 1 FROM pg_tables WHERE tableowner='quanlib' AND tablename='books'"
     found = false
     @conn.exec(sql).each do |row|
@@ -180,12 +198,11 @@ EOS
     end
 
     if ! found
-      create_schema()
+      create_schema(skip_class)
     end
   end
 
   def load_author(id)
-    #puts 'DEBUG:  load_author(' + id + ')'
     sqlSelect = "SELECT grouping, reading, sort FROM Authors WHERE id=$1"
     args = [id]
     @conn.exec_params(sqlSelect, args) do |rs|
@@ -194,10 +211,8 @@ EOS
       end
       row = rs[0]
       author = Author.new(row['grouping'], row['reading'], row['sort'])
-      #puts 'DEBUG:  author:  ' + author.inspect()
       return author
     end
-    #puts 'DEBUG:  NOT FOUND'
     return nil
   end
 
@@ -221,8 +236,7 @@ EOS
   end
 
   def load_book(id)
-    #puts 'DEBUG:  load_book(' + id + ')'
-    sql = "SELECT author, classification, cover, description, path, series, title, volume FROM Books WHERE id=$1;"
+    sql = "SELECT author, classification, cover, description, language, path, series, title, volume FROM Books WHERE id=$1;"
     book = nil
 
     begin
@@ -238,6 +252,7 @@ EOS
         book.classification_id = row['classification']
         book.cover = load_cover(row['cover'])
         book.description = row['description']
+        book.language = row['language']
         book.path = row['path']
         book.series_id = row['series']
         book.title = row['title']
@@ -249,19 +264,18 @@ EOS
       puts $@
     end
 
-    #puts 'DEBUG:  loaded book:   ' + book.inspect()
     return book
   end
 
   def store_book(book)
-    sql = "INSERT INTO Books (id, author, classification, cover, description, path, series, title, volume) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9);"
+    sql = "INSERT INTO Books (id, author, classification, cover, description, language, path, series, title, volume) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);"
 
     book_id = next_id('book_id')
 
     author_id = store_author(book.author)
     (efs_id, mime_type) = store_cover(book)
 
-    args = [book_id, author_id, book.classification_id, efs_id, book.description(), book.path(), book.series_id(), book.title(), book.volume()]
+    args = [book_id, author_id, book.classification_id, efs_id, book.description, book.language, book.path, book.series_id, book.title, book.volume]
 
     begin
       rs = @conn.exec_params(sql, args)
@@ -277,15 +291,34 @@ EOS
   end
 
   def find_classification(author_grouping, title_grouping)
-    #puts 'find_classification("' + author_grouping.inspect + '", "' + title_grouping.inspect + '")...'
     sql = "SELECT id FROM Classifications WHERE author_grouping = $1 AND title_grouping = $2;"
     @conn.exec_params(sql, [author_grouping, title_grouping]) do |rs|
       if rs.ntuples > 0
-        #puts '  --> ' + rs[0]['id'].inspect
         return rs[0]['id']
       end
     end
-    #puts '  --> NIL'
+    return nil
+  end
+
+  def load_classification(id)
+    sql  = "SELECT ddc, lcc, author_grouping, author_sort, title_grouping, title "
+    sql += " FROM Classifications WHERE id=$1"
+    @conn.exec_params(sql, [id]) do |rs|
+      if rs.ntuples > 0
+        row = rs[0]
+        ddc = row['ddc']
+        lcc = row['lcc']
+        author_grouping = row['author_grouping']
+        author = row['author_sort']
+        title_grouping = row['title_grouping']
+        title = row['title']
+
+        result = Classification.new(ddc, lcc, author_grouping, author, title_grouping, title)
+        result.id = id
+        return result
+      end
+    end
+
     return nil
   end
 
@@ -306,15 +339,9 @@ EOS
 
     (efspath, efsname) = construct_efs_path(id)
 
-    fullpath = @basepath + '/efs/' + efspath + '/' + efsname
+    fullpath = @basePath + '/efs/' + efspath + '/' + efsname
 
     return Cover.new(nil, fullpath, mime_type)
-
-    #File.open(fullpath, 'rb') do |is|
-    #  return Cover.new(is, fullpath, mime_type)
-    #end
-    #
-    #return nil
   end
 
   def store_cover(book)
@@ -335,7 +362,7 @@ EOS
 
     (efspath, efsname) = construct_efs_path(efs_id)
 
-    efspath = @basepath + '/efs/' + efspath
+    efspath = @basePath + '/efs/' + efspath
 
     FileUtils.mkdir_p(efspath)
 
@@ -422,7 +449,7 @@ EOS
   def populate_classifications_table
     puts "Populating the Classifications table..."
     first = true
-    CSV.foreach(@basepath + '/csv/class.csv') do |row|
+    CSV.foreach(@basePath + '/csv/class.csv') do |row|
       if first
         # skip the header row
         first = false
@@ -462,7 +489,7 @@ EOS
   def populate_fast_table
     puts "Populating the FAST table..."
     first = true
-    CSV.foreach(@basepath + '/csv/fast.csv') do |row|
+    CSV.foreach(@basePath + '/csv/fast.csv') do |row|
       if first
         first = false  # skip the header row
       else
@@ -476,7 +503,7 @@ EOS
 
   def populate_series_table
     puts "Populating the Series table..."
-    CSV.foreach(@basepath + '/csv/series.csv') do |row|
+    CSV.foreach(@basePath + '/csv/series.csv') do |row|
       id = next_id('series_id')
       sqlInsert = "INSERT INTO Series (id, age, genre, grouping, code, descr) VALUES ($1, $2, $3, $4, $5, $6);"
       args = [id] + row