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