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.journalcontent;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.portlet.PortletLayoutListener;
020    import com.liferay.portal.kernel.portlet.PortletLayoutListenerException;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.service.LayoutLocalServiceUtil;
025    import com.liferay.portlet.PortletPreferencesFactoryUtil;
026    import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
027    
028    import javax.portlet.PortletPreferences;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class JournalContentPortletLayoutListener
034            implements PortletLayoutListener {
035    
036            @Override
037            public void onAddToLayout(String portletId, long plid)
038                    throws PortletLayoutListenerException {
039    
040                    if (_log.isDebugEnabled()) {
041                            _log.debug("Add " + portletId + " to layout " + plid);
042                    }
043            }
044    
045            @Override
046            public void onMoveInLayout(String portletId, long plid)
047                    throws PortletLayoutListenerException {
048    
049                    if (_log.isDebugEnabled()) {
050                            _log.debug("Move " + portletId + " from in " + plid);
051                    }
052            }
053    
054            @Override
055            public void onRemoveFromLayout(String portletId, long plid)
056                    throws PortletLayoutListenerException {
057    
058                    if (_log.isDebugEnabled()) {
059                            _log.debug("Remove " + portletId + " from layout " + plid);
060                    }
061    
062                    try {
063                            deleteContentSearch(portletId, plid);
064                    }
065                    catch (Exception e) {
066                            throw new PortletLayoutListenerException(e);
067                    }
068            }
069    
070            protected void deleteContentSearch(String portletId, long plid)
071                    throws Exception {
072    
073                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
074    
075                    PortletPreferences preferences =
076                            PortletPreferencesFactoryUtil.getPortletSetup(
077                                    layout, portletId, StringPool.BLANK);
078    
079                    String articleId = preferences.getValue("articleId", null);
080    
081                    if (Validator.isNull(articleId)) {
082                            return;
083                    }
084    
085                    JournalContentSearchLocalServiceUtil.deleteArticleContentSearch(
086                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
087                            portletId, articleId);
088            }
089    
090            private static Log _log = LogFactoryUtil.getLog(
091                    JournalContentPortletLayoutListener.class);
092    
093    }