module RailsWorkflow
  class ProcessImporter
    def initialize json
      @json = json['process_template']
    end

    def process
      process = ProcessTemplate.
          where(uuid: @json['uuid']).first_or_create!

      @json['child_processes'] && @json['child_processes'].each do |child_process|
        ProcessImporter.new({"process_template" => child_process}).process
      end

      process.attributes = @json.except('operations', 'child_processes')
      process.save

      operations = []
      ids_to_delete = process.operations.pluck(:id)

      @json['operations'].each do |operation_json|

        operation = process.
            operations.where(uuid: operation_json['uuid']).first_or_create!

        operation.attributes = operation_json.except('child_process')
        if (operation_json['child_process'].present?)
          child_template = ProcessTemplate.find_by_uuid(operation_json['child_process'])
          raise ActiveRecord::RecordNotFound, "Operation #{operation.title} child process template not found by UUID" if child_template.blank?
          operation.child_process = child_template
        end
        operation.save

        ids_to_delete.delete(operation.id)
        operations << operation
      end


      OperationTemplate.delete(ids_to_delete) if ids_to_delete.present?


      operations.each do |operation|
        operation.dependencies.each do |d|
          d['id'] = OperationTemplate.
              find_by_uuid(d['uuid']).try(:id)

          d.delete("uuid")
        end

        operation.save
      end


    end
  end
end


module RailsWorkflow
  class ProcessImporter
    def initialize json
      if json['operations']
        json['process_template']['operations'] = json['operations']
      end
      @json = json['process_template']
    end

    def process
      process = ProcessTemplate.
          where(uuid: @json['uuid']).first_or_create!

      @json['child_processes'] && @json['child_processes'].each do |child_process|
        ProcessImporter.new({"process_template" => child_process}).process
      end

      process.attributes = @json.except('operations', 'child_processes')
      process.save

      operations = []
      ids_to_delete = process.operations.pluck(:id)

      @json['operations'].each do |operation_json|

        operation = process.
            operations.where(uuid: operation_json['uuid']).first_or_create!

        operation.attributes = operation_json.except('child_process')
        if (operation_json['child_process'].present?)
          child_template = ProcessTemplate.find_by_uuid(operation_json['child_process'])
          raise ActiveRecord::RecordNotFound, "Operation #{operation.title} child process template not found by UUID" if child_template.blank?
          operation.child_process = child_template
        end
        operation.save

        ids_to_delete.delete(operation.id)
        operations << operation
      end


      OperationTemplate.delete(ids_to_delete) if ids_to_delete.present?


      operations.each do |operation|
        operation.dependencies.each do |d|
          d['id'] = OperationTemplate.
              find_by_uuid(d['uuid']).try(:id)

          d.delete("uuid")
        end

        operation.save
      end


    end
  end
end

Allow for operations to be embedded alongside (instead of inside) process templates in JSON exports of a Process
Allow for operations to be embedded alongside (instead of inside) process templates in JSON exports of a Process
