001    /**
002     * Copyright (c) 2000-present 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.exception.PortalException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.WebKeys;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.service.ServiceContextFactory;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portal.util.PropsValues;
027    import com.liferay.portlet.social.model.SocialActivity;
028    import com.liferay.portlet.social.model.SocialActivityFeedEntry;
029    import com.liferay.portlet.social.model.SocialActivityInterpreter;
030    import com.liferay.portlet.social.model.SocialActivitySet;
031    import com.liferay.portlet.social.model.impl.SocialActivityInterpreterImpl;
032    import com.liferay.portlet.social.model.impl.SocialRequestInterpreterImpl;
033    import com.liferay.portlet.social.service.base.SocialActivityInterpreterLocalServiceBaseImpl;
034    import com.liferay.registry.Filter;
035    import com.liferay.registry.Registry;
036    import com.liferay.registry.RegistryUtil;
037    import com.liferay.registry.ServiceReference;
038    import com.liferay.registry.ServiceRegistration;
039    import com.liferay.registry.ServiceTracker;
040    import com.liferay.registry.ServiceTrackerCustomizer;
041    import com.liferay.registry.collections.ServiceRegistrationMap;
042    
043    import java.util.ArrayList;
044    import java.util.HashMap;
045    import java.util.List;
046    import java.util.Map;
047    
048    import javax.servlet.http.HttpServletRequest;
049    
050    /**
051     * The social activity interpreter local service. Activity interpreters are
052     * classes responsible for translating activity records into human readable
053     * form. This service holds a list of interpreters and provides methods to add
054     * or remove items from this list.
055     *
056     * <p>
057     * Activity interpreters use the language files to get text fragments based on
058     * the activity's type and the type of asset on which the activity was done.
059     * Interpreters are created for specific asset types and are only capable of
060     * translating activities done on assets of those types. As an example, there is
061     * an interpreter BlogsActivityInterpreter that can only translate activity
062     * records for blog entries.
063     * </p>
064     *
065     * @author Brian Wing Shun Chan
066     */
067    public class SocialActivityInterpreterLocalServiceImpl
068            extends SocialActivityInterpreterLocalServiceBaseImpl {
069    
070            /**
071             * Adds the activity interpreter to the list of available interpreters.
072             *
073             * @param activityInterpreter the activity interpreter
074             */
075            @Override
076            public void addActivityInterpreter(
077                    SocialActivityInterpreter activityInterpreter) {
078    
079                    Registry registry = RegistryUtil.getRegistry();
080    
081                    Map<String, Object> properties = new HashMap<>();
082    
083                    SocialActivityInterpreterImpl activityInterpreterImpl =
084                            (SocialActivityInterpreterImpl)activityInterpreter;
085    
086                    properties.put(
087                            "javax.portlet.name", activityInterpreterImpl.getPortletId());
088    
089                    ServiceRegistration<SocialActivityInterpreter> serviceRegistration =
090                            registry.registerService(
091                                    SocialActivityInterpreter.class, activityInterpreter,
092                                    properties);
093    
094                    _serviceRegistrations.put(activityInterpreter, serviceRegistration);
095            }
096    
097            @Override
098            public void afterPropertiesSet() {
099                    Registry registry = RegistryUtil.getRegistry();
100    
101                    Filter filter = registry.getFilter(
102                            "(&(javax.portlet.name=*)(objectClass=" +
103                                    SocialActivityInterpreter.class.getName() + "))");
104    
105                    _serviceTracker = registry.trackServices(
106                            filter, new SocialActivityInterpreterServiceTrackerCustomizer());
107    
108                    _serviceTracker.open();
109            }
110    
111            /**
112             * Removes the activity interpreter from the list of available interpreters.
113             *
114             * @param activityInterpreter the activity interpreter
115             */
116            @Override
117            public void deleteActivityInterpreter(
118                    SocialActivityInterpreter activityInterpreter) {
119    
120                    ServiceRegistration<SocialActivityInterpreter> serviceRegistration =
121                            _serviceRegistrations.remove(activityInterpreter);
122    
123                    if (serviceRegistration != null) {
124                            serviceRegistration.unregister();
125                    }
126            }
127    
128            @Override
129            public Map<String, List<SocialActivityInterpreter>>
130                    getActivityInterpreters() {
131    
132                    return _activityInterpreters;
133            }
134    
135            @Override
136            public List<SocialActivityInterpreter> getActivityInterpreters(
137                    String selector) {
138    
139                    return _activityInterpreters.get(selector);
140            }
141    
142            /**
143             * @deprecated As of 6.2.0, replaced by {@link #interpret(String,
144             *             SocialActivity, ServiceContext)}
145             */
146            @Deprecated
147            @Override
148            public SocialActivityFeedEntry interpret(
149                    SocialActivity activity, ThemeDisplay themeDisplay) {
150    
151                    ServiceContext serviceContext = null;
152    
153                    try {
154                            serviceContext = ServiceContextFactory.getInstance(
155                                    themeDisplay.getRequest());
156                    }
157                    catch (Exception e) {
158                            return null;
159                    }
160    
161                    return interpret(StringPool.BLANK, activity, serviceContext);
162            }
163    
164            /**
165             * Creates a human readable activity feed entry for the activity using an
166             * available compatible activity interpreter.
167             *
168             * <p>
169             * This method finds the appropriate interpreter for the activity by going
170             * through the available interpreters and asking them if they can handle the
171             * asset type of the activity.
172             * </p>
173             *
174             * @param  selector the context in which the activity interpreter is used
175             * @param  activity the activity to be translated to human readable form
176             * @param  serviceContext the service context to be applied
177             * @return the activity feed that is a human readable form of the activity
178             *         record or <code>null</code> if a compatible interpreter is not
179             *         found
180             */
181            @Override
182            public SocialActivityFeedEntry interpret(
183                    String selector, SocialActivity activity,
184                    ServiceContext serviceContext) {
185    
186                    HttpServletRequest request = serviceContext.getRequest();
187    
188                    if (request == null) {
189                            return null;
190                    }
191    
192                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
193                            WebKeys.THEME_DISPLAY);
194    
195                    try {
196                            if (activity.getUserId() == themeDisplay.getDefaultUserId()) {
197                                    return null;
198                            }
199                    }
200                    catch (Exception e) {
201                            _log.error(e, e);
202                    }
203    
204                    if (activity.getMirrorActivityId() > 0) {
205                            SocialActivity mirrorActivity = null;
206    
207                            try {
208                                    mirrorActivity = socialActivityLocalService.getActivity(
209                                            activity.getMirrorActivityId());
210                            }
211                            catch (Exception e) {
212                            }
213    
214                            if (mirrorActivity != null) {
215                                    activity = mirrorActivity;
216                            }
217                    }
218    
219                    List<SocialActivityInterpreter> activityInterpreters =
220                            _activityInterpreters.get(selector);
221    
222                    if (activityInterpreters == null) {
223                            return null;
224                    }
225    
226                    String className = PortalUtil.getClassName(activity.getClassNameId());
227    
228                    for (int i = 0; i < activityInterpreters.size(); i++) {
229                            SocialActivityInterpreterImpl activityInterpreter =
230                                    (SocialActivityInterpreterImpl)activityInterpreters.get(i);
231    
232                            if (activityInterpreter.hasClassName(className)) {
233                                    SocialActivityFeedEntry activityFeedEntry =
234                                            activityInterpreter.interpret(activity, serviceContext);
235    
236                                    if (activityFeedEntry != null) {
237                                            activityFeedEntry.setPortletId(
238                                                    activityInterpreter.getPortletId());
239    
240                                            return activityFeedEntry;
241                                    }
242                            }
243                    }
244    
245                    return null;
246            }
247    
248            @Override
249            public SocialActivityFeedEntry interpret(
250                    String selector, SocialActivitySet activitySet,
251                    ServiceContext serviceContext) {
252    
253                    HttpServletRequest request = serviceContext.getRequest();
254    
255                    if (request == null) {
256                            return null;
257                    }
258    
259                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
260                            WebKeys.THEME_DISPLAY);
261    
262                    try {
263                            if (activitySet.getUserId() == themeDisplay.getDefaultUserId()) {
264                                    return null;
265                            }
266                    }
267                    catch (Exception e) {
268                            _log.error(e, e);
269                    }
270    
271                    List<SocialActivityInterpreter> activityInterpreters =
272                            _activityInterpreters.get(selector);
273    
274                    if (activityInterpreters == null) {
275                            return null;
276                    }
277    
278                    String className = PortalUtil.getClassName(
279                            activitySet.getClassNameId());
280    
281                    for (int i = 0; i < activityInterpreters.size(); i++) {
282                            SocialActivityInterpreterImpl activityInterpreter =
283                                    (SocialActivityInterpreterImpl)activityInterpreters.get(i);
284    
285                            if (activityInterpreter.hasClassName(className)) {
286                                    SocialActivityFeedEntry activityFeedEntry =
287                                            activityInterpreter.interpret(activitySet, serviceContext);
288    
289                                    if (activityFeedEntry != null) {
290                                            activityFeedEntry.setPortletId(
291                                                    activityInterpreter.getPortletId());
292    
293                                            return activityFeedEntry;
294                                    }
295                            }
296                    }
297    
298                    return null;
299            }
300    
301            @Override
302            public void updateActivitySet(long activityId) throws PortalException {
303                    if (!PropsValues.SOCIAL_ACTIVITY_SETS_BUNDLING_ENABLED) {
304                            socialActivitySetLocalService.addActivitySet(activityId);
305    
306                            return;
307                    }
308    
309                    List<SocialActivityInterpreter> activityInterpreters =
310                            _activityInterpreters.get(
311                                    PropsValues.SOCIAL_ACTIVITY_SETS_SELECTOR);
312    
313                    if (activityInterpreters != null) {
314                            SocialActivity activity =
315                                    socialActivityPersistence.findByPrimaryKey(activityId);
316    
317                            String className = PortalUtil.getClassName(
318                                    activity.getClassNameId());
319    
320                            for (int i = 0; i < activityInterpreters.size(); i++) {
321                                    SocialActivityInterpreterImpl activityInterpreter =
322                                            (SocialActivityInterpreterImpl)activityInterpreters.get(i);
323    
324                                    if (activityInterpreter.hasClassName(className)) {
325                                            activityInterpreter.updateActivitySet(activityId);
326    
327                                            return;
328                                    }
329                            }
330                    }
331            }
332    
333            private static final Log _log = LogFactoryUtil.getLog(
334                    SocialActivityInterpreterLocalServiceImpl.class);
335    
336            private final Map<String, List<SocialActivityInterpreter>>
337                    _activityInterpreters = new HashMap<>();
338            private final ServiceRegistrationMap<SocialActivityInterpreter>
339                    _serviceRegistrations = new ServiceRegistrationMap<>();
340            private ServiceTracker<SocialActivityInterpreter, SocialActivityInterpreter>
341                    _serviceTracker;
342    
343            private class SocialActivityInterpreterServiceTrackerCustomizer
344                    implements ServiceTrackerCustomizer
345                            <SocialActivityInterpreter, SocialActivityInterpreter> {
346    
347                    @Override
348                    public SocialActivityInterpreter addingService(
349                            ServiceReference<SocialActivityInterpreter> serviceReference) {
350    
351                            Registry registry = RegistryUtil.getRegistry();
352    
353                            SocialActivityInterpreter activityInterpreter = registry.getService(
354                                    serviceReference);
355    
356                            String portletId = (String)serviceReference.getProperty(
357                                    "javax.portlet.name");
358    
359                            if (!(activityInterpreter instanceof
360                                            SocialRequestInterpreterImpl)) {
361    
362                                    activityInterpreter = new SocialActivityInterpreterImpl(
363                                            portletId, activityInterpreter);
364                            }
365    
366                            List<SocialActivityInterpreter> activityInterpreters =
367                                    _activityInterpreters.get(activityInterpreter.getSelector());
368    
369                            if (activityInterpreters == null) {
370                                    activityInterpreters = new ArrayList<>();
371                            }
372    
373                            activityInterpreters.add(activityInterpreter);
374    
375                            _activityInterpreters.put(
376                                    activityInterpreter.getSelector(), activityInterpreters);
377    
378                            return activityInterpreter;
379                    }
380    
381                    @Override
382                    public void modifiedService(
383                            ServiceReference<SocialActivityInterpreter> serviceReference,
384                            SocialActivityInterpreter activityInterpreter) {
385                    }
386    
387                    @Override
388                    public void removedService(
389                            ServiceReference<SocialActivityInterpreter> serviceReference,
390                            SocialActivityInterpreter activityInterpreter) {
391    
392                            Registry registry = RegistryUtil.getRegistry();
393    
394                            registry.ungetService(serviceReference);
395    
396                            List<SocialActivityInterpreter> activityInterpreters =
397                                    _activityInterpreters.get(activityInterpreter.getSelector());
398    
399                            if (activityInterpreters == null) {
400                                    return;
401                            }
402    
403                            activityInterpreters.remove(activityInterpreter);
404                    }
405    
406            }
407    
408    }