001    /**
002     * Copyright (c) 2000-present 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;
016    
017    import com.liferay.portal.kernel.util.HashUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.io.Serializable;
023    
024    /**
025     * @author Zsolt Berentey
026     */
027    public class SocialActivityCounterDefinition implements Serializable {
028    
029            public static final int LIMIT_PERIOD_DAY = 1;
030    
031            public static final int LIMIT_PERIOD_LIFETIME = 2;
032    
033            public static final int LIMIT_PERIOD_PERIOD = 3;
034    
035            public SocialActivityCounterDefinition() {
036            }
037    
038            public SocialActivityCounterDefinition(String name, int ownerType) {
039                    _name = name;
040                    _ownerType = ownerType;
041            }
042    
043            @Override
044            public SocialActivityCounterDefinition clone() {
045                    SocialActivityCounterDefinition activityCounterDefinition =
046                            new SocialActivityCounterDefinition();
047    
048                    activityCounterDefinition.setEnabled(_enabled);
049                    activityCounterDefinition.setIncrement(_increment);
050                    activityCounterDefinition.setLimitEnabled(_limitEnabled);
051                    activityCounterDefinition.setLimitPeriod(_limitPeriod);
052                    activityCounterDefinition.setLimitValue(_limitValue);
053                    activityCounterDefinition.setName(_name);
054                    activityCounterDefinition.setOwnerType(_ownerType);
055                    activityCounterDefinition.setPeriodLength(_periodLength);
056                    activityCounterDefinition.setTransient(_transient);
057    
058                    return activityCounterDefinition;
059            }
060    
061            @Override
062            public boolean equals(Object obj) {
063                    if (this == obj) {
064                            return true;
065                    }
066    
067                    if (!(obj instanceof SocialActivityCounterDefinition)) {
068                            return false;
069                    }
070    
071                    SocialActivityCounterDefinition activityCounterDefinition =
072                            (SocialActivityCounterDefinition)obj;
073    
074                    if ((activityCounterDefinition != null) &&
075                            Validator.equals(_enabled, activityCounterDefinition._enabled) &&
076                            Validator.equals(
077                                    _increment, activityCounterDefinition._increment) &&
078                            Validator.equals(
079                                    _limitEnabled, activityCounterDefinition._limitEnabled) &&
080                            Validator.equals(
081                                    _limitPeriod, activityCounterDefinition._limitPeriod) &&
082                            Validator.equals(
083                                    _limitValue, activityCounterDefinition._limitValue) &&
084                            Validator.equals(_name, activityCounterDefinition._name) &&
085                            Validator.equals(
086                                    _ownerType, activityCounterDefinition._ownerType) &&
087                            Validator.equals(
088                                    _periodLength, activityCounterDefinition._periodLength) &&
089                            Validator.equals(
090                                    _transient, activityCounterDefinition._transient)) {
091    
092                            return true;
093                    }
094    
095                    return false;
096            }
097    
098            public int getIncrement() {
099                    return _increment;
100            }
101    
102            public String getKey() {
103                    return _name.concat(StringPool.SLASH).concat(
104                            String.valueOf(_ownerType));
105            }
106    
107            public int getLimitPeriod() {
108                    return _limitPeriod;
109            }
110    
111            public int getLimitValue() {
112                    return _limitValue;
113            }
114    
115            public String getName() {
116                    return _name;
117            }
118    
119            public int getOwnerType() {
120                    return _ownerType;
121            }
122    
123            public int getPeriodLength() {
124                    return _periodLength;
125            }
126    
127            @Override
128            public int hashCode() {
129                    int hash = HashUtil.hash(0, _enabled);
130    
131                    hash = HashUtil.hash(hash, _increment);
132                    hash = HashUtil.hash(hash, _limitEnabled);
133                    hash = HashUtil.hash(hash, _limitPeriod);
134                    hash = HashUtil.hash(hash, _limitValue);
135                    hash = HashUtil.hash(hash, _name);
136                    hash = HashUtil.hash(hash, _ownerType);
137                    hash = HashUtil.hash(hash, _periodLength);
138    
139                    return HashUtil.hash(hash, _transient);
140            }
141    
142            public boolean isEnabled() {
143                    return _enabled;
144            }
145    
146            public boolean isLimitEnabled() {
147                    return _limitEnabled;
148            }
149    
150            public boolean isTransient() {
151                    return _transient;
152            }
153    
154            public void setEnabled(boolean enabled) {
155                    _enabled = enabled;
156            }
157    
158            public void setIncrement(int increment) {
159                    _increment = increment;
160            }
161    
162            public void setLimitEnabled(boolean limitEnabled) {
163                    _limitEnabled = limitEnabled;
164            }
165    
166            public void setLimitPeriod(int limitPeriod) {
167                    _limitPeriod = limitPeriod;
168            }
169    
170            public void setLimitPeriod(String limitPeriod) {
171                    if (StringUtil.equalsIgnoreCase(limitPeriod, "day")) {
172                            setLimitPeriod(LIMIT_PERIOD_DAY);
173                    }
174                    else if (StringUtil.equalsIgnoreCase(limitPeriod, "lifetime")) {
175                            setLimitPeriod(LIMIT_PERIOD_LIFETIME);
176                    }
177                    else {
178                            setLimitPeriod(LIMIT_PERIOD_PERIOD);
179                    }
180            }
181    
182            public void setLimitValue(int limitValue) {
183                    _limitValue = limitValue;
184            }
185    
186            public void setName(String name) {
187                    _name = name;
188            }
189    
190            public void setOwnerType(int ownerType) {
191                    _ownerType = ownerType;
192            }
193    
194            public void setOwnerType(String ownerType) {
195                    if (StringUtil.equalsIgnoreCase(ownerType, "actor")) {
196                            setOwnerType(SocialActivityCounterConstants.TYPE_ACTOR);
197                    }
198                    else if (StringUtil.equalsIgnoreCase(ownerType, "asset")) {
199                            setOwnerType(SocialActivityCounterConstants.TYPE_ASSET);
200                    }
201                    else if (StringUtil.equalsIgnoreCase(ownerType, "creator")) {
202                            setOwnerType(SocialActivityCounterConstants.TYPE_CREATOR);
203                    }
204            }
205    
206            public void setPeriodLength(int periodLength) {
207                    _periodLength = periodLength;
208            }
209    
210            public void setTransient(boolean transientCounter) {
211                    _transient = transientCounter;
212            }
213    
214            private boolean _enabled = true;
215            private int _increment = 1;
216            private boolean _limitEnabled = true;
217            private int _limitPeriod = LIMIT_PERIOD_DAY;
218            private int _limitValue;
219            private String _name;
220            private int _ownerType;
221            private int _periodLength =
222                    SocialActivityCounterConstants.PERIOD_LENGTH_SYSTEM;
223            private boolean _transient;
224    
225    }