001    /**
002     * Copyright (c) 2000-2012 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    
029    /**
030     * @author Raymond Augé
031     */
032    public class LayoutSetPrototypeLayoutListener
033            extends BaseModelListener<Layout> {
034    
035            @Override
036            public void onAfterCreate(Layout layout) {
037                    updateLayoutSetPrototype(layout);
038            }
039    
040            @Override
041            public void onAfterRemove(Layout layout) {
042                    updateLayoutSetPrototype(layout);
043            }
044    
045            @Override
046            public void onAfterUpdate(Layout layout) {
047                    updateLayoutSetPrototype(layout);
048            }
049    
050            protected void updateLayoutSetPrototype(Layout layout) {
051                    try {
052                            Group group = layout.getGroup();
053    
054                            if (!group.isLayoutSetPrototype()) {
055                                    return;
056                            }
057    
058                            LayoutSetPrototype layoutSetPrototype =
059                                    LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototype(
060                                            group.getClassPK());
061    
062                            layoutSetPrototype.setModifiedDate(layout.getModifiedDate());
063    
064                            LayoutSetPrototypeUtil.update(layoutSetPrototype, false);
065    
066                            LayoutSet layoutSet = layoutSetPrototype.getLayoutSet();
067    
068                            layoutSet.setModifiedDate(layout.getModifiedDate());
069    
070                            UnicodeProperties settingsProperties =
071                                    layoutSet.getSettingsProperties();
072    
073                            settingsProperties.remove("merge-fail-count");
074    
075                            LayoutSetLocalServiceUtil.updateLayoutSet(layoutSet, false);
076                    }
077                    catch (Exception e) {
078                            _log.error(e, e);
079                    }
080            }
081    
082            private static Log _log = LogFactoryUtil.getLog(
083                    LayoutSetPrototypeLayoutListener.class);
084    
085    }