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.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    }