Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add portal languages preference #118

Closed
17 changes: 17 additions & 0 deletions lib/goo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ module Goo

@@resource_options = Set.new([:persistent]).freeze

# Define the languages from which the properties values will be taken
# It choose the first language that match otherwise return all the values
@@main_languages = %w[en]

@@configure_flag = false
@@sparql_backends = {}
@@model_by_name = {}
Expand All @@ -47,6 +51,19 @@ module Goo

@@slice_loading_size = 500


def self.main_languages
@@main_languages
end
def self.main_languages=(lang)
@@main_languages = lang
end

def self.language_includes(lang)
lang_str = lang.to_s
main_languages.index { |l| lang_str.downcase.eql?(l) || lang_str.upcase.eql?(l)}
end

def self.add_namespace(shortcut, namespace,default=false)
if !(namespace.instance_of? RDF::Vocabulary)
raise ArgumentError, "Namespace must be a RDF::Vocabulary object"
Expand Down
204 changes: 96 additions & 108 deletions lib/goo/base/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ def valid?
self.class.attributes.each do |attr|
inst_value = self.instance_variable_get("@#{attr}")
attr_errors = Goo::Validators::Enforce.enforce(self,attr,inst_value)
unless attr_errors.nil?
validation_errors[attr] = attr_errors
end
validation_errors[attr] = attr_errors unless attr_errors.nil?
end

if !@persistent && validation_errors.length == 0
Expand All @@ -70,9 +68,7 @@ def valid?
end

def id=(new_id)
if [email protected]? and @persistent
raise ArgumentError, "The id of a persistent object cannot be changed."
end
raise ArgumentError, "The id of a persistent object cannot be changed." if [email protected]? and @persistent
raise ArgumentError, "ID must be an RDF::URI" unless new_id.kind_of?(RDF::URI)
@id = new_id
end
Expand All @@ -81,7 +77,7 @@ def id
@id = generate_id if @id.nil?

@id
end
end

def persistent?
return @persistent
Expand Down Expand Up @@ -136,19 +132,15 @@ def unmmaped_to_array

def delete(*args)
if self.kind_of?(Goo::Base::Enum)
unless args[0] && args[0][:init_enum]
raise ArgumentError, "Enums cannot be deleted"
end
raise ArgumentError, "Enums cannot be deleted" unless args[0] && args[0][:init_enum]
end

raise ArgumentError, "This object is not persistent and cannot be deleted" if !@persistent

if !fully_loaded?
missing = missing_load_attributes
options_load = { models: [ self ], klass: self.class, :include => missing }
if self.class.collection_opts
options_load[:collection] = self.collection
end
options_load[:collection] = self.collection if self.class.collection_opts
Goo::SPARQL::Queries.model_load(options_load)
end

Expand All @@ -164,25 +156,19 @@ def delete(*args)
end
@persistent = false
@modified = true
if self.class.inmutable? && self.class.inm_instances
self.class.load_inmutable_instances
end
self.class.load_inmutable_instances if self.class.inmutable? && self.class.inm_instances
return nil
end

def bring(*opts)
opts.each do |k|
if k.kind_of?(Hash)
k.each do |k2,v|
if self.class.handler?(k2)
raise ArgumentError, "Unable to bring a method based attr #{k2}"
end
raise ArgumentError, "Unable to bring a method based attr #{k2}" if self.class.handler?(k2)
self.instance_variable_set("@#{k2}",nil)
end
else
if self.class.handler?(k)
raise ArgumentError, "Unable to bring a method based attr #{k}"
end
raise ArgumentError, "Unable to bring a method based attr #{k}" if self.class.handler?(k)
self.instance_variable_set("@#{k}",nil)
end
end
Expand All @@ -197,9 +183,7 @@ def bring(*opts)

def graph
opts = self.class.collection_opts
if opts.nil?
return self.class.uri_type
end
return self.class.uri_type if opts.nil?
col = collection
if col.is_a?Array
if col.length == 1
Expand All @@ -211,79 +195,14 @@ def graph
return col ? col.id : nil
end

