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