001    /**
002     * Copyright (c) 2000-2013 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.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.spring.aop.Skip;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.model.Portlet;
029    import com.liferay.portal.model.PortletConstants;
030    import com.liferay.portal.model.PortletPreferences;
031    import com.liferay.portal.model.PortletPreferencesIds;
032    import com.liferay.portal.service.base.PortletPreferencesLocalServiceBaseImpl;
033    import com.liferay.portal.util.PropsValues;
034    import com.liferay.portlet.PortletPreferencesFactoryUtil;
035    import com.liferay.portlet.PortletPreferencesImpl;
036    import com.liferay.portlet.PortletPreferencesThreadLocal;
037    
038    import java.util.List;
039    import java.util.concurrent.locks.Lock;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @author Shuyang Zhou
044     */
045    public class PortletPreferencesLocalServiceImpl
046            extends PortletPreferencesLocalServiceBaseImpl {
047    
048            @Override
049            public PortletPreferences addPortletPreferences(
050                            long companyId, long ownerId, int ownerType, long plid,
051                            String portletId, Portlet portlet, String defaultPreferences)
052                    throws SystemException {
053    
054                    long portletPreferencesId = counterLocalService.increment();
055    
056                    PortletPreferences portletPreferences =
057                            portletPreferencesPersistence.create(portletPreferencesId);
058    
059                    portletPreferences.setOwnerId(ownerId);
060                    portletPreferences.setOwnerType(ownerType);
061                    portletPreferences.setPlid(plid);
062                    portletPreferences.setPortletId(portletId);
063    
064                    if (Validator.isNull(defaultPreferences)) {
065                            if (portlet == null) {
066                                    defaultPreferences = PortletConstants.DEFAULT_PREFERENCES;
067                            }
068                            else {
069                                    defaultPreferences = portlet.getDefaultPreferences();
070                            }
071                    }
072    
073                    portletPreferences.setPreferences(defaultPreferences);
074    
075                    try {
076                            portletPreferencesPersistence.update(portletPreferences, false);
077                    }
078                    catch (SystemException se) {
079                            if (_log.isWarnEnabled()) {
080                                    _log.warn(
081                                            "Add failed, fetch {ownerId=" + ownerId + ", ownerType=" +
082                                                    ownerType + ", plid=" + plid + ", portletId=" +
083                                                            portletId + "}");
084                            }
085    
086                            portletPreferences = portletPreferencesPersistence.fetchByO_O_P_P(
087                                    ownerId, ownerType, plid, portletId, false);
088    
089                            if (portletPreferences == null) {
090                                    throw se;
091                            }
092                    }
093    
094                    return portletPreferences;
095            }
096    
097            @Override
098            public void deletePortletPreferences(long ownerId, int ownerType, long plid)
099                    throws SystemException {
100    
101                    portletPreferencesPersistence.removeByO_O_P(ownerId, ownerType, plid);
102            }
103    
104            @Override
105            public void deletePortletPreferences(
106                            long ownerId, int ownerType, long plid, String portletId)
107                    throws PortalException, SystemException {
108    
109                    portletPreferencesPersistence.removeByO_O_P_P(
110                            ownerId, ownerType, plid, portletId);
111            }
112    
113            @Override
114            @Skip
115            public javax.portlet.PortletPreferences getDefaultPreferences(
116                            long companyId, String portletId)
117                    throws SystemException {
118    
119                    Portlet portlet = portletLocalService.getPortletById(
120                            companyId, portletId);
121    
122                    return PortletPreferencesFactoryUtil.fromDefaultXML(
123                            portlet.getDefaultPreferences());
124            }
125    
126            @Override
127            public List<PortletPreferences> getPortletPreferences()
128                    throws SystemException {
129    
130                    return portletPreferencesPersistence.findAll();
131            }
132    
133            @Override
134            public List<PortletPreferences> getPortletPreferences(
135                            int ownerType, long plid, String portletId)
136                    throws SystemException {
137    
138                    return portletPreferencesPersistence.findByO_P_P(
139                            ownerType, plid, portletId);
140            }
141    
142            @Override
143            public List<PortletPreferences> getPortletPreferences(
144                            long ownerId, int ownerType, long plid)
145                    throws SystemException {
146    
147                    return portletPreferencesPersistence.findByO_O_P(
148                            ownerId, ownerType, plid);
149            }
150    
151            @Override
152            public PortletPreferences getPortletPreferences(
153                            long ownerId, int ownerType, long plid, String portletId)
154                    throws PortalException, SystemException {
155    
156                    return portletPreferencesPersistence.findByO_O_P_P(
157                            ownerId, ownerType, plid, portletId);
158            }
159    
160            @Override
161            public List<PortletPreferences> getPortletPreferences(
162                            long companyId, long groupId, long ownerId, int ownerType,
163                            String portletId, boolean privateLayout)
164                    throws SystemException {
165    
166                    return portletPreferencesFinder.findByC_G_O_O_P_P(
167                            companyId, groupId, ownerId, ownerType, portletId, privateLayout);
168            }
169    
170            @Override
171            public List<PortletPreferences> getPortletPreferences(
172                            long plid, String portletId)
173                    throws SystemException {
174    
175                    return portletPreferencesPersistence.findByP_P(plid, portletId);
176            }
177    
178            @Override
179            public List<PortletPreferences> getPortletPreferencesByPlid(long plid)
180                    throws SystemException {
181    
182                    return portletPreferencesPersistence.findByPlid(plid);
183            }
184    
185            @Override
186            public javax.portlet.PortletPreferences getPreferences(
187                            long companyId, long ownerId, int ownerType, long plid,
188                            String portletId)
189                    throws SystemException {
190    
191                    return getPreferences(
192                            companyId, ownerId, ownerType, plid, portletId, null);
193            }
194    
195            @Override
196            public javax.portlet.PortletPreferences getPreferences(
197                            long companyId, long ownerId, int ownerType, long plid,
198                            String portletId, String defaultPreferences)
199                    throws SystemException {
200    
201                    DB db = DBFactoryUtil.getDB();
202    
203                    String dbType = db.getType();
204    
205                    if (!dbType.equals(DB.TYPE_HYPERSONIC)) {
206                            return doGetPreferences(
207                                    companyId, ownerId, ownerType, plid, portletId,
208                                    defaultPreferences);
209                    }
210    
211                    StringBundler sb = new StringBundler(7);
212    
213                    sb.append(ownerId);
214                    sb.append(StringPool.POUND);
215                    sb.append(ownerType);
216                    sb.append(StringPool.POUND);
217                    sb.append(plid);
218                    sb.append(StringPool.POUND);
219                    sb.append(portletId);
220    
221                    String groupName = getClass().getName();
222                    String key = sb.toString();
223    
224                    Lock lock = LockRegistry.allocateLock(groupName, key);
225    
226                    lock.lock();
227    
228                    try {
229                            return doGetPreferences(
230                                    companyId, ownerId, ownerType, plid, portletId,
231                                    defaultPreferences);
232                    }
233                    finally {
234                            lock.unlock();
235    
236                            LockRegistry.freeLock(groupName, key);
237                    }
238            }
239    
240            @Override
241            public javax.portlet.PortletPreferences getPreferences(
242                            PortletPreferencesIds portletPreferencesIds)
243                    throws SystemException {
244    
245                    return getPreferences(
246                            portletPreferencesIds.getCompanyId(),
247                            portletPreferencesIds.getOwnerId(),
248                            portletPreferencesIds.getOwnerType(),
249                            portletPreferencesIds.getPlid(),
250                            portletPreferencesIds.getPortletId());
251            }
252    
253            @Override
254            public javax.portlet.PortletPreferences getStrictPreferences(
255                            long companyId, long ownerId, int ownerType, long plid,
256                            String portletId)
257                    throws SystemException {
258    
259                    boolean strict = PortletPreferencesThreadLocal.isStrict();
260    
261                    PortletPreferencesThreadLocal.setStrict(!PropsValues.TCK_URL);
262    
263                    try {
264                            return getPreferences(
265                                    companyId, ownerId, ownerType, plid, portletId, null);
266                    }
267                    finally {
268                            PortletPreferencesThreadLocal.setStrict(strict);
269                    }
270            }
271    
272            @Override
273            public javax.portlet.PortletPreferences getStrictPreferences(
274                            PortletPreferencesIds portletPreferencesIds)
275                    throws SystemException {
276    
277                    return getStrictPreferences(
278                            portletPreferencesIds.getCompanyId(),
279                            portletPreferencesIds.getOwnerId(),
280                            portletPreferencesIds.getOwnerType(),
281                            portletPreferencesIds.getPlid(),
282                            portletPreferencesIds.getPortletId());
283            }
284    
285            @Override
286            public PortletPreferences updatePreferences(
287                            long ownerId, int ownerType, long plid, String portletId,
288                            javax.portlet.PortletPreferences portletPreferences)
289                    throws SystemException {
290    
291                    String xml = PortletPreferencesFactoryUtil.toXML(portletPreferences);
292    
293                    return updatePreferences(ownerId, ownerType, plid, portletId, xml);
294            }
295    
296            @Override
297            public PortletPreferences updatePreferences(
298                            long ownerId, int ownerType, long plid, String portletId,
299                            String xml)
300                    throws SystemException {
301    
302                    PortletPreferences portletPreferences =
303                            portletPreferencesPersistence.fetchByO_O_P_P(
304                                    ownerId, ownerType, plid, portletId);
305    
306                    if (portletPreferences == null) {
307                            long portletPreferencesId = counterLocalService.increment();
308    
309                            portletPreferences = portletPreferencesPersistence.create(
310                                    portletPreferencesId);
311    
312                            portletPreferences.setOwnerId(ownerId);
313                            portletPreferences.setOwnerType(ownerType);
314                            portletPreferences.setPlid(plid);
315                            portletPreferences.setPortletId(portletId);
316                    }
317    
318                    portletPreferences.setPreferences(xml);
319    
320                    portletPreferencesPersistence.update(portletPreferences, false);
321    
322                    return portletPreferences;
323            }
324    
325            protected javax.portlet.PortletPreferences doGetPreferences(
326                            long companyId, long ownerId, int ownerType, long plid,
327                            String portletId, String defaultPreferences)
328                    throws SystemException {
329    
330                    PortletPreferences portletPreferences =
331                            portletPreferencesPersistence.fetchByO_O_P_P(
332                                    ownerId, ownerType, plid, portletId);
333    
334                    if (portletPreferences == null) {
335                            Portlet portlet = portletLocalService.getPortletById(
336                                    companyId, portletId);
337    
338                            if (PortletPreferencesThreadLocal.isStrict() &&
339                                    (Validator.isNull(defaultPreferences) ||
340                                     ((portlet != null) && portlet.isUndeployedPortlet()))) {
341    
342                                    return new PortletPreferencesImpl();
343                            }
344    
345                            portletPreferences =
346                                    portletPreferencesLocalService.addPortletPreferences(
347                                            companyId, ownerId, ownerType, plid, portletId, portlet,
348                                            defaultPreferences);
349                    }
350    
351                    PortletPreferencesImpl portletPreferencesImpl =
352                            (PortletPreferencesImpl)PortletPreferencesFactoryUtil.fromXML(
353                                    companyId, ownerId, ownerType, plid, portletId,
354                                    portletPreferences.getPreferences());
355    
356                    return portletPreferencesImpl;
357            }
358    
359            private static Log _log = LogFactoryUtil.getLog(
360                    PortletPreferencesLocalServiceImpl.class);
361    
362    }