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.portlet.social.model.SocialActivity;
021    import com.liferay.portlet.social.model.SocialActivityFeedEntry;
022    import com.liferay.portlet.social.model.SocialActivityInterpreter;
023    import com.liferay.portlet.social.model.SocialActivitySet;
024    import com.liferay.portlet.social.model.impl.SocialActivityInterpreterImpl;
025    import com.liferay.portlet.social.service.base.SocialActivityInterpreterLocalServiceBaseImpl;
026    
027    import java.util.HashMap;
028    import java.util.Map;
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                    String[] classNames = activityInterpreter.getClassNames();
059    
060                    for (String className : classNames) {
061                            _activityInterpreters.put(className, activityInterpreter);
062                    }
063            }
064    
065            /**
066             * Removes the activity interpreter from the list of available interpreters.
067             *
068             * @param activityInterpreter the activity interpreter
069             */
070            public void deleteActivityInterpreter(
071                    SocialActivityInterpreter activityInterpreter) {
072    
073                    if (activityInterpreter != null) {
074                            String[] classNames = activityInterpreter.getClassNames();
075    
076                            for (String className : classNames) {
077                                    _activityInterpreters.remove(className);
078                            }
079                    }
080            }
081    
082            /**
083             * Creates a human readable activity feed entry for the activity using an
084             * available compatible activity interpreter.
085             *
086             * <p>
087             * This method finds the appropriate interpreter for the activity by going
088             * through the available interpreters and asking them if they can handle the
089             * asset type of the activity.
090             * </p>
091             *
092             * @param  activity the activity to be translated to human readable form
093             * @param  themeDisplay the theme display needed by interpreters to create
094             *         links and get localized text fragments
095             * @return the activity feed that is a human readable form of the activity
096             *         record or <code>null</code> if a compatible interpreter is not
097             *         found
098             */
099            public SocialActivityFeedEntry interpret(
100                    SocialActivity activity, ThemeDisplay themeDisplay) {
101    
102                    try {
103                            if (activity.getUserId() == themeDisplay.getDefaultUserId()) {
104                                    return null;
105                            }
106                    }
107                    catch (Exception e) {
108                            _log.error(e, e);
109                    }
110    
111                    if (activity.getMirrorActivityId() > 0) {
112                            SocialActivity mirrorActivity = null;
113    
114                            try {
115                                    mirrorActivity = socialActivityLocalService.getActivity(
116                                            activity.getMirrorActivityId());
117                            }
118                            catch (Exception e) {
119                            }
120    
121                            if (mirrorActivity != null) {
122                                    activity = mirrorActivity;
123                            }
124                    }
125    
126                    SocialActivityInterpreterImpl activityInterpreter =
127                            (SocialActivityInterpreterImpl)_activityInterpreters.get(
128                                    activity.getClassName());
129    
130                    if (activityInterpreter == null) {
131                            return null;
132                    }
133    
134                    SocialActivityFeedEntry activityFeedEntry =
135                            activityInterpreter.interpret(activity, themeDisplay);
136    
137                    if (activityFeedEntry == null) {
138                            return null;
139                    }
140    
141                    activityFeedEntry.setPortletId(activityInterpreter.getPortletId());
142    
143                    return activityFeedEntry;
144            }
145    
146            public SocialActivityFeedEntry interpret(
147                    SocialActivitySet activitySet, ThemeDisplay themeDisplay) {
148    
149                    try {
150                            if (activitySet.getUserId() == themeDisplay.getDefaultUserId()) {
151                                    return null;
152                            }
153                    }
154                    catch (Exception e) {
155                            _log.error(e, e);
156                    }
157    
158                    SocialActivityInterpreterImpl activityInterpreter =
159                            (SocialActivityInterpreterImpl)_activityInterpreters.get(
160                                    activitySet.getClassName());
161    
162                    if (activityInterpreter == null) {
163                            return null;
164                    }
165    
166                    SocialActivityFeedEntry activityFeedEntry =
167                            activityInterpreter.interpret(activitySet, themeDisplay);
168    
169                    if (activityFeedEntry == null) {
170                            return null;
171                    }
172    
173                    activityFeedEntry.setPortletId(activityInterpreter.getPortletId());
174    
175                    return activityFeedEntry;
176            }
177    
178            private static Log _log = LogFactoryUtil.getLog(
179                    SocialActivityInterpreterLocalServiceImpl.class);
180    
181            private Map<String, SocialActivityInterpreter> _activityInterpreters =
182                    new HashMap<String, SocialActivityInterpreter>();
183    
184    }