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.util;
016    
017    import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.TimeZoneUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.webcache.WebCacheItem;
022    import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
023    import com.liferay.portal.util.PropsValues;
024    import com.liferay.portlet.amazonrankings.model.AmazonRankings;
025    
026    import java.text.DateFormat;
027    
028    import java.util.Calendar;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class AmazonRankingsUtil {
034    
035            public static String getAmazonAccessKeyId() {
036                    return PropsValues.AMAZON_ACCESS_KEY_ID;
037            }
038    
039            public static String getAmazonAssociateTag() {
040                    return PropsValues.AMAZON_ASSOCIATE_TAG;
041            }
042    
043            public static AmazonRankings getAmazonRankings(String isbn) {
044                    if (!Validator.isDigit(isbn)) {
045                            return null;
046                    }
047    
048                    WebCacheItem wci = new AmazonRankingsWebCacheItem(isbn);
049    
050                    return (AmazonRankings)WebCachePoolUtil.get(
051                            AmazonRankingsUtil.class.getName() + StringPool.PERIOD + isbn, wci);
052            }
053    
054            public static String getAmazonSecretAccessKey() {
055                    return PropsValues.AMAZON_SECRET_ACCESS_KEY;
056            }
057    
058            public static String getTimestamp() {
059                    DateFormat dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
060                            _TIMESTAMP);
061    
062                    dateFormat.setTimeZone(TimeZoneUtil.getDefault());
063    
064                    Calendar calendar = Calendar.getInstance();
065    
066                    return dateFormat.format(calendar.getTime());
067            }
068    
069            public static boolean isEnabled() {
070                    if (Validator.isNull(PropsValues.AMAZON_ACCESS_KEY_ID) ||
071                            Validator.isNull(PropsValues.AMAZON_ASSOCIATE_TAG) ||
072                            Validator.isNull(PropsValues.AMAZON_SECRET_ACCESS_KEY)) {
073    
074                            return false;
075                    }
076                    else {
077                            return true;
078                    }
079            }
080    
081            private static final String _TIMESTAMP = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
082    
083    }