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            @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, String portletId,
211                            boolean excludeDefaultPreferences)
212                    throws SystemException {
213    
214                    return portletPreferencesFinder.countByO_O_P(
215                            ownerId, ownerType, portletId, excludeDefaultPreferences);
216            }
217    
218            @Override
219            public long getPortletPreferencesCount(
220                            long companyId, long groupId, long ownerId, int ownerType,
221                            long plid, Portlet portlet, boolean privateLayout,
222                            boolean excludeDefaultPreferences)
223                    throws SystemException {
224    
225                    String portletId = StringPool.BLANK;
226    
227                    if (plid == -1) {
228                            portletId = portlet.getRootPortletId();
229                    }
230                    else {
231                            portletId = portlet.getPortletId();
232                    }
233    
234                    return portletPreferencesFinder.countByC_G_O_O_P_P_P(
235                            companyId, groupId, ownerId, ownerType, plid, portletId,
236                            privateLayout, excludeDefaultPreferences);
237            }
238    
239            @Override
240            public javax.portlet.PortletPreferences getPreferences(
241                            long companyId, long ownerId, int ownerType, long plid,
242                            String portletId)
243                    throws SystemException {
244    
245                    return getPreferences(
246                            companyId, ownerId, ownerType, plid, portletId, null);
247            }
248    
249            @Override
250            public javax.portlet.PortletPreferences getPreferences(
251                            long companyId, long ownerId, int ownerType, long plid,
252                            String portletId, String defaultPreferences)
253                    throws SystemException {
254    
255                    DB db = DBFactoryUtil.getDB();
256    
257                    String dbType = db.getType();
258    
259                    if (!dbType.equals(DB.TYPE_HYPERSONIC)) {
260                            return doGetPreferences(
261                                    companyId, ownerId, ownerType, plid, portletId,
262                                    defaultPreferences);
263                    }
264    
265                    StringBundler sb = new StringBundler(7);
266    
267                    sb.append(ownerId);
268                    sb.append(StringPool.POUND);
269                    sb.append(ownerType);
270                    sb.append(StringPool.POUND);
271                    sb.append(plid);
272                    sb.append(StringPool.POUND);
273                    sb.append(portletId);
274    
275                    String groupName = getClass().getName();
276                    String key = sb.toString();
277    
278                    Lock lock = LockRegistry.allocateLock(groupName, key);
279    
280                    lock.lock();
281    
282                    try {
283                            return doGetPreferences(
284                                    companyId, ownerId, ownerType, plid, portletId,
285                                    defaultPreferences);
286                    }
287                    finally {
288                            lock.unlock();
289    
290                            LockRegistry.freeLock(groupName, key);
291                    }
292            }
293    
294            @Override
295            public javax.portlet.PortletPreferences getPreferences(
296                            PortletPreferencesIds portletPreferencesIds)
297                    throws SystemException {
298    
299                    return getPreferences(
300                            portletPreferencesIds.getCompanyId(),
301                            portletPreferencesIds.getOwnerId(),
302                            portletPreferencesIds.getOwnerType(),
303                            portletPreferencesIds.getPlid(),
304                            portletPreferencesIds.getPortletId());
305            }
306    
307            @Override
308            public javax.portlet.PortletPreferences getStrictPreferences(
309                            long companyId, long ownerId, int ownerType, long plid,
310                            String portletId)
311                    throws SystemException {
312    
313                    boolean strict = PortletPreferencesThreadLocal.isStrict();
314    
315                    PortletPreferencesThreadLocal.setStrict(!PropsValues.TCK_URL);
316    
317                    try {
318                            return getPreferences(
319                                    companyId, ownerId, ownerType, plid, portletId, null);
320                    }
321                    finally {
322                            PortletPreferencesThreadLocal.setStrict(strict);
323                    }
324            }
325    
326            @Override
327            public javax.portlet.PortletPreferences getStrictPreferences(
328                            PortletPreferencesIds portletPreferencesIds)
329                    throws SystemException {
330    
331                    return getStrictPreferences(
332                            portletPreferencesIds.getCompanyId(),
333                            portletPreferencesIds.getOwnerId(),
334                            portletPreferencesIds.getOwnerType(),
335                            portletPreferencesIds.getPlid(),
336                            portletPreferencesIds.getPortletId());
337            }
338    
339            @Override
340            public PortletPreferences updatePreferences(
341                            long ownerId, int ownerType, long plid, String portletId,
342                            javax.portlet.PortletPreferences portletPreferences)
343                    throws SystemException {
344    
345                    String xml = PortletPreferencesFactoryUtil.toXML(portletPreferences);
346    
347                    return updatePreferences(ownerId, ownerType, plid, portletId, xml);
348            }
349    
350            @Override
351            public PortletPreferences updatePreferences(
352                            long ownerId, int ownerType, long plid, String portletId,
353                            String xml)
354                    throws SystemException {
355    
356                    PortletPreferences portletPreferences =
357                            portletPreferencesPersistence.fetchByO_O_P_P(
358                                    ownerId, ownerType, plid, portletId);
359    
360                    if (portletPreferences == null) {
361                            long portletPreferencesId = counterLocalService.increment();
362    
363                            portletPreferences = portletPreferencesPersistence.create(
364                                    portletPreferencesId);
365    
366                            portletPreferences.setOwnerId(ownerId);
367                            portletPreferences.setOwnerType(ownerType);
368                            portletPreferences.setPlid(plid);
369                            portletPreferences.setPortletId(portletId);
370                    }
371    
372                    portletPreferences.setPreferences(xml);
373    
374                    portletPreferencesPersistence.update(portletPreferences);
375    
376                    return portletPreferences;
377            }
378    
379            protected javax.portlet.PortletPreferences doGetPreferences(
380                            long companyId, long ownerId, int ownerType, long plid,
381                            String portletId, String defaultPreferences)
382                    throws SystemException {
383    
384                    PortletPreferences portletPreferences =
385                            portletPreferencesPersistence.fetchByO_O_P_P(
386                                    ownerId, ownerType, plid, portletId);
387    
388                    if (portletPreferences == null) {
389                            Portlet portlet = portletLocalService.getPortletById(
390                                    companyId, portletId);
391    
392                            if (PortletPreferencesThreadLocal.isStrict() &&
393                                    (Validator.isNull(defaultPreferences) ||
394                                     ((portlet != null) && portlet.isUndeployedPortlet()))) {
395    
396                                    return new PortletPreferencesImpl();
397                            }
398    
399                            portletPreferences =
400                                    portletPreferencesLocalService.addPortletPreferences(
401                                            companyId, ownerId, ownerType, plid, portletId, portlet,
402                                            defaultPreferences);
403                    }
404    
405                    PortletPreferencesImpl portletPreferencesImpl =
406                            (PortletPreferencesImpl)PortletPreferencesFactoryUtil.fromXML(
407                                    companyId, ownerId, ownerType, plid, portletId,
408                                    portletPreferences.getPreferences());
409    
410                    return portletPreferencesImpl;
411            }
412    
413            private static Log _log = LogFactoryUtil.getLog(
414                    PortletPreferencesLocalServiceImpl.class);
415    
416    }