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.SocialRequest;
020    import com.liferay.portlet.social.model.SocialRequestFeedEntry;
021    import com.liferay.portlet.social.model.SocialRequestInterpreter;
022    import com.liferay.portlet.social.model.impl.SocialRequestInterpreterImpl;
023    import com.liferay.portlet.social.service.base.SocialRequestInterpreterLocalServiceBaseImpl;
024    
025    import java.util.ArrayList;
026    import java.util.List;
027    
028    /**
029     * The social request interpreter local service. Social request interpreters are
030     * responsible for translating social requests into human readable form as well
031     * as handling social request confirmations and rejections. This service holds a
032     * list of interpreters and provides methods to add or remove items from this
033     * list.
034     *
035     * <p>
036     * Social request interpreters use the language files to get text fragments
037     * based on the request's type. An interpreter is created for a specific request
038     * type and is only capable of handling requests of that type. As an example,
039     * there is an interpreter FriendsRequestInterpreter in the social networking
040     * portlet can only translate and handle interpretation, confirmation, and
041     * rejection of friend requests.
042     * </p>
043     *
044     * @author Brian Wing Shun Chan
045     */
046    public class SocialRequestInterpreterLocalServiceImpl
047            extends SocialRequestInterpreterLocalServiceBaseImpl {
048    
049            /**
050             * Adds the social request interpreter to the list of available
051             * interpreters.
052             *
053             * @param requestInterpreter the social request interpreter
054             */
055            public void addRequestInterpreter(
056                    SocialRequestInterpreter requestInterpreter) {
057    
058                    _requestInterpreters.add(requestInterpreter);
059            }
060    
061            /**
062             * Removes the social request interpreter from the list of available
063             * interpreters.
064             *
065             * @param requestInterpreter the social request interpreter
066             */
067            public void deleteRequestInterpreter(
068                    SocialRequestInterpreter requestInterpreter) {
069    
070                    if (requestInterpreter != null) {
071                            _requestInterpreters.remove(requestInterpreter);
072                    }
073            }
074    
075            /**
076             * Creates a human readable request feed entry for the social request using
077             * an available compatible request interpreter.
078             *
079             * <p>
080             * This method finds the appropriate interpreter for the request by going
081             * through the available interpreters to find one that can handle the asset
082             * type of the request.
083             * </p>
084             *
085             * @param  request the social request to be translated to human readable
086             *         form
087             * @param  themeDisplay the theme display needed by interpreters to create
088             *         links and get localized text fragments
089             * @return the social request feed entry
090             */
091            public SocialRequestFeedEntry interpret(
092                    SocialRequest request, ThemeDisplay themeDisplay) {
093    
094                    String className = PortalUtil.getClassName(request.getClassNameId());
095    
096                    for (int i = 0; i < _requestInterpreters.size(); i++) {
097                            SocialRequestInterpreterImpl requestInterpreter =
098                                    (SocialRequestInterpreterImpl)_requestInterpreters.get(i);
099    
100                            if (requestInterpreter.hasClassName(className)) {
101                                    SocialRequestFeedEntry requestFeedEntry =
102                                            requestInterpreter.interpret(request, themeDisplay);
103    
104                                    if (requestFeedEntry != null) {
105                                            requestFeedEntry.setPortletId(
106                                                    requestInterpreter.getPortletId());
107    
108                                            return requestFeedEntry;
109                                    }
110                            }
111                    }
112    
113                    return null;
114            }
115    
116            /**
117             * Processes the confirmation of the social request.
118             *
119             * <p>
120             * Confirmations are handled by finding the appropriate social request
121             * interpreter and calling its processConfirmation() method. To find the
122             * appropriate interpreter this method goes through the available
123             * interpreters to find one that can handle the asset type of the request.
124             * </p>
125             *
126             * @param request the social request being confirmed
127             * @param themeDisplay the theme display needed by interpreters to create
128             *        links and get localized text fragments
129             */
130            public void processConfirmation(
131                    SocialRequest request, ThemeDisplay themeDisplay) {
132    
133                    String className = PortalUtil.getClassName(request.getClassNameId());
134    
135                    for (int i = 0; i < _requestInterpreters.size(); i++) {
136                            SocialRequestInterpreterImpl requestInterpreter =
137                                    (SocialRequestInterpreterImpl)_requestInterpreters.get(i);
138    
139                            if (requestInterpreter.hasClassName(className)) {
140                                    boolean value = requestInterpreter.processConfirmation(
141                                            request, themeDisplay);
142    
143                                    if (value) {
144                                            return;
145                                    }
146                            }
147                    }
148            }
149    
150            /**
151             * Processes the rejection of the social request.
152             *
153             * <p>
154             * Rejections are handled by finding the appropriate social request
155             * interpreters and calling their processRejection() methods. To find the
156             * appropriate interpreters this method goes through the available
157             * interpreters and asks them if they can handle the asset type of the
158             * request.
159             * </p>
160             *
161             * @param request the social request being rejected
162             * @param themeDisplay the theme display needed by interpreters to create
163             *        links and get localized text fragments
164             */
165            public void processRejection(
166                    SocialRequest request, ThemeDisplay themeDisplay) {
167    
168                    String className = PortalUtil.getClassName(request.getClassNameId());
169    
170                    for (int i = 0; i < _requestInterpreters.size(); i++) {
171                            SocialRequestInterpreterImpl requestInterpreter =
172                                    (SocialRequestInterpreterImpl)_requestInterpreters.get(i);
173    
174                            if (requestInterpreter.hasClassName(className)) {
175                                    boolean value = requestInterpreter.processRejection(
176                                            request, themeDisplay);
177    
178                                    if (value) {
179                                            return;
180                                    }
181                            }
182                    }
183            }
184    
185            private List<SocialRequestInterpreter> _requestInterpreters =
186                    new ArrayList<SocialRequestInterpreter>();
187    
188    }