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.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            @Override
056            public void addActivityInterpreter(
057                    SocialActivityInterpreter activityInterpreter) {
058    
059                    _activityInterpreters.add(activityInterpreter);
060            }
061    
062            /**
063             * Removes the activity interpreter from the list of available interpreters.
064             *
065             * @param activityInterpreter the activity interpreter
066             */
067            @Override
068            public void deleteActivityInterpreter(
069                    SocialActivityInterpreter activityInterpreter) {
070    
071                    if (activityInterpreter != null) {
072                            _activityInterpreters.remove(activityInterpreter);
073                    }
074            }
075    
076            /**
077             * Creates a human readable activity feed entry for the activity using an
078             * available compatible activity interpreter.
079             *
080             * <p>
081             * This method finds the appropriate interpreter for the activity by going
082             * through the available interpreters and asking them if they can handle the
083             * asset type of the activity.
084             * </p>
085             *
086             * @param  activity the activity to be translated to human readable form
087             * @param  themeDisplay the theme display needed by interpreters to create
088             *         links and get localized text fragments
089             * @return the activity feed that is a human readable form of the activity
090             *         record or <code>null</code> if a compatible interpreter is not
091             *         found
092             */
093            @Override
094            public SocialActivityFeedEntry interpret(
095                    SocialActivity activity, ThemeDisplay themeDisplay) {
096    
097                    try {
098                            if (activity.getUserId() == themeDisplay.getDefaultUserId()) {
099                                    return null;
100                            }
101                    }
102                    catch (Exception e) {
103                            _log.error(e, e);
104                    }
105    
106                    if (activity.getMirrorActivityId() > 0) {
107                            SocialActivity mirrorActivity = null;
108    
109                            try {
110                                    mirrorActivity = socialActivityLocalService.getActivity(
111                                            activity.getMirrorActivityId());
112                            }
113                            catch (Exception e) {
114                            }
115    
116                            if (mirrorActivity != null) {
117                                    activity = mirrorActivity;
118                            }
119                    }
120    
121                    String className = PortalUtil.getClassName(activity.getClassNameId());
122    
123                    for (int i = 0; i < _activityInterpreters.size(); i++) {
124                            SocialActivityInterpreterImpl activityInterpreter =
125                                    (SocialActivityInterpreterImpl)_activityInterpreters.get(i);
126    
127                            if (activityInterpreter.hasClassName(className)) {
128                                    SocialActivityFeedEntry activityFeedEntry =
129                                            activityInterpreter.interpret(activity, themeDisplay);
130    
131                                    if (activityFeedEntry != null) {
132                                            activityFeedEntry.setPortletId(
133                                                    activityInterpreter.getPortletId());
134    
135                                            return activityFeedEntry;
136                                    }
137                            }
138                    }
139    
140                    return null;
141            }
142    
143            private static Log _log = LogFactoryUtil.getLog(
144                    SocialActivityInterpreterLocalServiceImpl.class);
145    
146            private List<SocialActivityInterpreter> _activityInterpreters =
147                    new ArrayList<SocialActivityInterpreter>();
148    
149    }