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.model;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.model.BaseModelListener;
020    import com.liferay.portal.kernel.model.Group;
021    import com.liferay.portal.kernel.model.Layout;
022    import com.liferay.portal.kernel.model.LayoutRevision;
023    import com.liferay.portal.kernel.model.LayoutSetPrototype;
024    import com.liferay.portal.kernel.model.PortletPreferences;
025    import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
026    import com.liferay.portal.kernel.service.LayoutLocalServiceUtil;
027    import com.liferay.portal.kernel.service.LayoutSetPrototypeLocalServiceUtil;
028    import com.liferay.portal.kernel.service.SubscriptionLocalServiceUtil;
029    import com.liferay.portal.kernel.service.persistence.LayoutRevisionUtil;
030    import com.liferay.portal.kernel.service.persistence.LayoutUtil;
031    import com.liferay.portal.kernel.util.PortletKeys;
032    import com.liferay.portal.servlet.filters.cache.CacheUtil;
033    
034    import java.util.Date;
035    
036    /**
037     * @author Alexander Chow
038     * @author Raymond Augé
039     */
040    public class PortletPreferencesModelListener
041            extends BaseModelListener<PortletPreferences> {
042    
043            @Override
044            public void onAfterRemove(PortletPreferences portletPreferences) {
045                    clearCache(portletPreferences);
046    
047                    deleteSubscriptions(portletPreferences);
048            }
049    
050            @Override
051            public void onAfterUpdate(PortletPreferences portletPreferences) {
052                    clearCache(portletPreferences);
053    
054                    updateLayout(portletPreferences);
055            }
056    
057            protected void clearCache(PortletPreferences portletPreferences) {
058                    if (portletPreferences == null) {
059                            return;
060                    }
061    
062                    try {
063                            long companyId = 0;
064    
065                            Layout layout = LayoutUtil.fetchByPrimaryKey(
066                                    portletPreferences.getPlid());
067    
068                            if ((layout != null) && !layout.isPrivateLayout()) {
069                                    companyId = layout.getCompanyId();
070                            }
071                            else {
072                                    LayoutRevision layoutRevision =
073                                            LayoutRevisionUtil.fetchByPrimaryKey(
074                                                    portletPreferences.getPlid());
075    
076                                    if ((layoutRevision != null) &&
077                                            !layoutRevision.isPrivateLayout()) {
078    
079                                            companyId = layoutRevision.getCompanyId();
080                                    }
081                            }
082    
083                            if (companyId > 0) {
084                                    CacheUtil.clearCache(companyId);
085                            }
086                    }
087                    catch (Exception e) {
088                            CacheUtil.clearCache();
089                    }
090            }
091    
092            protected void deleteSubscriptions(PortletPreferences portletPreferences) {
093                    if (portletPreferences == null) {
094                            return;
095                    }
096    
097                    try {
098                            SubscriptionLocalServiceUtil.deleteSubscriptions(
099                                    portletPreferences.getCompanyId(),
100                                    portletPreferences.getModelClassName(),
101                                    portletPreferences.getPortletPreferencesId());
102                    }
103                    catch (Exception e) {
104                            _log.error("Unable to delete subscriptions", e);
105                    }
106            }
107    
108            protected void updateLayout(PortletPreferences portletPreferences) {
109                    try {
110                            if ((portletPreferences.getOwnerType() ==
111                                            PortletKeys.PREFS_OWNER_TYPE_GROUP) &&
112                                    (portletPreferences.getOwnerId() > 0)) {
113    
114                                    Group group = GroupLocalServiceUtil.fetchGroup(
115                                            portletPreferences.getOwnerId());
116    
117                                    if (group == null) {
118                                            return;
119                                    }
120    
121                                    String className = group.getClassName();
122    
123                                    if (!className.equals(LayoutSetPrototype.class.getName())) {
124                                            return;
125                                    }
126    
127                                    LayoutSetPrototype layoutSetPrototype =
128                                            LayoutSetPrototypeLocalServiceUtil.fetchLayoutSetPrototype(
129                                                    group.getClassPK());
130    
131                                    if (layoutSetPrototype == null) {
132                                            return;
133                                    }
134    
135                                    layoutSetPrototype.setModifiedDate(new Date());
136    
137                                    LayoutSetPrototypeLocalServiceUtil.updateLayoutSetPrototype(
138                                            layoutSetPrototype);
139                            }
140                            else if ((portletPreferences.getOwnerType() ==
141                                                    PortletKeys.PREFS_OWNER_TYPE_LAYOUT) &&
142                                             (portletPreferences.getPlid() > 0)) {
143    
144                                    Layout layout = LayoutLocalServiceUtil.fetchLayout(
145                                            portletPreferences.getPlid());
146    
147                                    if (layout == null) {
148                                            return;
149                                    }
150    
151                                    layout.setModifiedDate(new Date());
152    
153                                    LayoutLocalServiceUtil.updateLayout(layout);
154                            }
155                    }
156                    catch (Exception e) {
157                            _log.error("Unable to update the layout's modified date", e);
158                    }
159            }
160    
161            private static final Log _log = LogFactoryUtil.getLog(
162                    PortletPreferencesModelListener.class);
163    
164    }