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.concurrent.LockRegistry;
018    import com.liferay.portal.kernel.dao.db.DB;
019    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.model.PortalPreferences;
027    import com.liferay.portal.model.PortletConstants;
028    import com.liferay.portal.service.base.PortalPreferencesLocalServiceBaseImpl;
029    import com.liferay.portlet.PortalPreferencesImpl;
030    import com.liferay.portlet.PortalPreferencesWrapper;
031    import com.liferay.portlet.PortalPreferencesWrapperCacheUtil;
032    import com.liferay.portlet.PortletPreferencesFactoryUtil;
033    
034    import java.util.concurrent.locks.Lock;
035    
036    import javax.portlet.PortletPreferences;
037    
038    /**
039     * @author Alexander Chow
040     */
041    public class PortalPreferencesLocalServiceImpl
042            extends PortalPreferencesLocalServiceBaseImpl {
043    
044            @Override
045            public PortalPreferences addPortalPreferences(
046                    long ownerId, int ownerType, String defaultPreferences) {
047    
048                    PortalPreferencesWrapperCacheUtil.remove(ownerId, ownerType);
049    
050                    long portalPreferencesId = counterLocalService.increment();
051    
052                    PortalPreferences portalPreferences =
053                            portalPreferencesPersistence.create(portalPreferencesId);
054    
055                    portalPreferences.setOwnerId(ownerId);
056                    portalPreferences.setOwnerType(ownerType);
057    
058                    if (Validator.isNull(defaultPreferences)) {
059                            defaultPreferences = PortletConstants.DEFAULT_PREFERENCES;
060                    }
061    
062                    portalPreferences.setPreferences(defaultPreferences);
063    
064                    try {
065                            portalPreferencesPersistence.update(portalPreferences);
066                    }
067                    catch (SystemException se) {
068                            if (_log.isWarnEnabled()) {
069                                    _log.warn(
070                                            "Add failed, fetch {ownerId=" + ownerId + ", ownerType=" +
071                                                    ownerType + "}");
072                            }
073    
074                            portalPreferences = portalPreferencesPersistence.fetchByO_O(
075                                    ownerId, ownerType, false);
076    
077                            if (portalPreferences == null) {
078                                    throw se;
079                            }
080                    }
081    
082                    return portalPreferences;
083            }
084    
085            /**
086             * @deprecated As of 6.2.0, replaced by {@link #addPortalPreferences(long,
087             *             int, String)}
088             */
089            @Deprecated
090            @Override
091            public PortalPreferences addPortalPreferences(
092                    long companyId, long ownerId, int ownerType,
093                    String defaultPreferences) {
094    
095                    return addPortalPreferences(ownerId, ownerType, defaultPreferences);
096            }
097    
098            @Override
099            public PortletPreferences getPreferences(long ownerId, int ownerType) {
100                    return getPreferences(ownerId, ownerType, null);
101            }
102    
103            @Override
104            public PortletPreferences getPreferences(
105                    long ownerId, int ownerType, String defaultPreferences) {
106    
107                    DB db = DBFactoryUtil.getDB();
108    
109                    String dbType = db.getType();
110    
111                    if (!dbType.equals(DB.TYPE_HYPERSONIC)) {
112                            return doGetPreferences(ownerId, ownerType, defaultPreferences);
113                    }
114    
115                    StringBundler sb = new StringBundler(4);
116    
117                    sb.append(ownerId);
118                    sb.append(StringPool.POUND);
119                    sb.append(ownerType);
120                    sb.append(StringPool.POUND);
121    
122                    String groupName = getClass().getName();
123                    String key = sb.toString();
124    
125                    Lock lock = LockRegistry.allocateLock(groupName, key);
126    
127                    lock.lock();
128    
129                    try {
130                            return doGetPreferences(ownerId, ownerType, defaultPreferences);
131                    }
132                    finally {
133                            lock.unlock();
134    
135                            LockRegistry.freeLock(groupName, key);
136                    }
137            }
138    
139            /**
140             * @deprecated As of 6.2.0, replaced by {@link #getPreferences(long, int)}
141             */
142            @Deprecated
143            @Override
144            public PortletPreferences getPreferences(
145                    long companyId, long ownerId, int ownerType) {
146    
147                    return getPreferences(ownerId, ownerType);
148            }
149    
150            /**
151             * @deprecated As of 6.2.0, replaced by {@link #getPreferences(long, int,
152             *             String)}
153             */
154            @Deprecated
155            @Override
156            public PortletPreferences getPreferences(
157                    long companyId, long ownerId, int ownerType,
158                    String defaultPreferences) {
159    
160                    return getPreferences(ownerId, ownerType, defaultPreferences);
161            }
162    
163            @Override
164            public PortalPreferences updatePreferences(
165                    long ownerId, int ownerType,
166                    com.liferay.portlet.PortalPreferences portalPreferences) {
167    
168                    String xml = PortletPreferencesFactoryUtil.toXML(portalPreferences);
169    
170                    return updatePreferences(ownerId, ownerType, xml);
171            }
172    
173            @Override
174            public PortalPreferences updatePreferences(
175                    long ownerId, int ownerType, String xml) {
176    
177                    PortalPreferencesWrapperCacheUtil.remove(ownerId, ownerType);
178    
179                    PortalPreferences portalPreferences =
180                            portalPreferencesPersistence.fetchByO_O(ownerId, ownerType);
181    
182                    if (portalPreferences == null) {
183                            long portalPreferencesId = counterLocalService.increment();
184    
185                            portalPreferences = portalPreferencesPersistence.create(
186                                    portalPreferencesId);
187    
188                            portalPreferences.setOwnerId(ownerId);
189                            portalPreferences.setOwnerType(ownerType);
190                    }
191    
192                    portalPreferences.setPreferences(xml);
193    
194                    portalPreferencesPersistence.update(portalPreferences);
195    
196                    return portalPreferences;
197            }
198    
199            protected PortletPreferences doGetPreferences(
200                    long ownerId, int ownerType, String defaultPreferences) {
201    
202                    PortalPreferencesWrapper portalPreferencesWrapper =
203                            PortalPreferencesWrapperCacheUtil.get(ownerId, ownerType);
204    
205                    if (portalPreferencesWrapper != null) {
206                            return portalPreferencesWrapper.clone();
207                    }
208    
209                    PortalPreferences portalPreferences =
210                            portalPreferencesPersistence.fetchByO_O(ownerId, ownerType);
211    
212                    if (portalPreferences == null) {
213                            portalPreferences =
214                                    portalPreferencesLocalService.addPortalPreferences(
215                                            ownerId, ownerType, defaultPreferences);
216                    }
217    
218                    PortalPreferencesImpl portalPreferencesImpl =
219                            (PortalPreferencesImpl)PortletPreferencesFactoryUtil.fromXML(
220                                    ownerId, ownerType, portalPreferences.getPreferences());
221    
222                    portalPreferencesWrapper = new PortalPreferencesWrapper(
223                            portalPreferencesImpl);
224    
225                    PortalPreferencesWrapperCacheUtil.put(
226                            ownerId, ownerType, portalPreferencesWrapper);
227    
228                    return portalPreferencesWrapper.clone();
229            }
230    
231            private static final Log _log = LogFactoryUtil.getLog(
232                    PortalPreferencesLocalServiceImpl.class);
233    
234    }