001    /**
002     * Copyright (c) 2000-2012 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.PortletPreferencesFactoryUtil;
032    import com.liferay.portlet.PortletPreferencesThreadLocal;
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            public PortalPreferences addPortalPreferences(
045                            long companyId, long ownerId, int ownerType,
046                            String defaultPreferences)
047                    throws SystemException {
048    
049                    long portalPreferencesId = counterLocalService.increment();
050    
051                    PortalPreferences portalPreferences =
052                            portalPreferencesPersistence.create(portalPreferencesId);
053    
054                    portalPreferences.setOwnerId(ownerId);
055                    portalPreferences.setOwnerType(ownerType);
056    
057                    if (Validator.isNull(defaultPreferences)) {
058                            defaultPreferences = PortletConstants.DEFAULT_PREFERENCES;
059                    }
060    
061                    portalPreferences.setPreferences(defaultPreferences);
062    
063                    try {
064                            portalPreferencesPersistence.update(portalPreferences);
065                    }
066                    catch (SystemException se) {
067                            if (_log.isWarnEnabled()) {
068                                    _log.warn(
069                                            "Add failed, fetch {ownerId=" + ownerId + ", ownerType=" +
070                                                    ownerType + "}");
071                            }
072    
073                            portalPreferences = portalPreferencesPersistence.fetchByO_O(
074                                    ownerId, ownerType, false);
075    
076                            if (portalPreferences == null) {
077                                    throw se;
078                            }
079                    }
080    
081                    return portalPreferences;
082            }
083    
084            public PortletPreferences getPreferences(
085                            long companyId, long ownerId, int ownerType)
086                    throws SystemException {
087    
088                    return getPreferences(companyId, ownerId, ownerType, null);
089            }
090    
091            public PortletPreferences getPreferences(
092                            long companyId, long ownerId, int ownerType,
093                            String defaultPreferences)
094                    throws SystemException {
095    
096                    DB db = DBFactoryUtil.getDB();
097    
098                    String dbType = db.getType();
099    
100                    if (!dbType.equals(DB.TYPE_HYPERSONIC)) {
101                            return doGetPreferences(
102                                    companyId, ownerId, ownerType, defaultPreferences);
103                    }
104    
105                    StringBundler sb = new StringBundler(4);
106    
107                    sb.append(ownerId);
108                    sb.append(StringPool.POUND);
109                    sb.append(ownerType);
110                    sb.append(StringPool.POUND);
111    
112                    String groupName = getClass().getName();
113                    String key = sb.toString();
114    
115                    Lock lock = LockRegistry.allocateLock(groupName, key);
116    
117                    lock.lock();
118    
119                    try {
120                            return doGetPreferences(
121                                    companyId, ownerId, ownerType, defaultPreferences);
122                    }
123                    finally {
124                            lock.unlock();
125    
126                            LockRegistry.freeLock(groupName, key);
127                    }
128            }
129    
130            public PortalPreferences updatePreferences(
131                            long ownerId, int ownerType,
132                            com.liferay.portlet.PortalPreferences portalPreferences)
133                    throws SystemException {
134    
135                    String xml = PortletPreferencesFactoryUtil.toXML(portalPreferences);
136    
137                    return updatePreferences(ownerId, ownerType, xml);
138            }
139    
140            public PortalPreferences updatePreferences(
141                            long ownerId, int ownerType, String xml)
142                    throws SystemException {
143    
144                    PortalPreferences portalPreferences =
145                            portalPreferencesPersistence.fetchByO_O(ownerId, ownerType);
146    
147                    if (portalPreferences == null) {
148                            long portalPreferencesId = counterLocalService.increment();
149    
150                            portalPreferences = portalPreferencesPersistence.create(
151                                    portalPreferencesId);
152    
153                            portalPreferences.setOwnerId(ownerId);
154                            portalPreferences.setOwnerType(ownerType);
155                    }
156    
157                    portalPreferences.setPreferences(xml);
158    
159                    portalPreferencesPersistence.update(portalPreferences);
160    
161                    return portalPreferences;
162            }
163    
164            protected PortletPreferences doGetPreferences(
165                            long companyId, long ownerId, int ownerType,
166                            String defaultPreferences)
167                    throws SystemException {
168    
169                    PortalPreferences portalPreferences =
170                            portalPreferencesPersistence.fetchByO_O(ownerId, ownerType);
171    
172                    if (portalPreferences == null) {
173                            if (PortletPreferencesThreadLocal.isStrict() &&
174                                    Validator.isNull(defaultPreferences)) {
175    
176                                    return new PortalPreferencesWrapper(
177                                            new PortalPreferencesImpl());
178                            }
179    
180                            portalPreferences =
181                                    portalPreferencesLocalService.addPortalPreferences(
182                                            companyId, ownerId, ownerType, defaultPreferences);
183                    }
184    
185                    PortalPreferencesImpl portalPreferencesImpl =
186                            (PortalPreferencesImpl)PortletPreferencesFactoryUtil.fromXML(
187                                    companyId, ownerId, ownerType,
188                                    portalPreferences.getPreferences());
189    
190                    return new PortalPreferencesWrapper(portalPreferencesImpl);
191            }
192    
193            private static Log _log = LogFactoryUtil.getLog(
194                    PortalPreferencesLocalServiceImpl.class);
195    
196    }