001    /**
002     * Copyright (c) 2000-2012 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(valueParts[valueParts.length-1], 0);
040    
041                    if (limitPeriod == SocialActivityCounterDefinition.LIMIT_PERIOD_DAY) {
042                            int activityDay = SocialCounterPeriodUtil.getActivityDay();
043    
044                            if (activityDay == GetterUtil.getInteger(valueParts[0], 0)) {
045                                    return count;
046                            }
047                    }
048                    else if (limitPeriod ==
049                                                    SocialActivityCounterDefinition.LIMIT_PERIOD_LIFETIME) {
050    
051                            return count;
052                    }
053                    else if (limitPeriod ==
054                                    SocialActivityCounterDefinition.LIMIT_PERIOD_PERIOD) {
055    
056                            int activityDay = SocialCounterPeriodUtil.getActivityDay();
057    
058                            String[] periodParts = StringUtil.split(
059                                    valueParts[0], StringPool.DASH);
060    
061                            int startPeriod = GetterUtil.getInteger(periodParts[0]);
062                            int endPeriod = GetterUtil.getInteger(periodParts[1]);
063    
064                            if ((activityDay >= startPeriod) && (activityDay <= endPeriod)) {
065                                    return count;
066                            }
067                    }
068    
069                    return 0;
070            }
071    
072            public void setCount(int limitPeriod, int count) {
073                    if (limitPeriod == SocialActivityCounterDefinition.LIMIT_PERIOD_DAY) {
074                            setValue(
075                                    String.valueOf(SocialCounterPeriodUtil.getActivityDay()) +
076                                            StringPool.SLASH + String.valueOf(count));
077                    }
078                    else if (limitPeriod ==
079                                                    SocialActivityCounterDefinition.LIMIT_PERIOD_LIFETIME) {
080    
081                            setValue(String.valueOf(count));
082                    }
083                    else if (limitPeriod ==
084                                            SocialActivityCounterDefinition.LIMIT_PERIOD_PERIOD) {
085    
086                            StringBundler sb = new StringBundler(5);
087    
088                            sb.append(SocialCounterPeriodUtil.getStartPeriod());
089                            sb.append(StringPool.DASH);
090                            sb.append(SocialCounterPeriodUtil.getEndPeriod());
091                            sb.append(StringPool.SLASH);
092                            sb.append(count);
093    
094                            setValue(sb.toString());
095                    }
096            }
097    
098    }