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;
016    
017    import com.liferay.portal.kernel.cache.PortalCache;
018    import com.liferay.portal.kernel.cache.SingleVMPoolUtil;
019    import com.liferay.portal.kernel.cache.key.CacheKeyGenerator;
020    import com.liferay.portal.kernel.cache.key.CacheKeyGeneratorUtil;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.portlet.LiferayPortletMode;
027    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
028    import com.liferay.portal.kernel.util.GetterUtil;
029    import com.liferay.portal.kernel.util.JavaConstants;
030    import com.liferay.portal.kernel.util.ParamUtil;
031    import com.liferay.portal.kernel.util.StringPool;
032    import com.liferay.portal.kernel.util.Validator;
033    import com.liferay.portal.kernel.util.WebKeys;
034    import com.liferay.portal.model.Group;
035    import com.liferay.portal.model.Layout;
036    import com.liferay.portal.model.LayoutConstants;
037    import com.liferay.portal.model.LayoutTypePortlet;
038    import com.liferay.portal.model.Portlet;
039    import com.liferay.portal.model.PortletConstants;
040    import com.liferay.portal.model.PortletPreferencesIds;
041    import com.liferay.portal.security.auth.PrincipalException;
042    import com.liferay.portal.security.permission.ActionKeys;
043    import com.liferay.portal.security.permission.PermissionChecker;
044    import com.liferay.portal.security.permission.PermissionThreadLocal;
045    import com.liferay.portal.service.GroupLocalServiceUtil;
046    import com.liferay.portal.service.PortalPreferencesLocalServiceUtil;
047    import com.liferay.portal.service.PortletLocalServiceUtil;
048    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
049    import com.liferay.portal.service.UserLocalServiceUtil;
050    import com.liferay.portal.service.permission.LayoutPermissionUtil;
051    import com.liferay.portal.service.permission.PortletPermissionUtil;
052    import com.liferay.portal.theme.ThemeDisplay;
053    import com.liferay.portal.util.PortalUtil;
054    import com.liferay.portal.util.PortletKeys;
055    import com.liferay.portal.util.PropsValues;
056    import com.liferay.portal.xml.StAXReaderUtil;
057    import com.liferay.portlet.portletconfiguration.util.ConfigurationPortletRequest;
058    
059    import java.util.ArrayList;
060    import java.util.Collections;
061    import java.util.HashMap;
062    import java.util.List;
063    import java.util.Map;
064    
065    import javax.portlet.PortletPreferences;
066    import javax.portlet.PortletRequest;
067    import javax.portlet.PreferencesValidator;
068    import javax.portlet.filter.PortletRequestWrapper;
069    
070    import javax.servlet.http.HttpServletRequest;
071    import javax.servlet.http.HttpSession;
072    
073    import javax.xml.stream.XMLEventReader;
074    import javax.xml.stream.XMLInputFactory;
075    import javax.xml.stream.XMLStreamException;
076    import javax.xml.stream.events.EndElement;
077    import javax.xml.stream.events.StartElement;
078    import javax.xml.stream.events.XMLEvent;
079    
080    /**
081     * @author Brian Wing Shun Chan
082     * @author Alexander Chow
083     * @author Minhchau Dang
084     * @author Raymond Aug??
085     */
086    @DoPrivileged
087    public class PortletPreferencesFactoryImpl
088            implements PortletPreferencesFactory {
089    
090            public static Map<String, Preference> createPreferencesMap(String xml) {
091                    XMLEventReader xmlEventReader = null;
092    
093                    Map<String, Preference> preferencesMap = new HashMap<>();
094    
095                    try {
096                            XMLInputFactory xmlInputFactory =
097                                    StAXReaderUtil.getXMLInputFactory();
098    
099                            xmlEventReader = xmlInputFactory.createXMLEventReader(
100                                    new UnsyncStringReader(xml));
101    
102                            while (xmlEventReader.hasNext()) {
103                                    XMLEvent xmlEvent = xmlEventReader.nextEvent();
104    
105                                    if (xmlEvent.isStartElement()) {
106                                            StartElement startElement = xmlEvent.asStartElement();
107    
108                                            String elementName = startElement.getName().getLocalPart();
109    
110                                            if (elementName.equals("preference")) {
111                                                    Preference preference = readPreference(xmlEventReader);
112    
113                                                    preferencesMap.put(preference.getName(), preference);
114                                            }
115                                    }
116                            }
117                    }
118                    catch (XMLStreamException xse) {
119                            throw new SystemException(xse);
120                    }
121                    finally {
122                            if (xmlEventReader != null) {
123                                    try {
124                                            xmlEventReader.close();
125                                    }
126                                    catch (XMLStreamException xse) {
127                                            if (_log.isDebugEnabled()) {
128                                                    _log.debug(xse, xse);
129                                            }
130                                    }
131                            }
132                    }
133    
134                    return preferencesMap;
135            }
136    
137            @Override
138            public void checkControlPanelPortletPreferences(
139                            ThemeDisplay themeDisplay, Portlet portlet)
140                    throws PortalException {
141    
142                    Layout layout = themeDisplay.getLayout();
143    
144                    Group group = layout.getGroup();
145    
146                    if (!group.isControlPanel()) {
147                            return;
148                    }
149    
150                    String portletId = portlet.getPortletId();
151    
152                    boolean hasControlPanelAccessPermission =
153                            PortletPermissionUtil.hasControlPanelAccessPermission(
154                                    themeDisplay.getPermissionChecker(),
155                                    themeDisplay.getScopeGroupId(), portletId);
156    
157                    if (!hasControlPanelAccessPermission) {
158                            return;
159                    }
160    
161                    PortletPreferences portletSetup = getStrictLayoutPortletSetup(
162                            layout, portletId);
163    
164                    if (portletSetup instanceof StrictPortletPreferencesImpl) {
165                            getLayoutPortletSetup(layout, portletId);
166                    }
167    
168                    if (portlet.isInstanceable()) {
169                            return;
170                    }
171    
172                    PortletPreferencesIds portletPreferencesIds = getPortletPreferencesIds(
173                            themeDisplay.getScopeGroupId(), themeDisplay.getUserId(), layout,
174                            portletId, false);
175    
176                    PortletPreferencesLocalServiceUtil.getPreferences(
177                            portletPreferencesIds);
178            }
179    
180            @Override
181            public PortletPreferences fromDefaultXML(String xml) {
182                    Map<String, Preference> preferencesMap = toPreferencesMap(xml);
183    
184                    return new PortletPreferencesImpl(xml, preferencesMap);
185            }
186    
187            @Override
188            public PortalPreferencesImpl fromXML(
189                    long ownerId, int ownerType, String xml) {
190    
191                    Map<String, Preference> preferencesMap = toPreferencesMap(xml);
192    
193                    return new PortalPreferencesImpl(
194                            ownerId, ownerType, xml, preferencesMap, false);
195            }
196    
197            @Override
198            public PortletPreferencesImpl fromXML(
199                    long companyId, long ownerId, int ownerType, long plid,
200                    String portletId, String xml) {
201    
202                    Map<String, Preference> preferencesMap = toPreferencesMap(xml);
203    
204                    return new PortletPreferencesImpl(
205                            companyId, ownerId, ownerType, plid, portletId, xml,
206                            preferencesMap);
207            }
208    
209            /**
210             * @deprecated As of 6.2.0, replaced by {@link #fromXML(long, int, String)}
211             */
212            @Deprecated
213            @Override
214            public PortalPreferences fromXML(
215                    long companyId, long ownerId, int ownerType, String xml) {
216    
217                    return fromXML(ownerId, ownerType, xml);
218            }
219    
220            @Override
221            public PortletPreferences getExistingPortletSetup(
222                            Layout layout, String portletId)
223                    throws PortalException {
224    
225                    if (Validator.isNull(portletId)) {
226                            return null;
227                    }
228    
229                    PortletPreferences portletPreferences = getStrictPortletSetup(
230                            layout, portletId);
231    
232                    if (portletPreferences instanceof StrictPortletPreferencesImpl) {
233                            throw new PrincipalException();
234                    }
235    
236                    return portletPreferences;
237            }
238    
239            @Override
240            public PortletPreferences getExistingPortletSetup(
241                            PortletRequest portletRequest)
242                    throws PortalException {
243    
244                    String portletResource = ParamUtil.getString(
245                            portletRequest, "portletResource");
246    
247                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
248                            WebKeys.THEME_DISPLAY);
249    
250                    return getExistingPortletSetup(
251                            themeDisplay.getLayout(), portletResource);
252            }
253    
254            @Override
255            public PortletPreferences getLayoutPortletSetup(
256                    Layout layout, String portletId) {
257    
258                    return getLayoutPortletSetup(layout, portletId, null);
259            }
260    
261            @Override
262            public PortletPreferences getLayoutPortletSetup(
263                    Layout layout, String portletId, String defaultPreferences) {
264    
265                    long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
266                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
267    
268                    if (PortletConstants.hasUserId(portletId)) {
269                            ownerId = PortletConstants.getUserId(portletId);
270                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
271                    }
272    
273                    return getLayoutPortletSetup(
274                            layout.getCompanyId(), ownerId, ownerType, layout.getPlid(),
275                            portletId, defaultPreferences);
276            }
277    
278            @Override
279            public PortletPreferences getLayoutPortletSetup(
280                    long companyId, long ownerId, int ownerType, long plid,
281                    String portletId, String defaultPreferences) {
282    
283                    return PortletPreferencesLocalServiceUtil.getPreferences(
284                            companyId, ownerId, ownerType, plid, portletId, defaultPreferences);
285            }
286    
287            @Override
288            public PortalPreferences getPortalPreferences(HttpServletRequest request) {
289                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
290                            WebKeys.THEME_DISPLAY);
291    
292                    return getPortalPreferences(
293                            request.getSession(), themeDisplay.getUserId(),
294                            themeDisplay.isSignedIn());
295            }
296    
297            @Override
298            public PortalPreferences getPortalPreferences(
299                    HttpSession session, long userId, boolean signedIn) {
300    
301                    long ownerId = userId;
302                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
303    
304                    PortalPreferences portalPreferences = null;
305    
306                    if (signedIn) {
307                            PortalPreferencesWrapper portalPreferencesWrapper =
308                                    PortalPreferencesWrapperCacheUtil.get(ownerId, ownerType);
309    
310                            if (portalPreferencesWrapper == null) {
311                                    portalPreferencesWrapper =
312                                            (PortalPreferencesWrapper)
313                                                    PortalPreferencesLocalServiceUtil.getPreferences(
314                                                            ownerId, ownerType);
315    
316                                    portalPreferences =
317                                            portalPreferencesWrapper.getPortalPreferencesImpl();
318                            }
319                            else {
320                                    PortalPreferencesImpl portalPreferencesImpl =
321                                            portalPreferencesWrapper.getPortalPreferencesImpl();
322    
323                                    portalPreferences = portalPreferencesImpl.clone();
324                            }
325                    }
326                    else {
327                            if (session != null) {
328                                    portalPreferences = (PortalPreferences)session.getAttribute(
329                                            WebKeys.PORTAL_PREFERENCES);
330                            }
331    
332                            if (portalPreferences == null) {
333                                    PortalPreferencesWrapper portalPreferencesWrapper =
334                                            PortalPreferencesWrapperCacheUtil.get(ownerId, ownerType);
335    
336                                    if (portalPreferencesWrapper == null) {
337                                            portalPreferencesWrapper =
338                                                    (PortalPreferencesWrapper)
339                                                            PortalPreferencesLocalServiceUtil.getPreferences(
340                                                                    ownerId, ownerType);
341    
342                                            portalPreferences =
343                                                    portalPreferencesWrapper.getPortalPreferencesImpl();
344                                    }
345                                    else {
346                                            PortalPreferencesImpl portalPreferencesImpl =
347                                                    portalPreferencesWrapper.getPortalPreferencesImpl();
348    
349                                            portalPreferences = portalPreferencesImpl.clone();
350                                    }
351    
352                                    if (session != null) {
353                                            session.setAttribute(
354                                                    WebKeys.PORTAL_PREFERENCES, portalPreferences);
355                                    }
356                            }
357                    }
358    
359                    portalPreferences.setSignedIn(signedIn);
360                    portalPreferences.setUserId(userId);
361    
362                    return portalPreferences;
363            }
364    
365            /**
366             * @deprecated As of 6.2.0, replaced by {@link
367             *             #getPortalPreferences(HttpSession, long, boolean)}
368             */
369            @Deprecated
370            @Override
371            public PortalPreferences getPortalPreferences(
372                    HttpSession session, long companyId, long userId, boolean signedIn) {
373    
374                    return getPortalPreferences(session, userId, signedIn);
375            }
376    
377            @Override
378            public PortalPreferences getPortalPreferences(
379                    long userId, boolean signedIn) {
380    
381                    return getPortalPreferences(null, userId, signedIn);
382            }
383    
384            /**
385             * @deprecated As of 6.2.0, replaced by {@link #getPortalPreferences(long,
386             *             boolean)}
387             */
388            @Deprecated
389            @Override
390            public PortalPreferences getPortalPreferences(
391                    long companyId, long userId, boolean signedIn) {
392    
393                    return getPortalPreferences(userId, signedIn);
394            }
395    
396            @Override
397            public PortalPreferences getPortalPreferences(
398                    PortletRequest portletRequest) {
399    
400                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
401                            portletRequest);
402    
403                    return getPortalPreferences(request);
404            }
405    
406            @Override
407            public PortletPreferences getPortletPreferences(
408                            HttpServletRequest request, String portletId)
409                    throws PortalException {
410    
411                    PortletPreferencesIds portletPreferencesIds = getPortletPreferencesIds(
412                            request, portletId);
413    
414                    return PortletPreferencesLocalServiceUtil.getPreferences(
415                            portletPreferencesIds);
416            }
417    
418            @Override
419            public PortletPreferencesIds getPortletPreferencesIds(
420                            HttpServletRequest request, Layout layout, String portletId)
421                    throws PortalException {
422    
423                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
424                            WebKeys.THEME_DISPLAY);
425    
426                    long siteGroupId = themeDisplay.getSiteGroupId();
427                    long userId = PortalUtil.getUserId(request);
428                    LayoutTypePortlet layoutTypePortlet =
429                            themeDisplay.getLayoutTypePortlet();
430    
431                    boolean modeEditGuest = false;
432    
433                    String portletMode = ParamUtil.getString(request, "p_p_mode");
434    
435                    if (portletMode.equals(LiferayPortletMode.EDIT_GUEST.toString()) ||
436                            ((layoutTypePortlet != null) &&
437                             layoutTypePortlet.hasModeEditGuestPortletId(portletId))) {
438    
439                            modeEditGuest = true;
440                    }
441    
442                    return getPortletPreferencesIds(
443                            siteGroupId, userId, layout, portletId, modeEditGuest);
444            }
445    
446            @Override
447            public PortletPreferencesIds getPortletPreferencesIds(
448                            HttpServletRequest request, String portletId)
449                    throws PortalException {
450    
451                    Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
452    
453                    return getPortletPreferencesIds(request, layout, portletId);
454            }
455    
456            @Override
457            public PortletPreferencesIds getPortletPreferencesIds(
458                            long siteGroupId, long userId, Layout layout, String portletId,
459                            boolean modeEditGuest)
460                    throws PortalException {
461    
462                    PermissionChecker permissionChecker =
463                            PermissionThreadLocal.getPermissionChecker();
464    
465                    String originalPortletId = portletId;
466    
467                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
468                            layout.getCompanyId(), portletId);
469    
470                    long ownerId = 0;
471                    int ownerType = 0;
472                    long plid = 0;
473    
474                    if (modeEditGuest) {
475                            boolean hasUpdateLayoutPermission = LayoutPermissionUtil.contains(
476                                    permissionChecker, layout, ActionKeys.UPDATE);
477    
478                            if (!layout.isPrivateLayout() && hasUpdateLayoutPermission) {
479                            }
480                            else {
481    
482                                    // Only users with the correct permissions can update guest
483                                    // preferences
484    
485                                    throw new PrincipalException.MustHavePermission(
486                                            permissionChecker, Layout.class.getName(),
487                                            layout.getLayoutId(), ActionKeys.UPDATE);
488                            }
489                    }
490    
491                    if (PortletConstants.hasUserId(originalPortletId) &&
492                            (PortletConstants.getUserId(originalPortletId) == userId)) {
493    
494                            ownerId = userId;
495                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
496                            plid = layout.getPlid();
497                    }
498                    else if (portlet.isPreferencesCompanyWide()) {
499                            ownerId = layout.getCompanyId();
500                            ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
501                            plid = PortletKeys.PREFS_PLID_SHARED;
502                            portletId = PortletConstants.getRootPortletId(portletId);
503                    }
504                    else {
505                            if (portlet.isPreferencesUniquePerLayout()) {
506                                    ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
507                                    ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
508                                    plid = layout.getPlid();
509    
510                                    LayoutTypePortlet layoutTypePortlet =
511                                            (LayoutTypePortlet)layout.getLayoutType();
512    
513                                    if (layoutTypePortlet.isPortletEmbedded(portletId)) {
514                                            ownerId = layout.getGroupId();
515                                            plid = PortletKeys.PREFS_PLID_SHARED;
516                                    }
517    
518                                    if (portlet.isPreferencesOwnedByGroup()) {
519                                    }
520                                    else {
521                                            if ((userId <= 0) || modeEditGuest) {
522                                                    userId = UserLocalServiceUtil.getDefaultUserId(
523                                                            layout.getCompanyId());
524                                            }
525    
526                                            ownerId = userId;
527                                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
528                                    }
529                            }
530                            else {
531                                    plid = PortletKeys.PREFS_PLID_SHARED;
532    
533                                    if (portlet.isPreferencesOwnedByGroup()) {
534                                            ownerId = siteGroupId;
535                                            ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
536                                            portletId = PortletConstants.getRootPortletId(portletId);
537                                    }
538                                    else {
539                                            if ((userId <= 0) || modeEditGuest) {
540                                                    userId = UserLocalServiceUtil.getDefaultUserId(
541                                                            layout.getCompanyId());
542                                            }
543    
544                                            ownerId = userId;
545                                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
546                                    }
547                            }
548                    }
549    
550                    return new PortletPreferencesIds(
551                            layout.getCompanyId(), ownerId, ownerType, plid, portletId);
552            }
553    
554            @Override
555            public PortletPreferencesIds getPortletPreferencesIds(
556                    long companyId, long siteGroupId, long plid, String portletId,
557                    String settingsScope) {
558    
559                    int ownerType = 0;
560                    long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
561    
562                    if (settingsScope.equals(
563                                    PortletPreferencesFactoryConstants.SETTINGS_SCOPE_COMPANY)) {
564    
565                            ownerId = companyId;
566                            ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
567                            plid = PortletKeys.PREFS_PLID_SHARED;
568                    }
569                    else if (settingsScope.equals(
570                                            PortletPreferencesFactoryConstants.SETTINGS_SCOPE_GROUP)) {
571    
572                            ownerId = siteGroupId;
573                            ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
574                            plid = PortletKeys.PREFS_PLID_SHARED;
575                    }
576                    else if (settingsScope.equals(
577                                            PortletPreferencesFactoryConstants.
578                                                    SETTINGS_SCOPE_PORTLET_INSTANCE)) {
579    
580                            ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
581                            ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
582    
583                            if (PortletConstants.hasUserId(portletId)) {
584                                    ownerId = PortletConstants.getUserId(portletId);
585                                    ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
586                            }
587                    }
588    
589                    return new PortletPreferencesIds(
590                            companyId, ownerId, ownerType, plid, portletId);
591            }
592    
593            @Override
594            public PortletPreferences getPortletSetup(
595                    HttpServletRequest request, String portletId) {
596    
597                    return getPortletSetup(request, portletId, null);
598            }
599    
600            @Override
601            public PortletPreferences getPortletSetup(
602                    HttpServletRequest request, String portletId,
603                    String defaultPreferences) {
604    
605                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
606                            JavaConstants.JAVAX_PORTLET_REQUEST);
607    
608                    if (portletRequest instanceof ConfigurationPortletRequest) {
609                            PortletRequestWrapper portletRequestWrapper =
610                                    (PortletRequestWrapper)portletRequest;
611    
612                            return portletRequestWrapper.getPreferences();
613                    }
614    
615                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
616                            WebKeys.THEME_DISPLAY);
617    
618                    return getPortletSetup(
619                            themeDisplay.getSiteGroupId(), themeDisplay.getLayout(), portletId,
620                            defaultPreferences);
621            }
622    
623            @Override
624            public PortletPreferences getPortletSetup(
625                    Layout layout, String portletId, String defaultPreferences) {
626    
627                    return getPortletSetup(
628                            LayoutConstants.DEFAULT_PLID, layout, portletId,
629                            defaultPreferences);
630            }
631    
632            @Override
633            public PortletPreferences getPortletSetup(
634                    long siteGroupId, Layout layout, String portletId,
635                    String defaultPreferences) {
636    
637                    return getPortletSetup(
638                            layout.getCompanyId(), siteGroupId, layout.getGroupId(),
639                            layout.getPlid(), portletId, defaultPreferences, false);
640            }
641    
642            @Override
643            public PortletPreferences getPortletSetup(PortletRequest portletRequest) {
644                    String portletId = PortalUtil.getPortletId(portletRequest);
645    
646                    return getPortletSetup(portletRequest, portletId);
647            }
648    
649            @Override
650            public PortletPreferences getPortletSetup(
651                    PortletRequest portletRequest, String portletId) {
652    
653                    if (portletRequest instanceof ConfigurationPortletRequest) {
654                            PortletRequestWrapper portletRequestWrapper =
655                                    (PortletRequestWrapper)portletRequest;
656    
657                            return portletRequestWrapper.getPreferences();
658                    }
659    
660                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
661                            portletRequest);
662    
663                    return getPortletSetup(request, portletId);
664            }
665    
666            @Override
667            public Map<Long, PortletPreferences> getPortletSetupMap(
668                    long companyId, long groupId, long ownerId, int ownerType,
669                    String portletId, boolean privateLayout) {
670    
671                    Map<Long, PortletPreferences> portletSetupMap = new HashMap<>();
672    
673                    List<com.liferay.portal.model.PortletPreferences>
674                            portletPreferencesList =
675                                    PortletPreferencesLocalServiceUtil.getPortletPreferences(
676                                            companyId, groupId, ownerId, ownerType, portletId,
677                                            privateLayout);
678    
679                    for (com.liferay.portal.model.PortletPreferences portletPreferences :
680                                    portletPreferencesList) {
681    
682                            PortletPreferences portletSetup =
683                                    PortletPreferencesLocalServiceUtil.getPreferences(
684                                            companyId, ownerId, ownerType, portletPreferences.getPlid(),
685                                            portletId);
686    
687                            portletSetupMap.put(portletPreferences.getPlid(), portletSetup);
688                    }
689    
690                    return portletSetupMap;
691            }
692    
693            @Override
694            public PortletPreferences getPreferences(HttpServletRequest request) {
695                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
696                            JavaConstants.JAVAX_PORTLET_REQUEST);
697    
698                    PortletPreferences portletPreferences = null;
699    
700                    if (portletRequest != null) {
701                            PortletPreferencesWrapper portletPreferencesWrapper =
702                                    (PortletPreferencesWrapper)portletRequest.getPreferences();
703    
704                            portletPreferences =
705                                    portletPreferencesWrapper.getPortletPreferencesImpl();
706                    }
707    
708                    return portletPreferences;
709            }
710    
711            @Override
712            public PreferencesValidator getPreferencesValidator(Portlet portlet) {
713                    return PortalUtil.getPreferencesValidator(portlet);
714            }
715    
716            @Override
717            public PortletPreferences getStrictLayoutPortletSetup(
718                    Layout layout, String portletId) {
719    
720                    long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
721                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
722    
723                    if (PortletConstants.hasUserId(portletId)) {
724                            ownerId = PortletConstants.getUserId(portletId);
725                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
726                    }
727    
728                    return PortletPreferencesLocalServiceUtil.getStrictPreferences(
729                            layout.getCompanyId(), ownerId, ownerType, layout.getPlid(),
730                            portletId);
731            }
732    
733            @Override
734            public PortletPreferences getStrictPortletSetup(
735                    Layout layout, String portletId) {
736    
737                    return getPortletSetup(
738                            layout.getCompanyId(), LayoutConstants.DEFAULT_PLID,
739                            layout.getGroupId(), layout.getPlid(), portletId, StringPool.BLANK,
740                            true);
741            }
742    
743            @Override
744            public PortletPreferences getStrictPortletSetup(
745                    long companyId, long groupId, String portletId) {
746    
747                    return getPortletSetup(
748                            companyId, LayoutConstants.DEFAULT_PLID, groupId,
749                            LayoutConstants.DEFAULT_PLID, portletId, StringPool.BLANK, true);
750            }
751    
752            @Override
753            public StrictPortletPreferencesImpl strictFromXML(
754                    long companyId, long ownerId, int ownerType, long plid,
755                    String portletId, String xml) {
756    
757                    Map<String, Preference> preferencesMap = toPreferencesMap(xml);
758    
759                    return new StrictPortletPreferencesImpl(
760                            companyId, ownerId, ownerType, plid, portletId, xml,
761                            preferencesMap);
762            }
763    
764            @Override
765            public String toXML(PortalPreferences portalPreferences) {
766                    PortalPreferencesImpl portalPreferencesImpl = null;
767    
768                    if (portalPreferences instanceof PortalPreferencesWrapper) {
769                            PortalPreferencesWrapper portalPreferencesWrapper =
770                                    (PortalPreferencesWrapper)portalPreferences;
771    
772                            portalPreferencesImpl =
773                                    portalPreferencesWrapper.getPortalPreferencesImpl();
774                    }
775                    else {
776                            portalPreferencesImpl = (PortalPreferencesImpl)portalPreferences;
777                    }
778    
779                    return portalPreferencesImpl.toXML();
780            }
781    
782            @Override
783            public String toXML(PortletPreferences portletPreferences) {
784                    PortletPreferencesImpl portletPreferencesImpl = null;
785    
786                    if (portletPreferences instanceof PortletPreferencesWrapper) {
787                            PortletPreferencesWrapper portletPreferencesWrapper =
788                                    (PortletPreferencesWrapper)portletPreferences;
789    
790                            portletPreferencesImpl =
791                                    portletPreferencesWrapper.getPortletPreferencesImpl();
792                    }
793                    else {
794                            portletPreferencesImpl = (PortletPreferencesImpl)portletPreferences;
795                    }
796    
797                    return portletPreferencesImpl.toXML();
798            }
799    
800            protected static Preference readPreference(XMLEventReader xmlEventReader)
801                    throws XMLStreamException {
802    
803                    String name = null;
804                    List<String> values = new ArrayList<>();
805                    boolean readOnly = false;
806    
807                    while (xmlEventReader.hasNext()) {
808                            XMLEvent xmlEvent = xmlEventReader.nextEvent();
809    
810                            if (xmlEvent.isStartElement()) {
811                                    StartElement startElement = xmlEvent.asStartElement();
812    
813                                    String elementName = startElement.getName().getLocalPart();
814    
815                                    if (elementName.equals("name")) {
816                                            name = StAXReaderUtil.read(xmlEventReader);
817                                    }
818                                    else if (elementName.equals("value")) {
819                                            String value = StAXReaderUtil.read(xmlEventReader);
820    
821                                            values.add(value);
822                                    }
823                                    else if (elementName.equals("read-only")) {
824                                            String value = StAXReaderUtil.read(xmlEventReader);
825    
826                                            readOnly = GetterUtil.getBoolean(value);
827                                    }
828                            }
829                            else if (xmlEvent.isEndElement()) {
830                                    EndElement endElement = xmlEvent.asEndElement();
831    
832                                    String elementName = endElement.getName().getLocalPart();
833    
834                                    if (elementName.equals("preference")) {
835                                            break;
836                                    }
837                            }
838                    }
839    
840                    return new Preference(
841                            name, values.toArray(new String[values.size()]), readOnly);
842            }
843    
844            protected PortletPreferences getPortletSetup(
845                    long companyId, long siteGroupId, long layoutGroupId, long plid,
846                    String portletId, String defaultPreferences, boolean strictMode) {
847    
848                    String originalPortletId = portletId;
849    
850                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
851                            companyId, portletId);
852    
853                    boolean uniquePerLayout = false;
854                    boolean uniquePerGroup = false;
855    
856                    if (portlet.isPreferencesCompanyWide()) {
857                            portletId = PortletConstants.getRootPortletId(portletId);
858                    }
859                    else {
860                            if (portlet.isPreferencesUniquePerLayout()) {
861                                    uniquePerLayout = true;
862    
863                                    if (portlet.isPreferencesOwnedByGroup()) {
864                                            uniquePerGroup = true;
865                                    }
866                            }
867                            else {
868                                    if (portlet.isPreferencesOwnedByGroup()) {
869                                            uniquePerGroup = true;
870                                            portletId = PortletConstants.getRootPortletId(portletId);
871                                    }
872                            }
873                    }
874    
875                    long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
876                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
877    
878                    Group group = GroupLocalServiceUtil.fetchGroup(siteGroupId);
879    
880                    if ((group != null) && group.isLayout()) {
881                            plid = group.getClassPK();
882                    }
883    
884                    if (PortletConstants.hasUserId(originalPortletId)) {
885                            ownerId = PortletConstants.getUserId(originalPortletId);
886                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
887                    }
888                    else if (!uniquePerLayout) {
889                            plid = PortletKeys.PREFS_PLID_SHARED;
890    
891                            if (uniquePerGroup) {
892                                    if (siteGroupId > LayoutConstants.DEFAULT_PLID) {
893                                            ownerId = siteGroupId;
894                                    }
895                                    else {
896                                            ownerId = layoutGroupId;
897                                    }
898    
899                                    ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
900                            }
901                            else {
902                                    ownerId = companyId;
903                                    ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
904                            }
905                    }
906    
907                    if (strictMode) {
908                            return PortletPreferencesLocalServiceUtil.getStrictPreferences(
909                                    companyId, ownerId, ownerType, plid, portletId);
910                    }
911    
912                    return PortletPreferencesLocalServiceUtil.getPreferences(
913                            companyId, ownerId, ownerType, plid, portletId, defaultPreferences);
914            }
915    
916            protected Map<String, Preference> toPreferencesMap(String xml) {
917                    if (Validator.isNull(xml)) {
918                            return Collections.emptyMap();
919                    }
920    
921                    String cacheKey = _encodeCacheKey(xml);
922    
923                    Map<String, Preference> preferencesMap = _preferencesMapPortalCache.get(
924                            cacheKey);
925    
926                    if (preferencesMap != null) {
927                            return preferencesMap;
928                    }
929    
930                    preferencesMap = createPreferencesMap(xml);
931    
932                    _preferencesMapPortalCache.put(cacheKey, preferencesMap);
933    
934                    return preferencesMap;
935            }
936    
937            private String _encodeCacheKey(String xml) {
938                    if (xml.length() <=
939                                    PropsValues.PORTLET_PREFERENCES_CACHE_KEY_THRESHOLD_SIZE) {
940    
941                            return xml;
942                    }
943    
944                    CacheKeyGenerator cacheKeyGenerator =
945                            CacheKeyGeneratorUtil.getCacheKeyGenerator(
946                                    PortletPreferencesFactoryImpl.class.getName());
947    
948                    if (_log.isDebugEnabled()) {
949                            _log.debug("Cache key generator " + cacheKeyGenerator.getClass());
950                    }
951    
952                    return String.valueOf(cacheKeyGenerator.getCacheKey(xml));
953            }
954    
955            private static final Log _log = LogFactoryUtil.getLog(
956                    PortletPreferencesFactoryImpl.class);
957    
958            private final PortalCache<String, Map<String, Preference>>
959                    _preferencesMapPortalCache = SingleVMPoolUtil.getPortalCache(
960                            PortletPreferencesFactoryImpl.class.getName());
961    
962    }