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.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    }