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