# Use case to offer the basics to export a register # @note # - You can define methods `filters` and `search` to change the target entries of the register # - You need to define the `process_ooze` method # This case expects `options[:source][:register_id]` class Eco::API::UseCases::OozeSamples::RegisterExportCase < Eco::API::Common::Loaders::UseCase class << self # @return [Integer] the number of pages to be processed in each batch def batch_size(size = nil) @batch_size ||= 25 return @batch_size unless size @batch_size = size end end include Eco::API::UseCases::OozeSamples::Helpers name "register-export-case" type :other attr_reader :session, :options, :usecase attr_reader :target def main(session, options, usecase) options[:end_get] = false @session = session; @options = options; @usecase = usecase @target = nil raise "You need to inherit from this class ('#{self.class}') and call super with a block" unless block_given? with_each_entry do |ooze| process_ooze(ooze) end yield end # Write here your script def process_ooze(ooze = target) raise "You need to inherit from this class ('#{self.class}') and call super with a block" unless block_given? yield(ooze) end private def new_target(object) @target = object end def with_each_entry batched_search_results do |page_results| page_results.each do |page_result| if ooz = build_full_ooze(page_result.id) yield(ooz) end end end end # It builds a full page model (not updatable) # @note If it's a page instance with stages, there's where it will be handy # @param ooze_id [String] # @return [Ecoportal::API::V2::Page] def build_full_ooze(ooze_id) if page = ooze(ooze_id) return page unless page.is_a?(Ecoportal::API::V2::Pages::PageStage) pending_stage_ids = page.stages.ordered.map(&:id) - [page.current_stage_id] state = page.state secs_doc = page.sections.doc flds_doc = page.components.doc hsecs = secs_doc.each_with_object({}) {|sec, h| h[sec["id"]] = sec} hflds = flds_doc.each_with_object({}) {|fld, h| h[fld["id"]] = fld} pending_stage_ids.each do |id| if other_stage = stage(id, ooze: page) state = "inprogress" unless other_stage.state == "complete" other_stage.sections.doc.each do |sec_doc| unless hsecs.key?(id = sec_doc["id"]) hsecs[id] = sec_doc secs_doc << sec_doc end end other_stage.components.doc.each do |comp_doc| unless hflds.key?(id = comp_doc["id"]) hflds[id] = comp_doc flds_doc << comp_doc end end end end Ecoportal::API::V2::Pages::PageStage.new(page.doc).tap do |ozz| ozz.state = state end end end def batched_search_results raise "Missing block. It yields in slices of #{self.class.batch_size} results" unless block_given? results_preview results = [] apiv2.registers.search(register_id, search_options) do |page_result| results << page_result if results.length >= self.class.batch_size yield(results) results = [] end end yield(results) unless results.empty? end def ooze(ooze_id = nil, stage_id: nil) return target unless ooze_id apiv2.pages.get(ooze_id, stage_id: stage_id).tap do |ooze| if ooze new_target(ooze) logger.info("Got #{object_reference(ooze)}") else exit_error "Could not get ooze '#{ooze_id}'" end end end def stage(id_name = nil, ooze: target) if ooze_id = ooze && ooze.id exit_error "#{object_reference(ooze)} does not have stages!" unless ooze.stages? else exit_error "There's no target ooze to get retrieve stages from" end if stg = ooze.stages[id_name] || ooze.stages.get_by_name(id_name) return ooze if ooze.respond_to?(:current_stage_id) && (ooze.current_stage_id == stg.id) return apiv2.pages.get(ooze_id, stage_id: stg.id).tap do |stage| if stage new_target(stage) logger.info("Got #{object_reference(stage)} from #{object_reference(ooze)}") else exit_error "Could not get stage '#{id_name}' in ooze '#{ooze_id}'" end end end exit_error "Stage '#{id_name}' doesn't exist in ooze '#{ooze_id}'" end def default_proceed "Y" end def results_preview apiv2.registers.search(register_id, search_options.merge(only_first: true)).tap do |search_results| str_results = "Total target entries: #{search_results.total} (out of #{search_results.total_before_filtering})" session.prompt_user("Do you want to proceed (y/N):", explanation: str_results, default: default_proceed, timeout: 10) do |res| unless res.upcase.start_with?("Y") puts "..." logger.info "Aborting script..." exit(0) end end end end def search_options @search_options ||= {}.tap do |opts| opts.merge!(sort: "created_at") opts.merge!(dir: "asc") opts.merge!(query: conf_search) if conf_search opts.merge!(filters: conf_filters) end end def conf_filters return filters if self.respond_to?(:filters) [] end def conf_search return search if self.respond_to?(:search) end def register_id options.dig(:source, :register_id) end def apiv2 @apiv2 ||= session.api(version: :oozes) end def exit_error(msg) logger.error(msg) exit(1) end end