001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.currencyconverter.util;
022:
023: import com.liferay.portal.kernel.language.LanguageUtil;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.webcache.WebCacheItem;
026: import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
027: import com.liferay.portlet.currencyconverter.model.Currency;
028: import com.liferay.util.CollectionFactory;
029:
030: import java.util.Iterator;
031: import java.util.Locale;
032: import java.util.Map;
033: import java.util.Set;
034: import java.util.TreeMap;
035:
036: import javax.servlet.http.HttpServletRequest;
037: import javax.servlet.jsp.PageContext;
038:
039: /**
040: * <a href="CurrencyUtil.java.html"><b><i>View Source</i></b></a>
041: *
042: * @author Brian Wing Shun Chan
043: *
044: */
045: public class CurrencyUtil {
046:
047: public static Map getAllSymbols(PageContext pageContext) {
048: HttpServletRequest req = (HttpServletRequest) pageContext
049: .getRequest();
050:
051: Locale locale = req.getLocale();
052:
053: String key = locale.toString();
054:
055: Map symbols = (Map) _symbolsPool.get(key);
056:
057: if (symbols != null) {
058: return symbols;
059: }
060:
061: symbols = new TreeMap();
062:
063: Iterator itr = _instance._currencyIds.iterator();
064:
065: while (itr.hasNext()) {
066: String symbol = (String) itr.next();
067:
068: symbols.put(LanguageUtil.get(pageContext, symbol), symbol);
069: }
070:
071: _symbolsPool.put(key, symbols);
072:
073: return symbols;
074: }
075:
076: public static Currency getCurrency(String symbol) {
077: WebCacheItem wci = new CurrencyWebCacheItem(symbol);
078:
079: return (Currency) WebCachePoolUtil.get(CurrencyUtil.class
080: .getName()
081: + StringPool.PERIOD + symbol, wci);
082: }
083:
084: public static boolean isCurrency(String symbol) {
085: return _instance._currencyIds.contains(symbol);
086: }
087:
088: private CurrencyUtil() {
089: _currencyIds = CollectionFactory.getHashSet();
090:
091: _currencyIds.add("ALL");
092: _currencyIds.add("DZD");
093: _currencyIds.add("XAL");
094: _currencyIds.add("ARS");
095: _currencyIds.add("AWG");
096: _currencyIds.add("AUD");
097: _currencyIds.add("BSD");
098: _currencyIds.add("BHD");
099: _currencyIds.add("BDT");
100: _currencyIds.add("BBD");
101: _currencyIds.add("BYR");
102: _currencyIds.add("BZD");
103: _currencyIds.add("BMD");
104: _currencyIds.add("BTN");
105: _currencyIds.add("BOB");
106: _currencyIds.add("BRL");
107: _currencyIds.add("GBP");
108: _currencyIds.add("BND");
109: _currencyIds.add("BGN");
110: _currencyIds.add("BIF");
111: _currencyIds.add("KHR");
112: _currencyIds.add("CAD");
113: _currencyIds.add("KYD");
114: _currencyIds.add("XOF");
115: _currencyIds.add("XAF");
116: _currencyIds.add("CLP");
117: _currencyIds.add("CNY");
118: _currencyIds.add("COP");
119: _currencyIds.add("KMF");
120: _currencyIds.add("XCP");
121: _currencyIds.add("CRC");
122: _currencyIds.add("HRK");
123: _currencyIds.add("CUP");
124: _currencyIds.add("CYP");
125: _currencyIds.add("CZK");
126: _currencyIds.add("DKK");
127: _currencyIds.add("DJF");
128: _currencyIds.add("DOP");
129: _currencyIds.add("XCD");
130: _currencyIds.add("ECS");
131: _currencyIds.add("EGP");
132: _currencyIds.add("SVC");
133: _currencyIds.add("ERN");
134: _currencyIds.add("EEK");
135: _currencyIds.add("ETB");
136: _currencyIds.add("EUR");
137: _currencyIds.add("FKP");
138: _currencyIds.add("GMD");
139: _currencyIds.add("GHC");
140: _currencyIds.add("GIP");
141: _currencyIds.add("XAU");
142: _currencyIds.add("GTQ");
143: _currencyIds.add("GNF");
144: _currencyIds.add("HTG");
145: _currencyIds.add("HNL");
146: _currencyIds.add("HKD");
147: _currencyIds.add("HUF");
148: _currencyIds.add("ISK");
149: _currencyIds.add("INR");
150: _currencyIds.add("IDR");
151: _currencyIds.add("IRR");
152: _currencyIds.add("ILS");
153: _currencyIds.add("JMD");
154: _currencyIds.add("JPY");
155: _currencyIds.add("JOD");
156: _currencyIds.add("KZT");
157: _currencyIds.add("KES");
158: _currencyIds.add("KRW");
159: _currencyIds.add("KWD");
160: _currencyIds.add("LAK");
161: _currencyIds.add("LVL");
162: _currencyIds.add("LBP");
163: _currencyIds.add("LSL");
164: _currencyIds.add("LYD");
165: _currencyIds.add("LTL");
166: _currencyIds.add("MOP");
167: _currencyIds.add("MKD");
168: _currencyIds.add("MGF");
169: _currencyIds.add("MWK");
170: _currencyIds.add("MYR");
171: _currencyIds.add("MVR");
172: _currencyIds.add("MTL");
173: _currencyIds.add("MRO");
174: _currencyIds.add("MUR");
175: _currencyIds.add("MXN");
176: _currencyIds.add("MDL");
177: _currencyIds.add("MNT");
178: _currencyIds.add("MAD");
179: _currencyIds.add("MZM");
180: _currencyIds.add("NAD");
181: _currencyIds.add("NPR");
182: _currencyIds.add("ANG");
183: _currencyIds.add("TRY");
184: _currencyIds.add("NZD");
185: _currencyIds.add("NIO");
186: _currencyIds.add("NGN");
187: _currencyIds.add("NOK");
188: _currencyIds.add("OMR");
189: _currencyIds.add("XPF");
190: _currencyIds.add("PKR");
191: _currencyIds.add("XPD");
192: _currencyIds.add("PAB");
193: _currencyIds.add("PGK");
194: _currencyIds.add("PYG");
195: _currencyIds.add("PEN");
196: _currencyIds.add("PHP");
197: _currencyIds.add("XPT");
198: _currencyIds.add("PLN");
199: _currencyIds.add("QAR");
200: _currencyIds.add("ROL");
201: _currencyIds.add("RON");
202: _currencyIds.add("RUB");
203: _currencyIds.add("RWF");
204: _currencyIds.add("WST");
205: _currencyIds.add("STD");
206: _currencyIds.add("SAR");
207: _currencyIds.add("SCR");
208: _currencyIds.add("SLL");
209: _currencyIds.add("XAG");
210: _currencyIds.add("SGD");
211: _currencyIds.add("SKK");
212: _currencyIds.add("SIT");
213: _currencyIds.add("SOS");
214: _currencyIds.add("ZAR");
215: _currencyIds.add("LKR");
216: _currencyIds.add("SHP");
217: _currencyIds.add("SDD");
218: _currencyIds.add("SRG");
219: _currencyIds.add("SZL");
220: _currencyIds.add("SEK");
221: _currencyIds.add("CHF");
222: _currencyIds.add("SYP");
223: _currencyIds.add("TWD");
224: _currencyIds.add("TZS");
225: _currencyIds.add("THB");
226: _currencyIds.add("TOP");
227: _currencyIds.add("TTD");
228: _currencyIds.add("TND");
229: _currencyIds.add("USD");
230: _currencyIds.add("AED");
231: _currencyIds.add("UGX");
232: _currencyIds.add("UAH");
233: _currencyIds.add("UYU");
234: _currencyIds.add("VUV");
235: _currencyIds.add("VEB");
236: _currencyIds.add("VND");
237: _currencyIds.add("YER");
238: _currencyIds.add("ZMK");
239: _currencyIds.add("ZWD");
240: }
241:
242: private static CurrencyUtil _instance = new CurrencyUtil();
243:
244: private static Map _symbolsPool = CollectionFactory
245: .getSyncHashMap();
246:
247: private Set _currencyIds;
248:
249: }
|