def self.map_attributes(inst,equivalent_predicates=nil)
if (inst.kind_of?(Goo::Base::Resource) && inst.unmapped.nil?) ||
(!inst.respond_to?(:unmapped) && inst[:unmapped].nil?)
raise ArgumentError, "Resource.map_attributes only works for :unmapped instances"
end
klass = inst.respond_to?(:klass) ? inst[:klass] : inst.class
unmapped = inst.respond_to?(:klass) ? inst[:unmapped] : inst.unmapped
list_attrs = klass.attributes(:list)
unmapped_string_keys = Hash.new
unmapped.each do |k,v|
unmapped_string_keys[k.to_s] = v
end
klass.attributes.each do |attr|
next if inst.class.collection?(attr) #collection is already there
next unless inst.respond_to?(attr)
attr_uri = klass.attribute_uri(attr,inst.collection).to_s
if unmapped_string_keys.include?(attr_uri.to_s) ||
(equivalent_predicates && equivalent_predicates.include?(attr_uri))
object = nil
if !unmapped_string_keys.include?(attr_uri)
equivalent_predicates[attr_uri].each do |eq_attr|
if object.nil? and !unmapped_string_keys[eq_attr].nil?
object = unmapped_string_keys[eq_attr].dup
else
if object.is_a?Array
if !unmapped_string_keys[eq_attr].nil?
object.concat(unmapped_string_keys[eq_attr])
end
end
end
end
if object.nil?
inst.send("#{attr}=",
list_attrs.include?(attr) ? [] : nil, on_load: true)
next
end
else
object = unmapped_string_keys[attr_uri]
end
object = object.map { |o| o.is_a?(RDF::URI) ? o : o.object }
if klass.range(attr)
object = object.map { |o|
o.is_a?(RDF::URI) ? klass.range_object(attr,o) : o }
end
unless list_attrs.include?(attr)
object = object.first
end
if inst.respond_to?(:klass)
inst[attr] = object
else
inst.send("#{attr}=",object, on_load: true)
end
else
inst.send("#{attr}=",
list_attrs.include?(attr) ? [] : nil, on_load: true)
if inst.id.to_s == "http://purl.obolibrary.org/obo/IAO_0000415"
if attr == :definition
# binding.pry
end
end
end

end
end

def collection
opts = self.class.collection_opts
if opts.instance_of?(Symbol)
if self.class.attributes.include?(opts)
value = self.send("#{opts}")
if value.nil?
raise ArgumentError, "Collection `#{opts}` is nil"
end
raise ArgumentError, "Collection `#{opts}` is nil" if value.nil?
return value
else
raise ArgumentError, "Collection `#{opts}` is not an attribute"
Expand All @@ -298,9 +217,7 @@ def add_aggregate(attribute,aggregate,value)
def save(*opts)

if self.kind_of?(Goo::Base::Enum)
unless opts[0] && opts[0][:init_enum]
raise ArgumentError, "Enums can only be created on initialization"
end
raise ArgumentError, "Enums can only be created on initialization" unless opts[0] && opts[0][:init_enum]
end
batch_file = nil
if opts && opts.length > 0
Expand All @@ -310,9 +227,7 @@ def save(*opts)
end

if !batch_file
if not modified?
return self
end
return self if not modified?
raise Goo::Base::NotValidException, "Object is not valid. Check errors." unless valid?
end

Expand Down Expand Up @@ -351,9 +266,7 @@ def save(*opts)

@modified_attributes = Set.new
@persistent = true
if self.class.inmutable? && self.class.inm_instances
self.class.load_inmutable_instances
end
self.class.load_inmutable_instances if self.class.inmutable? && self.class.inm_instances
return self
end

Expand Down Expand Up @@ -391,9 +304,7 @@ def to_hash
end
end
@unmapped.each do |attr,values|
unless all_attr_uris.include?(attr)
attr_hash[attr] = values.map { |v| v.to_s }
end
attr_hash[attr] = values.map { |v| v.to_s } unless all_attr_uris.include?(attr)
end
end
attr_hash[:id] = @id
Expand All @@ -413,13 +324,90 @@ def self.range_object(attr,id)
return range_object
end

def self.find(id, *options)
if !id.instance_of?(RDF::URI) && self.name_with == :id
id = RDF::URI.new(id)


