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