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 fromLanguageId = ParamUtil.getString(
043                                    actionRequest, "fromLanguageId");
044                            String toLanguageId = ParamUtil.getString(
045                                    actionRequest, "toLanguageId");
046                            String fromText = ParamUtil.getString(actionRequest, "text");
047    
048                            if (Validator.isNotNull(fromText)) {
049                                    Translation translation = TranslatorUtil.getTranslation(
050                                            fromLanguageId, toLanguageId, fromText);
051    
052                                    actionRequest.setAttribute(
053                                            WebKeys.TRANSLATOR_TRANSLATION, translation);
054                            }
055                    }
056                    catch (WebCacheException wce) {
057                            Throwable cause = wce.getCause();
058    
059                            if (cause instanceof MicrosoftTranslatorException) {
060                                    SessionErrors.add(actionRequest, cause.getClass(), cause);
061                            }
062                            else {
063                                    throw new PortletException(wce);
064                            }
065                    }
066                    catch (Exception e) {
067                            throw new PortletException(e);
068                    }
069            }
070    
071    }