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

JM - (SPARCCatalog) Service Linking Import Script Needed [#151553453] #1172

Merged
merged 2 commits into from
Nov 2, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 61 additions & 0 deletions lib/tasks/update_related_services.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright © 2011-2017 MUSC Foundation for Research Development~
# All rights reserved.~

# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:~

# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.~

# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following~
# disclaimer in the documentation and/or other materials provided with the distribution.~

# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products~
# derived from this software without specific prior written permission.~

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,~
# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT~
# SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL~
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS~
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR~
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.~

desc "Updating service relations"
task :update_related_services => :environment do

def prompt(*args)
print(*args)
STDIN.gets.strip
end

def get_file(error=false)
puts "No import file specified or the file specified does not exist in db/imports" if error
file = prompt "Please specify the file name to import from db/imports (must be a CSV, see db/imports/example.csv for formatting): "

while file.blank? or not File.exists?(Rails.root.join("db", "imports", file))
file = get_file(true)
end

file
end

puts ""
puts "Reading in file..."
input_file = Rails.root.join("db", "imports", get_file)
continue = prompt('Preparing to update related services. Are you sure you want to continue? (y/n): ')

if (continue == 'y') || (continue == 'Y')
ActiveRecord::Base.transaction do
CSV.foreach(input_file, headers: true) do |row|
service = Service.where(id: row['Service ID'].to_i).first
related_service_id = row['Related Service ID'].to_i
optional = row['Optional'].to_i
linked_quantity = row['Linked Quantity'].to_i
linked_quantity_total = row['Linked Quantity Total']
if service
puts "created service relation: service_id: #{service.id}, related_service_id: #{related_service_id}, linked_quantity: #{linked_quantity}, linked_quantity_total: null"
service.service_relations.create(related_service_id: related_service_id, optional: optional, linked_quantity: linked_quantity, linked_quantity_total: nil)
service.save
end
end
end
end
end