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