001    /**
002     * Copyright (c) 2000-2013 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.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(), true),
065                                                    false));
066                            }
067                            else {
068                                    sb.append(
069                                            LanguageUtil.format(
070                                                    pageContext, "within-x",
071                                                    LanguageUtil.getTimeDescription(
072                                                            pageContext,
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(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    }