1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journalcontent;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.portlet.PortletLayoutListener;
28  import com.liferay.portal.kernel.portlet.PortletLayoutListenerException;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Layout;
32  import com.liferay.portal.service.LayoutLocalServiceUtil;
33  import com.liferay.portlet.PortletPreferencesFactoryUtil;
34  import com.liferay.portlet.journal.NoSuchContentSearchException;
35  import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
36  
37  import javax.portlet.PortletPreferences;
38  
39  /**
40   * <a href="JournalContentPortletLayoutListener.java.html"><b><i>View Source</i>
41   * </b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class JournalContentPortletLayoutListener
47      implements PortletLayoutListener {
48  
49      public void onAddToLayout(String portletId, long plid)
50          throws PortletLayoutListenerException {
51  
52          if (_log.isDebugEnabled()) {
53              _log.debug("Add " + portletId + " to layout " + plid);
54          }
55      }
56  
57      public void onMoveInLayout(String portletId, long plid)
58          throws PortletLayoutListenerException {
59  
60          if (_log.isDebugEnabled()) {
61              _log.debug("Move " + portletId + " from in " + plid);
62          }
63      }
64  
65      public void onRemoveFromLayout(String portletId, long plid)
66          throws PortletLayoutListenerException {
67  
68          if (_log.isDebugEnabled()) {
69              _log.debug("Remove " + portletId + " from layout " + plid);
70          }
71  
72          try {
73              deleteContentSearch(portletId, plid);
74          }
75          catch (Exception e) {
76              throw new PortletLayoutListenerException(e);
77          }
78      }
79  
80      protected void deleteContentSearch(String portletId, long plid)
81          throws Exception {
82  
83          Layout layout = LayoutLocalServiceUtil.getLayout(plid);
84  
85          PortletPreferences prefs =
86              PortletPreferencesFactoryUtil.getPortletSetup(
87                  layout, portletId, StringPool.BLANK);
88  
89          String articleId = prefs.getValue("article-id", null);
90  
91          if (Validator.isNull(articleId)) {
92              return;
93          }
94  
95          try {
96              JournalContentSearchLocalServiceUtil.deleteArticleContentSearch(
97                  layout.getGroupId(), layout.isPrivateLayout(),
98                  layout.getLayoutId(), portletId, articleId);
99          }
100         catch (NoSuchContentSearchException nscse) {
101         }
102     }
103 
104     private static Log _log =
105         LogFactoryUtil.getLog(JournalContentPortletLayoutListener.class);
106 
107 }