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            public Object convert(String key) throws WebCacheException {
039                    Translation translation = new Translation(
040                            _fromLanguageId, _toLanguageId, _fromText);
041    
042                    try {
043                            MicrosoftTranslator microsoftTranslator =
044                                    MicrosoftTranslatorFactoryUtil.getMicrosoftTranslator();
045    
046                            String toText = microsoftTranslator.translate(
047                                    _fromLanguageId, _toLanguageId, _fromText);
048    
049                            translation.setToText(toText);
050                    }
051                    catch (Exception e) {
052                            throw new WebCacheException(e);
053                    }
054    
055                    return translation;
056            }
057    
058            public long getRefreshTime() {
059                    return _REFRESH_TIME;
060            }
061    
062            private static final long _REFRESH_TIME = Time.DAY * 90;
063    
064            private String _fromLanguageId;
065            private String _fromText;
066            private String _toLanguageId;
067    
068    }