def self.map_attributes(inst,equivalent_predicates=nil)
if (inst.kind_of?(Goo::Base::Resource) && inst.unmapped.nil?) ||
(!inst.respond_to?(:unmapped) && inst[:unmapped].nil?)
raise ArgumentError, "Resource.map_attributes only works for :unmapped instances"
end
unless id.instance_of?(RDF::URI)
id = id_from_unique_attribute(name_with(),id)
klass = inst.respond_to?(:klass) ? inst[:klass] : inst.class
unmapped = inst.respond_to?(:klass) ? inst[:unmapped] : inst.unmapped
list_attrs = klass.attributes(:list)
unmapped_string_keys = Hash.new
unmapped.each do |k,v|
unmapped_string_keys[k.to_s] = v
end
klass.attributes.each do |attr|
next if inst.class.collection?(attr) #collection is already there
next unless inst.respond_to?(attr)
attr_uri = klass.attribute_uri(attr,inst.collection).to_s
if unmapped_string_keys.include?(attr_uri.to_s) ||
(equivalent_predicates && equivalent_predicates.include?(attr_uri))
object = nil
if !unmapped_string_keys.include?(attr_uri)
equivalent_predicates[attr_uri].each do |eq_attr|
if object.nil? and !unmapped_string_keys[eq_attr].nil?
object = unmapped_string_keys[eq_attr].dup
else
if object.is_a?Array
object.concat(unmapped_string_keys[eq_attr]) if !unmapped_string_keys[eq_attr].nil?
end
end
end
if object.nil?
inst.send("#{attr}=", list_attrs.include?(attr) ? [] : nil, on_load: true)
next
end
else
object = unmapped_string_keys[attr_uri]
end

lang_filter = Goo::SPARQL::Solution::LanguageFilter.new

object = object.map do |o|
if o.is_a?(RDF::URI)
o
else
literal = o
index, lang_val = lang_filter.main_lang_filter inst.id.to_s, attr, literal, literal
lang_val.to_s if index.eql? :no_lang
end
end

object = object.compact

other_languages_values = lang_filter.other_languages_values
other_languages_values = other_languages_values[inst.id.to_s][attr] unless other_languages_values.empty?
unless other_languages_values.nil?
object = lang_filter.languages_values_to_set(other_languages_values, object)
end

if klass.range(attr)
object = object.map { |o|
o.is_a?(RDF::URI) ? klass.range_object(attr,o) : o }
end
object = object.first unless list_attrs.include?(attr)
if inst.respond_to?(:klass)
inst[attr] = object
else
inst.send("#{attr}=",object, on_load: true)
end
else
inst.send("#{attr}=",
list_attrs.include?(attr) ? [] : nil, on_load: true)
if inst.id.to_s == "http://purl.obolibrary.org/obo/IAO_0000415"
if attr == :definition
# binding.pry
end
end
end

end
end
def self.find(id, *options)
id = RDF::URI.new(id) if !id.instance_of?(RDF::URI) && self.name_with == :id
id = id_from_unique_attribute(name_with(),id) unless id.instance_of?(RDF::URI)
if self.inmutable? && self.inm_instances && self.inm_instances[id]
w = Goo::Base::Where.new(self)
w.instance_variable_set("@result", [self.inm_instances[id]])
Expand Down
7 changes: 4 additions & 3 deletions lib/goo/sparql/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ def self.model_load_sliced(*options)
expand_equivalent_predicates(select, equivalent_predicates)
solution_mapper = Goo::SPARQL::SolutionMapper.new aggregate_projections, bnode_extraction,
embed_struct, incl_embed, klass_struct, models_by_id,
predicates_map, unmapped, variables, options
predicates_map, unmapped, variables, incl, options

solution_mapper.map_each_solutions(select)
end

Expand Down Expand Up @@ -148,10 +149,10 @@ def self.get_predicate_map(predicates)
predicates_map = {}
uniq_p.each do |p|
i = 0
key = ("var_" + p.last_part + i.to_s).to_sym
key = ("var_#{p.last_part}#{i.to_s}").to_sym
while predicates_map.include?(key)
i += 1
key = ("var_" + p.last_part + i.to_s).to_sym
key = ("var_#{p.last_part}#{i.to_s}").to_sym
break if i > 10
end
predicates_map[key] = p
Expand Down
Loading
Loading