001    /**
002     * Copyright (c) 2000-2012 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.search.facet;
016    
017    import com.liferay.portal.kernel.json.JSONArray;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.search.BooleanClause;
020    import com.liferay.portal.kernel.search.Field;
021    import com.liferay.portal.kernel.search.SearchContext;
022    import com.liferay.portal.kernel.search.facet.config.FacetConfiguration;
023    import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
024    import com.liferay.portal.kernel.util.StringUtil;
025    
026    import java.text.DateFormat;
027    
028    import java.util.Calendar;
029    
030    /**
031     * @author Raymond Augé
032     */
033    public class ModifiedFacet extends RangeFacet {
034    
035            public ModifiedFacet(SearchContext searchContext) {
036                    super(searchContext);
037    
038                    setFieldName(Field.MODIFIED_DATE);
039            }
040    
041            @Override
042            protected BooleanClause doGetFacetClause() {
043                    SearchContext searchContext = getSearchContext();
044    
045                    FacetConfiguration facetConfiguration = getFacetConfiguration();
046    
047                    normalizeDates(searchContext, facetConfiguration);
048    
049                    return super.doGetFacetClause();
050            }
051    
052            protected void normalizeDates(
053                    SearchContext searchContext, FacetConfiguration facetConfiguration) {
054    
055                    Calendar now = Calendar.getInstance();
056    
057                    now.set(Calendar.SECOND, 0);
058                    now.set(Calendar.MINUTE, 0);
059    
060                    Calendar pastHour = (Calendar)now.clone();
061                    pastHour.set(Calendar.HOUR_OF_DAY, now.get(Calendar.HOUR_OF_DAY) - 1);
062    
063                    Calendar past24Hours = (Calendar)now.clone();
064                    past24Hours.set(
065                            Calendar.DAY_OF_YEAR, now.get(Calendar.DAY_OF_YEAR) - 1);
066    
067                    Calendar pastWeek = (Calendar)now.clone();
068                    pastWeek.set(Calendar.DAY_OF_YEAR, now.get(Calendar.DAY_OF_YEAR) - 7);
069    
070                    Calendar pastMonth = (Calendar)now.clone();
071                    pastMonth.set(Calendar.MONTH, now.get(Calendar.MONTH) - 1);
072    
073                    Calendar pastYear = (Calendar)now.clone();
074                    pastYear.set(Calendar.YEAR, now.get(Calendar.YEAR) - 1);
075    
076                    now.set(Calendar.HOUR_OF_DAY, now.get(Calendar.HOUR_OF_DAY) + 1);
077    
078                    DateFormat dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
079                            "yyyyMMddHHmmss");
080    
081                    JSONObject dataJSONObject = facetConfiguration.getData();
082    
083                    if (dataJSONObject.has("ranges")) {
084                            JSONArray rangesJSONArray = dataJSONObject.getJSONArray("ranges");
085    
086                            for (int i = 0; i < rangesJSONArray.length(); i++) {
087                                    JSONObject rangeObject = rangesJSONArray.getJSONObject(i);
088    
089                                    String rangeString = rangeObject.getString("range");
090    
091                                    rangeString = StringUtil.replace(
092                                            rangeString,
093                                            new String[] {
094                                                    "past-hour", "past-24-hours", "past-week", "past-month",
095                                                    "past-year", "*"
096                                            },
097                                            new String[] {
098                                                    dateFormat.format(pastHour.getTime()),
099                                                    dateFormat.format(past24Hours.getTime()),
100                                                    dateFormat.format(pastWeek.getTime()),
101                                                    dateFormat.format(pastMonth.getTime()),
102                                                    dateFormat.format(pastYear.getTime()),
103                                                    dateFormat.format(now.getTime())
104                                            }
105                                    );
106    
107                                    rangeObject.put("range", rangeString);
108                            }
109                    }
110            }
111    
112    }