Sha256: 8988dc8520bbe5bb2214d507e7f189418206ef9d4864cec4de4a73782c56e8f2

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

require 'dmao/ingesters/errors/generic_ingester'
require 'dmao/ingesters/errors/empty_attributes'
require 'dmao/ingesters/errors/ingest_organisation_unit_error'
require 'dmao/ingesters/errors/link_organisation_unit_error'
require 'dmao/ingesters/generic/ingester'
require 'dmao_api'
require 'time'

module DMAO
  module Ingesters
    module Generic

      class OrganisationIngester < Ingester

        ENTITY = DMAO::API::OrganisationUnit
        ENTITY_ERROR = DMAO::Ingesters::Errors::IngestOrganisationUnitError
        ENTITY_ERROR_MESSAGE = "Invalid organisation unit details"
        ERROR_HANDLING = {
            "DMAO::API::Errors::OrganisationUnitNotFound" => "Organisation unit not found, cannot update organisation unit that does not exist",
            "DMAO::API::Errors::InstitutionNotFound" => "Institution not found, cannot ingest organisation unit to non-existent institution",
            "DMAO::API::Errors::InvalidParentId" => "Parent ID not found in DMA Online, cannot link organisation unit to non-existent parent"
        }

        def link_child_to_parent child_system_uuid, parent_system_uuid

          child_unit = find_unit_or_raise_link_error child_system_uuid, "Organisation unit not found for child system uuid #{child_system_uuid}"
          parent_unit = find_unit_or_raise_link_error parent_system_uuid, "Organisation unit not found for parent system uuid #{parent_system_uuid}"

          attributes = {
              parent_id: parent_unit.id
          }

          update_organisation_unit child_unit.id, attributes

        end

        protected

        def find_unit_or_raise_link_error unit_system_uuid, error_message

          begin
            DMAO::API::OrganisationUnit.find_by_system_uuid unit_system_uuid
          rescue DMAO::API::Errors::OrganisationUnitNotFound
            raise DMAO::Ingesters::Errors::LinkOrganisationUnitError.new(error_message)
          end

        end

      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dmao-generic-ingesters-0.7.0 lib/dmao/ingesters/generic/organisation_ingester.rb
dmao-generic-ingesters-0.6.0 lib/dmao/ingesters/generic/organisation_ingester.rb