001    /**
002     * Copyright (c) 2000-2013 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            @Override
030            public int getCount(int limitPeriod) {
031                    String[] valueParts = StringUtil.split(getValue(), StringPool.SLASH);
032    
033                    if ((limitPeriod !=
034                                    SocialActivityCounterDefinition.LIMIT_PERIOD_LIFETIME) &&
035                            (valueParts.length < 2)) {
036    
037                            return 0;
038                    }
039    
040                    int count = GetterUtil.getInteger(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            @Override
074            public void setCount(int limitPeriod, int count) {
075                    if (limitPeriod == SocialActivityCounterDefinition.LIMIT_PERIOD_DAY) {
076                            setValue(
077                                    String.valueOf(SocialCounterPeriodUtil.getActivityDay()) +
078                                            StringPool.SLASH + String.valueOf(count));
079                    }
080                    else if (limitPeriod ==
081                                            SocialActivityCounterDefinition.LIMIT_PERIOD_LIFETIME) {
082    
083                            setValue(String.valueOf(count));
084                    }
085                    else if (limitPeriod ==
086                                            SocialActivityCounterDefinition.LIMIT_PERIOD_PERIOD) {
087    
088                            StringBundler sb = new StringBundler(5);
089    
090                            sb.append(SocialCounterPeriodUtil.getStartPeriod());
091                            sb.append(StringPool.DASH);
092                            sb.append(SocialCounterPeriodUtil.getEndPeriod());
093                            sb.append(StringPool.SLASH);
094                            sb.append(count);
095    
096                            setValue(sb.toString());
097                    }
098            }
099    
100    }