001    /**
002     * Copyright (c) 2000-present 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.taglib.search;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.WebKeys;
022    import com.liferay.portal.theme.ThemeDisplay;
023    
024    import java.text.Format;
025    
026    import java.util.Date;
027    import java.util.Locale;
028    import java.util.TimeZone;
029    
030    import javax.servlet.http.HttpServletRequest;
031    
032    /**
033     * @author Julio Camarero
034     */
035    public class DateSearchEntry extends TextSearchEntry {
036    
037            public Date getDate() {
038                    return _date;
039            }
040    
041            @Override
042            public String getName(HttpServletRequest request) {
043                    if (_date != null) {
044                            Object[] localeAndTimeZone = getLocaleAndTimeZone(request);
045    
046                            Locale locale = (Locale)localeAndTimeZone[0];
047    
048                            Format dateFormatDateTime = FastDateFormatFactoryUtil.getDateTime(
049                                    locale, (TimeZone)localeAndTimeZone[1]);
050    
051                            StringBundler sb = new StringBundler(5);
052    
053                            sb.append(
054                                    "<span onmouseover=\"Liferay.Portal.ToolTip.show(this, '");
055                            sb.append(dateFormatDateTime.format(_date));
056                            sb.append("')\">");
057    
058                            if (_date.before(new Date())) {
059                                    sb.append(
060                                            LanguageUtil.format(
061                                                    locale, "x-ago",
062                                                    LanguageUtil.getTimeDescription(
063                                                            locale,
064                                                            System.currentTimeMillis() - _date.getTime(), true),
065                                                    false));
066                            }
067                            else {
068                                    sb.append(
069                                            LanguageUtil.format(
070                                                    locale, "within-x",
071                                                    LanguageUtil.getTimeDescription(
072                                                            locale,
073                                                            _date.getTime() - System.currentTimeMillis(), true),
074                                                    false));
075                            }
076    
077                            sb.append("</span>");
078    
079                            return sb.toString();
080                    }
081                    else {
082                            return StringPool.BLANK;
083                    }
084            }
085    
086            public void setDate(Date date) {
087                    _date = date;
088            }
089    
090            protected Object[] getLocaleAndTimeZone(HttpServletRequest request) {
091                    if ((_locale != null) && (_timeZone != null)) {
092                            return new Object[] {_locale, _timeZone};
093                    }
094    
095                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
096                            WebKeys.THEME_DISPLAY);
097    
098                    _locale = themeDisplay.getLocale();
099                    _timeZone = themeDisplay.getTimeZone();
100    
101                    return new Object[] {_locale, _timeZone};
102            }
103    
104            private Date _date;
105            private Locale _locale;
106            private TimeZone _timeZone;
107    
108    }