001
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
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 }