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