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.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            @Override
036            public void validate(PortletPreferences preferences)
037                    throws ValidatorException {
038    
039                    List<String> badIsbns = new ArrayList<String>();
040    
041                    String[] isbns = preferences.getValues("isbns", new String[0]);
042    
043                    for (String isbn : isbns) {
044                            AmazonRankings amazonRankings =
045                                    AmazonRankingsUtil.getAmazonRankings(isbn);
046    
047                            if (amazonRankings == null) {
048                                    badIsbns.add(isbn);
049    
050                                    if (_log.isInfoEnabled()) {
051                                            _log.info("Invalid ISBN " + isbn);
052                                    }
053                            }
054                    }
055    
056                    if (badIsbns.size() > 0) {
057                            throw new ValidatorException("Failed to retrieve ISBNs", badIsbns);
058                    }
059            }
060    
061            private static Log _log = LogFactoryUtil.getLog(
062                    AmazonRankingsPreferencesValidator.class);
063    
064    }