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.amazonrankings;
016    
017    import com.liferay.portlet.amazonrankings.model.AmazonRankings;
018    import com.liferay.portlet.amazonrankings.util.AmazonRankingsUtil;
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 AmazonRankingsPreferencesValidator
031            implements PreferencesValidator {
032    
033            public void validate(PortletPreferences preferences)
034                    throws ValidatorException {
035    
036                    List<String> badIsbns = new ArrayList<String>();
037    
038                    String[] isbns = preferences.getValues("isbns", new String[0]);
039    
040                    for (int i = 0; i < isbns.length; i++) {
041                            AmazonRankings amazonRankings =
042                                    AmazonRankingsUtil.getAmazonRankings(isbns[i]);
043    
044                            if (amazonRankings == null) {
045                                    badIsbns.add(isbns[i]);
046                            }
047                    }
048    
049                    if (badIsbns.size() > 0) {
050                            throw new ValidatorException(
051                                    "Failed to retrieve ISBNs", badIsbns);
052                    }
053            }
054    
055    }