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