001    /**
002     * Copyright (c) 2000-2010 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;
016    
017    import com.liferay.portlet.currencyconverter.model.Currency;
018    import com.liferay.portlet.currencyconverter.util.CurrencyUtil;
019    
020    import java.util.ArrayList;
021    import java.util.List;
022    
023    import javax.portlet.PortletPreferences;
024    import javax.portlet.PreferencesValidator;
025    import javax.portlet.ValidatorException;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class CurrencyPreferencesValidator implements PreferencesValidator {
031    
032            public void validate(PortletPreferences preferences)
033                    throws ValidatorException {
034    
035                    List<String> badSymbols = new ArrayList<String>();
036    
037                    String[] symbols = preferences.getValues("symbols", new String[0]);
038    
039                    for (int i = 0; i < symbols.length; i++) {
040                            Currency currency = CurrencyUtil.getCurrency(symbols[i]);
041    
042                            if (currency == null) {
043                                    badSymbols.add(symbols[i]);
044                            }
045                    }
046    
047                    if (badSymbols.size() > 0) {
048                            throw new ValidatorException(
049                                    "Failed to retrieve symbols", badSymbols);
050                    }
051            }
052    
053    }