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