001    /**
002     * Copyright (c) 2000-2012 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.model;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.service.LayoutLocalServiceUtil;
020    import com.liferay.portal.service.persistence.LayoutRevisionUtil;
021    import com.liferay.portal.service.persistence.LayoutUtil;
022    import com.liferay.portal.servlet.filters.cache.CacheUtil;
023    import com.liferay.portal.util.PortletKeys;
024    
025    import java.util.Date;
026    
027    /**
028     * @author Alexander Chow
029     * @author Raymond Augé
030     */
031    public class PortletPreferencesListener
032            extends BaseModelListener<PortletPreferences> {
033    
034            @Override
035            public void onAfterRemove(PortletPreferences portletPreferences) {
036                    clearCache(portletPreferences);
037            }
038    
039            @Override
040            public void onAfterUpdate(PortletPreferences portletPreferences) {
041                    clearCache(portletPreferences);
042    
043                    updateLayout(portletPreferences);
044            }
045    
046            protected void clearCache(PortletPreferences portletPreferences) {
047                    try {
048                            long companyId = 0;
049    
050                            Layout layout = LayoutUtil.fetchByPrimaryKey(
051                                    portletPreferences.getPlid());
052    
053                            if ((layout != null) && !layout.isPrivateLayout()) {
054                                    companyId = layout.getCompanyId();
055                            }
056                            else {
057                                    LayoutRevision layoutRevision =
058                                            LayoutRevisionUtil.fetchByPrimaryKey(
059                                                    portletPreferences.getPlid());
060    
061                                    if ((layoutRevision != null) &&
062                                            !layoutRevision.isPrivateLayout()) {
063    
064                                            companyId = layoutRevision.getCompanyId();
065                                    }
066                            }
067    
068                            if (companyId > 0) {
069                                    CacheUtil.clearCache(companyId);
070                            }
071                    }
072                    catch (Exception e) {
073                            CacheUtil.clearCache();
074                    }
075            }
076    
077            protected void updateLayout(PortletPreferences portletPreferences) {
078                    try {
079                            if ((portletPreferences.getOwnerType() ==
080                                            PortletKeys.PREFS_OWNER_TYPE_LAYOUT) &&
081                                    (portletPreferences.getPlid() > 0)) {
082    
083                                    Layout layout = LayoutLocalServiceUtil.fetchLayout(
084                                            portletPreferences.getPlid());
085    
086                                    if (layout == null) {
087                                            return;
088                                    }
089    
090                                    layout.setModifiedDate(new Date());
091    
092                                    LayoutLocalServiceUtil.updateLayout(layout, true);
093                            }
094                    }
095                    catch (Exception e) {
096                            _log.error("Unable to update the layout's modified date", e);
097                    }
098            }
099    
100            private static Log _log = LogFactoryUtil.getLog(
101                    PortletPreferencesListener.class);
102    
103    }