Class: LocalAuthority

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/local_authority.rb

Class Method Summary collapse

Class Method Details

.entries_by_term(model, term, query) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/local_authority.rb', line 63

def self.entries_by_term(model, term, query)
  return if query.empty?
  lowQuery = query.downcase
  hits = []
  # move lc_subject into it's own table since being part of the usual structure caused it to be too slow.  
  # When/if we move to having multiple dictionaries for subject we will need to also do a check for the appropriate dictionary. 
  if (term == 'subject' && model == 'generic_files') # and local_authoritiy = lc_subject 
      sql = SubjectLocalAuthorityEntry.where("lowerLabel like ?", "#{lowQuery}%").select("label, uri").limit(25).to_sql
      SubjectLocalAuthorityEntry.find_by_sql(sql).each do |hit|
        hits << {:uri => hit.uri, :label => hit.label}
      end
  else 
    dterm = DomainTerm.where(:model => model, :term => term).first
    if dterm
      authorities = dterm.local_authorities.collect(&:id).uniq      
      sql = LocalAuthorityEntry.where("local_authority_id in (?)", authorities).where("lower(label) like ?", "#{lowQuery}%").select("label, uri").limit(25).to_sql
      LocalAuthorityEntry.find_by_sql(sql).each do |hit|
        hits << {:uri => hit.uri, :label => hit.label}
      end
    end
  end
  return hits
end

.harvest_rdf(name, sources, opts = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/local_authority.rb', line 8

def self.harvest_rdf(name, sources, opts = {})
  return unless self.where(:name => name).empty?
  authority = self.create(:name => name)
  format = opts.fetch(:format, :ntriples)
  predicate = opts.fetch(:predicate, RDF::SKOS.prefLabel)
  entries = []
  sources.each do |uri|
    RDF::Reader.open(uri, :format => format) do |reader|
      reader.each_statement do |statement|
        if statement.predicate == predicate
          entries << LocalAuthorityEntry.new(:local_authority => authority,
                                             :label => statement.object.to_s,
                                             :uri => statement.subject.to_s)
        end
      end
    end
  end
  if LocalAuthorityEntry.respond_to? :import
    LocalAuthorityEntry.import entries
  else
    entries.each { |e| e.save! }
  end
end

.harvest_tsv(name, sources, opts = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/local_authority.rb', line 32

def self.harvest_tsv(name, sources, opts = {})
  return unless self.where(:name => name).empty?
  authority = self.create(:name => name)
  prefix = opts.fetch(:prefix, "")
  entries = []
  sources.each do |uri|
    open(uri) do |f|
      f.each_line do |tsv|
        fields = tsv.split(/\t/)
        entries << LocalAuthorityEntry.new(:local_authority => authority,
                                           :uri => "#{prefix}#{fields[0]}/",
                                           :label => fields[2])
      end
    end
  end
  if LocalAuthorityEntry.respond_to? :import
    LocalAuthorityEntry.import entries
  else
    entries.each { |e| e.save! }
  end
end

.register_vocabulary(model, term, name) ⇒ Object



54
55
56
57
58
59
60
61
# File 'app/models/local_authority.rb', line 54

def self.register_vocabulary(model, term, name)
  authority = self.find_by_name(name)
  return if authority.blank?
  model = model.to_s.sub(/RdfDatastream$/, '').underscore.pluralize
  domain_term = DomainTerm.find_or_create_by(model: model, term: term)
  return if domain_term.local_authorities.include? authority
  domain_term.local_authorities << authority
end