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() {
031                    String value = getValue();
032    
033                    if (!value.contains(StringPool.SLASH)) {
034                            return getCount(
035                                    SocialActivityCounterDefinition.LIMIT_PERIOD_LIFETIME);
036                    }
037    
038                    String[] valueParts = StringUtil.split(value, StringPool.SLASH);
039    
040                    if (valueParts[0].contains(StringPool.DASH)) {
041                            return getCount(
042                                    SocialActivityCounterDefinition.LIMIT_PERIOD_PERIOD);
043                    }
044    
045                    return getCount(SocialActivityCounterDefinition.LIMIT_PERIOD_DAY);
046            }
047    
048            @Override
049            public int getCount(int limitPeriod) {
050                    String[] valueParts = StringUtil.split(getValue(), StringPool.SLASH);
051    
052                    if ((limitPeriod !=
053                                    SocialActivityCounterDefinition.LIMIT_PERIOD_LIFETIME) &&
054                            (valueParts.length < 2)) {
055    
056                            return 0;
057                    }
058    
059                    int count = GetterUtil.getInteger(valueParts[valueParts.length-1], 0);
060    
061                    if (limitPeriod == SocialActivityCounterDefinition.LIMIT_PERIOD_DAY) {
062                            int activityDay = SocialCounterPeriodUtil.getActivityDay();
063    
064                            if (activityDay == GetterUtil.getInteger(valueParts[0], 0)) {
065                                    return count;
066                            }
067                    }
068                    else if (limitPeriod ==
069                                            SocialActivityCounterDefinition.LIMIT_PERIOD_LIFETIME) {
070    
071                            return count;
072                    }
073                    else if (limitPeriod ==
074                                            SocialActivityCounterDefinition.LIMIT_PERIOD_PERIOD) {
075    
076                            int activityDay = SocialCounterPeriodUtil.getActivityDay();
077    
078                            String[] periodParts = StringUtil.split(
079                                    valueParts[0], StringPool.DASH);
080    
081                            int startPeriod = GetterUtil.getInteger(periodParts[0]);
082                            int endPeriod = GetterUtil.getInteger(periodParts[1]);
083    
084                            if ((activityDay >= startPeriod) && (activityDay <= endPeriod)) {
085                                    return count;
086                            }
087                    }
088    
089                    return 0;
090            }
091    
092            @Override
093            public void setCount(int limitPeriod, int count) {
094                    if (limitPeriod == SocialActivityCounterDefinition.LIMIT_PERIOD_DAY) {
095                            setValue(
096                                    String.valueOf(SocialCounterPeriodUtil.getActivityDay()) +
097                                            StringPool.SLASH + String.valueOf(count));
098                    }
099                    else if (limitPeriod ==
100                                            SocialActivityCounterDefinition.LIMIT_PERIOD_LIFETIME) {
101    
102                            setValue(String.valueOf(count));
103                    }
104                    else if (limitPeriod ==
105                                            SocialActivityCounterDefinition.LIMIT_PERIOD_PERIOD) {
106    
107                            StringBundler sb = new StringBundler(5);
108    
109                            sb.append(SocialCounterPeriodUtil.getStartPeriod());
110                            sb.append(StringPool.DASH);
111                            sb.append(SocialCounterPeriodUtil.getEndPeriod());
112                            sb.append(StringPool.SLASH);
113                            sb.append(count);
114    
115                            setValue(sb.toString());
116                    }
117            }
118    
119    }