001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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
039     * is 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
061             * interpreters.
062             *
063             * @param activityInterpreter the activity interpreter
064             */
065            public void deleteActivityInterpreter(
066                    SocialActivityInterpreter activityInterpreter) {
067    
068                    if (activityInterpreter != null) {
069                            _activityInterpreters.remove(activityInterpreter);
070                    }
071            }
072    
073            /**
074             * Creates a human readable activity feed entry for the activity using an
075             * available compatible activity interpreter.
076             *
077             * <p>
078             * This method finds the appropriate interpreter for the activity by going
079             * through the available interpreters and asking them if they can handle
080             * the asset type of the activity.
081             * </p>
082             *
083             * @param  activity the activity to be translated to human readable form
084             * @param  themeDisplay the theme display needed by interpreters to create
085             *         links and get localized text fragments
086             * @return the activity feed that is a human readable form of the activity
087             *         record or <code>null</code> if a compatible interpreter is not
088             *         found
089             */
090            public SocialActivityFeedEntry interpret(
091                    SocialActivity activity, ThemeDisplay themeDisplay) {
092    
093                    if (activity.getMirrorActivityId() > 0) {
094                            SocialActivity mirrorActivity = null;
095    
096                            try {
097                                    mirrorActivity = socialActivityLocalService.getActivity(
098                                            activity.getMirrorActivityId());
099                            }
100                            catch (Exception e) {
101                            }
102    
103                            if (mirrorActivity != null) {
104                                    activity = mirrorActivity;
105                            }
106                    }
107    
108                    String className = PortalUtil.getClassName(activity.getClassNameId());
109    
110                    for (int i = 0; i < _activityInterpreters.size(); i++) {
111                            SocialActivityInterpreterImpl activityInterpreter =
112                                    (SocialActivityInterpreterImpl)_activityInterpreters.get(i);
113    
114                            if (activityInterpreter.hasClassName(className)) {
115                                    SocialActivityFeedEntry activityFeedEntry =
116                                            activityInterpreter.interpret(activity, themeDisplay);
117    
118                                    if (activityFeedEntry != null) {
119                                            activityFeedEntry.setPortletId(
120                                                    activityInterpreter.getPortletId());
121    
122                                            return activityFeedEntry;
123                                    }
124                            }
125                    }
126    
127                    return null;
128            }
129    
130            private List<SocialActivityInterpreter> _activityInterpreters =
131                    new ArrayList<SocialActivityInterpreter>();
132    
133    }