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.DuplicatePageException;
37  import com.liferay.portlet.wiki.NoSuchNodeException;
38  import com.liferay.portlet.wiki.NoSuchPageException;
39  import com.liferay.portlet.wiki.PageContentException;
40  import com.liferay.portlet.wiki.PageTitleException;
41  import com.liferay.portlet.wiki.PageVersionException;
42  import com.liferay.portlet.wiki.model.WikiNode;
43  import com.liferay.portlet.wiki.model.WikiPage;
44  import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
45  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
46  
47  import javax.portlet.ActionRequest;
48  import javax.portlet.ActionResponse;
49  import javax.portlet.PortletConfig;
50  import javax.portlet.PortletPreferences;
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                     page = WikiPageServiceUtil.addPage(
209                         nodeId, title, null, WikiPageImpl.NEW, true, null,
210                         null);
211                 }
212                 else {
213                     throw nspe;
214                 }
215             }
216 
217             if (removeRedirect) {
218                 page.setContent(StringPool.BLANK);
219                 page.setRedirectTitle(StringPool.BLANK);
220             }
221         }
222 
223         renderRequest.setAttribute(WebKeys.WIKI_PAGE, page);
224     }
225 
226     protected String getSaveAndContinueRedirect(
227             PortletConfig portletConfig, ActionRequest actionRequest,
228             WikiPage page, String redirect)
229         throws Exception {
230 
231         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
232             WebKeys.THEME_DISPLAY);
233 
234         Layout layout = themeDisplay.getLayout();
235 
236         String originalRedirect = ParamUtil.getString(
237             actionRequest, "originalRedirect");
238 
239         PortletURLImpl portletURL = new PortletURLImpl(
240             (ActionRequestImpl)actionRequest, portletConfig.getPortletName(),
241             themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
242 
243         portletURL.setWindowState(WindowState.MAXIMIZED);
244 
245         portletURL.setParameter("struts_action", "/wiki/edit_page");
246         portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
247         portletURL.setParameter("redirect", redirect, false);
248         portletURL.setParameter("originalRedirect", originalRedirect, false);
249         portletURL.setParameter(
250             "groupId", String.valueOf(layout.getGroupId()), false);
251         portletURL.setParameter(
252             "nodeId", String.valueOf(page.getNodeId()), false);
253         portletURL.setParameter("title", page.getTitle(), false);
254 
255         return portletURL.toString();
256     }
257 
258     protected void revertPage(ActionRequest actionRequest) throws Exception {
259         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
260             WebKeys.THEME_DISPLAY);
261 
262         PortletPreferences prefs = actionRequest.getPreferences();
263 
264         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
265         String title = ParamUtil.getString(actionRequest, "title");
266         double version = ParamUtil.getDouble(actionRequest, "version");
267 
268         WikiPageServiceUtil.revertPage(
269             nodeId, title, version, prefs, themeDisplay);
270     }
271 
272     protected void subscribePage(ActionRequest actionRequest) throws Exception {
273         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
274         String title = ParamUtil.getString(actionRequest, "title");
275 
276         WikiPageServiceUtil.subscribePage(nodeId, title);
277     }
278 
279     protected void unsubscribePage(ActionRequest actionRequest)
280         throws Exception {
281 
282         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
283         String title = ParamUtil.getString(actionRequest, "title");
284 
285         WikiPageServiceUtil.unsubscribePage(nodeId, title);
286     }
287 
288     protected WikiPage updatePage(ActionRequest actionRequest)
289         throws Exception {
290 
291         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
292 
293         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
294             WebKeys.THEME_DISPLAY);
295 
296         PortletPreferences prefs = actionRequest.getPreferences();
297 
298         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
299         String title = ParamUtil.getString(actionRequest, "title");
300         double version = ParamUtil.getDouble(actionRequest, "version");
301 
302         String content = ParamUtil.getString(actionRequest, "content");
303         String summary = ParamUtil.getString(actionRequest, "summary");
304         boolean minorEdit = ParamUtil.getBoolean(actionRequest, "minorEdit");
305         String format = ParamUtil.getString(actionRequest, "format");
306         String parentTitle = ParamUtil.getString(actionRequest, "parentTitle");
307         String redirectTitle = null;
308 
309         String[] tagsEntries = StringUtil.split(
310             ParamUtil.getString(actionRequest, "tagsEntries"));
311 
312         WikiPage page = null;
313 
314         if (cmd.equals(Constants.ADD)) {
315             page = WikiPageServiceUtil.addPage(
316                 nodeId, title, content, summary, minorEdit, format,
317                 parentTitle, redirectTitle, tagsEntries, prefs, themeDisplay);
318         }
319         else {
320             page = WikiPageServiceUtil.updatePage(
321                 nodeId, title, version, content, summary, minorEdit, format,
322                 parentTitle, redirectTitle, tagsEntries, prefs, themeDisplay);
323         }
324 
325         return page;
326     }
327 
328     protected boolean isCheckMethodOnProcessAction() {
329         return _CHECK_METHOD_ON_PROCESS_ACTION;
330     }
331 
332     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
333 
334 }