1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.journalcontent;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.portlet.PortletLayoutListener;
25  import com.liferay.portal.kernel.portlet.PortletLayoutListenerException;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.Layout;
29  import com.liferay.portal.service.LayoutLocalServiceUtil;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.portlet.PortletPreferencesFactoryUtil;
32  import com.liferay.portlet.journal.NoSuchContentSearchException;
33  import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
34  
35  import javax.portlet.PortletPreferences;
36  
37  import org.springframework.mock.web.MockHttpServletRequest;
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          MockHttpServletRequest request = new MockHttpServletRequest();
84  
85          Layout layout = LayoutLocalServiceUtil.getLayout(plid);
86  
87          request.setAttribute(WebKeys.LAYOUT, layout);
88  
89          PortletPreferences prefs =
90              PortletPreferencesFactoryUtil.getPortletSetup(
91                  request, portletId, StringPool.BLANK);
92  
93          String articleId = prefs.getValue("article-id", null);
94  
95          if (Validator.isNull(articleId)) {
96              return;
97          }
98  
99          try {
100             JournalContentSearchLocalServiceUtil.deleteArticleContentSearch(
101                 layout.getGroupId(), layout.isPrivateLayout(),
102                 layout.getLayoutId(), portletId, articleId);
103         }
104         catch (NoSuchContentSearchException nscse) {
105         }
106     }
107 
108     private static Log _log =
109         LogFactoryUtil.getLog(JournalContentPortletLayoutListener.class);
110 
111 }