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