1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.currencyconverter.util;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.webcache.WebCacheItem;
28  import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
29  import com.liferay.portlet.currencyconverter.model.Currency;
30  
31  import java.util.HashSet;
32  import java.util.Iterator;
33  import java.util.Locale;
34  import java.util.Map;
35  import java.util.Set;
36  import java.util.TreeMap;
37  import java.util.concurrent.ConcurrentHashMap;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.jsp.PageContext;
41  
42  /**
43   * <a href="CurrencyUtil.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class CurrencyUtil {
49  
50      public static Map<String, String> getAllSymbols(PageContext pageContext) {
51          HttpServletRequest request =
52              (HttpServletRequest)pageContext.getRequest();
53  
54          Locale locale = request.getLocale();
55  
56          String key = locale.toString();
57  
58          Map<String, String> symbols = _symbolsPool.get(key);
59  
60          if (symbols != null) {
61              return symbols;
62          }
63  
64          symbols = new TreeMap<String, String>();
65  
66          Iterator<String> itr = _instance._currencyIds.iterator();
67  
68          while (itr.hasNext()) {
69              String symbol = itr.next();
70  
71              symbols.put(LanguageUtil.get(pageContext, symbol), symbol);
72          }
73  
74          _symbolsPool.put(key, symbols);
75  
76          return symbols;
77      }
78  
79      public static Currency getCurrency(String symbol) {
80          WebCacheItem wci = new CurrencyWebCacheItem(symbol);
81  
82          return (Currency)WebCachePoolUtil.get(
83              CurrencyUtil.class.getName() + StringPool.PERIOD + symbol, wci);
84      }
85  
86      public static boolean isCurrency(String symbol) {
87          return _instance._currencyIds.contains(symbol);
88      }
89  
90      private CurrencyUtil() {
91          _currencyIds = new HashSet<String>();
92  
93          _currencyIds.add("ALL");
94          _currencyIds.add("DZD");
95          _currencyIds.add("XAL");
96          _currencyIds.add("ARS");
97          _currencyIds.add("AWG");
98          _currencyIds.add("AUD");
99          _currencyIds.add("BSD");
100         _currencyIds.add("BHD");
101         _currencyIds.add("BDT");
102         _currencyIds.add("BBD");
103         _currencyIds.add("BYR");
104         _currencyIds.add("BZD");
105         _currencyIds.add("BMD");
106         _currencyIds.add("BTN");
107         _currencyIds.add("BOB");
108         _currencyIds.add("BRL");
109         _currencyIds.add("GBP");
110         _currencyIds.add("BND");
111         _currencyIds.add("BGN");
112         _currencyIds.add("BIF");
113         _currencyIds.add("KHR");
114         _currencyIds.add("CAD");
115         _currencyIds.add("KYD");
116         _currencyIds.add("XOF");
117         _currencyIds.add("XAF");
118         _currencyIds.add("CLP");
119         _currencyIds.add("CNY");
120         _currencyIds.add("COP");
121         _currencyIds.add("KMF");
122         _currencyIds.add("XCP");
123         _currencyIds.add("CRC");
124         _currencyIds.add("HRK");
125         _currencyIds.add("CUP");
126         _currencyIds.add("CYP");
127         _currencyIds.add("CZK");
128         _currencyIds.add("DKK");
129         _currencyIds.add("DJF");
130         _currencyIds.add("DOP");
131         _currencyIds.add("XCD");
132         _currencyIds.add("ECS");
133         _currencyIds.add("EGP");
134         _currencyIds.add("SVC");
135         _currencyIds.add("ERN");
136         _currencyIds.add("EEK");
137         _currencyIds.add("ETB");
138         _currencyIds.add("EUR");
139         _currencyIds.add("FKP");
140         _currencyIds.add("GMD");
141         _currencyIds.add("GHC");
142         _currencyIds.add("GIP");
143         _currencyIds.add("XAU");
144         _currencyIds.add("GTQ");
145         _currencyIds.add("GNF");
146         _currencyIds.add("HTG");
147         _currencyIds.add("HNL");
148         _currencyIds.add("HKD");
149         _currencyIds.add("HUF");
150         _currencyIds.add("ISK");
151         _currencyIds.add("INR");
152         _currencyIds.add("IDR");
153         _currencyIds.add("IRR");
154         _currencyIds.add("ILS");
155         _currencyIds.add("JMD");
156         _currencyIds.add("JPY");
157         _currencyIds.add("JOD");
158         _currencyIds.add("KZT");
159         _currencyIds.add("KES");
160         _currencyIds.add("KRW");
161         _currencyIds.add("KWD");
162         _currencyIds.add("LAK");
163         _currencyIds.add("LVL");
164         _currencyIds.add("LBP");
165         _currencyIds.add("LSL");
166         _currencyIds.add("LYD");
167         _currencyIds.add("LTL");
168         _currencyIds.add("MOP");
169         _currencyIds.add("MKD");
170         _currencyIds.add("MGF");
171         _currencyIds.add("MWK");
172         _currencyIds.add("MYR");
173         _currencyIds.add("MVR");
174         _currencyIds.add("MTL");
175         _currencyIds.add("MRO");
176         _currencyIds.add("MUR");
177         _currencyIds.add("MXN");
178         _currencyIds.add("MDL");
179         _currencyIds.add("MNT");
180         _currencyIds.add("MAD");
181         _currencyIds.add("MZM");
182         _currencyIds.add("NAD");
183         _currencyIds.add("NPR");
184         _currencyIds.add("ANG");
185         _currencyIds.add("TRY");
186         _currencyIds.add("NZD");
187         _currencyIds.add("NIO");
188         _currencyIds.add("NGN");
189         _currencyIds.add("NOK");
190         _currencyIds.add("OMR");
191         _currencyIds.add("XPF");
192         _currencyIds.add("PKR");
193         _currencyIds.add("XPD");
194         _currencyIds.add("PAB");
195         _currencyIds.add("PGK");
196         _currencyIds.add("PYG");
197         _currencyIds.add("PEN");
198         _currencyIds.add("PHP");
199         _currencyIds.add("XPT");
200         _currencyIds.add("PLN");
201         _currencyIds.add("QAR");
202         _currencyIds.add("ROL");
203         _currencyIds.add("RON");
204         _currencyIds.add("RUB");
205         _currencyIds.add("RWF");
206         _currencyIds.add("WST");
207         _currencyIds.add("STD");
208         _currencyIds.add("SAR");
209         _currencyIds.add("SCR");
210         _currencyIds.add("SLL");
211         _currencyIds.add("XAG");
212         _currencyIds.add("SGD");
213         _currencyIds.add("SKK");
214         _currencyIds.add("SIT");
215         _currencyIds.add("SOS");
216         _currencyIds.add("ZAR");
217         _currencyIds.add("LKR");
218         _currencyIds.add("SHP");
219         _currencyIds.add("SDD");
220         _currencyIds.add("SRG");
221         _currencyIds.add("SZL");
222         _currencyIds.add("SEK");
223         _currencyIds.add("CHF");
224         _currencyIds.add("SYP");
225         _currencyIds.add("TWD");
226         _currencyIds.add("TZS");
227         _currencyIds.add("THB");
228         _currencyIds.add("TOP");
229         _currencyIds.add("TTD");
230         _currencyIds.add("TND");
231         _currencyIds.add("USD");
232         _currencyIds.add("AED");
233         _currencyIds.add("UGX");
234         _currencyIds.add("UAH");
235         _currencyIds.add("UYU");
236         _currencyIds.add("VUV");
237         _currencyIds.add("VEB");
238         _currencyIds.add("VND");
239         _currencyIds.add("YER");
240         _currencyIds.add("ZMK");
241         _currencyIds.add("ZWD");
242     }
243 
244     private static CurrencyUtil _instance = new CurrencyUtil();
245 
246     private static Map<String, Map<String, String>> _symbolsPool =
247         new ConcurrentHashMap<String, Map<String, String>>();
248 
249     private Set<String> _currencyIds;
250 
251 }