Skip to content

Commit

Permalink
Merge pull request #904 from unepwcmc/feat/cites-trade-eu-query
Browse files Browse the repository at this point in the history
Cites trade eu query
  • Loading branch information
lucacug authored Dec 16, 2022
2 parents a15f582 + 9194463 commit 1ad3c2b
Show file tree
Hide file tree
Showing 15 changed files with 227 additions and 62 deletions.
108 changes: 56 additions & 52 deletions app/assets/javascripts/cites_trade/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ $(document).ready(function(){

var ajaxFail, initExpctyImpcty, initTerms, initSources, initPurposes,
countries = {}, units = {}, terms = {}, purposes = {}, sources = {},
euId = '',
selected_taxa = '',
is_search_page = $('#form_expert').length > 0,
is_download_page = $('#net_gross_options').length > 0,
Expand All @@ -19,14 +20,6 @@ $(document).ready(function(){
$.jGrowl(text);
};

function growlMeSticky(text){
$.jGrowl(text, {sticky: true});
};

function notyNormal(message){
noty({layout: 'topRight', text: message, timeout: 4000});
};

function notySticky(message){
noty({ layout: 'top',
type: 'information',
Expand Down Expand Up @@ -75,17 +68,6 @@ $(document).ready(function(){
$('input,select').keypress(function(event) { return event.keyCode != 13; });
};

function fixTaxonId (arr) {
return _.map(arr, function (obj) {
if (obj.name === 'taxon_concepts_ids[]') {
return {name: 'taxon_concepts_ids[]', value: selected_taxa};
} else {
return obj;
}
});
}


function parseInputs ($inputs) {
var values = {};
$inputs.each(function() {
Expand All @@ -104,6 +86,7 @@ $(document).ready(function(){

function getParamsFromInputs(){
var values = parseInputs($('#form_expert :input'));

return $.param({'filters': values});
}

Expand Down Expand Up @@ -183,6 +166,7 @@ $(document).ready(function(){
$('#reset_search').click(function() {
resetSelects();
show_values_selection();
setEuDisclaimerVisibility();
// Removing the table results on reset
$("#query_results_table").find('thead,tbody').remove();
$('#query_results').find('p.info').text('');
Expand All @@ -202,17 +186,6 @@ $(document).ready(function(){
hoverColor: '#D2EF9A'
});


function getSelectionTextNew(source) {
var values = [];

$('#ms-' + source).find('div.ms-selection ul.ms-list li').each(function() {
values.push($(this).text());
});

return values.join(',')
}

function getSelectionText(source) {
myValues = new Array();
$('#' + source + ' option:selected').each(function(index, value) {
Expand All @@ -231,6 +204,10 @@ $(document).ready(function(){
initCountriesObj = function (data) {
_.each(data.geo_entities, function (country) {
countries[country.id] = country;

if (country.iso_code2 == 'EU') {
euId = country.id.toString()
}
});
unLock('initCountriesObj');
}
Expand Down Expand Up @@ -318,6 +295,7 @@ $(document).ready(function(){
selection = getText(new_array);
}
$('#expcty_out').text(selection);
setEuDisclaimerVisibility();
});

populateSelect(_.extend(args, {
Expand Down Expand Up @@ -346,6 +324,7 @@ $(document).ready(function(){
selection = getText(new_array);
}
$('#impcty_out').text(selection);
setEuDisclaimerVisibility();
});
};

Expand Down Expand Up @@ -492,11 +471,6 @@ $(document).ready(function(){
function show_values_selection() {
var year_from = $('#qryFrom').val();
var year_to = $('#qryTo').val();
var exp_cty = $('#expctyms2side__dx').text();
var imp_cty = $('#impctyms2side__dx').text();
var sources = $('#sourcesms2side__dx').text();
var purposes = $('#purposesms2side__dx').text();
var terms = $('#termsms2side__dx').text();

$('#year_from > span').text(year_from);
$('#year_to > span').text(year_to);
Expand All @@ -508,6 +482,30 @@ $(document).ready(function(){
$('#genus_all_id').val();
};

function setEuDisclaimerVisibility () {
['imp', 'exp'].forEach(function (type) {
const disclaimerEl = $('#eu_disclaimer_' + type)

if (disclaimerEl.length) {
hasEuDisclaimer(type) ? disclaimerEl.show() : disclaimerEl.hide()
}
})
}

function hasEuDisclaimer (type) {
const selections = $('#'+ type + 'cty').val()

if (!selections) {
return false
}

return isEuInArray(selections)
}

function isEuInArray (array) {
return array.indexOf(euId) >= 0
}

$('#side .ui-button, #form .ui-button').hover(function() {
$(this).toggleClass('ui-state-hover');
});
Expand Down Expand Up @@ -541,18 +539,6 @@ $(document).ready(function(){
return taxonDisplayName.replace(new RegExp("(" + term + '|' + termWithHyphens+ ")", "gi"), transform);
}

function parseTaxonData (data, term, showSpp) {
var d = data.auto_complete_taxon_concepts;
return _.map(d, function (element, index) {
var displayName = getTaxonDisplayName(element, showSpp)
return {
'value': element.id,
'label': displayName,
'drop_label': getTaxonLabel(displayName, term)
};
});
}

function parseTaxonCascadeData(data, term, showSpp) {
var d = data.auto_complete_taxon_concepts;
var data_by_rank = [];
Expand Down Expand Up @@ -642,14 +628,32 @@ $(document).ready(function(){
}

show_values_selection();
setEuDisclaimerVisibility();

$('#qryFrom, #qryTo').on('change',function() {
var y_from = $('#qryFrom').val();
var y_to = $('#qryTo').val();
$('#year_from > span').text(y_from);
$('#year_to > span').text(y_to);
$('#qryFrom, #qryTo').on('change', function(e) {
year_range = handleYearRangeChange(e.target.id)

$('#year_from > span').text(year_range[0])
$('#year_to > span').text(year_range[1])
});

function handleYearRangeChange (id) {
var y_from = $('#qryFrom').val()
var y_to = $('#qryTo').val()

if (y_from > y_to) {
if (id === 'qryFrom') {
y_to = y_from
$('#qryTo').val(y_to)
} else {
y_from = y_to
$('#qryFrom').val(y_from)
}
}

return [y_from, y_to]
}

//Put functions to be executed here
initialiseControls();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@
{{/with}}
</div>

<div {{bind-attr class=":block :add controllers.taxonConcept.anyHistoricCitesProcesses citesProcessesExpanded:active"}} id="cites_processes">

<!-- TODO uncomment this section when RST can go live -->
<!-- <div {{bind-attr class=":block :add controllers.taxonConcept.anyHistoricCitesProcesses citesProcessesExpanded:active"}} id="cites_processes">
{{#with controllers.taxonConcept}}
{{ partial 'species/taxon_concept/cites_processes' }}
{{/with}}
</div>
</div> -->

<hr />

Expand Down
4 changes: 4 additions & 0 deletions app/assets/stylesheets/cites_trade/citestrade.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ font-weight:700;
padding: 3px 3px;
}

.select_notes {
margin-top: 10px;
}

.banner {
background-color: #ac9e89;
width: 95%;
Expand Down
15 changes: 15 additions & 0 deletions app/models/eu_country_date.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class EuCountryDate < ActiveRecord::Base
attr_accessible :eu_accession_year, :eu_exit_year, :geo_entity

belongs_to :geo_entity
validates :geo_entity, :eu_accession_year, :presence => true
validate :is_country

private

def is_country
unless self.geo_entity.is_country?
error.add(:geo_entity, "Must be a country")
end
end
end
1 change: 1 addition & 0 deletions app/models/geo_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class GeoEntity < ActiveRecord::Base
has_many :document_citation_geo_entities, dependent: :destroy
has_many :users
has_many :cites_processes
has_many :eu_country_dates
validates :geo_entity_type_id, :presence => true
validates :iso_code2, :uniqueness => true, :allow_blank => true
validates :iso_code2, :presence => true, :length => { :is => 2 },
Expand Down
2 changes: 1 addition & 1 deletion app/models/geo_entity_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class GeoEntityType < ActiveRecord::Base
"1" => [CITES_REGION], # CITES Checklist
"2" => [COUNTRY, TERRITORY], # CITES Checklist
"3" => [CITES_REGION, COUNTRY, TERRITORY], # Species+
"4" => [COUNTRY, TERRITORY, TRADE_ENTITY], # CITES Trade
"4" => [COUNTRY, REGION, TERRITORY, TRADE_ENTITY], # CITES Trade
"5" => [COUNTRY, TERRITORY] # E-library
}
CURRENT_ONLY_SETS = ['3']
Expand Down
73 changes: 69 additions & 4 deletions app/models/trade/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ def initialize_query
end

unless @importers_ids.empty?
@query = @query.where(:importer_id => @importers_ids)
importers_ids = sanitize_importer_ids(@importers_ids)
@query = @query.where(:importer_id => importers_ids)
end

unless @exporters_ids.empty?
@query = @query.where(:exporter_id => @exporters_ids)
exporters_ids = sanitize_exporter_ids(@exporters_ids)
@query = @query.where(:exporter_id => exporters_ids)
end

if !@units_ids.empty?
Expand Down Expand Up @@ -125,6 +127,71 @@ def initialize_query
end

# Other cases
time_range_query

unless @importer_eu_country_ids.blank?
query = eu_country_date_query(@time_range_start, @time_range_end, 'importer')
@query = @query.where.not(query) unless query.blank?
end

unless @exporter_eu_country_ids.blank?
query = eu_country_date_query(@time_range_start, @time_range_end, 'exporter')
@query = @query.where.not(query) unless query.blank?
end

initialize_internal_query if @internal
end

def eu_id
GeoEntity.where(iso_code2: 'EU').pluck(:id).first
end

def eu_country_ids
EuCountryDate.all.pluck(:geo_entity_id)
end

def sanitize_exporter_ids(ids)
return ids unless ids.include?(eu_id)

ids.delete(eu_id)
# this is to collect only eu country IDs to apply EU rules query to
# e.g. EU + Austria we don't have to apply EU rules to Austria
@exporter_eu_country_ids = eu_country_ids - ids
(eu_country_ids + ids).uniq
end

def sanitize_importer_ids(ids)
return ids unless ids.include?(eu_id)

ids.delete(eu_id)
@importer_eu_country_ids = eu_country_ids - ids
(eu_country_ids + ids).uniq
end

def eu_country_date_query(start_year, end_year, type)
eu_country_ids = instance_variable_get("@#{type}_eu_country_ids")
country_query_arr = []
eu_country_ids.each do |eu_country|
# check for multiple entries for the same countries(UK might rejoin at some point)
eu_entry_exit_dates(eu_country).each do |entry_date, exit_date|

# exclude countries for which we will need to retreive all the shipments
# within the user selected year range anyway
exit_date_check = exit_date.nil? ? true : (exit_date > end_year) # workaround to avoid nil > integer
next if (entry_date < start_year && exit_date_check)

exit_year_check = exit_date.nil? ? 'AND TRUE' : "OR year >= #{exit_date}"
country_query_arr << "(trade_shipments.#{type}_id = #{eu_country} AND (year < #{entry_date} #{exit_year_check}))"
end
end
country_query_arr.join(' OR ')
end

def eu_entry_exit_dates(country_id)
EuCountryDate.where(geo_entity_id: country_id).pluck(:eu_accession_year, :eu_exit_year)
end

def time_range_query
unless @time_range_start.blank? && @time_range_end.blank?
if @time_range_start.blank?
@query = @query.where(["year <= ?", @time_range_end])
Expand All @@ -134,8 +201,6 @@ def initialize_query
@query = @query.where(["year >= ? AND year <= ?", @time_range_start, @time_range_end])
end
end

initialize_internal_query if @internal
end

def initialize_internal_query
Expand Down
6 changes: 6 additions & 0 deletions app/views/cites_trade/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
<option selected value="all_exp"> <%= t('all_countries') %> </option>
</select>
</div>
<div id="eu_disclaimer_exp" class="select_notes">
<%= t('eu_disclaimer') %>
</div>
</div>
<div id="tabs-imp">
<div class="label_style"> <%= t('import_text') %>:
Expand All @@ -99,6 +102,9 @@
<option selected value="all_imp"> <%= t('all_countries') %> </option>
</select>
</div>
<div id="eu_disclaimer_imp" class="select_notes">
<%= t('eu_disclaimer') %>
</div>
</div>
<div id="tabs-source">
<div class="label_style"> <%= t('source_text') %>:
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ en:
taxon_search: "Search for species or higher taxon"
genus_title: "Search for genus:"

eu_disclaimer: "Selection of 'European Union' will include trade data involving countries that were EU Member States at the time of trade (i.e. the year range of the query). Where the year range includes a country’s year of accession to/exit from the European Union, all trade relevant to the search query reported for that country and year will be included within the search results even if the accession/exit date was partway through that year."

all_countries: "All Countries"
all_sources: "All Sources"
all_purposes: "All Purposes"
Expand Down
1 change: 1 addition & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ es:
taxon_search: "Buscar por especie o grupo taxonómico"
genus_title: "Seleccione un género:"

eu_disclaimer: "La selección de 'Unión Europea' incluirá datos de comercio que involucren a países que eran Estados miembros de la UE en el momento del comercio (es decir, el rango de años de la búsqueda). Cuando el rango de años incluya el año de ingreso/salida de un país de la Unión Europea, todo el comercio relevante para la búsqueda reportado para ese país y año será incluido en los resultados de la búsqueda incluso cuando la fecha de ingreso/salida haya sido a mitad de ese año."
all_countries: "Todos los Países"
all_sources: "Todos los Orígenes"
all_purposes: "Todos los Objetivos"
Expand Down
Loading

0 comments on commit 1ad3c2b

Please sign in to comment.