001    /**
002     * Copyright (c) 2000-2012 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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.theme.ThemeDisplay;
020    import com.liferay.portal.util.PortalUtil;
021    import com.liferay.portlet.social.model.SocialActivity;
022    import com.liferay.portlet.social.model.SocialActivityFeedEntry;
023    import com.liferay.portlet.social.model.SocialActivityInterpreter;
024    import com.liferay.portlet.social.model.impl.SocialActivityInterpreterImpl;
025    import com.liferay.portlet.social.service.base.SocialActivityInterpreterLocalServiceBaseImpl;
026    
027    import java.util.ArrayList;
028    import java.util.List;
029    
030    /**
031     * The social activity interpreter local service. Activity interpreters are
032     * classes responsible for translating activity records into human readable
033     * form. This service holds a list of interpreters and provides methods to add
034     * or remove items from this list.
035     *
036     * <p>
037     * Activity interpreters use the language files to get text fragments based on
038     * the activity's type and the type of asset on which the activity was done.
039     * Interpreters are created for specific asset types and are only capable of
040     * translating activities done on assets of those types. As an example, there is
041     * an interpreter BlogsActivityInterpreter that can only translate activity
042     * records for blog entries.
043     * </p>
044     *
045     * @author Brian Wing Shun Chan
046     */
047    public class SocialActivityInterpreterLocalServiceImpl
048            extends SocialActivityInterpreterLocalServiceBaseImpl {
049    
050            /**
051             * Adds the activity interpreter to the list of available interpreters.
052             *
053             * @param activityInterpreter the activity interpreter
054             */
055            public void addActivityInterpreter(
056                    SocialActivityInterpreter activityInterpreter) {
057    
058                    _activityInterpreters.add(activityInterpreter);
059            }
060    
061            /**
062             * Removes the activity interpreter from the list of available interpreters.
063             *
064             * @param activityInterpreter the activity interpreter
065             */
066            public void deleteActivityInterpreter(
067                    SocialActivityInterpreter activityInterpreter) {
068    
069                    if (activityInterpreter != null) {
070                            _activityInterpreters.remove(activityInterpreter);
071                    }
072            }
073    
074            /**
075             * Creates a human readable activity feed entry for the activity using an
076             * available compatible activity interpreter.
077             *
078             * <p>
079             * This method finds the appropriate interpreter for the activity by going
080             * through the available interpreters and asking them if they can handle the
081             * asset type of the activity.
082             * </p>
083             *
084             * @param  activity the activity to be translated to human readable form
085             * @param  themeDisplay the theme display needed by interpreters to create
086             *         links and get localized text fragments
087             * @return the activity feed that is a human readable form of the activity
088             *         record or <code>null</code> if a compatible interpreter is not
089             *         found
090             */
091            public SocialActivityFeedEntry interpret(
092                    SocialActivity activity, ThemeDisplay themeDisplay) {
093    
094                    try {
095                            if (activity.getUserId() == themeDisplay.getDefaultUserId()) {
096                                    return null;
097                            }
098                    }
099                    catch (Exception e) {
100                            _log.error(e, e);
101                    }
102    
103                    if (activity.getMirrorActivityId() > 0) {
104                            SocialActivity mirrorActivity = null;
105    
106                            try {
107                                    mirrorActivity = socialActivityLocalService.getActivity(
108                                            activity.getMirrorActivityId());
109                            }
110                            catch (Exception e) {
111                            }
112    
113                            if (mirrorActivity != null) {
114                                    activity = mirrorActivity;
115                            }
116                    }
117    
118                    String className = PortalUtil.getClassName(activity.getClassNameId());
119    
120                    for (int i = 0; i < _activityInterpreters.size(); i++) {
121                            SocialActivityInterpreterImpl activityInterpreter =
122                                    (SocialActivityInterpreterImpl)_activityInterpreters.get(i);
123    
124                            if (activityInterpreter.hasClassName(className)) {
125                                    SocialActivityFeedEntry activityFeedEntry =
126                                            activityInterpreter.interpret(activity, themeDisplay);
127    
128                                    if (activityFeedEntry != null) {
129                                            activityFeedEntry.setPortletId(
130                                                    activityInterpreter.getPortletId());
131    
132                                            return activityFeedEntry;
133                                    }
134                            }
135                    }
136    
137                    return null;
138            }
139    
140            private static Log _log = LogFactoryUtil.getLog(
141                    SocialActivityInterpreterLocalServiceImpl.class);
142    
143            private List<SocialActivityInterpreter> _activityInterpreters =
144                    new ArrayList<SocialActivityInterpreter>();
145    
146    }