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;
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            @Override
033            public void validate(PortletPreferences preferences)
034                    throws ValidatorException {
035    
036                    List<String> badSymbols = new ArrayList<String>();
037    
038                    String[] symbols = preferences.getValues("symbols", new String[0]);
039    
040                    for (int i = 0; i < symbols.length; i++) {
041                            Currency currency = CurrencyUtil.getCurrency(symbols[i]);
042    
043                            if (currency == null) {
044                                    badSymbols.add(symbols[i]);
045                            }
046                    }
047    
048                    if (badSymbols.size() > 0) {
049                            throw new ValidatorException(
050                                    "Failed to retrieve symbols", badSymbols);
051                    }
052            }
053    
054    }