Class: Labeling::SKOS::Base

Inherits:
Base
  • Object
show all
Defined in:
app/models/labeling/skos/base.rb

Overview

Copyright 2011-2013 innoQ Deutschland GmbH

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Direct Known Subclasses

AltLabel, HiddenLabel, PrefLabel

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_from_rdf(rdf_subject, rdf_predicate, rdf_object) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/models/labeling/skos/base.rb', line 130

def self.build_from_rdf(rdf_subject, rdf_predicate, rdf_object)
  unless rdf_subject.is_a?(Concept::Base)
    raise "#{self.name}#build_from_rdf: Subject (#{rdf_subject}) must be a Concept."
  end

  unless rdf_object =~ RDFAPI::LITERAL_REGEXP
    raise InvalidStringLiteralError, "#{self.name}#build_from_rdf: Object (#{rdf_object}) must be a string literal"
  end

  lang = $3
  value = begin
    JSON.parse(%Q{["#{$1}"]})[0].gsub('\\n', "\n") # Trick to decode \uHHHHH chars
  rescue JSON::ParserError
    $1
  end

  predicate_class = RDFAPI::PREDICATE_DICTIONARY[rdf_predicate] || self
  predicate_class.new(target: self.label_class.new(value: value, language: lang)).tap do |labeling|
    rdf_subject.send(predicate_class.name.to_relation_name) << labeling
  end
end

.by_label_with_language(label, language) ⇒ Object

********** Scopes



26
27
28
# File 'app/models/labeling/skos/base.rb', line 26

def self.by_label_with_language(label, language)
  includes(:target).merge(self.label_class.where(value: label, language: language))
end

.edit_partial_name(obj) ⇒ Object



40
41
42
# File 'app/models/labeling/skos/base.rb', line 40

def self.edit_partial_name(obj)
  'partials/labeling/skos/edit_base'
end

.label_classObject

********** Methods



32
33
34
# File 'app/models/labeling/skos/base.rb', line 32

def self.label_class
  Iqvoc::Label.base_class
end

.partial_name(obj) ⇒ Object



36
37
38
# File 'app/models/labeling/skos/base.rb', line 36

def self.partial_name(obj)
  'partials/labeling/skos/base'
end

.search_result_partial_nameObject



126
127
128
# File 'app/models/labeling/skos/base.rb', line 126

def self.search_result_partial_name
  'partials/labeling/skos/search_result'
end

.single_query(params = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/models/labeling/skos/base.rb', line 44

def self.single_query(params = {})
  query_str = build_query_string(params)

  scope = includes(:target).order("LENGTH(#{Label::Base.table_name}.value)")
  languages = Array(params[:languages])

  if params[:query].present?
    scope = scope.
      merge(Label::Base.by_query_value(query_str).by_language(languages).published).
      references(:labels)
  else
    scope = scope.merge(Label::Base.by_language(languages).published).
      references(:labels)
  end

  if params[:collection_origin].present?
    collection = Iqvoc::Collection.base_class.where(origin: params[:collection_origin]).last
    if collection
      scope = scope.includes(owner: :collection_members)
      scope = scope.where("#{Collection::Member::Base.table_name}.collection_id" => collection.id)
      scope = scope.references(:collection_members)
    else
      raise "Collection with Origin #{params[:collection_origin]} not found!"
    end
  end
  scope = scope.includes(:owner)

  scope = case params[:for]
  when 'concept'
    scope.where('concepts.type' => Iqvoc::Concept.base_class_name).
      references(:concepts)
  when 'collection'
    scope.where('concepts.type' => Iqvoc::Collection.base_class_name).
      references(:concepts)
  else
    # no additional conditions
    scope
  end

  if params[:change_note_date_from].present? || params[:change_note_date_to].present?
    change_note_relation = Iqvoc.change_note_class_name.to_relation_name
    concepts = Concept::Base.base_class.published
                            .includes(change_note_relation.to_sym => :annotations)
                            .references(change_note_relation)
                            .references('note_annotations')

    # change note type filtering
    concepts = case params[:change_note_type]
               when 'created'
                 concepts.where('note_annotations.predicate = ?', 'created')
               when 'modified'
                 concepts.where('note_annotations.predicate = ?', 'modified')
               else
                 concepts.where('note_annotations.predicate = ? OR note_annotations.predicate = ?', 'created', 'modified')
               end

    if params[:change_note_date_from].present?
      begin
        DateTime.parse(params[:change_note_date_from])
        date_from = params[:change_note_date_from]
        concepts = concepts.where('note_annotations.value >= ?', date_from)
      rescue ArgumentError
        Rails.logger.error "Invalid date was entered for search"
      end
    end

    if params[:change_note_date_to].present?
      begin
        date_to = DateTime.parse(params[:change_note_date_to]).end_of_day.to_s
        concepts = concepts.where('note_annotations.value <= ?', date_to)
      rescue ArgumentError
        Rails.logger.error "Invalid date was entered for search"
      end
    end

    scope = scope.includes(:owner).merge(concepts)
  end

  scope = yield(scope) if block_given?
  scope.map { |result| SearchResult.new(result) }
end

Instance Method Details

#build_rdf(document, subject) ⇒ Object



152
153
154
# File 'app/models/labeling/skos/base.rb', line 152

def build_rdf(document, subject)
  subject.send(self.rdf_namespace.camelcase).send(self.rdf_predicate, target.value.to_s, lang: target.language)
end

#build_search_result_rdf(document, result) ⇒ Object



156
157
158
159
# File 'app/models/labeling/skos/base.rb', line 156

def build_search_result_rdf(document, result)
  result.Sdc::link(IqRdf.build_uri(owner.origin))
  build_rdf(document, result)
end