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