1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.words.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.Constants;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.struts.PortletAction;
21  import com.liferay.portal.util.WebKeys;
22  import com.liferay.portlet.words.ScramblerException;
23  import com.liferay.portlet.words.util.WordsUtil;
24  
25  import javax.portlet.PortletConfig;
26  import javax.portlet.RenderRequest;
27  import javax.portlet.RenderResponse;
28  
29  import org.apache.struts.action.ActionForm;
30  import org.apache.struts.action.ActionForward;
31  import org.apache.struts.action.ActionMapping;
32  
33  /**
34   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class ViewAction extends PortletAction {
39  
40      public ActionForward render(
41              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
42              RenderRequest renderRequest, RenderResponse renderResponse)
43          throws Exception {
44  
45          try {
46              String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
47  
48              if (cmd.equals(Constants.SEARCH)) {
49                  String word = ParamUtil.getString(renderRequest, "word");
50                  boolean scramble = ParamUtil.getBoolean(
51                      renderRequest, "scramble");
52  
53                  String[] words = null;
54  
55                  if (scramble) {
56                      words = WordsUtil.scramble(word);
57                  }
58                  else {
59                      words = WordsUtil.unscramble(word);
60                  }
61  
62                  renderRequest.setAttribute(WebKeys.WORDS_LIST, words);
63              }
64          }
65          catch (ScramblerException se) {
66              SessionErrors.add(renderRequest, se.getClass().getName());
67          }
68  
69          return mapping.findForward("portlet.words.view");
70      }
71  
72  }