001    /**
002     * Copyright (c) 2000-2010 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.util.ListUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.security.permission.ResourceActionsUtil;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portlet.social.NoSuchEquitySettingException;
028    import com.liferay.portlet.social.model.SocialEquityActionMapping;
029    import com.liferay.portlet.social.model.SocialEquitySetting;
030    import com.liferay.portlet.social.model.SocialEquitySettingConstants;
031    import com.liferay.portlet.social.model.impl.SocialEquitySettingImpl;
032    import com.liferay.portlet.social.service.base.SocialEquitySettingLocalServiceBaseImpl;
033    
034    import java.util.Collections;
035    import java.util.List;
036    
037    /**
038     * @author Zsolt Berentey
039     * @author Brian Wing Shun Chan
040     */
041    public class SocialEquitySettingLocalServiceImpl
042            extends SocialEquitySettingLocalServiceBaseImpl {
043    
044            public static final String CACHE_NAME =
045                    SocialEquitySettingLocalServiceImpl.class.getName();
046    
047            public List<SocialEquitySetting> getEquitySettings(
048                            long groupId, String className, String actionId)
049                    throws SystemException {
050    
051                    long classNameId = PortalUtil.getClassNameId(className);
052    
053                    return getEquitySettings(groupId, classNameId, actionId);
054            }
055    
056            public List<SocialEquitySetting> getEquitySettings(
057                            long groupId, long classNameId, String actionId)
058                    throws SystemException {
059    
060                    String key = encodeKey(classNameId, actionId);
061    
062                    List<SocialEquitySetting> socialEquitySettings =
063                            (List<SocialEquitySetting>)_portalCache.get(key);
064    
065                    if (socialEquitySettings != null) {
066                            return socialEquitySettings;
067                    }
068    
069                    String className = PortalUtil.getClassName(classNameId);
070    
071                    SocialEquityActionMapping equityActionMapping =
072                            ResourceActionsUtil.getSocialEquityActionMapping(
073                                    className, actionId);
074    
075                    if (equityActionMapping == null) {
076                            socialEquitySettings = Collections.EMPTY_LIST;
077    
078                            _portalCache.put(key, socialEquitySettings);
079    
080                            return socialEquitySettings;
081                    }
082    
083                    List<SocialEquitySetting> equitySettings =
084                            socialEquitySettingPersistence.findByG_C_A(
085                                    groupId, classNameId, actionId);
086    
087                    equitySettings = ListUtil.copy(equitySettings);
088    
089                    if (equitySettings.isEmpty()) {
090                            if (equityActionMapping.getInformationValue() > 0) {
091                                    SocialEquitySetting informationEquitySetting =
092                                            getInformationEquitySetting(actionId, equityActionMapping);
093    
094                                    equitySettings.add(informationEquitySetting);
095                            }
096    
097                            if (equityActionMapping.getParticipationValue() > 0) {
098                                    SocialEquitySetting participationEquitySetting =
099                                            getParticipationEquitySetting(
100                                                    actionId, equityActionMapping);
101    
102                                    equitySettings.add(participationEquitySetting);
103                            }
104                    }
105                    else if (equitySettings.size() == 1) {
106                            SocialEquitySetting equitySetting = equitySettings.get(0);
107    
108                            if ((equityActionMapping.getInformationValue() > 0) &&
109                                    (equitySetting.getType() ==
110                                            SocialEquitySettingConstants.TYPE_PARTICIPATION)) {
111    
112                                    SocialEquitySetting informationEquitySetting =
113                                            getInformationEquitySetting(actionId, equityActionMapping);
114    
115                                    equitySettings.add(informationEquitySetting);
116                            }
117                            else if ((equityActionMapping.getParticipationValue() > 0) &&
118                                             (equitySetting.getType() ==
119                                                    SocialEquitySettingConstants.TYPE_INFORMATION)) {
120    
121                                    SocialEquitySetting participationEquitySetting =
122                                            getParticipationEquitySetting(
123                                                    actionId, equityActionMapping);
124    
125                                    equitySettings.add(participationEquitySetting);
126                            }
127                    }
128    
129                    _portalCache.put(key, socialEquitySettings);
130    
131                    return equitySettings;
132            }
133    
134            public void updateSocialEquitySettings(
135                            long groupId, String className,
136                            List<SocialEquityActionMapping> equityActionMappings)
137                    throws PortalException, SystemException {
138    
139                    for (SocialEquityActionMapping equityActionMapping :
140                                    equityActionMappings) {
141    
142                            updateSocialEquitySettings(groupId, className, equityActionMapping);
143                    }
144            }
145    
146            protected String encodeKey(long classNameId, String actionId) {
147                    StringBundler sb = new StringBundler(5);
148    
149                    sb.append(CACHE_NAME);
150                    sb.append(StringPool.POUND);
151                    sb.append(classNameId);
152                    sb.append(StringPool.POUND);
153                    sb.append(actionId);
154    
155                    return sb.toString();
156            }
157    
158            protected SocialEquitySetting getEquitySetting(
159                            long groupId, String className, String actionId, int type)
160                    throws PortalException, SystemException {
161    
162                    long classNameId = PortalUtil.getClassNameId(className);
163    
164                    SocialEquitySetting equitySetting = null;
165    
166                    try {
167                            equitySetting = socialEquitySettingPersistence.findByG_C_A_T(
168                                    groupId, classNameId, actionId, type);
169                    }
170                    catch (NoSuchEquitySettingException nsqse) {
171                            Group group = groupLocalService.getGroup(groupId);
172    
173                            long equitySettingId = counterLocalService.increment();
174    
175                            equitySetting = socialEquitySettingPersistence.create(
176                                    equitySettingId);
177    
178                            equitySetting.setGroupId(groupId);
179                            equitySetting.setCompanyId(group.getCompanyId());
180                            equitySetting.setClassNameId(classNameId);
181                            equitySetting.setActionId(actionId);
182                            equitySetting.setType(type);
183                    }
184    
185                    return equitySetting;
186            }
187    
188            protected SocialEquitySetting getInformationEquitySetting(
189                    String actionId, SocialEquityActionMapping equityActionMapping) {
190    
191                    SocialEquitySetting equitySetting = new SocialEquitySettingImpl();
192    
193                    equitySetting.setActionId(actionId);
194                    equitySetting.setDailyLimit(
195                            equityActionMapping.getInformationDailyLimit());
196                    equitySetting.setLifespan(equityActionMapping.getInformationLifespan());
197                    equitySetting.setType(SocialEquitySettingConstants.TYPE_INFORMATION);
198                    equitySetting.setUniqueEntry(equityActionMapping.isInformationUnique());
199                    equitySetting.setValue(equityActionMapping.getInformationValue());
200    
201                    return equitySetting;
202            }
203    
204            protected SocialEquitySetting getParticipationEquitySetting(
205                    String actionId, SocialEquityActionMapping equityActionMapping) {
206    
207                    SocialEquitySetting equitySetting = new SocialEquitySettingImpl();
208    
209                    equitySetting.setActionId(actionId);
210                    equitySetting.setDailyLimit(
211                            equityActionMapping.getParticipationDailyLimit());
212                    equitySetting.setLifespan(
213                            equityActionMapping.getParticipationLifespan());
214                    equitySetting.setType(SocialEquitySettingConstants.TYPE_PARTICIPATION);
215                    equitySetting.setUniqueEntry(
216                            equityActionMapping.isParticipationUnique());
217                    equitySetting.setValue(equityActionMapping.getParticipationValue());
218    
219                    return equitySetting;
220            }
221    
222            protected void updateSocialEquitySettings(
223                            long groupId, String className,
224                            SocialEquityActionMapping equityActionMapping)
225                    throws PortalException, SystemException {
226    
227                    SocialEquityActionMapping defaultEquityActionMapping =
228                            PortalUtil.getSocialEquityActionMapping(
229                                    className, equityActionMapping.getActionId());
230    
231                    if ((equityActionMapping.getInformationValue() > 0) &&
232                            !equityActionMapping.equals(
233                                    defaultEquityActionMapping,
234                                    SocialEquitySettingConstants.TYPE_INFORMATION)) {
235    
236                            SocialEquitySetting equitySetting = getEquitySetting(
237                                    groupId, className, equityActionMapping.getActionId(),
238                                    SocialEquitySettingConstants.TYPE_INFORMATION);
239    
240                            if (!equityActionMapping.equals(equitySetting)) {
241                                    equitySetting.update(equityActionMapping);
242    
243                                    socialEquitySettingPersistence.update(equitySetting, false);
244                            }
245                    }
246    
247                    if ((equityActionMapping.getParticipationValue() > 0) &&
248                            !equityActionMapping.equals(
249                                    defaultEquityActionMapping,
250                                    SocialEquitySettingConstants.TYPE_PARTICIPATION)) {
251    
252                            SocialEquitySetting equitySetting = getEquitySetting(
253                                    groupId, className, equityActionMapping.getActionId(),
254                                    SocialEquitySettingConstants.TYPE_PARTICIPATION);
255    
256                            if (!equityActionMapping.equals(equitySetting)) {
257                                    equitySetting.update(equityActionMapping);
258    
259                                    socialEquitySettingPersistence.update(equitySetting, false);
260                            }
261                    }
262    
263                    long classNameId = PortalUtil.getClassNameId(className);
264    
265                    String key = encodeKey(classNameId, equityActionMapping.getActionId());
266    
267                    _portalCache.remove(key);
268            }
269    
270            private static PortalCache _portalCache = MultiVMPoolUtil.getCache(
271                    CACHE_NAME);
272    
273    }