001    /**
002     * Copyright (c) 2000-2012 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(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    }