Improves handling of non-fiction classification data.
[quanlib.git] / store.rb
index 660fc80bb64464fcb9aa20e18aa4c9a797666294..4895a5bb7483bbcf474c62daca224eee2227e85c 100644 (file)
--- a/store.rb
+++ b/store.rb
@@ -6,8 +6,14 @@ require 'pg'
 require 'series'
 
 class Store
+  @@BASEPATH = '/arc/quanlib'  # TODO: FIXME: configure this in a sane way
+  @@UNCLASSIFIED_CSV = @@BASEPATH + '/unclassified.csv'
+
+  def self.unclassified_csv
+    @@UNCLASSIFIED_CSV
+  end
+
   def initialize
-    @basepath = '/arc/quanlib' # TODO: FIXME: configure this in a sane way
     @conn = nil
 
     #@dburl = 'dbi:Pg:quanlib:localhost'
@@ -41,8 +47,8 @@ class Store
       CREATE TABLE Authors (
         id          INTEGER PRIMARY KEY,
         grouping    VARCHAR(64),
-        reading     VARCHAR(128),
-        sort        VARCHAR(128)
+        reading     VARCHAR(196),
+        sort        VARCHAR(196)
       );
 EOS
 
@@ -289,6 +295,28 @@ EOS
     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
+
   def load_cover(id)
     if nil == id
       return nil
@@ -306,7 +334,7 @@ EOS
 
     (efspath, efsname) = construct_efs_path(id)
 
-    fullpath = @basepath + '/efs/' + efspath + '/' + efsname
+    fullpath = @@BASEPATH + '/efs/' + efspath + '/' + efsname
 
     return Cover.new(nil, fullpath, mime_type)
 
@@ -335,7 +363,7 @@ EOS
 
     (efspath, efsname) = construct_efs_path(efs_id)
 
-    efspath = @basepath + '/efs/' + efspath
+    efspath = @@BASEPATH + '/efs/' + efspath
 
     FileUtils.mkdir_p(efspath)
 
@@ -422,7 +450,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 +490,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 +504,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