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.portlet.assetpublisher;
016    
017    import com.liferay.portal.kernel.portlet.PortletLayoutListener;
018    import com.liferay.portal.kernel.portlet.PortletLayoutListenerException;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.model.Layout;
021    import com.liferay.portal.model.PortletConstants;
022    import com.liferay.portal.model.PortletPreferences;
023    import com.liferay.portal.service.LayoutLocalServiceUtil;
024    import com.liferay.portal.service.SubscriptionLocalServiceUtil;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portlet.asset.util.AssetUtil;
027    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
028    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
029    
030    /**
031     * @author Zsolt Berentey
032     */
033    public class AssetPublisherPortletLayoutListener
034            implements PortletLayoutListener {
035    
036            @Override
037            public void onAddToLayout(String portletId, long plid) {
038            }
039    
040            @Override
041            public void onMoveInLayout(String portletId, long plid) {
042            }
043    
044            @Override
045            public void onRemoveFromLayout(String portletId, long plid)
046                    throws PortletLayoutListenerException {
047    
048                    try {
049                            Layout layout = LayoutLocalServiceUtil.getLayout(plid);
050    
051                            if (AssetUtil.isDefaultAssetPublisher(
052                                            layout, portletId, StringPool.BLANK)) {
053    
054                                    JournalArticleLocalServiceUtil.deleteLayoutArticleReferences(
055                                            layout.getGroupId(), layout.getUuid());
056                            }
057    
058                            long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
059                            int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
060    
061                            if (PortletConstants.hasUserId(portletId)) {
062                                    ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
063                                    ownerId = PortletConstants.getUserId(portletId);
064                            }
065    
066                            SubscriptionLocalServiceUtil.deleteSubscriptions(
067                                    layout.getCompanyId(), PortletPreferences.class.getName(),
068                                    AssetPublisherUtil.getSubscriptionClassPK(
069                                            ownerId, ownerType, plid, portletId));
070                    }
071                    catch (Exception e) {
072                            throw new PortletLayoutListenerException(e);
073                    }
074            }
075    
076    }