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.StrictPortletPreferencesImpl;
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);
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            public void deletePortletPreferencesByPlid(long plid)
115                    throws SystemException {
116    
117                    portletPreferencesPersistence.removeByPlid(plid);
118            }
119    
120            @Override
121            public javax.portlet.PortletPreferences fetchPreferences(
122                            long companyId, long ownerId, int ownerType, long plid,
123                            String portletId)
124                    throws SystemException {
125    
126                    PortletPreferences portletPreferences =
127                            portletPreferencesPersistence.fetchByO_O_P_P(
128                                    ownerId, ownerType, plid, portletId);
129    
130                    if (portletPreferences == null) {
131                            return null;
132                    }
133    
134                    PortletPreferencesImpl portletPreferencesImpl =
135                            (PortletPreferencesImpl)PortletPreferencesFactoryUtil.fromXML(
136                                    companyId, ownerId, ownerType, plid, portletId,
137                                    portletPreferences.getPreferences());
138    
139                    return portletPreferencesImpl;
140            }
141    
142            @Override
143            public javax.portlet.PortletPreferences fetchPreferences(
144                            PortletPreferencesIds portletPreferencesIds)
145                    throws SystemException {
146    
147                    return fetchPreferences(
148                            portletPreferencesIds.getCompanyId(),
149                            portletPreferencesIds.getOwnerId(),
150                            portletPreferencesIds.getOwnerType(),
151                            portletPreferencesIds.getPlid(),
152                            portletPreferencesIds.getPortletId());
153            }
154    
155            @Override
156            @Skip
157            public javax.portlet.PortletPreferences getDefaultPreferences(
158                            long companyId, String portletId)
159                    throws SystemException {
160    
161                    Portlet portlet = portletLocalService.getPortletById(
162                            companyId, portletId);
163    
164                    return PortletPreferencesFactoryUtil.fromDefaultXML(
165                            portlet.getDefaultPreferences());
166            }
167    
168            @Override
169            public List<PortletPreferences> getPortletPreferences()
170                    throws SystemException {
171    
172                    return portletPreferencesPersistence.findAll();
173            }
174    
175            @Override
176            public List<PortletPreferences> getPortletPreferences(
177                            int ownerType, long plid, String portletId)
178                    throws SystemException {
179    
180                    return portletPreferencesPersistence.findByO_P_P(
181                            ownerType, plid, portletId);
182            }
183    
184            @Override
185            public List<PortletPreferences> getPortletPreferences(
186                            long ownerId, int ownerType, long plid)
187                    throws SystemException {
188    
189                    return portletPreferencesPersistence.findByO_O_P(
190                            ownerId, ownerType, plid);
191            }
192    
193            @Override
194            public PortletPreferences getPortletPreferences(
195                            long ownerId, int ownerType, long plid, String portletId)
196                    throws PortalException, SystemException {
197    
198                    return portletPreferencesPersistence.findByO_O_P_P(
199                            ownerId, ownerType, plid, portletId);
200            }
201    
202            @Override
203            public List<PortletPreferences> getPortletPreferences(
204                            long companyId, long groupId, long ownerId, int ownerType,
205                            String portletId, boolean privateLayout)
206                    throws SystemException {
207    
208                    return portletPreferencesFinder.findByC_G_O_O_P_P(
209                            companyId, groupId, ownerId, ownerType, portletId, privateLayout);
210            }
211    
212            @Override
213            public List<PortletPreferences> getPortletPreferences(
214                            long plid, String portletId)
215                    throws SystemException {
216    
217                    return portletPreferencesPersistence.findByP_P(plid, portletId);
218            }
219    
220            @Override
221            public List<PortletPreferences> getPortletPreferencesByPlid(long plid)
222                    throws SystemException {
223    
224                    return portletPreferencesPersistence.findByPlid(plid);
225            }
226    
227            @Override
228            public long getPortletPreferencesCount(
229                            int ownerType, long plid, String portletId)
230                    throws SystemException {
231    
232                    return portletPreferencesPersistence.countByO_P_P(
233                            ownerType, plid, portletId);
234            }
235    
236            @Override
237            public long getPortletPreferencesCount(int ownerType, String portletId)
238                    throws SystemException {
239    
240                    return portletPreferencesPersistence.countByO_P(ownerType, portletId);
241            }
242    
243            @Override
244            public long getPortletPreferencesCount(
245                            long ownerId, int ownerType, long plid, Portlet portlet,
246                            boolean excludeDefaultPreferences)
247                    throws SystemException {
248    
249                    String portletId = portlet.getPortletId();
250    
251                    if (plid == -1) {
252                            portletId = portlet.getRootPortletId();
253                    }
254    
255                    return portletPreferencesFinder.countByO_O_P_P_P(
256                            ownerId, ownerType, plid, portletId, excludeDefaultPreferences);
257            }
258    
259            @Override
260            public long getPortletPreferencesCount(
261                            long ownerId, int ownerType, String portletId,
262                            boolean excludeDefaultPreferences)
263                    throws SystemException {
264    
265                    return portletPreferencesFinder.countByO_O_P(
266                            ownerId, ownerType, portletId, excludeDefaultPreferences);
267            }
268    
269            @Override
270            public javax.portlet.PortletPreferences getPreferences(
271                            long companyId, long ownerId, int ownerType, long plid,
272                            String portletId)
273                    throws SystemException {
274    
275                    return getPreferences(
276                            companyId, ownerId, ownerType, plid, portletId, null);
277            }
278    
279            @Override
280            public javax.portlet.PortletPreferences getPreferences(
281                            long companyId, long ownerId, int ownerType, long plid,
282                            String portletId, String defaultPreferences)
283                    throws SystemException {
284    
285                    return getPreferences(
286                            companyId, ownerId, ownerType, plid, portletId, defaultPreferences,
287                            false);
288            }
289    
290            @Override
291            public javax.portlet.PortletPreferences getPreferences(
292                            PortletPreferencesIds portletPreferencesIds)
293                    throws SystemException {
294    
295                    return getPreferences(
296                            portletPreferencesIds.getCompanyId(),
297                            portletPreferencesIds.getOwnerId(),
298                            portletPreferencesIds.getOwnerType(),
299                            portletPreferencesIds.getPlid(),
300                            portletPreferencesIds.getPortletId());
301            }
302    
303            @Override
304            public javax.portlet.PortletPreferences getStrictPreferences(
305                            long companyId, long ownerId, int ownerType, long plid,
306                            String portletId)
307                    throws SystemException {
308    
309                    return getPreferences(
310                            companyId, ownerId, ownerType, plid, portletId, null,
311                            !PropsValues.TCK_URL);
312            }
313    
314            @Override
315            public javax.portlet.PortletPreferences getStrictPreferences(
316                            PortletPreferencesIds portletPreferencesIds)
317                    throws SystemException {
318    
319                    return getStrictPreferences(
320                            portletPreferencesIds.getCompanyId(),
321                            portletPreferencesIds.getOwnerId(),
322                            portletPreferencesIds.getOwnerType(),
323                            portletPreferencesIds.getPlid(),
324                            portletPreferencesIds.getPortletId());
325            }
326    
327            @Override
328            public PortletPreferences updatePreferences(
329                            long ownerId, int ownerType, long plid, String portletId,
330                            javax.portlet.PortletPreferences portletPreferences)
331                    throws SystemException {
332    
333                    String xml = PortletPreferencesFactoryUtil.toXML(portletPreferences);
334    
335                    return updatePreferences(ownerId, ownerType, plid, portletId, xml);
336            }
337    
338            @Override
339            public PortletPreferences updatePreferences(
340                            long ownerId, int ownerType, long plid, String portletId,
341                            String xml)
342                    throws SystemException {
343    
344                    PortletPreferences portletPreferences =
345                            portletPreferencesPersistence.fetchByO_O_P_P(
346                                    ownerId, ownerType, plid, portletId);
347    
348                    if (portletPreferences == null) {
349                            long portletPreferencesId = counterLocalService.increment();
350    
351                            portletPreferences = portletPreferencesPersistence.create(
352                                    portletPreferencesId);
353    
354                            portletPreferences.setOwnerId(ownerId);
355                            portletPreferences.setOwnerType(ownerType);
356                            portletPreferences.setPlid(plid);
357                            portletPreferences.setPortletId(portletId);
358                    }
359    
360                    portletPreferences.setPreferences(xml);
361    
362                    portletPreferencesPersistence.update(portletPreferences);
363    
364                    return portletPreferences;
365            }
366    
367            protected javax.portlet.PortletPreferences doGetPreferences(
368                            long companyId, long ownerId, int ownerType, long plid,
369                            String portletId, String defaultPreferences, boolean strict)
370                    throws SystemException {
371    
372                    PortletPreferences portletPreferences =
373                            portletPreferencesPersistence.fetchByO_O_P_P(
374                                    ownerId, ownerType, plid, portletId);
375    
376                    if (portletPreferences == null) {
377                            Portlet portlet = portletLocalService.getPortletById(
378                                    companyId, portletId);
379    
380                            if (strict &&
381                                    (Validator.isNull(defaultPreferences) ||
382                                     ((portlet != null) && portlet.isUndeployedPortlet()))) {
383    
384                                    return new StrictPortletPreferencesImpl();
385                            }
386    
387                            portletPreferences =
388                                    portletPreferencesLocalService.addPortletPreferences(
389                                            companyId, ownerId, ownerType, plid, portletId, portlet,
390                                            defaultPreferences);
391                    }
392    
393                    PortletPreferencesImpl portletPreferencesImpl =
394                            (PortletPreferencesImpl)PortletPreferencesFactoryUtil.fromXML(
395                                    companyId, ownerId, ownerType, plid, portletId,
396                                    portletPreferences.getPreferences());
397    
398                    return portletPreferencesImpl;
399            }
400    
401            protected javax.portlet.PortletPreferences getPreferences(
402                            long companyId, long ownerId, int ownerType, long plid,
403                            String portletId, String defaultPreferences, boolean strict)
404                    throws SystemException {
405    
406                    DB db = DBFactoryUtil.getDB();
407    
408                    String dbType = db.getType();
409    
410                    if (!dbType.equals(DB.TYPE_HYPERSONIC)) {
411                            return doGetPreferences(
412                                    companyId, ownerId, ownerType, plid, portletId,
413                                    defaultPreferences, strict);
414                    }
415    
416                    StringBundler sb = new StringBundler(7);
417    
418                    sb.append(ownerId);
419                    sb.append(StringPool.POUND);
420                    sb.append(ownerType);
421                    sb.append(StringPool.POUND);
422                    sb.append(plid);
423                    sb.append(StringPool.POUND);
424                    sb.append(portletId);
425    
426                    String groupName = getClass().getName();
427                    String key = sb.toString();
428    
429                    Lock lock = LockRegistry.allocateLock(groupName, key);
430    
431                    lock.lock();
432    
433                    try {
434                            return doGetPreferences(
435                                    companyId, ownerId, ownerType, plid, portletId,
436                                    defaultPreferences, strict);
437                    }
438                    finally {
439                            lock.unlock();
440    
441                            LockRegistry.freeLock(groupName, key);
442                    }
443            }
444    
445            private static Log _log = LogFactoryUtil.getLog(
446                    PortletPreferencesLocalServiceImpl.class);
447    
448    }