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.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.StringPool;
020    import com.liferay.portal.kernel.util.Time;
021    import com.liferay.portal.kernel.webcache.WebCacheException;
022    import com.liferay.portal.kernel.webcache.WebCacheItem;
023    import com.liferay.portlet.translator.model.Translation;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     * @author Hugo Huijser
028     */
029    public class TranslationWebCacheItem implements WebCacheItem {
030    
031            public TranslationWebCacheItem(String translationId, String fromText) {
032                    _translationId = translationId;
033                    _fromText = fromText;
034            }
035    
036            public Object convert(String key) throws WebCacheException {
037                    Translation translation = new Translation(_translationId, _fromText);
038    
039                    try {
040                            MicrosoftTranslator microsoftTranslator =
041                                    MicrosoftTranslatorFactoryUtil.getMicrosoftTranslator();
042    
043                            int x = _translationId.indexOf(StringPool.UNDERLINE);
044    
045                            if ((x == -1) || ((x + 1) == _translationId.length())) {
046                                    throw new WebCacheException(
047                                            "Invalid translation ID " + _translationId);
048                            }
049    
050                            if (Character.isUpperCase(_translationId.charAt(x + 1))) {
051                                    x = _translationId.indexOf(StringPool.UNDERLINE, x + 1);
052    
053                                    if ((x == -1) || ((x + 1) == _translationId.length())) {
054                                            throw new WebCacheException(
055                                                    "Invalid translation ID " + _translationId);
056                                    }
057                            }
058    
059                            String fromLanguage = _translationId.substring(0, x);
060                            String toLanguage = _translationId.substring(x + 1);
061    
062                            String toText = microsoftTranslator.translate(
063                                    fromLanguage, toLanguage, _fromText);
064    
065                            translation.setToText(toText);
066                    }
067                    catch (Exception e) {
068                            throw new WebCacheException(e);
069                    }
070    
071                    return translation;
072            }
073    
074            public long getRefreshTime() {
075                    return _REFRESH_TIME;
076            }
077    
078            private static final long _REFRESH_TIME = Time.DAY * 90;
079    
080            private String _fromText;
081            private String _translationId;
082    
083    }