001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }