001    /**
002     * Copyright (c) 2000-2013 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.util;
016    
017    import com.liferay.portal.kernel.microsofttranslator.MicrosoftTranslator;
018    import com.liferay.portal.kernel.microsofttranslator.MicrosoftTranslatorFactoryUtil;
019    import com.liferay.portal.kernel.util.Time;
020    import com.liferay.portal.kernel.webcache.WebCacheException;
021    import com.liferay.portal.kernel.webcache.WebCacheItem;
022    import com.liferay.portlet.translator.model.Translation;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     * @author Hugo Huijser
027     */
028    public class TranslationWebCacheItem implements WebCacheItem {
029    
030            public TranslationWebCacheItem(
031                    String fromLanguageId, String toLanguageId, String fromText) {
032    
033                    _fromLanguageId = fromLanguageId;
034                    _toLanguageId = toLanguageId;
035                    _fromText = fromText;
036            }
037    
038            @Override
039            public Object convert(String key) throws WebCacheException {
040                    Translation translation = new Translation(
041                            _fromLanguageId, _toLanguageId, _fromText);
042    
043                    try {
044                            MicrosoftTranslator microsoftTranslator =
045                                    MicrosoftTranslatorFactoryUtil.getMicrosoftTranslator();
046    
047                            String toText = microsoftTranslator.translate(
048                                    _fromLanguageId, _toLanguageId, _fromText);
049    
050                            translation.setToText(toText);
051                    }
052                    catch (Exception e) {
053                            throw new WebCacheException(e);
054                    }
055    
056                    return translation;
057            }
058    
059            @Override
060            public long getRefreshTime() {
061                    return _REFRESH_TIME;
062            }
063    
064            private static final long _REFRESH_TIME = Time.DAY * 90;
065    
066            private String _fromLanguageId;
067            private String _fromText;
068            private String _toLanguageId;
069    
070    }