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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.UnicodeProperties;
020    import com.liferay.portal.model.BaseModelListener;
021    import com.liferay.portal.model.Group;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.model.LayoutSet;
024    import com.liferay.portal.model.LayoutSetPrototype;
025    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
026    import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
027    import com.liferay.portal.service.persistence.LayoutSetPrototypeUtil;
028    import com.liferay.portlet.sites.util.SitesUtil;
029    
030    import java.util.Date;
031    
032    /**
033     * @author Raymond Aug??
034     */
035    public class LayoutSetPrototypeLayoutListener
036            extends BaseModelListener<Layout> {
037    
038            @Override
039            public void onAfterCreate(Layout layout) {
040                    updateLayoutSetPrototype(layout, layout.getModifiedDate());
041            }
042    
043            @Override
044            public void onAfterRemove(Layout layout) {
045                    updateLayoutSetPrototype(layout, new Date());
046            }
047    
048            @Override
049            public void onAfterUpdate(Layout layout) {
050                    updateLayoutSetPrototype(layout, layout.getModifiedDate());
051            }
052    
053            protected void updateLayoutSetPrototype(Layout layout, Date modifiedDate) {
054                    try {
055                            Group group = layout.getGroup();
056    
057                            if (!group.isLayoutSetPrototype()) {
058                                    return;
059                            }
060    
061                            LayoutSetPrototype layoutSetPrototype =
062                                    LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototype(
063                                            group.getClassPK());
064    
065                            layoutSetPrototype.setModifiedDate(modifiedDate);
066    
067                            LayoutSetPrototypeUtil.update(layoutSetPrototype, false);
068    
069                            LayoutSet layoutSet = layoutSetPrototype.getLayoutSet();
070    
071                            layoutSet.setModifiedDate(layout.getModifiedDate());
072    
073                            UnicodeProperties settingsProperties =
074                                    layoutSet.getSettingsProperties();
075    
076                            settingsProperties.remove(SitesUtil.MERGE_FAIL_COUNT);
077    
078                            LayoutSetLocalServiceUtil.updateLayoutSet(layoutSet, false);
079                    }
080                    catch (Exception e) {
081                            _log.error(e, e);
082                    }
083            }
084    
085            private static Log _log = LogFactoryUtil.getLog(
086                    LayoutSetPrototypeLayoutListener.class);
087    
088    }