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