lib/cultural_network/import/feature_importation.rb
# The optional column "feature_relations.related_feature.fid" can specify the THL ID for parent feature.
# If such parent is specified, the following columns are required:
# "perspectives.code"/"perspectives.name", "feature_relations.type.code"
The optional column "feature_relations.related_feature.fid" can specify the THL ID for parent feature.
The optional column " feature_relations . related_feature . fid " can specify the THL ID for parent feature .
def process_feature_relations(n)
      feature_ids_with_changed_relations = Array.new
      delete_relations = self.fields.delete('feature_relations.delete')
      if !delete_relations.blank? && delete_relations.downcase == 'yes'
        self.feature.all_child_relations.clear
        self.feature.all_parent_relations.clear
      end
      replace_relations_str = self.fields.delete('feature_relations.replace')
      if replace_relations_str.blank?
        replace_relation = false
      else
        replace_relations = replace_relations_str.downcase == 'yes'
      end
      0.upto(n) do |i|
        prefix = i>0 ? "#{i}." : ''
        parent_fid = self.fields.delete("#{prefix}feature_relations.related_feature.fid")
        next if parent_fid.blank?
        parent = Feature.get_by_fid(parent_fid)
        if parent.nil?
          puts "Parent feature with THL #{parent_fid} not found."
          next
        end
        perspective_code = self.fields.delete("#{prefix}perspectives.code")
        perspective_name = self.fields.delete("#{prefix}perspectives.name")
        perspective = nil
        if perspective_code.blank? && perspective_name.blank?
          if !replace_relations
            puts "Perspective type is required to establish a relationship between feature #{self.feature.pid} and feature #{parent_fid}."
            next
          end
        else
          begin
            perspective = Perspective.get_by_code_or_name(perspective_code, perspective_name)
          rescue Exception => e
            puts e.to_s
          end
          if perspective.nil?
            puts "Perspective #{perspective_code || perspective_name} was not found."
            next
          end
        end
        relation_type_str = self.fields.delete("#{prefix}feature_relations.type.code")
        relation_type = nil
        if relation_type_str.blank?
          if !replace_relations
            puts "Feature relation type is required to establish a relationship between feature #{self.feature.pid} and feature #{parent_fid}."
            next
          end
        else
          relation_type = FeatureRelationType.get_by_code(relation_type_str)
          if relation_type.nil?
            relation_type = FeatureRelationType.get_by_asymmetric_code(relation_type_str)
            if relation_type.nil?
              puts "Feature relation type #{relation_type_str} was not found."
              next
            else
              conditions = { :parent_node_id => self.feature.id, :child_node_id => parent.id }
            end
          else
            conditions = { :parent_node_id => parent.id, :child_node_id => self.feature.id }
          end
        end
        conditions.merge!(:feature_relation_type_id => relation_type.id, :perspective_id => perspective.id) if !replace_relations
        feature_relation = FeatureRelation.where(conditions).first
        changed = false
        if feature_relation.nil?
          feature_relation = FeatureRelation.create(conditions.merge({:skip_update => true}))
          if feature_relation.nil?
            put "Failed to create feature relation between #{parent.pid} and #{self.feature.pid}"
          else
            changed = true
          end
        elsif replace_relations 
          feature_relation.feature_relation_type = relation_type if !relation_type.nil?
          feature_relation.perspective = perspective if !perspective.nil?
          if feature_relation.changed?
            feature_relation.skip_update = true
            feature_relation.save
            changed = true
          end
        end
        if changed
          feature_ids_with_changed_relations << parent.id if !feature_ids_with_changed_relations.include? parent.id
          feature_ids_with_changed_relations << self.feature.id if !feature_ids_with_changed_relations.include? self.feature.id
        end
        if feature_relation.nil?
          puts "Couldn't establish relationship #{relation_type_str} between feature #{self.feature.pid} and #{parent_fid}."
        else
          second_prefix = "#{prefix}feature_relations"
          0.upto(3) do |j|
            third_prefix = j==0 ? second_prefix : "#{second_prefix}.#{j}"
            self.add_date(third_prefix, feature_relation)
            self.add_info_source(third_prefix, feature_relation)
            self.add_note(third_prefix, feature_relation)
          end
        end
      end
      return feature_ids_with_changed_relations
    end
