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.portal.service.impl;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.PortalPreferences;
022    import com.liferay.portal.model.PortletConstants;
023    import com.liferay.portal.service.base.PortalPreferencesLocalServiceBaseImpl;
024    import com.liferay.portlet.PortalPreferencesImpl;
025    import com.liferay.portlet.PortalPreferencesWrapper;
026    import com.liferay.portlet.PortalPreferencesWrapperCacheUtil;
027    import com.liferay.portlet.PortletPreferencesFactoryUtil;
028    
029    import javax.portlet.PortletPreferences;
030    
031    /**
032     * @author Alexander Chow
033     */
034    public class PortalPreferencesLocalServiceImpl
035            extends PortalPreferencesLocalServiceBaseImpl {
036    
037            @Override
038            public PortalPreferences addPortalPreferences(
039                    long ownerId, int ownerType, String defaultPreferences) {
040    
041                    PortalPreferencesWrapperCacheUtil.remove(ownerId, ownerType);
042    
043                    long portalPreferencesId = counterLocalService.increment();
044    
045                    PortalPreferences portalPreferences =
046                            portalPreferencesPersistence.create(portalPreferencesId);
047    
048                    portalPreferences.setOwnerId(ownerId);
049                    portalPreferences.setOwnerType(ownerType);
050    
051                    if (Validator.isNull(defaultPreferences)) {
052                            defaultPreferences = PortletConstants.DEFAULT_PREFERENCES;
053                    }
054    
055                    portalPreferences.setPreferences(defaultPreferences);
056    
057                    try {
058                            portalPreferencesPersistence.update(portalPreferences);
059                    }
060                    catch (SystemException se) {
061                            if (_log.isWarnEnabled()) {
062                                    _log.warn(
063                                            "Add failed, fetch {ownerId=" + ownerId + ", ownerType=" +
064                                                    ownerType + "}");
065                            }
066    
067                            portalPreferences = portalPreferencesPersistence.fetchByO_O(
068                                    ownerId, ownerType, false);
069    
070                            if (portalPreferences == null) {
071                                    throw se;
072                            }
073                    }
074    
075                    return portalPreferences;
076            }
077    
078            @Override
079            public PortletPreferences getPreferences(long ownerId, int ownerType) {
080                    return getPreferences(ownerId, ownerType, null);
081            }
082    
083            @Override
084            public PortletPreferences getPreferences(
085                    long ownerId, int ownerType, String defaultPreferences) {
086    
087                    PortalPreferencesWrapper portalPreferencesWrapper =
088                            PortalPreferencesWrapperCacheUtil.get(ownerId, ownerType);
089    
090                    if (portalPreferencesWrapper != null) {
091                            return portalPreferencesWrapper.clone();
092                    }
093    
094                    PortalPreferences portalPreferences =
095                            portalPreferencesPersistence.fetchByO_O(ownerId, ownerType);
096    
097                    if (portalPreferences == null) {
098                            portalPreferences =
099                                    portalPreferencesLocalService.addPortalPreferences(
100                                            ownerId, ownerType, defaultPreferences);
101                    }
102    
103                    PortalPreferencesImpl portalPreferencesImpl = new PortalPreferencesImpl(
104                            portalPreferences, false);
105    
106                    portalPreferencesWrapper = new PortalPreferencesWrapper(
107                            portalPreferencesImpl);
108    
109                    PortalPreferencesWrapperCacheUtil.put(
110                            ownerId, ownerType, portalPreferencesWrapper);
111    
112                    return portalPreferencesWrapper.clone();
113            }
114    
115            @Override
116            public PortalPreferences updatePreferences(
117                    long ownerId, int ownerType,
118                    com.liferay.portlet.PortalPreferences portalPreferences) {
119    
120                    String xml = PortletPreferencesFactoryUtil.toXML(portalPreferences);
121    
122                    return updatePreferences(ownerId, ownerType, xml);
123            }
124    
125            @Override
126            public PortalPreferences updatePreferences(
127                    long ownerId, int ownerType, String xml) {
128    
129                    PortalPreferencesWrapperCacheUtil.remove(ownerId, ownerType);
130    
131                    PortalPreferences portalPreferences =
132                            portalPreferencesPersistence.fetchByO_O(ownerId, ownerType);
133    
134                    if (portalPreferences == null) {
135                            long portalPreferencesId = counterLocalService.increment();
136    
137                            portalPreferences = portalPreferencesPersistence.create(
138                                    portalPreferencesId);
139    
140                            portalPreferences.setOwnerId(ownerId);
141                            portalPreferences.setOwnerType(ownerType);
142                    }
143    
144                    portalPreferences.setPreferences(xml);
145    
146                    portalPreferencesPersistence.update(portalPreferences);
147    
148                    return portalPreferences;
149            }
150    
151            private static final Log _log = LogFactoryUtil.getLog(
152                    PortalPreferencesLocalServiceImpl.class);
153    
154    }