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.wiki.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.util.Constants;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.model.Layout;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.service.ServiceContext;
30  import com.liferay.portal.service.ServiceContextFactory;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.portlet.ActionRequestImpl;
35  import com.liferay.portlet.PortletURLImpl;
36  import com.liferay.portlet.tags.TagsEntryException;
37  import com.liferay.portlet.wiki.DuplicatePageException;
38  import com.liferay.portlet.wiki.NoSuchNodeException;
39  import com.liferay.portlet.wiki.NoSuchPageException;
40  import com.liferay.portlet.wiki.PageContentException;
41  import com.liferay.portlet.wiki.PageTitleException;
42  import com.liferay.portlet.wiki.PageVersionException;
43  import com.liferay.portlet.wiki.model.WikiNode;
44  import com.liferay.portlet.wiki.model.WikiPage;
45  import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
46  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
47  
48  import javax.portlet.ActionRequest;
49  import javax.portlet.ActionResponse;
50  import javax.portlet.PortletConfig;
51  import javax.portlet.PortletRequest;
52  import javax.portlet.RenderRequest;
53  import javax.portlet.RenderResponse;
54  import javax.portlet.WindowState;
55  
56  import org.apache.struts.action.ActionForm;
57  import org.apache.struts.action.ActionForward;
58  import org.apache.struts.action.ActionMapping;
59  
60  /**
61   * <a href="EditPageAction.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Brian Wing Shun Chan
64   * @author Jorge Ferrer
65   *
66   */
67  public class EditPageAction extends PortletAction {
68  
69      public void processAction(
70              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
71              ActionRequest actionRequest, ActionResponse actionResponse)
72          throws Exception {
73  
74          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
75  
76          WikiPage page = null;
77  
78          try {
79              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
80                  page = updatePage(actionRequest);
81              }
82              else if (cmd.equals(Constants.DELETE)) {
83                  deletePage(actionRequest);
84              }
85              else if (cmd.equals(Constants.REVERT)) {
86                  revertPage(actionRequest);
87              }
88              else if (cmd.equals(Constants.SUBSCRIBE)) {
89                  subscribePage(actionRequest);
90              }
91              else if (cmd.equals(Constants.UNSUBSCRIBE)) {
92                  unsubscribePage(actionRequest);
93              }
94  
95              if (Validator.isNotNull(cmd)) {
96                  String redirect = ParamUtil.getString(
97                      actionRequest, "redirect");
98  
99                  if (page != null) {
100                     boolean saveAndContinue = ParamUtil.getBoolean(
101                         actionRequest, "saveAndContinue");
102 
103                     if (saveAndContinue) {
104                         redirect = getSaveAndContinueRedirect(
105                             portletConfig, actionRequest, page, redirect);
106                     }
107                     else if (redirect.endsWith("title=")) {
108                         redirect += page.getTitle();
109                     }
110                 }
111 
112                 sendRedirect(actionRequest, actionResponse, redirect);
113             }
114         }
115         catch (Exception e) {
116             if (e instanceof NoSuchNodeException ||
117                 e instanceof NoSuchPageException ||
118                 e instanceof PrincipalException) {
119 
120                 SessionErrors.add(actionRequest, e.getClass().getName());
121 
122                 setForward(actionRequest, "portlet.wiki.error");
123             }
124             else if (e instanceof DuplicatePageException ||
125                      e instanceof PageContentException ||
126                      e instanceof PageVersionException ||
127                      e instanceof PageTitleException) {
128 
129                 SessionErrors.add(actionRequest, e.getClass().getName());
130             }
131             else if (e instanceof TagsEntryException) {
132                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
133             }
134             else {
135                 throw e;
136             }
137         }
138     }
139 
140     public ActionForward render(
141             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
142             RenderRequest renderRequest, RenderResponse renderResponse)
143         throws Exception {
144 
145         try {
146             ActionUtil.getNode(renderRequest);
147 
148             if (!SessionErrors.contains(
149                     renderRequest, DuplicatePageException.class.getName())) {
150 
151                 getPage(renderRequest);
152             }
153         }
154         catch (Exception e) {
155             if (e instanceof NoSuchNodeException ||
156                 e instanceof PageTitleException ||
157                 e instanceof PrincipalException) {
158 
159                 SessionErrors.add(renderRequest, e.getClass().getName());
160 
161                 return mapping.findForward("portlet.wiki.error");
162             }
163             else if (e instanceof NoSuchPageException) {
164 
165                 // Let edit_page.jsp handle this case
166 
167             }
168             else {
169                 throw e;
170             }
171         }
172 
173         return mapping.findForward(
174             getForward(renderRequest, "portlet.wiki.edit_page"));
175     }
176 
177     protected void deletePage(ActionRequest actionRequest) throws Exception {
178         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
179         String title = ParamUtil.getString(actionRequest, "title");
180 
181         WikiPageServiceUtil.deletePage(nodeId, title);
182     }
183 
184     protected void getPage(RenderRequest renderRequest) throws Exception {
185         long nodeId = ParamUtil.getLong(renderRequest, "nodeId");
186         String title = ParamUtil.getString(renderRequest, "title");
187         double version = ParamUtil.getDouble(renderRequest, "version");
188         boolean removeRedirect = ParamUtil.getBoolean(
189             renderRequest, "removeRedirect");
190 
191         if (nodeId == 0) {
192             WikiNode node = (WikiNode)renderRequest.getAttribute(
193                 WebKeys.WIKI_NODE);
194 
195             if (node != null) {
196                 nodeId = node.getNodeId();
197             }
198         }
199 
200         WikiPage page = null;
201 
202         if (Validator.isNotNull(title)) {
203             try {
204                 page = WikiPageServiceUtil.getPage(nodeId, title, version);
205             }
206             catch (NoSuchPageException nspe) {
207                 if (title.equals(WikiPageImpl.FRONT_PAGE) && (version == 0)) {
208                     ServiceContext serviceContext = new ServiceContext();
209 
210                     page = WikiPageServiceUtil.addPage(
211                         nodeId, title, null, WikiPageImpl.NEW, true,
212                         serviceContext);
213                 }
214                 else {
215                     throw nspe;
216                 }
217             }
218 
219             if (removeRedirect) {
220                 page.setContent(StringPool.BLANK);
221                 page.setRedirectTitle(StringPool.BLANK);
222             }
223         }
224 
225         renderRequest.setAttribute(WebKeys.WIKI_PAGE, page);
226     }
227 
228     protected String getSaveAndContinueRedirect(
229             PortletConfig portletConfig, ActionRequest actionRequest,
230             WikiPage page, String redirect)
231         throws Exception {
232 
233         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
234             WebKeys.THEME_DISPLAY);
235 
236         Layout layout = themeDisplay.getLayout();
237 
238         String originalRedirect = ParamUtil.getString(
239             actionRequest, "originalRedirect");
240 
241         PortletURLImpl portletURL = new PortletURLImpl(
242             (ActionRequestImpl)actionRequest, portletConfig.getPortletName(),
243             themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
244 
245         portletURL.setWindowState(WindowState.MAXIMIZED);
246 
247         portletURL.setParameter("struts_action", "/wiki/edit_page");
248         portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
249         portletURL.setParameter("redirect", redirect, false);
250         portletURL.setParameter("originalRedirect", originalRedirect, false);
251         portletURL.setParameter(
252             "groupId", String.valueOf(layout.getGroupId()), false);
253         portletURL.setParameter(
254             "nodeId", String.valueOf(page.getNodeId()), false);
255         portletURL.setParameter("title", page.getTitle(), false);
256 
257         return portletURL.toString();
258     }
259 
260     protected void revertPage(ActionRequest actionRequest) throws Exception {
261         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
262         String title = ParamUtil.getString(actionRequest, "title");
263         double version = ParamUtil.getDouble(actionRequest, "version");
264 
265         ServiceContext serviceContext = ServiceContextFactory.getInstance(
266             WikiPage.class.getName(), actionRequest);
267 
268         WikiPageServiceUtil.revertPage(nodeId, title, version, serviceContext);
269     }
270 
271     protected void subscribePage(ActionRequest actionRequest) throws Exception {
272         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
273         String title = ParamUtil.getString(actionRequest, "title");
274 
275         WikiPageServiceUtil.subscribePage(nodeId, title);
276     }
277 
278     protected void unsubscribePage(ActionRequest actionRequest)
279         throws Exception {
280 
281         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
282         String title = ParamUtil.getString(actionRequest, "title");
283 
284         WikiPageServiceUtil.unsubscribePage(nodeId, title);
285     }
286 
287     protected WikiPage updatePage(ActionRequest actionRequest)
288         throws Exception {
289 
290         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
291 
292         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
293         String title = ParamUtil.getString(actionRequest, "title");
294         double version = ParamUtil.getDouble(actionRequest, "version");
295 
296         String content = ParamUtil.getString(actionRequest, "content");
297         String summary = ParamUtil.getString(actionRequest, "summary");
298         boolean minorEdit = ParamUtil.getBoolean(actionRequest, "minorEdit");
299         String format = ParamUtil.getString(actionRequest, "format");
300         String parentTitle = ParamUtil.getString(actionRequest, "parentTitle");
301         String redirectTitle = null;
302 
303         ServiceContext serviceContext = ServiceContextFactory.getInstance(
304             WikiPage.class.getName(), actionRequest);
305 
306         WikiPage page = null;
307 
308         if (cmd.equals(Constants.ADD)) {
309             page = WikiPageServiceUtil.addPage(
310                 nodeId, title, content, summary, minorEdit, format, parentTitle,
311                 redirectTitle, serviceContext);
312         }
313         else {
314             page = WikiPageServiceUtil.updatePage(
315                 nodeId, title, version, content, summary, minorEdit, format,
316                 parentTitle, redirectTitle, serviceContext);
317         }
318 
319         return page;
320     }
321 
322     protected boolean isCheckMethodOnProcessAction() {
323         return _CHECK_METHOD_ON_PROCESS_ACTION;
324     }
325 
326     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
327 
328 }