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