-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCOUCH_IMPORT.rb
More file actions
193 lines (129 loc) · 4.18 KB
/
Copy pathCOUCH_IMPORT.rb
File metadata and controls
193 lines (129 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# IMPORT PEOPLE
puts "IMPORTING PEOPLE"
Person.destroy_all
CouchPerson.get_all.each do |p|
hash = p.to_hash
q = Instructor.new(hash)
#name = q.name.sub(/[.]/,"").split(" ", 2)
q.slug = hash["_id"]#|| name[0][0].chr.downcase + name[1].sub(/ /,"").underscore
puts q.slug
q.save
puts q.errors if q.errors.length > 0
end
# IMPORT RESOURCES
puts "IMPORTING RESOURCES"
Resource.destroy_all
CouchResource.get_all.each do |r|
hash = r.to_hash
authors = hash["author"].class == String ? hash["author"].to_a : hash["author"]
hash["name"] ||= hash["_id"]
hash["isbn"] = hash["ISBN"]
puts hash["name"].to_s + " (" + hash["type"].to_s + ")"
#hash["old_id"] = hash["_id"]
if hash["ISBN"]
resource = Textbook.new(hash)
authors.each do |a|
resource.authors << Author.new(:name => a)
end if authors && authors.length > 0
resource.save
end
end
# NOTE: COURSES MUST BE IMPORTED AFTER PROFS
# IMPORT COURSES AND COURSE INSTANCES
puts "IMPORTING COURSES"
Course.destroy_all
CourseInstance.destroy_all
CouchCourse.get_all.each do |c|
hash = c.to_hash
hash["calendar_description"] = hash["calendar_entry"]
hash.delete("calendar_entry")
profs = hash["staff"].keys if hash["staff"]
resources = hash["resources"]
main_topics = hash["main_topics"] || hash["main topics"]
puts hash["_id"] #LOG
hash["course_code"] = hash["_id"].split("-")[0].strip #remove '- 2010' classification
hash["delivered_year"] = hash["year"].to_i
# CREATE OR FIND RELATED COURSE
unless course = Course.where(:_id => hash["course_code"][0,6].downcase).limit(1)[0]
course = Course.new
course.name = hash["name"]
course.course_code = hash["course_code"][0,6]
course.save
puts course.errors if course.errors.length > 0
puts course.course_code
end
course_inst = CourseInstance.new(hash)
# PROTECTED ATTRIBUTES
course_inst.delivered_year = hash["delivered_year"]
course_inst.course_code = hash["course_code"]
# CONTACT HOURS
course_inst.contact_hours = ContactHours.new(hash["workload"])
# MAIN TOPICS
main_topics.each do |t|
topic = Topic.new(:name => t)
course_inst.main_topics << topic
end if main_topics && main_topics.length > 0
# ACTIVITIES
activities = hash["activities"]
activities.each do |src_activity|
activity = src_activity[1]["type"].titleize.sub(/ /,'').classify.constantize.new(src_activity[1])
src_activity[1]["outcomes"].each do |outcome|
if outcome[0].length > 0
topic = Topic.new(:name => outcome[0])
activity.topics << topic
end
end
course_inst.activities << activity
end if activities && activities.length > 0
course_inst.save
puts course_inst.errors.inspect if course_inst.errors.length > 0
break if course_inst.errors.length > 0
# LINK TO COURSE
course.course_instances << course_inst
# LINK TO PROFS
profs.each do |id|
prof = Instructor.find(id)
course_inst.instructors << prof
end if profs
# LINK TO RESOURCES
resources.each do |id|
if resource = Resource.where(:isbn => id)[0]
course_inst.resources << resource
end
end if resources
end
# IMPORT COLLECTIONS
puts "IMPORTING COLLECTIONS"
Collection.destroy_all
CouchCollection.get_all.each do |c|
hash = c.to_hash
courses = hash["courses"]
hash["name"] = hash["_id"]
collection = Collection.new(hash)
collection.save
courses.each do |id|
course = Course.where(:course_code => /^#{id[0,6]}/)[0]
collection.courses << course
end if courses
end
# LINK COLLECTIONS to COLLECTIONS
puts "LINKING COLLECTIONS"
CouchCollection.get_all.each do |c|
hash = c.to_hash
collections = hash["collections"]
collection = Collection.where(:name => hash["_id"])[0]
collections.each do |id|
col = Collection.where(:name => id)[0]
collection.child_collections << col
end if collections
end
# INDEX
puts "CLEARING SOLR INDEX"
Sunspot.remove_all
puts "CREATING SOLR INDEX"
Sunspot.index(Instructor.all)
Sunspot.index(Resource.all)
Sunspot.index(CourseInstance.all)
Sunspot.index(Collection.all)
puts "COMMITTING SOLR INDEX"
Sunspot.commit