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