001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.currencyconverter.util;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.webcache.WebCacheItem;
020    import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
021    import com.liferay.portlet.currencyconverter.model.Currency;
022    
023    import java.util.HashSet;
024    import java.util.Locale;
025    import java.util.Map;
026    import java.util.Set;
027    import java.util.TreeMap;
028    import java.util.concurrent.ConcurrentHashMap;
029    
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.jsp.PageContext;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class CurrencyUtil {
037    
038            public static Map<String, String> getAllSymbols(PageContext pageContext) {
039                    HttpServletRequest request =
040                            (HttpServletRequest)pageContext.getRequest();
041    
042                    Locale locale = request.getLocale();
043    
044                    String key = locale.toString();
045    
046                    Map<String, String> symbols = _symbolsPool.get(key);
047    
048                    if (symbols != null) {
049                            return symbols;
050                    }
051    
052                    symbols = new TreeMap<String, String>();
053    
054                    for (String symbol : _instance._currencyIds) {
055                            symbols.put(LanguageUtil.get(pageContext, symbol), symbol);
056                    }
057    
058                    _symbolsPool.put(key, symbols);
059    
060                    return symbols;
061            }
062    
063            public static Currency getCurrency(String symbol) {
064                    WebCacheItem wci = new CurrencyWebCacheItem(symbol);
065    
066                    return (Currency)WebCachePoolUtil.get(
067                            CurrencyUtil.class.getName() + StringPool.PERIOD + symbol, wci);
068            }
069    
070            public static boolean isCurrency(String symbol) {
071                    return _instance._currencyIds.contains(symbol);
072            }
073    
074            private CurrencyUtil() {
075                    _currencyIds = new HashSet<String>();
076    
077                    _currencyIds.add("ALL");
078                    _currencyIds.add("DZD");
079                    _currencyIds.add("XAL");
080                    _currencyIds.add("ARS");
081                    _currencyIds.add("AWG");
082                    _currencyIds.add("AUD");
083                    _currencyIds.add("BSD");
084                    _currencyIds.add("BHD");
085                    _currencyIds.add("BDT");
086                    _currencyIds.add("BBD");
087                    _currencyIds.add("BYR");
088                    _currencyIds.add("BZD");
089                    _currencyIds.add("BMD");
090                    _currencyIds.add("BTN");
091                    _currencyIds.add("BOB");
092                    _currencyIds.add("BRL");
093                    _currencyIds.add("GBP");
094                    _currencyIds.add("BND");
095                    _currencyIds.add("BGN");
096                    _currencyIds.add("BIF");
097                    _currencyIds.add("KHR");
098                    _currencyIds.add("CAD");
099                    _currencyIds.add("KYD");
100                    _currencyIds.add("XOF");
101                    _currencyIds.add("XAF");
102                    _currencyIds.add("CLP");
103                    _currencyIds.add("CNY");
104                    _currencyIds.add("COP");
105                    _currencyIds.add("KMF");
106                    _currencyIds.add("XCP");
107                    _currencyIds.add("CRC");
108                    _currencyIds.add("HRK");
109                    _currencyIds.add("CUP");
110                    _currencyIds.add("CYP");
111                    _currencyIds.add("CZK");
112                    _currencyIds.add("DKK");
113                    _currencyIds.add("DJF");
114                    _currencyIds.add("DOP");
115                    _currencyIds.add("XCD");
116                    _currencyIds.add("ECS");
117                    _currencyIds.add("EGP");
118                    _currencyIds.add("SVC");
119                    _currencyIds.add("ERN");
120                    _currencyIds.add("EEK");
121                    _currencyIds.add("ETB");
122                    _currencyIds.add("EUR");
123                    _currencyIds.add("FKP");
124                    _currencyIds.add("GMD");
125                    _currencyIds.add("GHC");
126                    _currencyIds.add("GIP");
127                    _currencyIds.add("XAU");
128                    _currencyIds.add("GTQ");
129                    _currencyIds.add("GNF");
130                    _currencyIds.add("HTG");
131                    _currencyIds.add("HNL");
132                    _currencyIds.add("HKD");
133                    _currencyIds.add("HUF");
134                    _currencyIds.add("ISK");
135                    _currencyIds.add("INR");
136                    _currencyIds.add("IDR");
137                    _currencyIds.add("IRR");
138                    _currencyIds.add("ILS");
139                    _currencyIds.add("JMD");
140                    _currencyIds.add("JPY");
141                    _currencyIds.add("JOD");
142                    _currencyIds.add("KZT");
143                    _currencyIds.add("KES");
144                    _currencyIds.add("KRW");
145                    _currencyIds.add("KWD");
146                    _currencyIds.add("LAK");
147                    _currencyIds.add("LVL");
148                    _currencyIds.add("LBP");
149                    _currencyIds.add("LSL");
150                    _currencyIds.add("LYD");
151                    _currencyIds.add("LTL");
152                    _currencyIds.add("MOP");
153                    _currencyIds.add("MKD");
154                    _currencyIds.add("MGF");
155                    _currencyIds.add("MWK");
156                    _currencyIds.add("MYR");
157                    _currencyIds.add("MVR");
158                    _currencyIds.add("MTL");
159                    _currencyIds.add("MRO");
160                    _currencyIds.add("MUR");
161                    _currencyIds.add("MXN");
162                    _currencyIds.add("MDL");
163                    _currencyIds.add("MNT");
164                    _currencyIds.add("MAD");
165                    _currencyIds.add("MZM");
166                    _currencyIds.add("NAD");
167                    _currencyIds.add("NPR");
168                    _currencyIds.add("ANG");
169                    _currencyIds.add("TRY");
170                    _currencyIds.add("NZD");
171                    _currencyIds.add("NIO");
172                    _currencyIds.add("NGN");
173                    _currencyIds.add("NOK");
174                    _currencyIds.add("OMR");
175                    _currencyIds.add("XPF");
176                    _currencyIds.add("PKR");
177                    _currencyIds.add("XPD");
178                    _currencyIds.add("PAB");
179                    _currencyIds.add("PGK");
180                    _currencyIds.add("PYG");
181                    _currencyIds.add("PEN");
182                    _currencyIds.add("PHP");
183                    _currencyIds.add("XPT");
184                    _currencyIds.add("PLN");
185                    _currencyIds.add("QAR");
186                    _currencyIds.add("ROL");
187                    _currencyIds.add("RON");
188                    _currencyIds.add("RUB");
189                    _currencyIds.add("RWF");
190                    _currencyIds.add("WST");
191                    _currencyIds.add("STD");
192                    _currencyIds.add("SAR");
193                    _currencyIds.add("SCR");
194                    _currencyIds.add("SLL");
195                    _currencyIds.add("XAG");
196                    _currencyIds.add("SGD");
197                    _currencyIds.add("SKK");
198                    _currencyIds.add("SIT");
199                    _currencyIds.add("SOS");
200                    _currencyIds.add("ZAR");
201                    _currencyIds.add("LKR");
202                    _currencyIds.add("SHP");
203                    _currencyIds.add("SDD");
204                    _currencyIds.add("SRG");
205                    _currencyIds.add("SZL");
206                    _currencyIds.add("SEK");
207                    _currencyIds.add("CHF");
208                    _currencyIds.add("SYP");
209                    _currencyIds.add("TWD");
210                    _currencyIds.add("TZS");
211                    _currencyIds.add("THB");
212                    _currencyIds.add("TOP");
213                    _currencyIds.add("TTD");
214                    _currencyIds.add("TND");
215                    _currencyIds.add("USD");
216                    _currencyIds.add("AED");
217                    _currencyIds.add("UGX");
218                    _currencyIds.add("UAH");
219                    _currencyIds.add("UYU");
220                    _currencyIds.add("VUV");
221                    _currencyIds.add("VEB");
222                    _currencyIds.add("VND");
223                    _currencyIds.add("YER");
224                    _currencyIds.add("ZMK");
225                    _currencyIds.add("ZWD");
226            }
227    
228            private static CurrencyUtil _instance = new CurrencyUtil();
229    
230            private static Map<String, Map<String, String>> _symbolsPool =
231                    new ConcurrentHashMap<String, Map<String, String>>();
232    
233            private Set<String> _currencyIds;
234    
235    }