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