package net.canadensys.dataportal.occurrence.search.config;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import net.canadensys.query.QueryOperatorEnum;

import org.apache.commons.lang3.StringUtils;

/**
 * This class contains configurations related to the language support on client side.
 * We only keep language strings that could be used in the view dynamic parts.
 * TODO : prefil the map with OccurrencePortalConfig.getSupportedLocale() to avoid Map creation on each call.
 * @author canadensys
 *
 */
public class OccurrenceSearchableFieldLanguageSupport {
	
	private static final String OPERATOR_PREFIX = "operator.";
	private static final String FILTER_PREFIX = "filter.";
	private static final String INVALID_PREFIX = "control.invalid.";
	private static final String DOWNLOAD_PREFIX = "control.download.";
	private static final String CHART_PREFIX = "view.stats.chart.";
	private static final String OCC_PAGE_MENU_PREFIX = "occpage.menu.";
	
	/**
	 * This is used for dynamic components only. It doesn't contain all the resources.
	 * Contains language string for operators, filters, download and stats components.
	 * @param locale
	 * @return
	 */
	public Map<String,String> getLanguageResources(ResourceBundle resourceBundle){
		Map<String,String> languageResources = new HashMap<String, String>();
		for(QueryOperatorEnum op : QueryOperatorEnum.values()){
			String key = OPERATOR_PREFIX + op.toString().toLowerCase();
			if(!StringUtils.isBlank(resourceBundle.getString(key))){
				languageResources.put(key, resourceBundle.getString(key));
			}
		}
		
		Enumeration<String> rbKeys = resourceBundle.getKeys();
		String currKey = null;
		while(rbKeys.hasMoreElements()){
			currKey = rbKeys.nextElement();
			if(currKey.startsWith(FILTER_PREFIX) 
			    || currKey.startsWith(INVALID_PREFIX) 
	        || currKey.startsWith(DOWNLOAD_PREFIX) 
					|| currKey.startsWith(CHART_PREFIX) 
					|| currKey.startsWith(OCC_PAGE_MENU_PREFIX)){
				languageResources.put(currKey, resourceBundle.getString(currKey));
			}
		}
		return languageResources;
	}
}

@@ -54,16 +54,44 @@
 onfig;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -134,24 +134,47 @@
 il.HashMap;%0A
+import java.util.List;%0A
 import java.
@@ -405,197 +405,8 @@
 de.%0A
- * We only keep language strings that could be used in the view dynamic parts.%0A * TODO : prefil the map with OccurrencePortalConfig.getSupportedLocale() to avoid Map creation on each call.%0A
  * @
@@ -574,88 +574,138 @@
 nal 
+List%3C
 String
- FILTER_PREFIX = %22filter.%22;%0A%09private static final String INVALID_PREFIX = 
+%3E PREFIX_FILTER_LIST = new ArrayList%3CString%3E();%0A%09static%7B%0A%09%09PREFIX_FILTER_LIST.add(%22filter.%22);%0A%09%09PREFIX_FILTER_LIST.add(
 %22con
@@ -722,57 +722,36 @@
 id.%22
+)
 ;%0A%09
-private static final String DOWNLOAD_PREFIX = 
+%09PREFIX_FILTER_LIST.add(
 %22con
@@ -769,54 +769,36 @@
 ad.%22
+)
 ;%0A%09
-private static final String CHART_PREFIX = 
+%09PREFIX_FILTER_LIST.add(
 %22vie
@@ -816,62 +816,36 @@
 rt.%22
+)
 ;%0A%09
-private static final String OCC_PAGE_MENU_PREFIX = 
+%09PREFIX_FILTER_LIST.add(
 %22occ
@@ -855,17 +855,21 @@
 e.menu.%22
-;
+);%0A%09%7D
 %0A%09%0A%09/**%0A
@@ -914,10 +914,20 @@
 s on
-ly
+ client-side
 . It
@@ -955,100 +955,25 @@
 the 
-resources.%0A%09 * Contains language string for operators, filters, download and stats component
+language resource
 s.%0A%09
@@ -1576,235 +1576,471 @@
 %09if(
-currKey.startsWith(FILTER_PREFIX) %0A%09%09%09    %7C%7C currKey.startsWith(INVALID_PREFIX) %0A%09        %7C%7C currKey.
+shouldInclude(currKey))%7B%0A%09%09%09%09languageResources.put(currKey, resourceBundle.getString(currKey));%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return languageResources;%0A%09%7D%0A%09%0A%09/**%0A%09 * Check if we should include this languageResource based on the PREFIX_FILTER_LIST.%0A%09 * @param languageResource%0A%09 * @return true if languageResource 
 starts
-With(DOWNLOAD_PREFIX) %0A%09%09%09%09%09%7C%7C currKey.startsWith(CHART_PREFIX) %0A%09%09%09%09%09%7C%7C currKey.startsWith(OCC_PAGE_MENU_PREFIX)
+ with a prefix in PREFIX_FILTER_LIST, otherwise, false.%0A%09 */%0A%09private boolean shouldInclude(String languageResource)%7B%0A%09%09for(String curr:PREFIX_FILTER_LIST
 )%7B%0A%09%09%09
-%09
+if(
 lang
@@ -2055,57 +2055,43 @@
 urce
-s.put(currKey, resourceBundle.getString(currKey))
+.startsWith(curr))%7B%0A%09%09%09%09return true
 ;%0A%09%09
@@ -2106,32 +2106,20 @@
 %09return 
-languageResources
+false
 ;%0A%09%7D%0A%7D%0A

make it easier to add language resource for client