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