001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.words.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.struts.PortletAction;
021    import com.liferay.portal.util.WebKeys;
022    import com.liferay.portlet.words.ScramblerException;
023    import com.liferay.portlet.words.util.WordsUtil;
024    
025    import javax.portlet.PortletConfig;
026    import javax.portlet.RenderRequest;
027    import javax.portlet.RenderResponse;
028    
029    import org.apache.struts.action.ActionForm;
030    import org.apache.struts.action.ActionForward;
031    import org.apache.struts.action.ActionMapping;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class ViewAction extends PortletAction {
037    
038            public ActionForward render(
039                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
040                            RenderRequest renderRequest, RenderResponse renderResponse)
041                    throws Exception {
042    
043                    try {
044                            String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
045    
046                            if (cmd.equals(Constants.SEARCH)) {
047                                    String word = ParamUtil.getString(renderRequest, "word");
048                                    boolean scramble = ParamUtil.getBoolean(
049                                            renderRequest, "scramble");
050    
051                                    String[] words = null;
052    
053                                    if (scramble) {
054                                            words = WordsUtil.scramble(word);
055                                    }
056                                    else {
057                                            words = WordsUtil.unscramble(word);
058                                    }
059    
060                                    renderRequest.setAttribute(WebKeys.WORDS_LIST, words);
061                            }
062                    }
063                    catch (ScramblerException se) {
064                            SessionErrors.add(renderRequest, se.getClass().getName());
065                    }
066    
067                    return mapping.findForward("portlet.words.view");
068            }
069    
070    }