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            /**
079             * @deprecated As of 6.2.0, replaced by {@link #addPortalPreferences(long,
080             *             int, String)}
081             */
082            @Deprecated
083            @Override
084            public PortalPreferences addPortalPreferences(
085                    long companyId, long ownerId, int ownerType,
086                    String defaultPreferences) {
087    
088                    return addPortalPreferences(ownerId, ownerType, defaultPreferences);
089            }
090    
091            @Override
092            public PortletPreferences getPreferences(long ownerId, int ownerType) {
093                    return getPreferences(ownerId, ownerType, null);
094            }
095    
096            @Override
097            public PortletPreferences getPreferences(
098                    long ownerId, int ownerType, String defaultPreferences) {
099    
100                    PortalPreferencesWrapper portalPreferencesWrapper =
101                            PortalPreferencesWrapperCacheUtil.get(ownerId, ownerType);
102    
103                    if (portalPreferencesWrapper != null) {
104                            return portalPreferencesWrapper.clone();
105                    }
106    
107                    PortalPreferences portalPreferences =
108                            portalPreferencesPersistence.fetchByO_O(ownerId, ownerType);
109    
110                    if (portalPreferences == null) {
111                            portalPreferences =
112                                    portalPreferencesLocalService.addPortalPreferences(
113                                            ownerId, ownerType, defaultPreferences);
114                    }
115    
116                    PortalPreferencesImpl portalPreferencesImpl = new PortalPreferencesImpl(
117                            portalPreferences, false);
118    
119                    portalPreferencesWrapper = new PortalPreferencesWrapper(
120                            portalPreferencesImpl);
121    
122                    PortalPreferencesWrapperCacheUtil.put(
123                            ownerId, ownerType, portalPreferencesWrapper);
124    
125                    return portalPreferencesWrapper.clone();
126            }
127    
128            /**
129             * @deprecated As of 6.2.0, replaced by {@link #getPreferences(long, int)}
130             */
131            @Deprecated
132            @Override
133            public PortletPreferences getPreferences(
134                    long companyId, long ownerId, int ownerType) {
135    
136                    return getPreferences(ownerId, ownerType);
137            }
138    
139            /**
140             * @deprecated As of 6.2.0, replaced by {@link #getPreferences(long, int,
141             *             String)}
142             */
143            @Deprecated
144            @Override
145            public PortletPreferences getPreferences(
146                    long companyId, long ownerId, int ownerType,
147                    String defaultPreferences) {
148    
149                    return getPreferences(ownerId, ownerType, defaultPreferences);
150            }
151    
152            @Override
153            public PortalPreferences updatePreferences(
154                    long ownerId, int ownerType,
155                    com.liferay.portlet.PortalPreferences portalPreferences) {
156    
157                    String xml = PortletPreferencesFactoryUtil.toXML(portalPreferences);
158    
159                    return updatePreferences(ownerId, ownerType, xml);
160            }
161    
162            @Override
163            public PortalPreferences updatePreferences(
164                    long ownerId, int ownerType, String xml) {
165    
166                    PortalPreferencesWrapperCacheUtil.remove(ownerId, ownerType);
167    
168                    PortalPreferences portalPreferences =
169                            portalPreferencesPersistence.fetchByO_O(ownerId, ownerType);
170    
171                    if (portalPreferences == null) {
172                            long portalPreferencesId = counterLocalService.increment();
173    
174                            portalPreferences = portalPreferencesPersistence.create(
175                                    portalPreferencesId);
176    
177                            portalPreferences.setOwnerId(ownerId);
178                            portalPreferences.setOwnerType(ownerType);
179                    }
180    
181                    portalPreferences.setPreferences(xml);
182    
183                    portalPreferencesPersistence.update(portalPreferences);
184    
185                    return portalPreferences;
186            }
187    
188            private static final Log _log = LogFactoryUtil.getLog(
189                    PortalPreferencesLocalServiceImpl.class);
190    
191    }