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.LayoutSet;
023    import com.liferay.portal.model.LayoutSetPrototype;
024    import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
025    import com.liferay.portal.service.persistence.LayoutSetPrototypeUtil;
026    
027    import java.util.Date;
028    
029    /**
030     * @author Raymond Augé
031     */
032    public class LayoutSetPrototypeLayoutSetListener
033            extends BaseModelListener<LayoutSet> {
034    
035            @Override
036            public void onAfterCreate(LayoutSet layoutSet) {
037                    updateLayoutSetPrototype(layoutSet, layoutSet.getModifiedDate());
038            }
039    
040            @Override
041            public void onAfterRemove(LayoutSet layoutSet) {
042                    updateLayoutSetPrototype(layoutSet, new Date());
043            }
044    
045            @Override
046            public void onAfterUpdate(LayoutSet layoutSet) {
047                    updateLayoutSetPrototype(layoutSet, layoutSet.getModifiedDate());
048            }
049    
050            protected void updateLayoutSetPrototype(
051                    LayoutSet layoutSet, Date modifiedDate) {
052    
053                    try {
054                            Group group = layoutSet.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                            UnicodeProperties settingsProperties =
067                                    layoutSet.getSettingsProperties();
068    
069                            settingsProperties.remove("merge-fail-count");
070    
071                            LayoutSetPrototypeUtil.update(layoutSetPrototype);
072                    }
073                    catch (Exception e) {
074                            _log.error(e, e);
075                    }
076            }
077    
078            private static Log _log = LogFactoryUtil.getLog(
079                    LayoutSetPrototypeLayoutSetListener.class);
080    
081    }