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