def process_feature_relations ( n ) feature_ids_with_changed_relations = Array . new delete_relations = self . fields . delete ( 'feature_relations.delete' ) if ! delete_relations . blank? && delete_relations . downcase == 'yes' self . feature . all_child_relations . clear self . feature . all_parent_relations . clear end replace_relations_str = self . fields . delete ( 'feature_relations.replace' ) if replace_relations_str . blank? replace_relation = false else replace_relations = replace_relations_str . downcase == 'yes' end 0 . upto ( n ) do | i | prefix = i > 0 ? "#{i}." : '' parent_fid = self . fields . delete ( "#{prefix}feature_relations.related_feature.fid" ) next if parent_fid . blank? parent = Feature . get_by_fid ( parent_fid ) if parent . nil? puts "Parent feature with THL #{parent_fid} not found." next end perspective_code = self . fields . delete ( "#{prefix}perspectives.code" ) perspective_name = self . fields . delete ( "#{prefix}perspectives.name" ) perspective = nil if perspective_code . blank? && perspective_name . blank? if ! replace_relations puts "Perspective type is required to establish a relationship between feature #{self.feature.pid} and feature #{parent_fid}." next end else begin perspective = Perspective . get_by_code_or_name ( perspective_code , perspective_name ) rescue Exception => e puts e . to_s end if perspective . nil? puts "Perspective #{perspective_code || perspective_name} was not found." next end end relation_type_str = self . fields . delete ( "#{prefix}feature_relations.type.code" ) relation_type = nil if relation_type_str . blank? if ! replace_relations puts "Feature relation type is required to establish a relationship between feature #{self.feature.pid} and feature #{parent_fid}." next end else relation_type = FeatureRelationType . get_by_code ( relation_type_str ) if relation_type . nil? relation_type = FeatureRelationType . get_by_asymmetric_code ( relation_type_str ) if relation_type . nil? puts "Feature relation type #{relation_type_str} was not found." next else conditions = { :parent_node_id => self . feature . id , :child_node_id => parent . id } end else conditions = { :parent_node_id => parent . id , :child_node_id => self . feature . id } end end conditions . merge! ( :feature_relation_type_id => relation_type . id , :perspective_id => perspective . id ) if ! replace_relations feature_relation = FeatureRelation . where ( conditions ) . first changed = false if feature_relation . nil? feature_relation = FeatureRelation . create ( conditions . merge ( { :skip_update => true } ) ) if feature_relation . nil? put "Failed to create feature relation between #{parent.pid} and #{self.feature.pid}" else changed = true end elsif replace_relations feature_relation . feature_relation_type = relation_type if ! relation_type . nil? feature_relation . perspective = perspective if ! perspective . nil? if feature_relation . changed? feature_relation . skip_update = true feature_relation . save changed = true end end if changed feature_ids_with_changed_relations << parent . id if ! feature_ids_with_changed_relations . include? parent . id feature_ids_with_changed_relations << self . feature . id if ! feature_ids_with_changed_relations . include? self . feature . id end if feature_relation . nil? puts "Couldn't establish relationship #{relation_type_str} between feature #{self.feature.pid} and #{parent_fid}." else second_prefix = "#{prefix}feature_relations" 0 . upto ( 3 ) do | j | third_prefix = j == 0 ? second_prefix : "#{second_prefix}.#{j}" self . add_date ( third_prefix , feature_relation ) self . add_info_source ( third_prefix , feature_relation ) self . add_note ( third_prefix , feature_relation ) end end end return feature_ids_with_changed_relations end
The optional column "feature_relations.related_feature.fid" can specify the THL ID for parent feature.
The optional column " feature_relations . related_feature . fid " can specify the THL ID for parent feature .
n
<not_specific>