001
014
015 package com.liferay.portlet.social.model;
016
017 import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.exception.SystemException;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.util.GetterUtil;
023 import com.liferay.portal.kernel.util.StringPool;
024 import com.liferay.portal.kernel.util.StringUtil;
025 import com.liferay.portal.model.User;
026 import com.liferay.portal.util.PortalUtil;
027 import com.liferay.portlet.asset.model.AssetEntry;
028 import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
029 import com.liferay.portlet.social.service.SocialActivityAchievementLocalServiceUtil;
030 import com.liferay.portlet.social.service.SocialActivityCounterLocalServiceUtil;
031
032
035 public class BaseSocialAchievement implements SocialAchievement {
036
037 public int getCounterIncrement() {
038 return _counterIncrement;
039 }
040
041 public String getCounterName() {
042 return _counterName;
043 }
044
045 public String getCounterOwner() {
046 return _counterOwner;
047 }
048
049 public int getCounterPeriodLength() {
050 return _counterPeriodLength;
051 }
052
053 public int getCounterThreshold() {
054 return _counterThreshold;
055 }
056
057 public String getDescriptionKey() {
058 return _ACHIEVEMENT_DESCRIPTION_PREFIX.concat(_name);
059 }
060
061 public String getIcon() {
062 if (_icon == null) {
063 return _name.concat(_ICON_SUFFIX);
064 }
065
066 return _icon;
067 }
068
069 public String getName() {
070 return _name;
071 }
072
073 public String getNameKey() {
074 return _ACHIEVEMENT_NAME_PREFIX.concat(_name);
075 }
076
077 public void initialize(SocialActivityDefinition activityDefinition) {
078 SocialActivityCounterDefinition activityCounterDefinition =
079 activityDefinition.getActivityCounterDefinition(_counterName);
080
081 if (activityCounterDefinition != null) {
082 return;
083 }
084
085 activityCounterDefinition = new SocialActivityCounterDefinition();
086
087 activityCounterDefinition.setEnabled(true);
088 activityCounterDefinition.setIncrement(_counterIncrement);
089 activityCounterDefinition.setName(_counterName);
090 activityCounterDefinition.setOwnerType(_counterOwner);
091
092 if (_counterPeriodLength > 0) {
093 activityCounterDefinition.setPeriodLength(_counterPeriodLength);
094 activityCounterDefinition.setTransient(true);
095 }
096
097 activityDefinition.addCounter(activityCounterDefinition);
098 }
099
100 public void processActivity(SocialActivity activity) {
101 try {
102 doProcessActivity(activity);
103 }
104 catch (Exception e) {
105 if (_log.isWarnEnabled()) {
106 _log.warn("Unable to process activity", e);
107 }
108 }
109 }
110
111 public void setCounterIncrement(int counterIncrement) {
112 _counterIncrement = counterIncrement;
113 }
114
115 public void setCounterName(String counterName) {
116 _counterName = counterName;
117 }
118
119 public void setCounterOwner(String counterOwner) {
120 _counterOwner = counterOwner;
121
122 if (counterOwner.equalsIgnoreCase("actor")) {
123 _ownerType = SocialActivityCounterConstants.TYPE_ACTOR;
124 }
125 else if (counterOwner.equalsIgnoreCase("asset")) {
126 _ownerType = SocialActivityCounterConstants.TYPE_ASSET;
127 }
128 else if (counterOwner.equalsIgnoreCase("creator")) {
129 _ownerType = SocialActivityCounterConstants.TYPE_CREATOR;
130 }
131 }
132
133 public void setCounterPeriodLength(int counterPeriodLength) {
134 _counterPeriodLength = counterPeriodLength;
135 }
136
137 public void setCounterThreshold(int counterThreshold) {
138 _counterThreshold = counterThreshold;
139 }
140
141 public void setIcon(String icon) {
142 _icon = icon;
143 }
144
145 public void setName(String name) {
146 name = StringUtil.replace(name, StringPool.SPACE, StringPool.UNDERLINE);
147 name = name.toLowerCase();
148
149 _name = StringUtil.extract(name, _NAME_SUPPORTED_CHARS);
150 }
151
152 public void setProperty(String name, String value) {
153 if (name.equals("counterIncrement") ||
154 name.equals("counterPeriodLength") ||
155 name.equals("counterThreshold")) {
156
157 BeanPropertiesUtil.setProperty(
158 this, name, GetterUtil.getInteger(value, 0));
159 }
160 else {
161 BeanPropertiesUtil.setProperty(this, name, value);
162 }
163 }
164
165 protected void doProcessActivity(SocialActivity activity)
166 throws PortalException, SystemException {
167
168 if (_counterThreshold == 0) {
169 return;
170 }
171
172 int count =
173 SocialActivityAchievementLocalServiceUtil.getUserAchievementCount(
174 activity.getUserId(), activity.getGroupId(), _name);
175
176 if (count > 0) {
177 return;
178 }
179
180 long classNameId = activity.getClassNameId();
181 long classPK = activity.getClassPK();
182
183 if (_ownerType != SocialActivityCounterConstants.TYPE_ASSET) {
184 classNameId = PortalUtil.getClassNameId(User.class);
185 classPK = activity.getUserId();
186 }
187
188 if (_ownerType == SocialActivityCounterConstants.TYPE_ASSET) {
189 AssetEntry assetEntry = AssetEntryLocalServiceUtil.fetchEntry(
190 activity.getClassName(), activity.getClassPK());
191
192 if (assetEntry != null) {
193 classPK = assetEntry.getUserId();
194 }
195 }
196
197 SocialActivityCounter activityCounter =
198 SocialActivityCounterLocalServiceUtil.fetchLatestActivityCounter(
199 activity.getGroupId(), classNameId, classPK, _counterName,
200 _ownerType);
201
202 if ((activityCounter != null) &&
203 (activityCounter.getCurrentValue() >= _counterThreshold)) {
204
205 SocialActivityAchievementLocalServiceUtil.addActivityAchievement(
206 activity.getUserId(), activity.getGroupId(), this);
207 }
208 }
209
210 private static final String _ACHIEVEMENT_DESCRIPTION_PREFIX =
211 "social.achievement.description.";
212
213 private static final String _ACHIEVEMENT_NAME_PREFIX =
214 "social.achievement.name.";
215
216 private static final String _ICON_SUFFIX = "-icon.jpg";
217
218 private static final char[] _NAME_SUPPORTED_CHARS =
219 "abcdefghijklmnopqrstuvwxyz123456789_-.".toCharArray();
220
221 private static Log _log = LogFactoryUtil.getLog(
222 BaseSocialAchievement.class);
223
224 private int _counterIncrement = 1;
225 private String _counterName;
226 private String _counterOwner;
227 private int _counterPeriodLength =
228 SocialActivityCounterConstants.PERIOD_LENGTH_SYSTEM;
229 private int _counterThreshold;
230 private String _icon;
231 private String _name;
232 private int _ownerType;
233
234 }