load_location_data.rb
1 #!/bin/bash 2 # 3 # Run with ./scripts/load_location_data.rb 4 5 "exec" "`dirname $0`/jruby" "$0" "$@" 6 7 $: << File.join(File.dirname(__FILE__), "..", "common") 8 9 # Number of locations to generate 10 BUILDING_COUNT = 100 11 LOCATIONS_PER_BUILDING = 5 12 13 SAMPLE_LOCATION_PROFILES = [ 14 {:name => 'record carton shelf (Remarque)', :depth => '15.5', :height => '10.75', :width => '39.25'}, 15 {:name => 'oversize - short - TAM', :depth => '31.75', :height => '6.5', :width => '39.5'}, 16 {:name => 'oversize - short - TAM 2', :depth => '31.5', :height => '6.5', :width => '27.5'}, 17 {:name => '2 deep - 3 wide - 1 high', :depth => '31.75', :height => '11.75', :width => '39.5'}, 18 {:name => '2 deep - 3 wide - 2 high', :depth => '31.75', :height => '22.75', :width => '39.5'}, 19 {:name => 'oversize - short', :depth => '41.5', :height => '10.75', :width => '29.25'}, 20 {:name => 'cd shelf 1', :depth => '7', :height => '11', :width => '41.5'}, 21 {:name => 'cd shelf 2', :depth => '8.5', :height => '12', :width => '29.5'}, 22 {:name => 'Fales Media Storage 1', :depth => '15', :height => '16.75', :width => '34'}, 23 {:name => 'Fales Media Storage 2', :depth => '15', :height => '12.75', :width => '34'}, 24 {:name => 'map case 2', :depth => '38', :height => '1.75', :width => '50'}, 25 {:name => 'map case 3', :depth => '40.5', :height => '0.75', :width => '49.5'}, 26 {:name => 'CSQ standard compact shelf', :depth => '15.5', :height => '11', :width => '27.75'}, 27 {:name => 'CSQ extra high compact shelf with set back', :depth => '15.5', :height => '17.25', :width => '27.75'}, 28 {:name => 'CSQ extra high compact shelf with no set back', :depth => '15.5', :height => '17.25', :width => '27.75'}, 29 {:name => 'UA Shelving (varies)', :depth => '15', :height => '12', :width => '35.5'}, 30 {:name => 'Fales 10W - 2 wide', :depth => '15.5', :height => '11.25', :width => '27.75'}, 31 ] 32 33 SAMPLE_CONTAINER_PROFILES = [ 34 {:name => 'Paige 15', :depth => '15.5', :height => '10.5', :width => '13', :dimension_units => 'inches', :extent_dimension => 'width'}, 35 {:name => 'archive legal', :depth => '15.5', :height => '10.25', :width => '5', :dimension_units => 'inches', :extent_dimension => 'width'}, 36 {:name => 'archive letter', :depth => '12.5', :height => '10.25', :width => '5', :dimension_units => 'inches', :extent_dimension => 'width'}, 37 {:name => 'archive half legal', :depth => '15.5', :height => '10.25', :width => '2.5', :dimension_units => 'inches', :extent_dimension => 'width'}, 38 {:name => 'Flat Box', :width => '15', :height => '3', :depth => '18.5', :dimension_units => 'inches', :extent_dimension => 'width'}, 39 {:name => 'Oversize Folder - half case', :depth => '35.75', :height => '0.15', :width => '23.75', :dimension_units => 'inches', :extent_dimension => 'width'}, 40 {:name => 'Oversize Folder - full case', :depth => '35.75', :height => '0.15', :width => '48', :dimension_units => 'inches', :extent_dimension => 'width'}, 41 {:name => 'CD ', :width => '0.4', :height => '4.92', :depth => '5.59', :dimension_units => 'inches', :extent_dimension => 'width'}, 42 {:name => 'DVD ', :width => '0.55', :height => '7.6', :depth => '5.4', :dimension_units => 'inches', :extent_dimension => 'width'} 43 ] 44 45 46 # Some packings that will mostly fill the different location profile types. 47 # 48 # Of the form: 49 # 50 # { 'location profile name' => [packing1, packing2, packing3] } 51 # 52 # where each 'packing' is a list of pairs like: 53 # 54 # ['container profile name', number_of_boxes] 55 # 56 # 57 SAMPLE_LOCATION_PROFILE_PACKINGS = { 58 "record carton shelf (Remarque)" => 59 [[["archive half legal", 6], ["Paige 15", 1], ["archive legal", 2]], 60 [["archive half legal", 15]]], 61 62 "oversize - short - TAM" => 63 [[["Flat Box", 4]]], 64 65 "oversize - short - TAM 2" => 66 [[["Flat Box", 2], ["CD ", 23]]], 67 68 "2 deep - 3 wide - 1 high" => 69 [[["Paige 15", 6]], 70 [["archive legal", 4], ["archive letter", 8], ["archive half legal", 6]]], 71 72 "2 deep - 3 wide - 2 high" => 73 [[["Flat Box", 8], ["archive half legal", 12]]], 74 75 "oversize - short" => 76 [[["Oversize Folder - half case", 29]]], 77 78 "cd shelf 1" => 79 [[["CD ", 90]]], 80 81 "cd shelf 2" => 82 [[["DVD ", 42]]], 83 84 "Fales Media Storage 1" => 85 [[["archive letter", 6]]], 86 87 "Fales Media Storage 2" => 88 [[["archive letter", 6]]], 89 90 "map case 2" => 91 [[["Oversize Folder - full case", 9]]], 92 93 "map case 3" => 94 [[["Oversize Folder - half case", 12]]], 95 96 "CSQ standard compact shelf" => 97 [[["Paige 15", 1], ["archive legal", 2]]], 98 99 "CSQ extra high compact shelf with set back" => 100 [[["archive half legal", 11]]], 101 102 "CSQ extra high compact shelf with no set back" => 103 [[["Paige 15", 2]]], 104 105 "UA Shelving (varies)" => 106 [[["archive letter", 7]]], 107 108 "Fales 10W - 2 wide" => 109 [[["Paige 15", 1], ["archive letter", 2]]]} 110 111 # Place names 112 PLACES = java.util.TimeZone.getAvailableIDs.map {|z| z.to_s.split('/')[1]}.compact.reject {|p| p =~ /[A-Z]{3}/}.map {|p| p.sub('_', ' ')}.uniq 113 114 # Suffixes 115 LOCATION_SUFFIXES = ['Square', 'Gardens', 'Library', 'Museum', 'Warehouse', 'Treehouse', 'Bunker'] 116 117 118 require 'jsonmodel' 119 require 'securerandom' 120 121 JSONModel::init(:client_mode => true, :url => 'http://localhost:4567') 122 JSONModel.set_repository(2) 123 124 include JSONModel 125 126 def login(username, password) 127 response = JSONModel::HTTP.post_form(JSONModel(:user).uri_for("#{username}/login"), :password => password) 128 JSONModel::HTTP.current_backend_session = ASUtils.json_parse(response.body)['session'] 129 end 130 131 def create_location_profiles 132 SAMPLE_LOCATION_PROFILES.map do |location| 133 p [:creating, location] 134 profile_id = JSONModel(:location_profile).from_hash(location).save 135 136 { 137 :name => location[:name], 138 :uri => JSONModel(:location_profile).uri_for(profile_id) 139 } 140 end 141 end 142 143 def create_locations(profiles) 144 result = [] 145 combinations = PLACES.flat_map {|place| LOCATION_SUFFIXES.map {|suffix| "#{place} #{suffix}"}} 146 147 raise "Not enough location names" if combinations.length < BUILDING_COUNT 148 149 combinations.shuffle.take(BUILDING_COUNT).each do |building| 150 LOCATIONS_PER_BUILDING.times do |i| 151 location_profile = profiles.sample 152 location_id = JSONModel(:location).from_hash({:building => building, 153 :floor => (i % 2).to_s, 154 :room => (i % 2).to_s, 155 :area => (i % 2).to_s, 156 157 :coordinate_1_label => 'shelf', 158 :coordinate_1_indicator => i.to_s, 159 160 :barcode => SecureRandom.hex, 161 :location_profile => { 162 'ref' => location_profile[:uri] 163 } 164 }) 165 .save 166 167 puts "Created location #{building}: #{JSONModel(:location).uri_for(location_id)}" 168 169 result << { 170 :location_uri => JSONModel(:location).uri_for(location_id), 171 :location_profile => location_profile 172 } 173 end 174 end 175 176 result 177 end 178 179 def create_container_profiles 180 result = {} 181 182 SAMPLE_CONTAINER_PROFILES.each do |container_profile| 183 container_profile_id = JSONModel(:container_profile).from_hash(container_profile).save 184 185 puts "Created container profile #{container_profile[:name]}: #{JSONModel(:container_profile).uri_for(container_profile_id)}" 186 187 result[container_profile[:name]] = JSONModel(:container_profile).uri_for(container_profile_id) 188 end 189 190 result 191 end 192 193 # Generate the appropriate container records for each location of interest 194 def fill_locations_with_containers(locations, container_profiles) 195 locations.each do |location| 196 p [:location, location] 197 location_profile_name = location[:location_profile][:name] 198 packing = SAMPLE_LOCATION_PROFILE_PACKINGS.fetch(location_profile_name).sample 199 200 packing.each do |container_type, count| 201 container_profile_uri = container_profiles.fetch(container_type) 202 203 count.times do 204 top_container_id = JSONModel(:top_container).from_hash(:indicator => SecureRandom.hex, 205 :barcode => SecureRandom.hex, 206 :container_profile => {'ref' => container_profile_uri}, 207 :container_locations => [ 208 {'ref' => location[:location_uri], 209 'status' => 'current', 210 'start_date' => '2000-01-01'} 211 ]).save 212 213 puts "Created top container with ID: #{top_container_id}" 214 end 215 end 216 end 217 end 218 219 220 def main(username = 'admin', password = 'admin') 221 login(username, password) 222 223 profiles = create_location_profiles 224 locations = create_locations(profiles) 225 container_profiles = create_container_profiles 226 fill_locations_with_containers(locations, container_profiles) 227 end 228 229 main