001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                    Map<String, Preference> preferencesMap = toPreferencesMap(xml);
151    
152                    return new PortalPreferencesImpl(
153                            ownerId, ownerType, xml, preferencesMap, false);
154            }
155    
156            @Override
157            public PortletPreferencesImpl fromXML(
158                            long companyId, long ownerId, int ownerType, long plid,
159                            String portletId, String xml)
160                    throws SystemException {
161    
162                    Map<String, Preference> preferencesMap = toPreferencesMap(xml);
163    
164                    return new PortletPreferencesImpl(
165                            companyId, ownerId, ownerType, plid, portletId, xml,
166                            preferencesMap);
167            }
168    
169            /**
170             * @deprecated As of 6.2.0, replaced by {@link #fromXML(long, int, String)}
171             */
172            @Override
173            public PortalPreferences fromXML(
174                            long companyId, long ownerId, int ownerType, String xml)
175                    throws SystemException {
176    
177                    return fromXML(ownerId, ownerType, xml);
178            }
179    
180            @Override
181            public PortletPreferences getLayoutPortletSetup(
182                            Layout layout, String portletId)
183                    throws SystemException {
184    
185                    long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
186                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
187    
188                    if (PortletConstants.hasUserId(portletId)) {
189                            ownerId = PortletConstants.getUserId(portletId);
190                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
191                    }
192    
193                    return PortletPreferencesLocalServiceUtil.getPreferences(
194                            layout.getCompanyId(), ownerId, ownerType, layout.getPlid(),
195                            portletId);
196            }
197    
198            @Override
199            public PortalPreferences getPortalPreferences(HttpServletRequest request)
200                    throws SystemException {
201    
202                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
203                            WebKeys.THEME_DISPLAY);
204    
205                    return getPortalPreferences(
206                            request.getSession(), themeDisplay.getUserId(),
207                            themeDisplay.isSignedIn());
208            }
209    
210            @Override
211            public PortalPreferences getPortalPreferences(
212                            HttpSession session, long userId, boolean signedIn)
213                    throws SystemException {
214    
215                    long ownerId = userId;
216                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
217    
218                    PortalPreferences portalPreferences = null;
219    
220                    if (signedIn) {
221                            PortalPreferencesWrapper portalPreferencesWrapper =
222                                    PortalPreferencesWrapperCacheUtil.get(ownerId, ownerType);
223    
224                            if (portalPreferencesWrapper == null) {
225                                    portalPreferencesWrapper =
226                                            (PortalPreferencesWrapper)
227                                                    PortalPreferencesLocalServiceUtil.getPreferences(
228                                                            ownerId, ownerType);
229    
230                                    portalPreferences =
231                                            portalPreferencesWrapper.getPortalPreferencesImpl();
232                            }
233                            else {
234                                    PortalPreferencesImpl portalPreferencesImpl =
235                                            portalPreferencesWrapper.getPortalPreferencesImpl();
236    
237                                    portalPreferences = portalPreferencesImpl.clone();
238                            }
239                    }
240                    else {
241                            if (session != null) {
242                                    portalPreferences = (PortalPreferences)session.getAttribute(
243                                            WebKeys.PORTAL_PREFERENCES);
244                            }
245    
246                            if (portalPreferences == null) {
247                                    PortalPreferencesWrapper portalPreferencesWrapper =
248                                            PortalPreferencesWrapperCacheUtil.get(ownerId, ownerType);
249    
250                                    if (portalPreferencesWrapper == null) {
251                                            portalPreferencesWrapper =
252                                                    (PortalPreferencesWrapper)
253                                                            PortalPreferencesLocalServiceUtil.getPreferences(
254                                                                    ownerId, ownerType);
255    
256                                            portalPreferences =
257                                                    portalPreferencesWrapper.getPortalPreferencesImpl();
258                                    }
259                                    else {
260                                            PortalPreferencesImpl portalPreferencesImpl =
261                                                    portalPreferencesWrapper.getPortalPreferencesImpl();
262    
263                                            portalPreferences = portalPreferencesImpl.clone();
264                                    }
265    
266                                    if (session != null) {
267                                            session.setAttribute(
268                                                    WebKeys.PORTAL_PREFERENCES, portalPreferences);
269                                    }
270                            }
271                    }
272    
273                    portalPreferences.setSignedIn(signedIn);
274                    portalPreferences.setUserId(userId);
275    
276                    return portalPreferences;
277            }
278    
279            /**
280             * @deprecated As of 6.2.0, replaced by {@link
281             *             #getPortalPreferences(HttpSession, long, boolean)}
282             */
283            @Override
284            public PortalPreferences getPortalPreferences(
285                            HttpSession session, long companyId, long userId, boolean signedIn)
286                    throws SystemException {
287    
288                    return getPortalPreferences(session, userId, signedIn);
289            }
290    
291            @Override
292            public PortalPreferences getPortalPreferences(long userId, boolean signedIn)
293                    throws SystemException {
294    
295                    return getPortalPreferences(null, userId, signedIn);
296            }
297    
298            /**
299             * @deprecated As of 6.2.0, replaced by {@link #getPortalPreferences(long,
300             *             boolean)}
301             */
302            @Override
303            public PortalPreferences getPortalPreferences(
304                            long companyId, long userId, boolean signedIn)
305                    throws SystemException {
306    
307                    return getPortalPreferences(userId, signedIn);
308            }
309    
310            @Override
311            public PortalPreferences getPortalPreferences(PortletRequest portletRequest)
312                    throws SystemException {
313    
314                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
315                            portletRequest);
316    
317                    return getPortalPreferences(request);
318            }
319    
320            @Override
321            public PortletPreferences getPortletPreferences(
322                            HttpServletRequest request, String portletId)
323                    throws PortalException, SystemException {
324    
325                    PortletPreferencesIds portletPreferencesIds = getPortletPreferencesIds(
326                            request, portletId);
327    
328                    return PortletPreferencesLocalServiceUtil.getPreferences(
329                            portletPreferencesIds);
330            }
331    
332            @Override
333            public PortletPreferencesIds getPortletPreferencesIds(
334                            HttpServletRequest request, Layout layout, String portletId)
335                    throws PortalException, SystemException {
336    
337                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
338                            WebKeys.THEME_DISPLAY);
339    
340                    long scopeGroupId = PortalUtil.getScopeGroupId(
341                            request, portletId, true);
342                    long userId = PortalUtil.getUserId(request);
343                    LayoutTypePortlet layoutTypePortlet =
344                            themeDisplay.getLayoutTypePortlet();
345    
346                    boolean modeEditGuest = false;
347    
348                    String portletMode = ParamUtil.getString(request, "p_p_mode");
349    
350                    if (portletMode.equals(LiferayPortletMode.EDIT_GUEST.toString()) ||
351                            ((layoutTypePortlet != null) &&
352                             layoutTypePortlet.hasModeEditGuestPortletId(portletId))) {
353    
354                            modeEditGuest = true;
355                    }
356    
357                    return getPortletPreferencesIds(
358                            scopeGroupId, userId, layout, portletId, modeEditGuest);
359            }
360    
361            @Override
362            public PortletPreferencesIds getPortletPreferencesIds(
363                            HttpServletRequest request, String portletId)
364                    throws PortalException, SystemException {
365    
366                    Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
367    
368                    return getPortletPreferencesIds(request, layout, portletId);
369            }
370    
371            @Override
372            public PortletPreferencesIds getPortletPreferencesIds(
373                            long scopeGroupId, long userId, Layout layout, String portletId,
374                            boolean modeEditGuest)
375                    throws PortalException, SystemException {
376    
377                    PermissionChecker permissionChecker =
378                            PermissionThreadLocal.getPermissionChecker();
379    
380                    String originalPortletId = portletId;
381    
382                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
383                            layout.getCompanyId(), portletId);
384    
385                    long ownerId = 0;
386                    int ownerType = 0;
387                    long plid = 0;
388    
389                    if (modeEditGuest) {
390                            boolean hasUpdateLayoutPermission = LayoutPermissionUtil.contains(
391                                    permissionChecker, layout, ActionKeys.UPDATE);
392    
393                            if (!layout.isPrivateLayout() && hasUpdateLayoutPermission) {
394                            }
395                            else {
396    
397                                    // Only users with the correct permissions can update guest
398                                    // preferences
399    
400                                    throw new PrincipalException();
401                            }
402                    }
403    
404                    if (PortletConstants.hasUserId(originalPortletId) &&
405                            (PortletConstants.getUserId(originalPortletId) == userId)) {
406    
407                            ownerId = userId;
408                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
409                            plid = layout.getPlid();
410                    }
411                    else if (portlet.isPreferencesCompanyWide()) {
412                            ownerId = layout.getCompanyId();
413                            ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
414                            plid = PortletKeys.PREFS_PLID_SHARED;
415                            portletId = PortletConstants.getRootPortletId(portletId);
416                    }
417                    else {
418                            if (portlet.isPreferencesUniquePerLayout()) {
419                                    ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
420                                    ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
421                                    plid = layout.getPlid();
422    
423                                    if (portlet.isPreferencesOwnedByGroup()) {
424                                    }
425                                    else {
426                                            if ((userId <= 0) || modeEditGuest) {
427                                                    userId = UserLocalServiceUtil.getDefaultUserId(
428                                                            layout.getCompanyId());
429                                            }
430    
431                                            ownerId = userId;
432                                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
433                                    }
434                            }
435                            else {
436                                    plid = PortletKeys.PREFS_PLID_SHARED;
437    
438                                    if (portlet.isPreferencesOwnedByGroup()) {
439                                            ownerId = scopeGroupId;
440                                            ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
441                                            portletId = PortletConstants.getRootPortletId(portletId);
442                                    }
443                                    else {
444                                            if ((userId <= 0) || modeEditGuest) {
445                                                    userId = UserLocalServiceUtil.getDefaultUserId(
446                                                            layout.getCompanyId());
447                                            }
448    
449                                            ownerId = userId;
450                                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
451                                    }
452                            }
453                    }
454    
455                    return new PortletPreferencesIds(
456                            layout.getCompanyId(), ownerId, ownerType, plid, portletId);
457            }
458    
459            @Override
460            public PortletPreferences getPortletSetup(
461                            HttpServletRequest request, String portletId)
462                    throws SystemException {
463    
464                    return getPortletSetup(request, portletId, null);
465            }
466    
467            @Override
468            public PortletPreferences getPortletSetup(
469                            HttpServletRequest request, String portletId,
470                            String defaultPreferences)
471                    throws SystemException {
472    
473                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
474                            JavaConstants.JAVAX_PORTLET_REQUEST);
475    
476                    if (portletRequest instanceof ConfigurationPortletRequest) {
477                            PortletRequestWrapper portletRequestWrapper =
478                                    (PortletRequestWrapper)portletRequest;
479    
480                            return portletRequestWrapper.getPreferences();
481                    }
482    
483                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
484                            WebKeys.THEME_DISPLAY);
485    
486                    return getPortletSetup(
487                            themeDisplay.getScopeGroupId(), themeDisplay.getLayout(), portletId,
488                            defaultPreferences);
489            }
490    
491            @Override
492            public PortletPreferences getPortletSetup(
493                            Layout layout, String portletId, String defaultPreferences)
494                    throws SystemException {
495    
496                    return getPortletSetup(
497                            LayoutConstants.DEFAULT_PLID, layout, portletId,
498                            defaultPreferences);
499            }
500    
501            @Override
502            public PortletPreferences getPortletSetup(
503                            long scopeGroupId, Layout layout, String portletId,
504                            String defaultPreferences)
505                    throws SystemException {
506    
507                    return getPortletSetup(
508                            scopeGroupId, layout, portletId, defaultPreferences, false);
509            }
510    
511            @Override
512            public PortletPreferences getPortletSetup(PortletRequest portletRequest)
513                    throws SystemException {
514    
515                    String portletId = PortalUtil.getPortletId(portletRequest);
516    
517                    return getPortletSetup(portletRequest, portletId);
518            }
519    
520            @Override
521            public PortletPreferences getPortletSetup(
522                            PortletRequest portletRequest, String portletId)
523                    throws SystemException {
524    
525                    if (portletRequest instanceof ConfigurationPortletRequest) {
526                            PortletRequestWrapper portletRequestWrapper =
527                                    (PortletRequestWrapper)portletRequest;
528    
529                            return portletRequestWrapper.getPreferences();
530                    }
531    
532                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
533                            portletRequest);
534    
535                    return getPortletSetup(request, portletId);
536            }
537    
538            @Override
539            public Map<Long, PortletPreferences> getPortletSetupMap(
540                            long companyId, long groupId, long ownerId, int ownerType,
541                            String portletId, boolean privateLayout)
542                    throws SystemException {
543    
544                    Map<Long, PortletPreferences> portletSetupMap =
545                            new HashMap<Long, PortletPreferences>();
546    
547                    List<com.liferay.portal.model.PortletPreferences>
548                            portletPreferencesList =
549                                    PortletPreferencesLocalServiceUtil.getPortletPreferences(
550                                            companyId, groupId, ownerId, ownerType, portletId,
551                                            privateLayout);
552    
553                    for (com.liferay.portal.model.PortletPreferences portletPreferences :
554                                    portletPreferencesList) {
555    
556                            PortletPreferences portletSetup =
557                                    PortletPreferencesLocalServiceUtil.getPreferences(
558                                            companyId, ownerId, ownerType, portletPreferences.getPlid(),
559                                            portletId);
560    
561                            portletSetupMap.put(portletPreferences.getPlid(), portletSetup);
562                    }
563    
564                    return portletSetupMap;
565            }
566    
567            @Override
568            public PortletPreferences getPreferences(HttpServletRequest request) {
569                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
570                            JavaConstants.JAVAX_PORTLET_REQUEST);
571    
572                    PortletPreferences portletPreferences = null;
573    
574                    if (portletRequest != null) {
575                            PortletPreferencesWrapper portletPreferencesWrapper =
576                                    (PortletPreferencesWrapper)portletRequest.getPreferences();
577    
578                            portletPreferences =
579                                    portletPreferencesWrapper.getPortletPreferencesImpl();
580                    }
581    
582                    return portletPreferences;
583            }
584    
585            @Override
586            public PreferencesValidator getPreferencesValidator(Portlet portlet) {
587                    return PortalUtil.getPreferencesValidator(portlet);
588            }
589    
590            @Override
591            public PortletPreferences getStrictLayoutPortletSetup(
592                            Layout layout, String portletId)
593                    throws SystemException {
594    
595                    long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
596                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
597    
598                    if (PortletConstants.hasUserId(portletId)) {
599                            ownerId = PortletConstants.getUserId(portletId);
600                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
601                    }
602    
603                    return PortletPreferencesLocalServiceUtil.getStrictPreferences(
604                            layout.getCompanyId(), ownerId, ownerType, layout.getPlid(),
605                            portletId);
606            }
607    
608            @Override
609            public PortletPreferences getStrictPortletSetup(
610                            Layout layout, String portletId)
611                    throws SystemException {
612    
613                    return getPortletSetup(
614                            LayoutConstants.DEFAULT_PLID, layout, portletId, StringPool.BLANK,
615                            true);
616            }
617    
618            @Override
619            public StrictPortletPreferencesImpl strictFromXML(
620                            long companyId, long ownerId, int ownerType, long plid,
621                            String portletId, String xml)
622                    throws SystemException {
623    
624                    Map<String, Preference> preferencesMap = toPreferencesMap(xml);
625    
626                    return new StrictPortletPreferencesImpl(
627                            companyId, ownerId, ownerType, plid, portletId, xml,
628                            preferencesMap);
629            }
630    
631            @Override
632            public String toXML(PortalPreferences portalPreferences) {
633                    PortalPreferencesImpl portalPreferencesImpl =
634                            (PortalPreferencesImpl)portalPreferences;
635    
636                    return portalPreferencesImpl.toXML();
637            }
638    
639            @Override
640            public String toXML(PortletPreferences portletPreferences) {
641                    PortletPreferencesImpl portletPreferencesImpl =
642                            (PortletPreferencesImpl)portletPreferences;
643    
644                    return portletPreferencesImpl.toXML();
645            }
646    
647            protected PortletPreferences getPortletSetup(
648                            long scopeGroupId, Layout layout, String portletId,
649                            String defaultPreferences, boolean strictMode)
650                    throws SystemException {
651    
652                    String originalPortletId = portletId;
653    
654                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
655                            layout.getCompanyId(), portletId);
656    
657                    boolean uniquePerLayout = false;
658                    boolean uniquePerGroup = false;
659    
660                    if (portlet.isPreferencesCompanyWide()) {
661                            portletId = PortletConstants.getRootPortletId(portletId);
662                    }
663                    else {
664                            if (portlet.isPreferencesUniquePerLayout()) {
665                                    uniquePerLayout = true;
666    
667                                    if (portlet.isPreferencesOwnedByGroup()) {
668                                            uniquePerGroup = true;
669                                    }
670                            }
671                            else {
672                                    if (portlet.isPreferencesOwnedByGroup()) {
673                                            uniquePerGroup = true;
674                                            portletId = PortletConstants.getRootPortletId(portletId);
675                                    }
676                            }
677                    }
678    
679                    long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
680                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
681                    long plid = layout.getPlid();
682    
683                    Group group = GroupLocalServiceUtil.fetchGroup(scopeGroupId);
684    
685                    if ((group != null) && group.isLayout()) {
686                            plid = group.getClassPK();
687                    }
688    
689                    if (PortletConstants.hasUserId(originalPortletId)) {
690                            ownerId = PortletConstants.getUserId(originalPortletId);
691                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
692                    }
693                    else if (!uniquePerLayout) {
694                            plid = PortletKeys.PREFS_PLID_SHARED;
695    
696                            if (uniquePerGroup) {
697                                    if (scopeGroupId > LayoutConstants.DEFAULT_PLID) {
698                                            ownerId = scopeGroupId;
699                                    }
700                                    else {
701                                            ownerId = layout.getGroupId();
702                                    }
703    
704                                    ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
705                            }
706                            else {
707                                    ownerId = layout.getCompanyId();
708                                    ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
709                            }
710                    }
711    
712                    if (strictMode) {
713                            return PortletPreferencesLocalServiceUtil.getStrictPreferences(
714                                    layout.getCompanyId(), ownerId, ownerType, plid, portletId);
715                    }
716    
717                    return PortletPreferencesLocalServiceUtil.getPreferences(
718                            layout.getCompanyId(), ownerId, ownerType, plid, portletId,
719                            defaultPreferences);
720            }
721    
722            protected Preference readPreference(XMLEventReader xmlEventReader)
723                    throws XMLStreamException {
724    
725                    String name = null;
726                    List<String> values = new ArrayList<String>();
727                    boolean readOnly = false;
728    
729                    while (xmlEventReader.hasNext()) {
730                            XMLEvent xmlEvent = xmlEventReader.nextEvent();
731    
732                            if (xmlEvent.isStartElement()) {
733                                    StartElement startElement = xmlEvent.asStartElement();
734    
735                                    String elementName = startElement.getName().getLocalPart();
736    
737                                    if (elementName.equals("name")) {
738                                            name = StAXReaderUtil.read(xmlEventReader);
739                                    }
740                                    else if (elementName.equals("value")) {
741                                            String value = StAXReaderUtil.read(xmlEventReader);
742    
743                                            values.add(value);
744                                    }
745                                    else if (elementName.equals("read-only")) {
746                                            String value = StAXReaderUtil.read(xmlEventReader);
747    
748                                            readOnly = GetterUtil.getBoolean(value);
749                                    }
750                            }
751                            else if (xmlEvent.isEndElement()) {
752                                    EndElement endElement = xmlEvent.asEndElement();
753    
754                                    String elementName = endElement.getName().getLocalPart();
755    
756                                    if (elementName.equals("preference")) {
757                                            break;
758                                    }
759                            }
760                    }
761    
762                    return new Preference(
763                            name, values.toArray(new String[values.size()]), readOnly);
764            }
765    
766            protected Map<String, Preference> toPreferencesMap(String xml)
767                    throws SystemException {
768    
769                    if (Validator.isNull(xml)) {
770                            return Collections.emptyMap();
771                    }
772    
773                    Map<String, Preference> preferencesMap = _preferencesMapPortalCache.get(
774                            xml);
775    
776                    if (preferencesMap != null) {
777                            return preferencesMap;
778                    }
779    
780                    XMLEventReader xmlEventReader = null;
781    
782                    try {
783                            XMLInputFactory xmlInputFactory =
784                                    StAXReaderUtil.getXMLInputFactory();
785    
786                            xmlEventReader = xmlInputFactory.createXMLEventReader(
787                                    new UnsyncStringReader(xml));
788    
789                            while (xmlEventReader.hasNext()) {
790                                    XMLEvent xmlEvent = xmlEventReader.nextEvent();
791    
792                                    if (xmlEvent.isStartElement()) {
793                                            StartElement startElement = xmlEvent.asStartElement();
794    
795                                            String elementName = startElement.getName().getLocalPart();
796    
797                                            if (elementName.equals("preference")) {
798                                                    Preference preference = readPreference(xmlEventReader);
799    
800                                                    if (preferencesMap == null) {
801                                                            preferencesMap = new HashMap<String, Preference>();
802                                                    }
803    
804                                                    preferencesMap.put(preference.getName(), preference);
805                                            }
806                                    }
807                            }
808                    }
809                    catch (XMLStreamException xse) {
810                            throw new SystemException(xse);
811                    }
812                    finally {
813                            if (xmlEventReader != null) {
814                                    try {
815                                            xmlEventReader.close();
816                                    }
817                                    catch (XMLStreamException xse) {
818                                    }
819                            }
820                    }
821    
822                    if (preferencesMap == null) {
823                            preferencesMap = Collections.emptyMap();
824                    }
825    
826                    _preferencesMapPortalCache.put(xml, preferencesMap);
827    
828                    return preferencesMap;
829            }
830    
831            private PortalCache<String, Map<String, Preference>>
832                    _preferencesMapPortalCache = SingleVMPoolUtil.getCache(
833                            PortletPreferencesFactoryImpl.class.getName());
834    
835    }