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