001    /**
002     * Copyright (c) 2000-2013 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.translator;
016    
017    import com.liferay.portal.kernel.microsofttranslator.MicrosoftTranslatorException;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.webcache.WebCacheException;
022    import com.liferay.portal.util.WebKeys;
023    import com.liferay.portlet.translator.model.Translation;
024    import com.liferay.portlet.translator.util.TranslatorUtil;
025    import com.liferay.util.bridges.mvc.MVCPortlet;
026    
027    import javax.portlet.ActionRequest;
028    import javax.portlet.ActionResponse;
029    import javax.portlet.PortletException;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class TranslatorPortlet extends MVCPortlet {
035    
036            @Override
037            public void processAction(
038                            ActionRequest actionRequest, ActionResponse actionResponse)
039                    throws PortletException {
040    
041                    try {
042                            String translationId = ParamUtil.getString(actionRequest, "id");
043                            String fromText = ParamUtil.getString(actionRequest, "text");
044    
045                            if (Validator.isNotNull(fromText)) {
046                                    Translation translation = TranslatorUtil.getTranslation(
047                                            translationId, fromText);
048    
049                                    actionRequest.setAttribute(
050                                            WebKeys.TRANSLATOR_TRANSLATION, translation);
051                            }
052                    }
053                    catch (WebCacheException wce) {
054                            Throwable cause = wce.getCause();
055    
056                            if (cause instanceof MicrosoftTranslatorException) {
057                                    SessionErrors.add(actionRequest, cause.getClass(), cause);
058                            }
059                            else {
060                                    throw new PortletException(wce);
061                            }
062                    }
063                    catch (Exception e) {
064                            throw new PortletException(e);
065                    }
066            }
067    
068    }