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.portal.kernel.dao.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.Validator;
022    import com.liferay.portal.kernel.util.WebKeys;
023    import com.liferay.portal.theme.ThemeDisplay;
024    
025    import java.text.Format;
026    
027    import java.util.Date;
028    import java.util.Locale;
029    import java.util.TimeZone;
030    
031    import javax.servlet.http.HttpServletRequest;
032    import javax.servlet.jsp.PageContext;
033    
034    /**
035     * @author Julio Camarero
036     */
037    public class DateSearchEntry extends TextSearchEntry {
038    
039            public Date getDate() {
040                    return _date;
041            }
042    
043            @Override
044            public String getName(PageContext pageContext) {
045                    if (Validator.isNotNull(_date)) {
046                            Object[] localeAndTimeZone = getLocaleAndTimeZone(pageContext);
047    
048                            Format dateFormatDateTime = FastDateFormatFactoryUtil.getDateTime(
049                                    (Locale)localeAndTimeZone[0], (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                                                    pageContext, "x-ago",
062                                                    LanguageUtil.getTimeDescription(
063                                                            pageContext,
064                                                            System.currentTimeMillis() - _date.getTime(),
065                                                            true)));
066                            }
067                            else {
068                                    sb.append(
069                                            LanguageUtil.format(
070                                                    pageContext, "within-x",
071                                                    LanguageUtil.getTimeDescription(
072                                                            pageContext,
073                                                            _date.getTime() - System.currentTimeMillis(),
074                                                            true)));
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(PageContext pageContext) {
091                    if ((_locale != null) && (_timeZone != null)) {
092                            return new Object[] {_locale, _timeZone};
093                    }
094    
095                    HttpServletRequest request =
096                            (HttpServletRequest)pageContext.getRequest();
097    
098                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
099                            WebKeys.THEME_DISPLAY);
100    
101                    _locale = themeDisplay.getLocale();
102                    _timeZone = themeDisplay.getTimeZone();
103    
104                    return new Object[] {_locale, _timeZone};
105            }
106    
107            private Date _date;
108            private Locale _locale;
109            private TimeZone _timeZone;
110    
111    }