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.service.impl;
016    
017    import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
018    import com.liferay.portal.kernel.cache.PortalCache;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.json.JSONFactoryUtil;
022    import com.liferay.portal.kernel.json.JSONObject;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.model.Group;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portlet.social.model.SocialActivityCounterDefinition;
029    import com.liferay.portlet.social.model.SocialActivityDefinition;
030    import com.liferay.portlet.social.model.SocialActivitySetting;
031    import com.liferay.portlet.social.model.SocialActivitySettingConstants;
032    import com.liferay.portlet.social.service.base.SocialActivitySettingLocalServiceBaseImpl;
033    import com.liferay.portlet.social.util.SocialConfigurationUtil;
034    
035    import java.util.ArrayList;
036    import java.util.List;
037    
038    /**
039     * @author Zsolt Berentey
040     */
041    public class SocialActivitySettingLocalServiceImpl
042            extends SocialActivitySettingLocalServiceBaseImpl {
043    
044            @Override
045            public SocialActivityDefinition getActivityDefinition(
046                            long groupId, String className, int activityType)
047                    throws SystemException {
048    
049                    String key = encodeKey(groupId, className, activityType);
050    
051                    SocialActivityDefinition activityDefinition =
052                            (SocialActivityDefinition)_activityDefinitions.get(key);
053    
054                    if (activityDefinition != null) {
055                            return activityDefinition;
056                    }
057    
058                    SocialActivityDefinition defaultActivityDefinition =
059                            SocialConfigurationUtil.getActivityDefinition(
060                                    className, activityType);
061    
062                    if (defaultActivityDefinition == null) {
063                            return null;
064                    }
065    
066                    activityDefinition = getActivityDefinition(
067                            groupId, className, activityType, defaultActivityDefinition);
068    
069                    _activityDefinitions.put(key, activityDefinition);
070    
071                    return activityDefinition;
072            }
073    
074            @Override
075            public List<SocialActivityDefinition> getActivityDefinitions(
076                            long groupId, String className)
077                    throws SystemException {
078    
079                    List<SocialActivityDefinition> activityDefinitions =
080                            new ArrayList<SocialActivityDefinition>();
081    
082                    List<SocialActivityDefinition> defaultActivityDefinitions =
083                            SocialConfigurationUtil.getActivityDefinitions(className);
084    
085                    for (SocialActivityDefinition defaultActivityDefinition :
086                                    defaultActivityDefinitions) {
087    
088                            SocialActivityDefinition activityDefinition = getActivityDefinition(
089                                    groupId, className,
090                                    defaultActivityDefinition.getActivityType());
091    
092                            activityDefinitions.add(activityDefinition);
093                    }
094    
095                    return activityDefinitions;
096            }
097    
098            @Override
099            public List<SocialActivitySetting> getActivitySettings(long groupId)
100                    throws SystemException {
101    
102                    return socialActivitySettingPersistence.findByG_A(groupId, 0);
103            }
104    
105            @Override
106            public boolean isEnabled(long groupId, long classNameId)
107                    throws SystemException {
108    
109                    SocialActivitySetting activitySetting =
110                            socialActivitySettingPersistence.fetchByG_C_A_N(
111                                    groupId, classNameId, 0,
112                                    SocialActivitySettingConstants.NAME_ENABLED);
113    
114                    if (activitySetting == null) {
115                            return false;
116                    }
117    
118                    return GetterUtil.getBoolean(activitySetting.getValue());
119            }
120    
121            @Override
122            public void updateActivitySetting(
123                            long groupId, String className, boolean enabled)
124                    throws PortalException, SystemException {
125    
126                    long classNameId = PortalUtil.getClassNameId(className);
127    
128                    SocialActivitySetting activitySetting =
129                            socialActivitySettingPersistence.fetchByG_C_A_N(
130                                    groupId, classNameId, 0,
131                                    SocialActivitySettingConstants.NAME_ENABLED);
132    
133                    if (activitySetting == null) {
134                            Group group = groupLocalService.getGroup(groupId);
135    
136                            long activitySettingId = counterLocalService.increment();
137    
138                            activitySetting = socialActivitySettingPersistence.create(
139                                    activitySettingId);
140    
141                            activitySetting.setGroupId(groupId);
142                            activitySetting.setCompanyId(group.getCompanyId());
143                            activitySetting.setClassNameId(classNameId);
144                            activitySetting.setName(
145                                    SocialActivitySettingConstants.NAME_ENABLED);
146                    }
147    
148                    activitySetting.setValue(String.valueOf(enabled));
149    
150                    socialActivitySettingPersistence.update(activitySetting, false);
151            }
152    
153            @Override
154            public void updateActivitySetting(
155                            long groupId, String className, int activityType,
156                            SocialActivityCounterDefinition activityCounterDefinition)
157                    throws PortalException, SystemException {
158    
159                    long classNameId = PortalUtil.getClassNameId(className);
160    
161                    SocialActivityDefinition defaultActivityDefinition =
162                            SocialConfigurationUtil.getActivityDefinition(
163                                    className, activityType);
164    
165                    SocialActivityCounterDefinition defaultActivityCounterDefinition =
166                            defaultActivityDefinition.getActivityCounterDefinition(
167                                    activityCounterDefinition.getName());
168    
169                    SocialActivitySetting activitySetting =
170                            socialActivitySettingPersistence.fetchByG_C_A_N(
171                                    groupId, classNameId, activityType,
172                                    activityCounterDefinition.getName());
173    
174                    if ((defaultActivityCounterDefinition != null) &&
175                            defaultActivityCounterDefinition.equals(
176                                    activityCounterDefinition)) {
177    
178                            if (activitySetting != null) {
179                                    socialActivitySettingPersistence.remove(activitySetting);
180                            }
181    
182                            return;
183                    }
184    
185                    if (activitySetting != null) {
186                            activitySetting.setValue(toJSON(activityCounterDefinition));
187                    }
188                    else {
189                            Group group = groupLocalService.getGroup(groupId);
190    
191                            long activitySettingId = counterLocalService.increment();
192    
193                            activitySetting = socialActivitySettingPersistence.create(
194                                    activitySettingId);
195    
196                            activitySetting.setGroupId(groupId);
197                            activitySetting.setCompanyId(group.getCompanyId());
198                            activitySetting.setClassNameId(classNameId);
199                            activitySetting.setActivityType(activityType);
200                            activitySetting.setName(activityCounterDefinition.getName());
201                            activitySetting.setValue(toJSON(activityCounterDefinition));
202                    }
203    
204                    socialActivitySettingPersistence.update(activitySetting, false);
205    
206                    String key = encodeKey(groupId, className, activityType);
207    
208                    _activityDefinitions.remove(key);
209            }
210    
211            @Override
212            public void updateActivitySettings(
213                            long groupId, String className, int activityType,
214                            List<SocialActivityCounterDefinition> activityCounterDefinitions)
215                    throws PortalException, SystemException {
216    
217                    for (SocialActivityCounterDefinition activityCounterDefinition :
218                                    activityCounterDefinitions) {
219    
220                            updateActivitySetting(
221                                    groupId, className, activityType, activityCounterDefinition);
222                    }
223            }
224    
225            protected String encodeKey(
226                    long groupId, String className, int activityType) {
227    
228                    StringBundler sb = new StringBundler(5);
229    
230                    sb.append(groupId);
231                    sb.append(StringPool.POUND);
232                    sb.append(className);
233                    sb.append(StringPool.POUND);
234                    sb.append(activityType);
235    
236                    return sb.toString();
237            }
238    
239            protected SocialActivityDefinition getActivityDefinition(
240                            long groupId, String className, int activityType,
241                            SocialActivityDefinition defaultActivityDefinition)
242                    throws SystemException {
243    
244                    SocialActivityDefinition activityDefinition =
245                            defaultActivityDefinition.clone();
246    
247                    List<SocialActivitySetting> activitySettings = getActivitySettings(
248                            groupId, className, defaultActivityDefinition.getActivityType());
249    
250                    for (SocialActivitySetting activitySetting : activitySettings) {
251                            String name = activitySetting.getName();
252    
253                            if (name.equals(SocialActivitySettingConstants.NAME_ENABLED)) {
254                                    activityDefinition.setEnabled(
255                                            GetterUtil.getBoolean(
256                                                    activitySetting.getValue(),
257                                                    defaultActivityDefinition.isEnabled()));
258                            }
259                            else if (name.equals(
260                                                    SocialActivitySettingConstants.NAME_LOG_ENABLED)) {
261    
262                                    activityDefinition.setLogActivity(
263                                            GetterUtil.getBoolean(
264                                                    activitySetting.getValue(),
265                                                    defaultActivityDefinition.isLogActivity()));
266                            }
267                            else {
268                                    JSONObject jsonObject = null;
269    
270                                    try {
271                                            jsonObject = JSONFactoryUtil.createJSONObject(
272                                                    activitySetting.getValue());
273                                    }
274                                    catch (Exception e) {
275                                            jsonObject = JSONFactoryUtil.createJSONObject();
276                                    }
277    
278                                    SocialActivityCounterDefinition activityCounterDefinition =
279                                            activityDefinition.getActivityCounterDefinition(name);
280    
281                                    if (activityCounterDefinition == null) {
282                                            activityCounterDefinition =
283                                                    new SocialActivityCounterDefinition();
284    
285                                            activityCounterDefinition.setName(name);
286    
287                                            activityDefinition.addCounter(activityCounterDefinition);
288                                    }
289    
290                                    activityCounterDefinition.setEnabled(
291                                            jsonObject.getBoolean("enabled"));
292                                    activityCounterDefinition.setIncrement(
293                                            jsonObject.getInt("value"));
294                                    activityCounterDefinition.setLimitEnabled(
295                                            jsonObject.getBoolean("limitEnabled"));
296                                    activityCounterDefinition.setLimitPeriod(
297                                            jsonObject.getInt("limitPeriod"));
298                                    activityCounterDefinition.setLimitValue(
299                                            jsonObject.getInt("limitValue"));
300                                    activityCounterDefinition.setOwnerType(
301                                            jsonObject.getInt("ownerType"));
302                            }
303                    }
304    
305                    return activityDefinition;
306            }
307    
308            protected List<SocialActivitySetting> getActivitySettings(
309                            long groupId, String className, int activityType)
310                    throws SystemException {
311    
312                    long classNameId = PortalUtil.getClassNameId(className);
313    
314                    List<SocialActivitySetting> activitySettings =
315                            socialActivitySettingPersistence.findByG_C_A(
316                                    groupId, classNameId, activityType);
317    
318                    return activitySettings;
319            }
320    
321            protected String toJSON(
322                    SocialActivityCounterDefinition activityCounterDefinition) {
323    
324                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
325    
326                    jsonObject.put("enabled", activityCounterDefinition.isEnabled());
327                    jsonObject.put(
328                            "limitEnabled", activityCounterDefinition.isLimitEnabled());
329                    jsonObject.put(
330                            "limitPeriod", activityCounterDefinition.getLimitPeriod());
331                    jsonObject.put("limitValue", activityCounterDefinition.getLimitValue());
332                    jsonObject.put("ownerType", activityCounterDefinition.getOwnerType());
333                    jsonObject.put("value", activityCounterDefinition.getIncrement());
334    
335                    return jsonObject.toString();
336            }
337    
338            private static PortalCache _activityDefinitions = MultiVMPoolUtil.getCache(
339                    SocialActivitySettingLocalServiceImpl.class.getName());
340    
341    }