View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.layout.impl;
18  
19  import java.util.Map;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.jetspeed.JetspeedActions;
24  import org.apache.jetspeed.ajax.AJAXException;
25  import org.apache.jetspeed.ajax.AjaxAction;
26  import org.apache.jetspeed.ajax.AjaxBuilder;
27  import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
28  import org.apache.jetspeed.layout.PortletPlacementContext;
29  import org.apache.jetspeed.om.page.Fragment;
30  import org.apache.jetspeed.om.page.Page;
31  import org.apache.jetspeed.page.PageManager;
32  import org.apache.jetspeed.pipeline.PipelineException;
33  import org.apache.jetspeed.request.RequestContext;
34  
35  
36  /***
37   * Remove Portlet portlet placement action
38   * 
39   * AJAX Parameters: 
40   *    id = the fragment id of the portlet to remove
41   *    page = (implied in the URL)
42   *    
43   * @author <a>David Gurney </a>
44   * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
45   * @version $Id: $
46   */
47  public class RemovePortletAction 
48      extends BasePortletAction 
49      implements AjaxAction, AjaxBuilder, Constants
50  {
51      protected static final Log log = LogFactory.getLog(RemovePortletAction.class);
52  
53      public RemovePortletAction(String template, String errorTemplate)
54              throws PipelineException
55      {
56          this(template, errorTemplate, null, null);
57      }
58  
59      public RemovePortletAction(String template, 
60                                 String errorTemplate, 
61                                 PageManager pageManager, 
62                                 PortletActionSecurityBehavior securityBehavior)
63      throws PipelineException
64      {
65          super(template, errorTemplate, pageManager, securityBehavior);
66      }
67      
68      public boolean runBatch(RequestContext requestContext, Map resultMap) throws AJAXException
69      {
70          return runAction(requestContext, resultMap, true);
71      }    
72      
73      public boolean run(RequestContext requestContext, Map resultMap)
74              throws AJAXException
75      {
76          return runAction(requestContext, resultMap, false);
77      }
78      
79      public boolean runAction(RequestContext requestContext, Map resultMap, boolean batch)
80      {
81          boolean success = true;
82          String status = "success";
83          try
84          {
85              resultMap.put(ACTION, "remove");
86              // Get the necessary parameters off of the request
87              String portletId = getActionParameter(requestContext, PORTLETID);
88              if (portletId == null) 
89              { 
90                  success = false;
91                  resultMap.put(REASON, "Portlet ID not provided");
92                  return success;
93              }
94              resultMap.put(PORTLETID, portletId);
95              if (false == checkAccess(requestContext, JetspeedActions.EDIT))
96              {
97                  Page page = requestContext.getPage();
98                  Fragment fragment = page.getFragmentById(portletId);
99                  if (fragment == null)
100                 {
101                     success = false;
102                     resultMap.put(REASON, "Fragment not found");
103                     return success;                    
104                 }
105                 int column = fragment.getLayoutColumn();
106                 int row = fragment.getLayoutRow();
107                 if (!createNewPageOnEdit(requestContext))
108                 {                
109                     success = false;
110                     resultMap.put(REASON, "Insufficient access to edit page");
111                     return success;
112                 }
113                 status = "refresh";                
114                 // translate old portlet id to new portlet id
115                 Fragment newFragment = getFragmentIdFromLocation(row, column, requestContext.getPage());
116                 if (newFragment == null)
117                 {
118                     success = false;
119                     resultMap.put(REASON, "Failed to find new fragment");
120                     return success;                    
121                 }
122                 portletId = newFragment.getId();
123             }
124             
125             // Use the Portlet Placement Manager to accomplish the removal
126             PortletPlacementContext placement = new PortletPlacementContextImpl(requestContext);
127             Fragment fragment = placement.getFragmentById(portletId);
128             if (fragment == null)
129             {
130                 success = false;
131                 resultMap.put(REASON, "Fragment not found");
132                 return success;                
133             }
134             placement.remove(fragment);
135             Page page = placement.syncPageFragments();
136             page.removeFragmentById(fragment.getId());
137             if (!batch)
138             {
139                 if (pageManager != null)
140                     pageManager.updatePage(page);
141             }
142             // Build the results for the response
143             resultMap.put(PORTLETID, portletId);            
144             resultMap.put(STATUS, status);
145             resultMap.put(OLDCOL, String.valueOf(fragment.getLayoutColumn()));
146             resultMap.put(OLDROW, String.valueOf(fragment.getLayoutRow()));
147         } 
148         catch (Exception e)
149         {
150             // Log the exception
151             log.error("exception while adding a portlet", e);
152             resultMap.put(REASON, e.toString());
153             // Return a failure indicator
154             success = false;
155         }
156 
157         return success;
158     }
159 }