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