001    /**
002     * Copyright (c) 2000-2010 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.NoSuchContentSearchException;
027    import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
028    
029    import javax.portlet.PortletPreferences;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class JournalContentPortletLayoutListener
035            implements PortletLayoutListener {
036    
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            public void onMoveInLayout(String portletId, long plid)
046                    throws PortletLayoutListenerException {
047    
048                    if (_log.isDebugEnabled()) {
049                            _log.debug("Move " + portletId + " from in " + plid);
050                    }
051            }
052    
053            public void onRemoveFromLayout(String portletId, long plid)
054                    throws PortletLayoutListenerException {
055    
056                    if (_log.isDebugEnabled()) {
057                            _log.debug("Remove " + portletId + " from layout " + plid);
058                    }
059    
060                    try {
061                            deleteContentSearch(portletId, plid);
062                    }
063                    catch (Exception e) {
064                            throw new PortletLayoutListenerException(e);
065                    }
066            }
067    
068            protected void deleteContentSearch(String portletId, long plid)
069                    throws Exception {
070    
071                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
072    
073                    PortletPreferences preferences =
074                            PortletPreferencesFactoryUtil.getPortletSetup(
075                                    layout, portletId, StringPool.BLANK);
076    
077                    String articleId = preferences.getValue("article-id", null);
078    
079                    if (Validator.isNull(articleId)) {
080                            return;
081                    }
082    
083                    try {
084                            JournalContentSearchLocalServiceUtil.deleteArticleContentSearch(
085                                    layout.getGroupId(), layout.isPrivateLayout(),
086                                    layout.getLayoutId(), portletId, articleId);
087                    }
088                    catch (NoSuchContentSearchException nscse) {
089                    }
090            }
091    
092            private static Log _log = LogFactoryUtil.getLog(
093                    JournalContentPortletLayoutListener.class);
094    
095    }