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