001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.amazonrankings;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portlet.amazonrankings.model.AmazonRankings;
020    import com.liferay.portlet.amazonrankings.util.AmazonRankingsUtil;
021    
022    import java.util.ArrayList;
023    import java.util.List;
024    
025    import javax.portlet.PortletPreferences;
026    import javax.portlet.PreferencesValidator;
027    import javax.portlet.ValidatorException;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class AmazonRankingsPreferencesValidator
033            implements PreferencesValidator {
034    
035            public void validate(PortletPreferences preferences)
036                    throws ValidatorException {
037    
038                    List<String> badIsbns = new ArrayList<String>();
039    
040                    String[] isbns = preferences.getValues("isbns", new String[0]);
041    
042                    for (String isbn : isbns) {
043                            AmazonRankings amazonRankings =
044                                    AmazonRankingsUtil.getAmazonRankings(isbn);
045    
046                            if (amazonRankings == null) {
047                                    badIsbns.add(isbn);
048    
049                                    if (_log.isInfoEnabled()) {
050                                            _log.info("Invalid ISBN " + isbn);
051                                    }
052                            }
053                    }
054    
055                    if (badIsbns.size() > 0) {
056                            throw new ValidatorException(
057                                    "Failed to retrieve ISBNs", badIsbns);
058                    }
059            }
060    
061            private static Log _log = LogFactoryUtil.getLog(
062                    AmazonRankingsPreferencesValidator.class);
063    
064    }