001    /**
002     * Copyright (c) 2000-2011 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.social.model.impl;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portlet.social.model.SocialActivityCounterDefinition;
022    import com.liferay.portlet.social.util.SocialCounterPeriodUtil;
023    
024    /**
025     * @author Zsolt Berentey
026     */
027    public class SocialActivityLimitImpl extends SocialActivityLimitBaseImpl {
028    
029            public int getCount(int limitPeriod) {
030                    String[] valueParts = StringUtil.split(getValue(), StringPool.SLASH);
031    
032                    if ((limitPeriod !=
033                                    SocialActivityCounterDefinition.LIMIT_PERIOD_LIFETIME) &&
034                            (valueParts.length < 2)) {
035    
036                            return 0;
037                    }
038    
039                    int count = GetterUtil.getInteger(
040                            valueParts[valueParts.length-1], 0);
041    
042                    if (limitPeriod == SocialActivityCounterDefinition.LIMIT_PERIOD_DAY) {
043                            int activityDay = SocialCounterPeriodUtil.getActivityDay();
044    
045                            if (activityDay == GetterUtil.getInteger(valueParts[0], 0)) {
046                                    return count;
047                            }
048                    }
049                    else if (limitPeriod ==
050                                                    SocialActivityCounterDefinition.LIMIT_PERIOD_LIFETIME) {
051    
052                            return count;
053                    }
054                    else if (limitPeriod ==
055                                    SocialActivityCounterDefinition.LIMIT_PERIOD_PERIOD) {
056    
057                            int activityDay = SocialCounterPeriodUtil.getActivityDay();
058    
059                            String[] periodParts = StringUtil.split(
060                                    valueParts[0], StringPool.DASH);
061    
062                            int startPeriod = GetterUtil.getInteger(periodParts[0]);
063                            int endPeriod = GetterUtil.getInteger(periodParts[1]);
064    
065                            if ((activityDay >= startPeriod) && (activityDay <= endPeriod)) {
066                                    return count;
067                            }
068                    }
069    
070                    return 0;
071            }
072    
073            public void setCount(int limitPeriod, int count) {
074                    if (limitPeriod == SocialActivityCounterDefinition.LIMIT_PERIOD_DAY) {
075                            setValue(
076                                    String.valueOf(SocialCounterPeriodUtil.getActivityDay()) +
077                                            StringPool.SLASH + String.valueOf(count));
078                    }
079                    else if (limitPeriod ==
080                                                    SocialActivityCounterDefinition.LIMIT_PERIOD_LIFETIME) {
081    
082                            setValue(String.valueOf(count));
083                    }
084                    else if (limitPeriod ==
085                                            SocialActivityCounterDefinition.LIMIT_PERIOD_PERIOD) {
086    
087                            StringBundler sb = new StringBundler(5);
088    
089                            sb.append(SocialCounterPeriodUtil.getStartPeriod());
090                            sb.append(StringPool.DASH);
091                            sb.append(SocialCounterPeriodUtil.getEndPeriod());
092                            sb.append(StringPool.SLASH);
093                            sb.append(count);
094    
095                            setValue(sb.toString());
096                    }
097            }
098    
099    }