001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.staging.LayoutStagingUtil;
026    import com.liferay.portal.kernel.staging.MergeLayoutPrototypesThreadLocal;
027    import com.liferay.portal.kernel.staging.StagingUtil;
028    import com.liferay.portal.kernel.util.ParamUtil;
029    import com.liferay.portal.kernel.util.ReflectionUtil;
030    import com.liferay.portal.kernel.util.StringBundler;
031    import com.liferay.portal.kernel.util.StringPool;
032    import com.liferay.portal.kernel.util.Validator;
033    import com.liferay.portal.kernel.workflow.WorkflowConstants;
034    import com.liferay.portal.model.Layout;
035    import com.liferay.portal.model.LayoutRevision;
036    import com.liferay.portal.model.Portlet;
037    import com.liferay.portal.model.PortletConstants;
038    import com.liferay.portal.model.PortletPreferences;
039    import com.liferay.portal.model.PortletPreferencesIds;
040    import com.liferay.portal.model.User;
041    import com.liferay.portal.security.auth.PrincipalThreadLocal;
042    import com.liferay.portal.service.ServiceContext;
043    import com.liferay.portal.service.ServiceContextThreadLocal;
044    import com.liferay.portal.service.base.PortletPreferencesLocalServiceBaseImpl;
045    import com.liferay.portal.staging.ProxiedLayoutsThreadLocal;
046    import com.liferay.portal.staging.StagingAdvicesThreadLocal;
047    import com.liferay.portlet.PortletPreferencesFactoryUtil;
048    import com.liferay.portlet.PortletPreferencesImpl;
049    
050    import java.util.List;
051    import java.util.concurrent.locks.Lock;
052    
053    /**
054     * @author Brian Wing Shun Chan
055     * @author Shuyang Zhou
056     */
057    public class PortletPreferencesLocalServiceImpl
058            extends PortletPreferencesLocalServiceBaseImpl {
059    
060            @Override
061            public PortletPreferences addPortletPreferences(
062                            long companyId, long ownerId, int ownerType, long plid,
063                            String portletId, Portlet portlet, String defaultPreferences)
064                    throws SystemException {
065    
066                    long portletPreferencesId = counterLocalService.increment();
067    
068                    PortletPreferences portletPreferences =
069                            portletPreferencesPersistence.create(portletPreferencesId);
070    
071                    portletPreferences.setOwnerId(ownerId);
072                    portletPreferences.setOwnerType(ownerType);
073                    portletPreferences.setPlid(plid);
074                    portletPreferences.setPortletId(portletId);
075    
076                    if (Validator.isNull(defaultPreferences)) {
077                            if (portlet == null) {
078                                    defaultPreferences = PortletConstants.DEFAULT_PREFERENCES;
079                            }
080                            else {
081                                    defaultPreferences = portlet.getDefaultPreferences();
082                            }
083                    }
084    
085                    portletPreferences.setPreferences(defaultPreferences);
086    
087                    if (_log.isDebugEnabled()) {
088                            StringBundler sb = new StringBundler(13);
089    
090                            sb.append("Add {companyId=");
091                            sb.append(companyId);
092                            sb.append(", ownerId=");
093                            sb.append(ownerId);
094                            sb.append(", ownerType=");
095                            sb.append(ownerType);
096                            sb.append(", plid=");
097                            sb.append(plid);
098                            sb.append(", portletId=");
099                            sb.append(portletId);
100                            sb.append(", defaultPreferences=");
101                            sb.append(defaultPreferences);
102                            sb.append("}");
103    
104                            _log.debug(sb.toString());
105                    }
106    
107                    try {
108                            portletPreferencesPersistence.update(portletPreferences);
109                    }
110                    catch (SystemException se) {
111                            if (_log.isWarnEnabled()) {
112                                    _log.warn(
113                                            "Add failed, fetch {ownerId=" + ownerId + ", ownerType=" +
114                                                    ownerType + ", plid=" + plid + ", portletId=" +
115                                                            portletId + "}");
116                            }
117    
118                            portletPreferences = portletPreferencesPersistence.fetchByO_O_P_P(
119                                    ownerId, ownerType, plid, portletId, false);
120    
121                            if (portletPreferences == null) {
122                                    throw se;
123                            }
124                    }
125    
126                    return portletPreferences;
127            }
128    
129            @Override
130            public void deletePortletPreferences(long ownerId, int ownerType, long plid)
131                    throws SystemException {
132    
133                    portletPreferencesPersistence.removeByO_O_P(ownerId, ownerType, plid);
134            }
135    
136            @Override
137            public void deletePortletPreferences(
138                            long ownerId, int ownerType, long plid, String portletId)
139                    throws PortalException, SystemException {
140    
141                    if (_log.isDebugEnabled()) {
142                            _log.debug(
143                                    "Delete {ownerId=" + ownerId + ", ownerType=" + ownerType +
144                                            ", plid=" + plid + ", portletId=" + portletId + "}");
145                    }
146    
147                    portletPreferencesPersistence.removeByO_O_P_P(
148                            ownerId, ownerType, plid, portletId);
149            }
150    
151            @Override
152            public void deletePortletPreferencesByPlid(long plid)
153                    throws SystemException {
154    
155                    if (_log.isDebugEnabled()) {
156                            _log.debug("Delete {plid=" + plid + "}");
157                    }
158    
159                    portletPreferencesPersistence.removeByPlid(plid);
160            }
161    
162            @Override
163            public javax.portlet.PortletPreferences fetchPreferences(
164                            long companyId, long ownerId, int ownerType, long plid,
165                            String portletId)
166                    throws SystemException {
167    
168                    PortletPreferences portletPreferences =
169                            portletPreferencesPersistence.fetchByO_O_P_P(
170                                    ownerId, ownerType, plid, portletId);
171    
172                    if (portletPreferences == null) {
173                            return null;
174                    }
175    
176                    PortletPreferencesImpl portletPreferencesImpl =
177                            (PortletPreferencesImpl)PortletPreferencesFactoryUtil.fromXML(
178                                    companyId, ownerId, ownerType, plid, portletId,
179                                    portletPreferences.getPreferences());
180    
181                    return portletPreferencesImpl;
182            }
183    
184            @Override
185            public javax.portlet.PortletPreferences fetchPreferences(
186                            PortletPreferencesIds portletPreferencesIds)
187                    throws SystemException {
188    
189                    return fetchPreferences(
190                            portletPreferencesIds.getCompanyId(),
191                            portletPreferencesIds.getOwnerId(),
192                            portletPreferencesIds.getOwnerType(),
193                            portletPreferencesIds.getPlid(),
194                            portletPreferencesIds.getPortletId());
195            }
196    
197            @Override
198            @Skip
199            public javax.portlet.PortletPreferences getDefaultPreferences(
200                            long companyId, String portletId)
201                    throws SystemException {
202    
203                    Portlet portlet = portletLocalService.getPortletById(
204                            companyId, portletId);
205    
206                    return PortletPreferencesFactoryUtil.fromDefaultXML(
207                            portlet.getDefaultPreferences());
208            }
209    
210            @Override
211            public List<PortletPreferences> getPortletPreferences()
212                    throws SystemException {
213    
214                    return portletPreferencesPersistence.findAll();
215            }
216    
217            @Override
218            public List<PortletPreferences> getPortletPreferences(
219                            int ownerType, long plid, String portletId)
220                    throws SystemException {
221    
222                    return portletPreferencesPersistence.findByO_P_P(
223                            ownerType, _swapPlidForPortletPreferences(plid), portletId);
224            }
225    
226            @Override
227            public List<PortletPreferences> getPortletPreferences(
228                            long ownerId, int ownerType, long plid)
229                    throws SystemException {
230    
231                    return portletPreferencesPersistence.findByO_O_P(
232                            ownerId, ownerType, _swapPlidForPortletPreferences(plid));
233            }
234    
235            @Override
236            public PortletPreferences getPortletPreferences(
237                            long ownerId, int ownerType, long plid, String portletId)
238                    throws PortalException, SystemException {
239    
240                    return portletPreferencesPersistence.findByO_O_P_P(
241                            ownerId, ownerType, _swapPlidForPortletPreferences(plid),
242                            portletId);
243            }
244    
245            @Override
246            public List<PortletPreferences> getPortletPreferences(
247                            long companyId, long groupId, long ownerId, int ownerType,
248                            String portletId, boolean privateLayout)
249                    throws SystemException {
250    
251                    return portletPreferencesFinder.findByC_G_O_O_P_P(
252                            companyId, groupId, ownerId, ownerType, portletId, privateLayout);
253            }
254    
255            @Override
256            public List<PortletPreferences> getPortletPreferences(
257                            long plid, String portletId)
258                    throws SystemException {
259    
260                    return portletPreferencesPersistence.findByP_P(
261                            _swapPlidForPortletPreferences(plid), portletId);
262            }
263    
264            @Override
265            public List<PortletPreferences> getPortletPreferencesByPlid(long plid)
266                    throws SystemException {
267    
268                    return portletPreferencesPersistence.findByPlid(plid);
269            }
270    
271            @Override
272            public long getPortletPreferencesCount(
273                            int ownerType, long plid, String portletId)
274                    throws SystemException {
275    
276                    return portletPreferencesPersistence.countByO_P_P(
277                            ownerType, _swapPlidForPortletPreferences(plid), portletId);
278            }
279    
280            @Override
281            public long getPortletPreferencesCount(int ownerType, String portletId)
282                    throws SystemException {
283    
284                    return portletPreferencesPersistence.countByO_P(ownerType, portletId);
285            }
286    
287            @Override
288            public long getPortletPreferencesCount(
289                            long ownerId, int ownerType, long plid, Portlet portlet,
290                            boolean excludeDefaultPreferences)
291                    throws SystemException {
292    
293                    plid = _swapPlidForPortletPreferences(plid);
294    
295                    String portletId = portlet.getPortletId();
296    
297                    if (plid == -1) {
298                            portletId = portlet.getRootPortletId();
299                    }
300    
301                    return portletPreferencesFinder.countByO_O_P_P_P(
302                            ownerId, ownerType, plid, portletId, excludeDefaultPreferences);
303            }
304    
305            @Override
306            public long getPortletPreferencesCount(
307                            long ownerId, int ownerType, String portletId,
308                            boolean excludeDefaultPreferences)
309                    throws SystemException {
310    
311                    return portletPreferencesFinder.countByO_O_P(
312                            ownerId, ownerType, portletId, excludeDefaultPreferences);
313            }
314    
315            @Override
316            public javax.portlet.PortletPreferences getPreferences(
317                            long companyId, long ownerId, int ownerType, long plid,
318                            String portletId)
319                    throws SystemException {
320    
321                    return getPreferences(
322                            companyId, ownerId, ownerType, plid, portletId, null);
323            }
324    
325            @Override
326            public javax.portlet.PortletPreferences getPreferences(
327                            long companyId, long ownerId, int ownerType, long plid,
328                            String portletId, String defaultPreferences)
329                    throws SystemException {
330    
331                    return getPreferences(
332                            companyId, ownerId, ownerType, plid, portletId, defaultPreferences,
333                            false);
334            }
335    
336            @Override
337            public javax.portlet.PortletPreferences getPreferences(
338                            PortletPreferencesIds portletPreferencesIds)
339                    throws SystemException {
340    
341                    return getPreferences(
342                            portletPreferencesIds.getCompanyId(),
343                            portletPreferencesIds.getOwnerId(),
344                            portletPreferencesIds.getOwnerType(),
345                            portletPreferencesIds.getPlid(),
346                            portletPreferencesIds.getPortletId());
347            }
348    
349            @Override
350            public javax.portlet.PortletPreferences getStrictPreferences(
351                            long companyId, long ownerId, int ownerType, long plid,
352                            String portletId)
353                    throws SystemException {
354    
355                    return getPreferences(
356                            companyId, ownerId, ownerType, plid, portletId, null, true);
357            }
358    
359            @Override
360            public javax.portlet.PortletPreferences getStrictPreferences(
361                            PortletPreferencesIds portletPreferencesIds)
362                    throws SystemException {
363    
364                    return getStrictPreferences(
365                            portletPreferencesIds.getCompanyId(),
366                            portletPreferencesIds.getOwnerId(),
367                            portletPreferencesIds.getOwnerType(),
368                            portletPreferencesIds.getPlid(),
369                            portletPreferencesIds.getPortletId());
370            }
371    
372            @Override
373            public PortletPreferences updatePreferences(
374                            long ownerId, int ownerType, long plid, String portletId,
375                            javax.portlet.PortletPreferences portletPreferences)
376                    throws SystemException {
377    
378                    String xml = PortletPreferencesFactoryUtil.toXML(portletPreferences);
379    
380                    return updatePreferences(ownerId, ownerType, plid, portletId, xml);
381            }
382    
383            @Override
384            public PortletPreferences updatePreferences(
385                            long ownerId, int ownerType, long plid, String portletId,
386                            String xml)
387                    throws SystemException {
388    
389                    plid = _swapPlidForUpdatePreferences(plid);
390    
391                    if (_log.isDebugEnabled()) {
392                            _log.debug(
393                                    "Update {ownerId=" + ownerId + ", ownerType=" + ownerType +
394                                            ", plid=" + plid + ", portletId=" + portletId + ", xml=" +
395                                                    xml + "}");
396                    }
397    
398                    PortletPreferences portletPreferences =
399                            portletPreferencesPersistence.fetchByO_O_P_P(
400                                    ownerId, ownerType, plid, portletId);
401    
402                    if (portletPreferences == null) {
403                            long portletPreferencesId = counterLocalService.increment();
404    
405                            portletPreferences = portletPreferencesPersistence.create(
406                                    portletPreferencesId);
407    
408                            portletPreferences.setOwnerId(ownerId);
409                            portletPreferences.setOwnerType(ownerType);
410                            portletPreferences.setPlid(plid);
411                            portletPreferences.setPortletId(portletId);
412                    }
413    
414                    portletPreferences.setPreferences(xml);
415    
416                    portletPreferencesPersistence.update(portletPreferences);
417    
418                    return portletPreferences;
419            }
420    
421            protected javax.portlet.PortletPreferences doGetPreferences(
422                            long companyId, long ownerId, int ownerType, long plid,
423                            String portletId, String defaultPreferences, boolean strict)
424                    throws SystemException {
425    
426                    PortletPreferences portletPreferences =
427                            portletPreferencesPersistence.fetchByO_O_P_P(
428                                    ownerId, ownerType, plid, portletId);
429    
430                    if (portletPreferences == null) {
431                            Portlet portlet = portletLocalService.getPortletById(
432                                    companyId, portletId);
433    
434                            if (strict &&
435                                    (Validator.isNull(defaultPreferences) ||
436                                     ((portlet != null) && portlet.isUndeployedPortlet()))) {
437    
438                                    if (portlet == null) {
439                                            defaultPreferences = PortletConstants.DEFAULT_PREFERENCES;
440                                    }
441                                    else {
442                                            defaultPreferences = portlet.getDefaultPreferences();
443                                    }
444    
445                                    return PortletPreferencesFactoryUtil.strictFromXML(
446                                            companyId, ownerId, ownerType, plid, portletId,
447                                            defaultPreferences);
448                            }
449    
450                            portletPreferences =
451                                    portletPreferencesLocalService.addPortletPreferences(
452                                            companyId, ownerId, ownerType, plid, portletId, portlet,
453                                            defaultPreferences);
454                    }
455    
456                    PortletPreferencesImpl portletPreferencesImpl =
457                            (PortletPreferencesImpl)PortletPreferencesFactoryUtil.fromXML(
458                                    companyId, ownerId, ownerType, plid, portletId,
459                                    portletPreferences.getPreferences());
460    
461                    return portletPreferencesImpl;
462            }
463    
464            protected javax.portlet.PortletPreferences getPreferences(
465                            long companyId, long ownerId, int ownerType, long plid,
466                            String portletId, String defaultPreferences, boolean strict)
467                    throws SystemException {
468    
469                    plid = _swapPlidForPreferences(plid);
470    
471                    DB db = DBFactoryUtil.getDB();
472    
473                    String dbType = db.getType();
474    
475                    if (!dbType.equals(DB.TYPE_HYPERSONIC)) {
476                            return doGetPreferences(
477                                    companyId, ownerId, ownerType, plid, portletId,
478                                    defaultPreferences, strict);
479                    }
480    
481                    StringBundler sb = new StringBundler(7);
482    
483                    sb.append(ownerId);
484                    sb.append(StringPool.POUND);
485                    sb.append(ownerType);
486                    sb.append(StringPool.POUND);
487                    sb.append(plid);
488                    sb.append(StringPool.POUND);
489                    sb.append(portletId);
490    
491                    String groupName = getClass().getName();
492                    String key = sb.toString();
493    
494                    Lock lock = LockRegistry.allocateLock(groupName, key);
495    
496                    lock.lock();
497    
498                    try {
499                            return doGetPreferences(
500                                    companyId, ownerId, ownerType, plid, portletId,
501                                    defaultPreferences, strict);
502                    }
503                    finally {
504                            lock.unlock();
505    
506                            LockRegistry.freeLock(groupName, key);
507                    }
508            }
509    
510            private LayoutRevision _getLayoutRevision(long plid)
511                    throws SystemException {
512    
513                    if (plid <= 0) {
514                            return null;
515                    }
516    
517                    LayoutRevision layoutRevision =
518                            layoutRevisionPersistence.fetchByPrimaryKey(plid);
519    
520                    if (layoutRevision != null) {
521                            return layoutRevision;
522                    }
523    
524                    Layout layout = layoutLocalService.fetchLayout(plid);
525    
526                    if (layout == null) {
527                            return null;
528                    }
529    
530                    if (!LayoutStagingUtil.isBranchingLayout(layout)) {
531                            return null;
532                    }
533    
534                    return LayoutStagingUtil.getLayoutRevision(layout);
535            }
536    
537            private long _swapPlidForPortletPreferences(long plid)
538                    throws SystemException {
539    
540                    if (!StagingAdvicesThreadLocal.isEnabled()) {
541                            return plid;
542                    }
543    
544                    LayoutRevision layoutRevision = _getLayoutRevision(plid);
545    
546                    if (layoutRevision == null) {
547                            return plid;
548                    }
549    
550                    return layoutRevision.getLayoutRevisionId();
551            }
552    
553            private long _swapPlidForPreferences(long plid) throws SystemException {
554                    if (!StagingAdvicesThreadLocal.isEnabled()) {
555                            return plid;
556                    }
557    
558                    LayoutRevision layoutRevision = _getLayoutRevision(plid);
559    
560                    if (layoutRevision == null) {
561                            return plid;
562                    }
563    
564                    User user = userPersistence.fetchByPrimaryKey(
565                            PrincipalThreadLocal.getUserId());
566    
567                    if ((user == null) || user.isDefaultUser()) {
568                            return layoutRevision.getLayoutRevisionId();
569                    }
570    
571                    try {
572                            return StagingUtil.getRecentLayoutRevisionId(
573                                    user, layoutRevision.getLayoutSetBranchId(),
574                                    layoutRevision.getPlid());
575                    }
576                    catch (PortalException pe) {
577                            return ReflectionUtil.<Long>throwException(pe);
578                    }
579            }
580    
581            private long _swapPlidForUpdatePreferences(long plid)
582                    throws SystemException {
583    
584                    if (!StagingAdvicesThreadLocal.isEnabled()) {
585                            return plid;
586                    }
587    
588                    LayoutRevision layoutRevision = _getLayoutRevision(plid);
589    
590                    if (layoutRevision == null) {
591                            return plid;
592                    }
593    
594                    ServiceContext serviceContext =
595                            ServiceContextThreadLocal.getServiceContext();
596    
597                    if (serviceContext == null) {
598                            return plid;
599                    }
600    
601                    boolean exporting = ParamUtil.getBoolean(serviceContext, "exporting");
602    
603                    if (exporting) {
604                            return plid;
605                    }
606    
607                    if (!MergeLayoutPrototypesThreadLocal.isInProgress()) {
608                            serviceContext.setWorkflowAction(
609                                    WorkflowConstants.ACTION_SAVE_DRAFT);
610                    }
611    
612                    try {
613                            layoutRevision = layoutRevisionLocalService.updateLayoutRevision(
614                                    serviceContext.getUserId(),
615                                    layoutRevision.getLayoutRevisionId(),
616                                    layoutRevision.getLayoutBranchId(), layoutRevision.getName(),
617                                    layoutRevision.getTitle(), layoutRevision.getDescription(),
618                                    layoutRevision.getKeywords(), layoutRevision.getRobots(),
619                                    layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
620                                    layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
621                                    layoutRevision.getColorSchemeId(),
622                                    layoutRevision.getWapColorSchemeId(),
623                                    layoutRevision.getWapThemeId(), layoutRevision.getCss(),
624                                    serviceContext);
625                    }
626                    catch (PortalException pe) {
627                            ReflectionUtil.throwException(pe);
628                    }
629    
630                    plid = layoutRevision.getLayoutRevisionId();
631    
632                    ProxiedLayoutsThreadLocal.clearProxiedLayouts();
633    
634                    return plid;
635            }
636    
637            private static Log _log = LogFactoryUtil.getLog(
638                    PortletPreferencesLocalServiceImpl.class);
639    
640    }