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