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.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, false);
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    }