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.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.portlet.PortletPreferencesFactoryUtil;
034    import com.liferay.portlet.PortletPreferencesImpl;
035    
036    import java.util.List;
037    import java.util.concurrent.locks.Lock;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Shuyang Zhou
042     */
043    public class PortletPreferencesLocalServiceImpl
044            extends PortletPreferencesLocalServiceBaseImpl {
045    
046            @Override
047            public PortletPreferences addPortletPreferences(
048                    long companyId, long ownerId, int ownerType, long plid,
049                    String portletId, Portlet portlet, String defaultPreferences) {
050    
051                    long portletPreferencesId = counterLocalService.increment();
052    
053                    PortletPreferences portletPreferences =
054                            portletPreferencesPersistence.create(portletPreferencesId);
055    
056                    portletPreferences.setOwnerId(ownerId);
057                    portletPreferences.setOwnerType(ownerType);
058                    portletPreferences.setPlid(plid);
059                    portletPreferences.setPortletId(portletId);
060    
061                    if (Validator.isNull(defaultPreferences)) {
062                            if (portlet == null) {
063                                    defaultPreferences = PortletConstants.DEFAULT_PREFERENCES;
064                            }
065                            else {
066                                    defaultPreferences = portlet.getDefaultPreferences();
067                            }
068                    }
069    
070                    portletPreferences.setPreferences(defaultPreferences);
071    
072                    if (_log.isDebugEnabled()) {
073                            StringBundler sb = new StringBundler(13);
074    
075                            sb.append("Add {companyId=");
076                            sb.append(companyId);
077                            sb.append(", ownerId=");
078                            sb.append(ownerId);
079                            sb.append(", ownerType=");
080                            sb.append(ownerType);
081                            sb.append(", plid=");
082                            sb.append(plid);
083                            sb.append(", portletId=");
084                            sb.append(portletId);
085                            sb.append(", defaultPreferences=");
086                            sb.append(defaultPreferences);
087                            sb.append("}");
088    
089                            _log.debug(sb.toString());
090                    }
091    
092                    try {
093                            portletPreferencesPersistence.update(portletPreferences);
094                    }
095                    catch (SystemException se) {
096                            if (_log.isWarnEnabled()) {
097                                    _log.warn(
098                                            "Add failed, fetch {ownerId=" + ownerId + ", ownerType=" +
099                                                    ownerType + ", plid=" + plid + ", portletId=" +
100                                                            portletId + "}");
101                            }
102    
103                            portletPreferences = portletPreferencesPersistence.fetchByO_O_P_P(
104                                    ownerId, ownerType, plid, portletId, false);
105    
106                            if (portletPreferences == null) {
107                                    throw se;
108                            }
109                    }
110    
111                    return portletPreferences;
112            }
113    
114            @Override
115            public void deletePortletPreferences(
116                    long ownerId, int ownerType, long plid) {
117    
118                    portletPreferencesPersistence.removeByO_O_P(ownerId, ownerType, plid);
119            }
120    
121            @Override
122            public void deletePortletPreferences(
123                            long ownerId, int ownerType, long plid, String portletId)
124                    throws PortalException {
125    
126                    if (_log.isDebugEnabled()) {
127                            _log.debug(
128                                    "Delete {ownerId=" + ownerId + ", ownerType=" + ownerType +
129                                            ", plid=" + plid + ", portletId=" + portletId + "}");
130                    }
131    
132                    portletPreferencesPersistence.removeByO_O_P_P(
133                            ownerId, ownerType, plid, portletId);
134            }
135    
136            @Override
137            public void deletePortletPreferencesByPlid(long plid) {
138                    if (_log.isDebugEnabled()) {
139                            _log.debug("Delete {plid=" + plid + "}");
140                    }
141    
142                    portletPreferencesPersistence.removeByPlid(plid);
143            }
144    
145            @Override
146            public javax.portlet.PortletPreferences fetchPreferences(
147                    long companyId, long ownerId, int ownerType, long plid,
148                    String portletId) {
149    
150                    PortletPreferences portletPreferences =
151                            portletPreferencesPersistence.fetchByO_O_P_P(
152                                    ownerId, ownerType, plid, portletId);
153    
154                    if (portletPreferences == null) {
155                            return null;
156                    }
157    
158                    PortletPreferencesImpl portletPreferencesImpl =
159                            (PortletPreferencesImpl)PortletPreferencesFactoryUtil.fromXML(
160                                    companyId, ownerId, ownerType, plid, portletId,
161                                    portletPreferences.getPreferences());
162    
163                    return portletPreferencesImpl;
164            }
165    
166            @Override
167            public javax.portlet.PortletPreferences fetchPreferences(
168                    PortletPreferencesIds portletPreferencesIds) {
169    
170                    return fetchPreferences(
171                            portletPreferencesIds.getCompanyId(),
172                            portletPreferencesIds.getOwnerId(),
173                            portletPreferencesIds.getOwnerType(),
174                            portletPreferencesIds.getPlid(),
175                            portletPreferencesIds.getPortletId());
176            }
177    
178            @Override
179            @Skip
180            public javax.portlet.PortletPreferences getDefaultPreferences(
181                    long companyId, String portletId) {
182    
183                    Portlet portlet = portletLocalService.getPortletById(
184                            companyId, portletId);
185    
186                    return PortletPreferencesFactoryUtil.fromDefaultXML(
187                            portlet.getDefaultPreferences());
188            }
189    
190            @Override
191            public List<PortletPreferences> getPortletPreferences() {
192                    return portletPreferencesPersistence.findAll();
193            }
194    
195            @Override
196            public List<PortletPreferences> getPortletPreferences(
197                    int ownerType, long plid, String portletId) {
198    
199                    return portletPreferencesPersistence.findByO_P_P(
200                            ownerType, plid, portletId);
201            }
202    
203            @Override
204            public List<PortletPreferences> getPortletPreferences(
205                    long ownerId, int ownerType, long plid) {
206    
207                    return portletPreferencesPersistence.findByO_O_P(
208                            ownerId, ownerType, plid);
209            }
210    
211            @Override
212            public PortletPreferences getPortletPreferences(
213                            long ownerId, int ownerType, long plid, String portletId)
214                    throws PortalException {
215    
216                    return portletPreferencesPersistence.findByO_O_P_P(
217                            ownerId, ownerType, plid, portletId);
218            }
219    
220            @Override
221            public List<PortletPreferences> getPortletPreferences(
222                    long companyId, long groupId, long ownerId, int ownerType,
223                    String portletId, boolean privateLayout) {
224    
225                    return portletPreferencesFinder.findByC_G_O_O_P_P(
226                            companyId, groupId, ownerId, ownerType, portletId, privateLayout);
227            }
228    
229            @Override
230            public List<PortletPreferences> getPortletPreferences(
231                    long plid, String portletId) {
232    
233                    return portletPreferencesPersistence.findByP_P(plid, portletId);
234            }
235    
236            @Override
237            public List<PortletPreferences> getPortletPreferencesByPlid(long plid) {
238                    return portletPreferencesPersistence.findByPlid(plid);
239            }
240    
241            @Override
242            public long getPortletPreferencesCount(
243                    int ownerType, long plid, String portletId) {
244    
245                    return portletPreferencesPersistence.countByO_P_P(
246                            ownerType, plid, portletId);
247            }
248    
249            @Override
250            public long getPortletPreferencesCount(int ownerType, String portletId) {
251                    return portletPreferencesPersistence.countByO_P(ownerType, portletId);
252            }
253    
254            @Override
255            public long getPortletPreferencesCount(
256                    long ownerId, int ownerType, long plid, Portlet portlet,
257                    boolean excludeDefaultPreferences) {
258    
259                    String portletId = portlet.getPortletId();
260    
261                    if (plid == -1) {
262                            portletId = portlet.getRootPortletId();
263                    }
264    
265                    return portletPreferencesFinder.countByO_O_P_P_P(
266                            ownerId, ownerType, plid, portletId, excludeDefaultPreferences);
267            }
268    
269            @Override
270            public long getPortletPreferencesCount(
271                    long ownerId, int ownerType, String portletId,
272                    boolean excludeDefaultPreferences) {
273    
274                    return portletPreferencesFinder.countByO_O_P(
275                            ownerId, ownerType, portletId, excludeDefaultPreferences);
276            }
277    
278            @Override
279            public javax.portlet.PortletPreferences getPreferences(
280                    long companyId, long ownerId, int ownerType, long plid,
281                    String portletId) {
282    
283                    return getPreferences(
284                            companyId, ownerId, ownerType, plid, portletId, null);
285            }
286    
287            @Override
288            public javax.portlet.PortletPreferences getPreferences(
289                    long companyId, long ownerId, int ownerType, long plid,
290                    String portletId, String defaultPreferences) {
291    
292                    return getPreferences(
293                            companyId, ownerId, ownerType, plid, portletId, defaultPreferences,
294                            false);
295            }
296    
297            @Override
298            public javax.portlet.PortletPreferences getPreferences(
299                    PortletPreferencesIds portletPreferencesIds) {
300    
301                    return getPreferences(
302                            portletPreferencesIds.getCompanyId(),
303                            portletPreferencesIds.getOwnerId(),
304                            portletPreferencesIds.getOwnerType(),
305                            portletPreferencesIds.getPlid(),
306                            portletPreferencesIds.getPortletId());
307            }
308    
309            @Override
310            public javax.portlet.PortletPreferences getStrictPreferences(
311                    long companyId, long ownerId, int ownerType, long plid,
312                    String portletId) {
313    
314                    return getPreferences(
315                            companyId, ownerId, ownerType, plid, portletId, null, true);
316            }
317    
318            @Override
319            public javax.portlet.PortletPreferences getStrictPreferences(
320                    PortletPreferencesIds portletPreferencesIds) {
321    
322                    return getStrictPreferences(
323                            portletPreferencesIds.getCompanyId(),
324                            portletPreferencesIds.getOwnerId(),
325                            portletPreferencesIds.getOwnerType(),
326                            portletPreferencesIds.getPlid(),
327                            portletPreferencesIds.getPortletId());
328            }
329    
330            @Override
331            public PortletPreferences updatePreferences(
332                    long ownerId, int ownerType, long plid, String portletId,
333                    javax.portlet.PortletPreferences portletPreferences) {
334    
335                    String xml = PortletPreferencesFactoryUtil.toXML(portletPreferences);
336    
337                    return updatePreferences(ownerId, ownerType, plid, portletId, xml);
338            }
339    
340            @Override
341            public PortletPreferences updatePreferences(
342                    long ownerId, int ownerType, long plid, String portletId, String xml) {
343    
344                    if (_log.isDebugEnabled()) {
345                            _log.debug(
346                                    "Update {ownerId=" + ownerId + ", ownerType=" + ownerType +
347                                            ", plid=" + plid + ", portletId=" + portletId + ", xml=" +
348                                                    xml + "}");
349                    }
350    
351                    PortletPreferences portletPreferences =
352                            portletPreferencesPersistence.fetchByO_O_P_P(
353                                    ownerId, ownerType, plid, portletId);
354    
355                    if (portletPreferences == null) {
356                            long portletPreferencesId = counterLocalService.increment();
357    
358                            portletPreferences = portletPreferencesPersistence.create(
359                                    portletPreferencesId);
360    
361                            portletPreferences.setOwnerId(ownerId);
362                            portletPreferences.setOwnerType(ownerType);
363                            portletPreferences.setPlid(plid);
364                            portletPreferences.setPortletId(portletId);
365                    }
366    
367                    portletPreferences.setPreferences(xml);
368    
369                    portletPreferencesPersistence.update(portletPreferences);
370    
371                    return portletPreferences;
372            }
373    
374            protected javax.portlet.PortletPreferences doGetPreferences(
375                    long companyId, long ownerId, int ownerType, long plid,
376                    String portletId, String defaultPreferences, boolean strict) {
377    
378                    PortletPreferences portletPreferences =
379                            portletPreferencesPersistence.fetchByO_O_P_P(
380                                    ownerId, ownerType, plid, portletId);
381    
382                    if (portletPreferences == null) {
383                            Portlet portlet = portletLocalService.getPortletById(
384                                    companyId, portletId);
385    
386                            if (strict &&
387                                    (Validator.isNull(defaultPreferences) ||
388                                     ((portlet != null) && portlet.isUndeployedPortlet()))) {
389    
390                                    if (portlet == null) {
391                                            defaultPreferences = PortletConstants.DEFAULT_PREFERENCES;
392                                    }
393                                    else {
394                                            defaultPreferences = portlet.getDefaultPreferences();
395                                    }
396    
397                                    return PortletPreferencesFactoryUtil.strictFromXML(
398                                            companyId, ownerId, ownerType, plid, portletId,
399                                            defaultPreferences);
400                            }
401    
402                            portletPreferences =
403                                    portletPreferencesLocalService.addPortletPreferences(
404                                            companyId, ownerId, ownerType, plid, portletId, portlet,
405                                            defaultPreferences);
406                    }
407    
408                    PortletPreferencesImpl portletPreferencesImpl =
409                            (PortletPreferencesImpl)PortletPreferencesFactoryUtil.fromXML(
410                                    companyId, ownerId, ownerType, plid, portletId,
411                                    portletPreferences.getPreferences());
412    
413                    return portletPreferencesImpl;
414            }
415    
416            protected javax.portlet.PortletPreferences getPreferences(
417                    long companyId, long ownerId, int ownerType, long plid,
418                    String portletId, String defaultPreferences, boolean strict) {
419    
420                    DB db = DBFactoryUtil.getDB();
421    
422                    String dbType = db.getType();
423    
424                    if (!dbType.equals(DB.TYPE_HYPERSONIC)) {
425                            return doGetPreferences(
426                                    companyId, ownerId, ownerType, plid, portletId,
427                                    defaultPreferences, strict);
428                    }
429    
430                    StringBundler sb = new StringBundler(7);
431    
432                    sb.append(ownerId);
433                    sb.append(StringPool.POUND);
434                    sb.append(ownerType);
435                    sb.append(StringPool.POUND);
436                    sb.append(plid);
437                    sb.append(StringPool.POUND);
438                    sb.append(portletId);
439    
440                    String groupName = getClass().getName();
441                    String key = sb.toString();
442    
443                    Lock lock = LockRegistry.allocateLock(groupName, key);
444    
445                    lock.lock();
446    
447                    try {
448                            return doGetPreferences(
449                                    companyId, ownerId, ownerType, plid, portletId,
450                                    defaultPreferences, strict);
451                    }
452                    finally {
453                            lock.unlock();
454    
455                            LockRegistry.freeLock(groupName, key);
456                    }
457            }
458    
459            private static final Log _log = LogFactoryUtil.getLog(
460                    PortletPreferencesLocalServiceImpl.class);
461    
462    }