001    /**
002     * Copyright (c) 2000-present 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    import com.liferay.registry.Filter;
032    import com.liferay.registry.Registry;
033    import com.liferay.registry.RegistryUtil;
034    import com.liferay.registry.ServiceReference;
035    import com.liferay.registry.ServiceRegistration;
036    import com.liferay.registry.ServiceTracker;
037    import com.liferay.registry.ServiceTrackerCustomizer;
038    import com.liferay.registry.collections.ServiceRegistrationMap;
039    
040    import java.util.HashMap;
041    import java.util.List;
042    import java.util.Map;
043    import java.util.concurrent.CopyOnWriteArrayList;
044    
045    /**
046     * The social request interpreter local service. Social request interpreters are
047     * responsible for translating social requests into human readable form as well
048     * as handling social request confirmations and rejections. This service holds a
049     * list of interpreters and provides methods to add or remove items from this
050     * list.
051     *
052     * <p>
053     * Social request interpreters use the language files to get text fragments
054     * based on the request's type. An interpreter is created for a specific request
055     * type and is only capable of handling requests of that type. As an example,
056     * there is an interpreter FriendsRequestInterpreter in the social networking
057     * portlet can only translate and handle interpretation, confirmation, and
058     * rejection of friend requests.
059     * </p>
060     *
061     * @author Brian Wing Shun Chan
062     */
063    public class SocialRequestInterpreterLocalServiceImpl
064            extends SocialRequestInterpreterLocalServiceBaseImpl {
065    
066            /**
067             * Adds the social request interpreter to the list of available
068             * interpreters.
069             *
070             * @param requestInterpreter the social request interpreter
071             */
072            @Override
073            public void addRequestInterpreter(
074                    SocialRequestInterpreter requestInterpreter) {
075    
076                    Registry registry = RegistryUtil.getRegistry();
077    
078                    Map<String, Object> properties = new HashMap<>();
079    
080                    SocialRequestInterpreterImpl requestInterpreterImpl =
081                            (SocialRequestInterpreterImpl)requestInterpreter;
082    
083                    properties.put(
084                            "javax.portlet.name", requestInterpreterImpl.getPortletId());
085    
086                    ServiceRegistration<SocialRequestInterpreter> serviceRegistration =
087                            registry.registerService(
088                                    SocialRequestInterpreter.class, requestInterpreter, properties);
089    
090                    _serviceRegistrations.put(requestInterpreter, serviceRegistration);
091            }
092    
093            @Override
094            public void afterPropertiesSet() {
095                    Registry registry = RegistryUtil.getRegistry();
096    
097                    Filter filter = registry.getFilter(
098                            "(&(javax.portlet.name=*)(objectClass=" +
099                                    SocialRequestInterpreter.class.getName() + "))");
100    
101                    _serviceTracker = registry.trackServices(
102                            filter, new SocialRequestInterpreterServiceTrackerCustomizer());
103    
104                    _serviceTracker.open();
105            }
106    
107            /**
108             * Removes the social request interpreter from the list of available
109             * interpreters.
110             *
111             * @param requestInterpreter the social request interpreter
112             */
113            @Override
114            public void deleteRequestInterpreter(
115                    SocialRequestInterpreter requestInterpreter) {
116    
117                    ServiceRegistration<SocialRequestInterpreter> serviceRegistration =
118                            _serviceRegistrations.remove(requestInterpreter);
119    
120                    if (serviceRegistration != null) {
121                            serviceRegistration.unregister();
122                    }
123            }
124    
125            /**
126             * Creates a human readable request feed entry for the social request using
127             * an available compatible request interpreter.
128             *
129             * <p>
130             * This method finds the appropriate interpreter for the request by going
131             * through the available interpreters to find one that can handle the asset
132             * type of the request.
133             * </p>
134             *
135             * @param  request the social request to be translated to human readable
136             *         form
137             * @param  themeDisplay the theme display needed by interpreters to create
138             *         links and get localized text fragments
139             * @return the social request feed entry
140             */
141            @Override
142            public SocialRequestFeedEntry interpret(
143                    SocialRequest request, ThemeDisplay themeDisplay) {
144    
145                    String className = PortalUtil.getClassName(request.getClassNameId());
146    
147                    for (int i = 0; i < _requestInterpreters.size(); i++) {
148                            SocialRequestInterpreterImpl requestInterpreter =
149                                    (SocialRequestInterpreterImpl)_requestInterpreters.get(i);
150    
151                            if (matches(requestInterpreter, className, request)) {
152                                    SocialRequestFeedEntry requestFeedEntry =
153                                            requestInterpreter.interpret(request, themeDisplay);
154    
155                                    if (requestFeedEntry != null) {
156                                            requestFeedEntry.setPortletId(
157                                                    requestInterpreter.getPortletId());
158    
159                                            return requestFeedEntry;
160                                    }
161                            }
162                    }
163    
164                    return null;
165            }
166    
167            /**
168             * Processes the confirmation of the social request.
169             *
170             * <p>
171             * Confirmations are handled by finding the appropriate social request
172             * interpreter and calling its processConfirmation() method. To find the
173             * appropriate interpreter this method goes through the available
174             * interpreters to find one that can handle the asset type of the request.
175             * </p>
176             *
177             * @param request the social request being confirmed
178             * @param themeDisplay the theme display needed by interpreters to create
179             *        links and get localized text fragments
180             */
181            @Override
182            public void processConfirmation(
183                    SocialRequest request, ThemeDisplay themeDisplay) {
184    
185                    String className = PortalUtil.getClassName(request.getClassNameId());
186    
187                    for (int i = 0; i < _requestInterpreters.size(); i++) {
188                            SocialRequestInterpreterImpl requestInterpreter =
189                                    (SocialRequestInterpreterImpl)_requestInterpreters.get(i);
190    
191                            if (matches(requestInterpreter, className, request)) {
192                                    boolean value = requestInterpreter.processConfirmation(
193                                            request, themeDisplay);
194    
195                                    if (value) {
196                                            return;
197                                    }
198                            }
199                    }
200            }
201    
202            /**
203             * Processes the rejection of the social request.
204             *
205             * <p>
206             * Rejections are handled by finding the appropriate social request
207             * interpreters and calling their processRejection() methods. To find the
208             * appropriate interpreters this method goes through the available
209             * interpreters and asks them if they can handle the asset type of the
210             * request.
211             * </p>
212             *
213             * @param request the social request being rejected
214             * @param themeDisplay the theme display needed by interpreters to create
215             *        links and get localized text fragments
216             */
217            @Override
218            public void processRejection(
219                    SocialRequest request, ThemeDisplay themeDisplay) {
220    
221                    String className = PortalUtil.getClassName(request.getClassNameId());
222    
223                    for (int i = 0; i < _requestInterpreters.size(); i++) {
224                            SocialRequestInterpreterImpl requestInterpreter =
225                                    (SocialRequestInterpreterImpl)_requestInterpreters.get(i);
226    
227                            if (matches(requestInterpreter, className, request)) {
228                                    boolean value = requestInterpreter.processRejection(
229                                            request, themeDisplay);
230    
231                                    if (value) {
232                                            return;
233                                    }
234                            }
235                    }
236            }
237    
238            protected String getSocialRequestPortletId(SocialRequest request) {
239                    try {
240                            JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject(
241                                    request.getExtraData());
242    
243                            return extraDataJSONObject.getString("portletId");
244                    }
245                    catch (JSONException jsone) {
246                            _log.error(
247                                    "Unable to create JSON object from " + request.getExtraData());
248    
249                            return StringPool.BLANK;
250                    }
251            }
252    
253            protected boolean matches(
254                    SocialRequestInterpreterImpl requestInterpreter, String className,
255                    SocialRequest request) {
256    
257                    if (!requestInterpreter.hasClassName(className)) {
258                            return false;
259                    }
260    
261                    String requestPortletId = getSocialRequestPortletId(request);
262    
263                    if (Validator.isNull(requestPortletId) ||
264                            requestPortletId.equals(requestInterpreter.getPortletId())) {
265    
266                            return true;
267                    }
268    
269                    return false;
270            }
271    
272            private static final Log _log = LogFactoryUtil.getLog(
273                    SocialRequestInterpreterLocalServiceImpl.class);
274    
275            private final List<SocialRequestInterpreter> _requestInterpreters =
276                    new CopyOnWriteArrayList<>();
277            private final ServiceRegistrationMap<SocialRequestInterpreter>
278                    _serviceRegistrations = new ServiceRegistrationMap<>();
279            private ServiceTracker<SocialRequestInterpreter, SocialRequestInterpreter>
280                    _serviceTracker;
281    
282            private class SocialRequestInterpreterServiceTrackerCustomizer
283                    implements ServiceTrackerCustomizer
284                            <SocialRequestInterpreter, SocialRequestInterpreter> {
285    
286                    @Override
287                    public SocialRequestInterpreter addingService(
288                            ServiceReference<SocialRequestInterpreter> serviceReference) {
289    
290                            Registry registry = RegistryUtil.getRegistry();
291    
292                            SocialRequestInterpreter requestInterpreter = registry.getService(
293                                    serviceReference);
294    
295                            String portletId = (String)serviceReference.getProperty(
296                                    "javax.portlet.name");
297    
298                            if (!(requestInterpreter instanceof SocialRequestInterpreterImpl)) {
299                                    requestInterpreter = new SocialRequestInterpreterImpl(
300                                            portletId, requestInterpreter);
301                            }
302    
303                            _requestInterpreters.add(requestInterpreter);
304    
305                            return requestInterpreter;
306                    }
307    
308                    @Override
309                    public void modifiedService(
310                            ServiceReference<SocialRequestInterpreter> serviceReference,
311                            SocialRequestInterpreter requestInterpreter) {
312                    }
313    
314                    @Override
315                    public void removedService(
316                            ServiceReference<SocialRequestInterpreter> serviceReference,
317                            SocialRequestInterpreter requestInterpreter) {
318    
319                            Registry registry = RegistryUtil.getRegistry();
320    
321                            registry.ungetService(serviceReference);
322    
323                            _requestInterpreters.remove(requestInterpreter);
324                    }
325    
326            }
327    
328    }