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.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            @Override
056            public void addRequestInterpreter(
057                    SocialRequestInterpreter requestInterpreter) {
058    
059                    _requestInterpreters.add(requestInterpreter);
060            }
061    
062            /**
063             * Removes the social request interpreter from the list of available
064             * interpreters.
065             *
066             * @param requestInterpreter the social request interpreter
067             */
068            @Override
069            public void deleteRequestInterpreter(
070                    SocialRequestInterpreter requestInterpreter) {
071    
072                    if (requestInterpreter != null) {
073                            _requestInterpreters.remove(requestInterpreter);
074                    }
075            }
076    
077            /**
078             * Creates a human readable request feed entry for the social request using
079             * an available compatible request interpreter.
080             *
081             * <p>
082             * This method finds the appropriate interpreter for the request by going
083             * through the available interpreters to find one that can handle the asset
084             * type of the request.
085             * </p>
086             *
087             * @param  request the social request to be translated to human readable
088             *         form
089             * @param  themeDisplay the theme display needed by interpreters to create
090             *         links and get localized text fragments
091             * @return the social request feed entry
092             */
093            @Override
094            public SocialRequestFeedEntry interpret(
095                    SocialRequest request, ThemeDisplay themeDisplay) {
096    
097                    String className = PortalUtil.getClassName(request.getClassNameId());
098    
099                    for (int i = 0; i < _requestInterpreters.size(); i++) {
100                            SocialRequestInterpreterImpl requestInterpreter =
101                                    (SocialRequestInterpreterImpl)_requestInterpreters.get(i);
102    
103                            if (requestInterpreter.hasClassName(className)) {
104                                    SocialRequestFeedEntry requestFeedEntry =
105                                            requestInterpreter.interpret(request, themeDisplay);
106    
107                                    if (requestFeedEntry != null) {
108                                            requestFeedEntry.setPortletId(
109                                                    requestInterpreter.getPortletId());
110    
111                                            return requestFeedEntry;
112                                    }
113                            }
114                    }
115    
116                    return null;
117            }
118    
119            /**
120             * Processes the confirmation of the social request.
121             *
122             * <p>
123             * Confirmations are handled by finding the appropriate social request
124             * interpreter and calling its processConfirmation() method. To find the
125             * appropriate interpreter this method goes through the available
126             * interpreters to find one that can handle the asset type of the request.
127             * </p>
128             *
129             * @param request the social request being confirmed
130             * @param themeDisplay the theme display needed by interpreters to create
131             *        links and get localized text fragments
132             */
133            @Override
134            public void processConfirmation(
135                    SocialRequest request, ThemeDisplay themeDisplay) {
136    
137                    String className = PortalUtil.getClassName(request.getClassNameId());
138    
139                    for (int i = 0; i < _requestInterpreters.size(); i++) {
140                            SocialRequestInterpreterImpl requestInterpreter =
141                                    (SocialRequestInterpreterImpl)_requestInterpreters.get(i);
142    
143                            if (requestInterpreter.hasClassName(className)) {
144                                    boolean value = requestInterpreter.processConfirmation(
145                                            request, themeDisplay);
146    
147                                    if (value) {
148                                            return;
149                                    }
150                            }
151                    }
152            }
153    
154            /**
155             * Processes the rejection of the social request.
156             *
157             * <p>
158             * Rejections are handled by finding the appropriate social request
159             * interpreters and calling their processRejection() methods. To find the
160             * appropriate interpreters this method goes through the available
161             * interpreters and asks them if they can handle the asset type of the
162             * request.
163             * </p>
164             *
165             * @param request the social request being rejected
166             * @param themeDisplay the theme display needed by interpreters to create
167             *        links and get localized text fragments
168             */
169            @Override
170            public void processRejection(
171                    SocialRequest request, ThemeDisplay themeDisplay) {
172    
173                    String className = PortalUtil.getClassName(request.getClassNameId());
174    
175                    for (int i = 0; i < _requestInterpreters.size(); i++) {
176                            SocialRequestInterpreterImpl requestInterpreter =
177                                    (SocialRequestInterpreterImpl)_requestInterpreters.get(i);
178    
179                            if (requestInterpreter.hasClassName(className)) {
180                                    boolean value = requestInterpreter.processRejection(
181                                            request, themeDisplay);
182    
183                                    if (value) {
184                                            return;
185                                    }
186                            }
187                    }
188            }
189    
190            private List<SocialRequestInterpreter> _requestInterpreters =
191                    new ArrayList<SocialRequestInterpreter>();
192    
193    }