From c3d2759aaa0b32adf4814d5ca89d7736bd1391c2 Mon Sep 17 00:00:00 2001 From: Niklaus Giger Date: Wed, 22 Jan 2014 21:48:06 +0100 Subject: [PATCH] Adapt URL for interactions Signed-off-by: Niklaus Giger --- src/state/global.rb | 5 +- src/state/interactions/interaction_chooser.rb | 88 +++++++++++++++++++-------- src/util/http.rb | 2 +- src/util/validator.rb | 3 +- src/view/interactions/interaction_chooser.rb | 34 +++++++++-- src/view/searchbar.rb | 34 +++++++++-- 6 files changed, 127 insertions(+), 39 deletions(-) diff --git a/src/state/global.rb b/src/state/global.rb index 6930c4c..4b820fd 100644 --- a/src/state/global.rb +++ b/src/state/global.rb @@ -183,12 +183,12 @@ module ODDB :home_doctors => State::Doctors::Init, :home_hospitals => State::Hospitals::Init, :home_drugs => State::Drugs::Init, - :home_interactions => State::Interactions::Init, :home_migel => State::Migel::Init, :home_substances => State::Substances::Init, :home_user => State::User::Init, :hospitallist => State::Hospitals::HospitalList, :interaction_chooser => State::Interactions::InteractionChooser, + :home_interactions => State::Interactions::Init, :limitation_text => State::Drugs::LimitationText, :limitation_texts => State::Drugs::LimitationTexts, :limitation_analysis => State::Analysis::LimitationText, @@ -624,7 +624,8 @@ module ODDB end end - def interaction_search + def home_interactions + $stdout.puts "home_interactions" State::Interactions::InteractionChooser.new(@session, nil) end diff --git a/src/state/interactions/interaction_chooser.rb b/src/state/interactions/interaction_chooser.rb index 8fb04da..47db9d1 100644 --- a/src/state/interactions/interaction_chooser.rb +++ b/src/state/interactions/interaction_chooser.rb @@ -8,49 +8,87 @@ require 'view/interactions/interaction_chooser' module ODDB module State module Interactions + class InteractionChooserDrug < Global VIEW = View::Interactions::InteractionChooserDrugDiv VOLATILE = true end + class InteractionChooser < State::Interactions::Global DIRECT_EVENT = :interaction_chooser VIEW = View::Interactions::InteractionChooser @@ean13_form = /^(7680)(\d{5})(\d{3})(\d)$/u + + def handle_drug_changes(drugs, msg) + path = @session.request_path + $stdout.puts "handle_drug_changes #{msg} path #{path.class} #{path.inspect} drugs #{drugs.inspect}" + @session.set_persistent_user_input(:drugs, drugs) + uri = @session.lookandfeel._event_url(:home_interactions, []) + first = true + drugs.each{|ean, pack| + if first + first = false + uri += pack.barcode + else + uri += ",#{pack.barcode}" + end + } + $stdout.puts "handle_drug_changes #{@session.request_path.inspect} => uri #{uri.class} #{uri.inspect}" + # @session.process(request) unless uri.eql?(request.to_s) + end + def init - $stdout.puts "State::Interactions::InteractionChooser init for #{@session.user_input(:search_query).inspect} and request_path #{@session.request_path.inspect}" - # http://oddb-ci2.dyndns.org/de/gcc/interaction_search/atc/C07AA05,N02BA01 + ean13 = @session.user_input(:search_query) + $stdout.puts "State::Interactions::InteractionChooser init ean13 #{@session.user_input(:search_query).inspect} and request_path #{@session.request_path.inspect}" path = @session.request_path - search_code = path.split('interaction_search/')[1] + search_code = path.split('home_interactions/')[1] + drugs = {} if search_code - kind = search_code.split('/')[0] - item_list = search_code.split('/')[1] - # we must pass ean => atc_class - # e.g View::Interactions::InteractionChooserDrug: init drugs {"7680390530474"=>#, "7680583920112"=>#} - # bin/admin: search_by_atc("N02BA01").first.packages.first.barcode - drugs = {} - item_list.split(',').each { + items = search_code.split(',') + # new approach unified + # http://oddb-ci2.dyndns.org/de/gcc/home_interactions/51795,C07AA05,7680583920112 + # Beispiel von Mepha. Losartan, Teva, Novaldex und Paroxetin + # http://matrix.epha.ch/#/56751,61537,39053,59256 + # http://oddb-ci2.dyndns.org/de/gcc/home_interactions/56751,61537,39053,59256 + $stdout.puts "State::Interactions::InteractionChooser items #{items.inspect}" + items.each{ |item| - $stdout.puts "search_code item #{item.inspect} with #{@session.app.atc_class(item).inspect}" - next unless atc = @session.app.atc_class(item) - $stdout.puts "search_code item #{atc.packages.first.inspect} with #{atc.packages.first.barcode.inspect}" - next unless atc.packages.first and atc.packages.first.barcode - drugs[atc.packages.first.barcode] = atc.packages.first + if item.kind_of?(String) and item.length == 5 # it is an iksrn + $stdout.puts "search_code iksnr #{item} " + registration = @session.app.registration(item) + pack = registration.packages.first + $stdout.puts "search_code item #{pack.inspect}" + next unless pack + drugs[pack.barcode] = pack + elsif item.kind_of?(String) and item.length == 7 # it is an ATC code + $stdout.puts "search_code ATC #{item}" + next unless atc = @session.app.atc_class(item) + $stdout.puts "search_code item #{atc.packages.first.inspect} with #{atc.packages.first.barcode.inspect}" + next unless atc.packages.first and atc.packages.first.barcode + drugs[atc.packages.first.barcode] = atc.packages.first + elsif item.kind_of?(String) and item.length == 13 # it is an barcode/ean13 + $stdout.puts "search_code barcode #{item} " + next unless item + pack = package_for(item) + next unless pack + drugs[item] = pack + else + $stdout.puts "unhandled item #{item.inspect}" + end } - $stdout.puts "search_code kind #{kind.inspect} with items #{item_list.inspect} => drugs #{drugs.inspect}" - @session.set_persistent_user_input(:drugs, drugs) + $stdout.puts "search_code items #{items.inspect} => drugs #{drugs.inspect}" + handle_drug_changes(drugs, 'init') else if @session.event.to_sym == self.class::DIRECT_EVENT - @session.set_persistent_user_input(:drugs, {}) + handle_drug_changes({}, 'init DIRECT_EVENT') end # from centeredsearchform if ean13 = @session.user_input(:search_query) check_model unless error? - pack = package_for(ean13.to_s) drugs = @session.persistent_user_input(:drugs) || {} - drugs[ean13] = pack unless drugs.has_key?(ean13) - $stdout.puts "State::Interactions::InteractionChooser init set_persistent_user_input #{drugs.inspect}}" - @session.set_persistent_user_input(:drugs, drugs) + drugs[ean13] = package_for(ean13) + handle_drug_changes(drugs, 'init: added ean13') end end end @@ -63,7 +101,7 @@ class InteractionChooser < State::Interactions::Global pack = package_for(ean13) drugs = @session.persistent_user_input(:drugs) || {} drugs[ean13] = pack unless drugs.has_key?(ean13) - @session.set_persistent_user_input(:drugs, drugs) + handle_drug_changes(drugs, 'ajax_add_drug') end end InteractionChooserDrug.new(@session, @model) @@ -74,14 +112,14 @@ class InteractionChooser < State::Interactions::Global if ean13 = @session.user_input(:ean).to_s drugs = @session.persistent_user_input(:drugs) || {} drugs.delete(ean13) - @session.set_persistent_user_input(:drugs, drugs) + handle_drug_changes(drugs, 'ajax_delete_drug') end end InteractionChooserDrug.new(@session, @model) end def delete_all unless error? - @session.set_persistent_user_input(:drugs, {}) + handle_drug_changes({}, 'delete_all') @model = [] end self.http_headers = { diff --git a/src/util/http.rb b/src/util/http.rb index 6baf569..c5883f1 100644 --- a/src/util/http.rb +++ b/src/util/http.rb @@ -95,7 +95,7 @@ module ODDB warn(sprintf("redirecting to: %s", path)) get(path) else - raise("could not connect to #{@http_server}: #{resp}") + raise("could not connect to #{@http_server} #{path}: #{resp}") end rescue Errno::ECONNRESET, EOFError if(retries > 0) diff --git a/src/util/validator.rb b/src/util/validator.rb index 0b2f940..bd8a9a9 100644 --- a/src/util/validator.rb +++ b/src/util/validator.rb @@ -208,8 +208,8 @@ module ODDB :home_doctors, :home_drugs, :home_hospitals, + :home_interactions, :home_migel, - :home_interactions, :home_substances, :home_user, :hospital, @@ -219,7 +219,6 @@ module ODDB :interaction_basket, :interaction_chooser, :interaction_detail, - :interaction_search, :interactions, :legal_note, :limitation_analysis, diff --git a/src/view/interactions/interaction_chooser.rb b/src/view/interactions/interaction_chooser.rb index 7505c96..93980ec 100644 --- a/src/view/interactions/interaction_chooser.rb +++ b/src/view/interactions/interaction_chooser.rb @@ -34,7 +34,8 @@ module ODDB atc_codes = [] if drugs and !drugs.empty? drugs.each{ |ean, drug| - atc_codes << drug.atc_class.code + $stdout.puts "calculate_atc_codes #{ean.inspect} #{drug.inspect}" + atc_codes << drug.atc_class.code if drug and drug.atc_class } end @@atc_codes = atc_codes @@ -44,6 +45,7 @@ module ODDB end def self.get_interactions(my_atc_code, session, atc_codes=@@atc_codes) results = [] + $stdout.puts "View::Interactions: get_interactions atc_codes #{atc_codes.inspect}" idx=atc_codes.index(my_atc_code) atc_codes[0..idx].combination(2).to_a.each { |combination| @@ -69,6 +71,7 @@ module ODDB } } } + $stdout.puts "View::Interactions: get_interactions results #{results.uniq.inspect}" results.uniq end class InteractionChooserDrugHeader < HtmlGrid::Composite @@ -119,7 +122,7 @@ class InteractionChooserDrugHeader < HtmlGrid::Composite div = HtmlGrid::Div.new(model, @session, self) div.set_attribute('class', 'interaction-atc') div.value = [] - div.value << model.atc_class.code + ': ' + model.atc_class.name + div.value << model.atc_class.code + ': ' + model.atc_class.name if model.atc_class div end @@ -142,7 +145,10 @@ class InteractionChooserDrug < HtmlGrid::Composite CSS_MAP = {} CSS_CLASS = 'composite' def init + ean13 = @session.user_input(:search_query) + path = @session.request_path @drugs = @session.persistent_user_input(:drugs) + $stdout.puts "View::Interactions::InteractionChooserDrug: init ean13 #{ean13.inspect} path #{path.inspect} drugs #{@drugs.inspect}" if @model.is_a? ODDB::Package components.store([0,0], :header_info) css_map.store([0,0], 'subheading') @@ -151,6 +157,19 @@ class InteractionChooserDrug < HtmlGrid::Composite end @attributes.store('id', 'drugs_' + @model.barcode) end + self.onsubmit = <<-JS +function get_to(url) { + var form = document.createElement("form"); + form.setAttribute("method", "GET"); + form.setAttribute("action", url); + document.body.appendChild(form); + form.submit(); +} +var url = searchbar.baseURI + 'home_interactions/' + ean13; +window.location = url; +get_to(href); +return false; + JS super end def header_info(model, session=@session) @@ -159,6 +178,8 @@ class InteractionChooserDrug < HtmlGrid::Composite def text_info(model, session=@session) div = HtmlGrid::Div.new(model, @session, self) # the first element cannot have an interaction + $stdout.puts "View::Interactions::InteractionChooserDrug: text_info index #{ODDB::View::Interactions.atc_codes(@session).index(model.atc_class).inspect}" + return div unless ODDB::View::Interactions.atc_codes(@session).index(model.atc_class) return div if ODDB::View::Interactions.atc_codes(@session).index(model.atc_class.code) == 0 div.set_attribute('class', 'interaction-info') div.value = [] @@ -166,6 +187,7 @@ class InteractionChooserDrug < HtmlGrid::Composite list.value = [] ODDB::View::Interactions.get_interactions(model.atc_class.code, @session).each { |interaction| + $stdout.puts "View::Interactions::InteractionChooserDrug: headerDiv interaction #{interaction.inspect}" headerDiv = HtmlGrid::Div.new(model, @session, self) headerDiv.value = [] headerDiv.value << interaction[:header] @@ -193,9 +215,11 @@ class InteractionChooserDrugList < HtmlGrid::List SORT_HEADER = false def initialize(model, session=@session, arg_self=nil) @drugs = session.persistent_user_input(:drugs) + $stdout.puts "View::Interactions::InteractionChooserDrugList: drugs 1 #{@drugs.inspect}" super # must come first or it will overwrite @value @value = [] ODDB::View::Interactions.calculate_atc_codes(@drugs) + $stdout.puts "View::Interactions::InteractionChooserDrugList: drugs 2 #{@drugs.inspect}" if @drugs and !@drugs.empty? @drugs.each{ |ean, drug| @value << InteractionChooserDrug.new(drug, @session, self) @@ -209,12 +233,14 @@ class InteractionChooserDrugDiv < HtmlGrid::Div super @value = [] @drugs = @session.persistent_user_input(:drugs) + $stdout.puts "View::Interactions::InteractionChooserDrugDiv: drugs #{@drugs.inspect}" if @drugs and !@drugs.empty? @value << InteractionChooserDrugList.new(@drugs, @session, self) end end def to_html(context) div = HtmlGrid::Div.new(@model, @session, self) + $stdout.puts "View::Interactions::InteractionChooserDrugDiv: to_html drugs #{@drugs.inspect}" if @drugs and !@drugs.empty? delete_all_link = HtmlGrid::Link.new(:delete, @model, @session, self) delete_all_link.href = @lookandfeel._event_url(:delete_all, []) @@ -314,9 +340,9 @@ class InteractionChooser < View::PrivateTemplate def backtracking(model, session=@session) fields = [] fields << @lookandfeel.lookup(:th_pointer_descr) - link = HtmlGrid::Link.new(:home_interaction, model, @session, self) + link = HtmlGrid::Link.new(:home_interactions, model, @session, self) link.css_class = "list" - link.href = @lookandfeel._event_url(:home_interaction, []) + link.href = @lookandfeel._event_url(:home_interactions, []) link.value = @lookandfeel.lookup(:home) fields << link fields << ' - ' diff --git a/src/view/searchbar.rb b/src/view/searchbar.rb index dc0a6fa..7b7bac4 100644 --- a/src/view/searchbar.rb +++ b/src/view/searchbar.rb @@ -82,8 +82,16 @@ function selectXhrRequest() { var searchbar = dojo.byId('#{id}'); if(!popup.style.overflowX.match(/auto/) && searchbar.value != '') { #{progressbar} - xhrGet(searchbar.value); + // xxxx + var ean13 = (searchbar.value.match(/^(\\d{13})$/)||[])[1]; + var path = searchbar.baseURI; searchbar.value = ''; + var found = path.match(/home_interactions/); + if(found && ean13) { + var url = path + ',' + ean13; + console.log('new url ' + url); + window.location = url; + } } } require(['dojo/ready'], function(ready) { @@ -132,6 +140,8 @@ class SearchBar < HtmlGrid::InputText 'onBlur' => "if (value=='') { value='#{val}' };", 'id' => "searchbar", }) + $stdout.puts "init @session.event == :interaction_chooser val ist #{val}"; $stdout.flush + $stdout.puts "init @session.zone #{@session.zone.inspect} @name#{@name.inspect}"; $stdout.flush if @session.event == :interaction_chooser # remove ean13 text @attributes.update({'value' => val}) end @@ -149,14 +159,28 @@ function get_to(url) { document.body.appendChild(form); form.submit(); } + +console.log('bin in searchbar') if (#{@name}.value!='#{val}') { #{timer} var href = '#{submit}' + encodeURIComponent(#{@name}.value.replace(/\\//, '%2F')); - if (this.search_type) { - href += '/search_type/' + this.search_type.value#{param}; + console.log('href ist: ' + href) + if (this.id == 'interaction_chooser_searchbar') { + var url = searchbar.baseURI; + url += '/home_interactions/' + this.value + // get_to(href); + console.log('url ist: ' + url) + window.location = url + console.log('window.location ist: ' + window.location) + } else { + if (this.search_type) { + href += '/search_type/' + this.search_type.value#{param}; + } + console.log('goto href: ' + href) + get_to(href); } - get_to(href); }; +console.log('return false: ') return false; JS end @@ -233,7 +257,7 @@ require(['dojo/ready'], function(ready) { html << super(context) end end -class InteractionSearchBar < AutocompleteSearchBar # home_interaction +class InteractionSearchBar < AutocompleteSearchBar # home_interactions def init @searchbar_id = 'interaction_searchbar' @label_attr = 'drug' -- 1.8.5.2