001    /**
002     * Copyright (c) 2000-2010 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.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    }