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.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    }