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.portal.util.WebKeys;
34  import com.liferay.portlet.PortletPreferencesFactoryUtil;
35  import com.liferay.portlet.journal.NoSuchContentSearchException;
36  import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
37  
38  import javax.portlet.PortletPreferences;
39  
40  import org.springframework.mock.web.MockHttpServletRequest;
41  
42  /**
43   * <a href="JournalContentPortletLayoutListener.java.html"><b><i>View Source</i>
44   * </b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class JournalContentPortletLayoutListener
50      implements PortletLayoutListener {
51  
52      public void onAddToLayout(String portletId, long plid)
53          throws PortletLayoutListenerException {
54  
55          if (_log.isDebugEnabled()) {
56              _log.debug("Add " + portletId + " to layout " + plid);
57          }
58      }
59  
60      public void onMoveInLayout(String portletId, long plid)
61          throws PortletLayoutListenerException {
62  
63          if (_log.isDebugEnabled()) {
64              _log.debug("Move " + portletId + " from in " + plid);
65          }
66      }
67  
68      public void onRemoveFromLayout(String portletId, long plid)
69          throws PortletLayoutListenerException {
70  
71          if (_log.isDebugEnabled()) {
72              _log.debug("Remove " + portletId + " from layout " + plid);
73          }
74  
75          try {
76              deleteContentSearch(portletId, plid);
77          }
78          catch (Exception e) {
79              throw new PortletLayoutListenerException(e);
80          }
81      }
82  
83      protected void deleteContentSearch(String portletId, long plid)
84          throws Exception {
85  
86          MockHttpServletRequest request = new MockHttpServletRequest();
87  
88          Layout layout = LayoutLocalServiceUtil.getLayout(plid);
89  
90          request.setAttribute(WebKeys.LAYOUT, layout);
91  
92          PortletPreferences preferences =
93              PortletPreferencesFactoryUtil.getPortletSetup(
94                  request, portletId, StringPool.BLANK);
95  
96          String articleId = preferences.getValue("article-id", null);
97  
98          if (Validator.isNull(articleId)) {
99              return;
100         }
101 
102         try {
103             JournalContentSearchLocalServiceUtil.deleteArticleContentSearch(
104                 layout.getGroupId(), layout.isPrivateLayout(),
105                 layout.getLayoutId(), portletId, articleId);
106         }
107         catch (NoSuchContentSearchException nscse) {
108         }
109     }
110 
111     private static Log _log =
112         LogFactoryUtil.getLog(JournalContentPortletLayoutListener.class);
113 
114 }